Jump to content

Bracket Tester


AlmarM
 Share

Recommended Posts

Hiya!

I had some problems with my Javascript if statement, so I made this.

This easy script checks how many open and closing brackets ("(" ")") the statement has.

Global $Open = 0, $Close = 0

$GUI = GUICreate("", 320, 205)
$Edit = GUICtrlCreateEdit("", 10, 10, 300, 150)
$Test = GUICtrlCreateButton("Test Brackets", 10, 170, 300)

GUISetState()
While 1
    Switch GUIGetMsg()
    Case -3
        Exit
        
    Case $Test
        $Split = StringSplit(GUICtrlRead($Edit), "")
        
        For $i = 1 To $Split[0]
            If ($Split[$i] == "(") Then $Open += 1
            If ($Split[$i] == ")") Then $Close += 1
        Next
            
        If ($Open > $Close) Then
            $ShouldClose = $Open - $Close
            Msgbox(0, "", "Open brackets:   " & $Open & _
            @CRLF & "Closing brackets:  " & $Close & _
            @CRLF & @CRLF & "You should close " & $ShouldClose & " more brackets.")
        ElseIf ($Close > $Open) Then
            $ShouldOpen = $Close - $Open
            Msgbox(0, "", "Open brackets:   " & $Open & _
            @CRLF & "Closing brackets:  " & $Close & _
            @CRLF & @CRLF & "You should open " & $ShouldOpen & " more brackets.")
        Else
            Msgbox(0, "", "Open brackets:   " & $Open & _
            @CRLF & "Closing brackets:  " & $Close & _
            @CRLF & @CRLF & "Statement should be O.K.")
        EndIf
        
        Dim $Open = 0, $Close = 0, $ShouldOpen = 0, $ShouldClose = 0
        
    EndSwitch
WEnd

You could test it with this statements:

O.K.

if ((Math.abs((gemArray[i].X() / 32) - (gemArray[trg1].X() / 32)) == 1 ||
Math.abs((gemArray[i].Y() / 32) - (gemArray[trg1].Y() / 32) == 1)) &&
!(Math.abs((gemArray[i].X() / 32) - (gemArray[trg1].X() / 32))) == 1 &&
Math.abs((gemArray[i].Y() / 32) - (gemArray[trg1].Y() / 32))) {

Open more brackets

if ((Math.abs((gemArray[i].X() / 32) - (gemArray[trg1].X() / 32)) == 1 ||
Math.abs((gemArray[i].Y() / 32)) - (gemArray[trg1].Y() / 32) == 1)) &&
!(Math.abs((gemArray[i].X() / 32) - (gemArray[trg1].X() / 32)))) == 1 &&
Math.abs((gemArray[i].Y() / 32) - (gemArray[trg1].Y() / 32))) {

Close more brackets

if ((Math.abs((gemArray[i].X() / 32) - (gemArray[trg1].X(() / 32)) == 1 ||
Math.abs((gemArray[i].Y() / 32) - (gemArray[trg1].Y() / 32) == 1)) &&
!(Math.abs(((gemArray[i].X(() / 32) - (gemArray[trg1].X() / 32))) == 1 &&
Math.abs((gemArray[i].Y() / 32) - (gemArray[trg1].Y() / 32))) {

There is probably a StringRegExp to check precisely where to add or remove brackets.

Unfortunately, I can't.

Good luck. ;)

Edited by AlmarM

Minesweeper

A minesweeper game created in autoit, source available.

_Mouse_UDF

An UDF for registering functions to mouse events, made in pure autoit.

2D Hitbox Editor

A 2D hitbox editor for quick creation of 2D sphere and rectangle hitboxes.

Link to comment
Share on other sites

Heres one made from a function for Au3Int that Wraithdu wrote for parsing array brackets (AutoIt won't let you declare variables etc. using execute so we did it all manually) based on a function I wrote for getting proper comma seperated statements that include strings. It's so useful I have reused the basic loop several times in different projects.

It will tell you where you have extra brackets, and supports strings.

Func _CheckBrackets($sString)
    Local $sInStr = "", $iIn = 0
    For $j = 1 To StringLen($sString)
        Switch StringMid($sString, $j, 1)
            Case """"
                If $sInStr = "" Then
                    $sInStr = """"
                ElseIf $sInStr = """" Then
                    $sInStr = ""
                EndIf
            Case "'"
                If $sInStr = "" Then
                    $sInStr = "'"
                ElseIf $sInStr = "'" Then
                    $sInStr = ""
                EndIf
            Case "("
                If $sInStr = "" Then $iIn += 1
            Case ")"
                If $sInStr = "" Then $iIn -= 1
                If $iIn < 0 Then
                    ConsoleWrite("Character: " & $j & ". Unexpected closing bracket." & @CRLF)
                    $iIn += 1
                EndIf
        EndSwitch
    Next

    If $iIn > 0 Then ConsoleWrite("Not enough closing brackets." & @CRLF)
EndFunc

Mat

Edit: If there is an extra closing bracket it considers it unwanted and ignores it...

Edited by Mat
Link to comment
Share on other sites

Ok... So I liked the idea. Here's the next step up. A more debugging approach that tells you where the bracket is matched.

Func _CheckBrackets($sString)
    Local $sInStr = "", $aiPos[1] = [0]

    For $j = 1 To StringLen($sString)
        Switch StringMid($sString, $j, 1)
            Case """"
                If $sInStr = "" Then
                    $sInStr = """"
                ElseIf $sInStr = """" Then
                    $sInStr = ""
                EndIf
            Case "'"
                If $sInStr = "" Then
                    $sInStr = "'"
                ElseIf $sInStr = "'" Then
                    $sInStr = ""
                EndIf
            Case "("
                If $sInStr = "" Then
                    $aiPos[0] += 1

                    ReDim $aiPos[$aiPos[0] + 1]
                    $aiPos[$aiPos[0]] = $j
                EndIf
            Case ")"
                If $sInStr = "" Then

                    If $aiPos[0] = 0 Then
                        ConsoleWrite("!>Unexpected closing bracket character: " & $j & "." & @CRLF)
                    Else
                        ConsoleWrite("Closing: " & $j & ", matched: " & $aiPos[$aiPos[0]] & ", depth: " & ($aiPos[0] - 1) & @CRLF)

                        ReDim $aiPos[$aiPos[0]]
                        $aiPos[0] -= 1
                    EndIf
                EndIf
        EndSwitch
    Next

    If $aiPos[0] > 0 Then ConsoleWrite("Not enough closing brackets. Requires " & $aiPos[0] & " more." & @CRLF)
EndFunc   ;==>_CheckBrackets
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...