About Checking the Balance of Parentheses (Visual LISP IDE)

AutoLISP uses parentheses more frequently than most other computer programming languages.

Note: The Visual LISP IDE is available on Windows only.

One of the most frequent syntax errors in AutoLISP is an unequal number of open and close parentheses. All parentheses must be balanced before the program can be loaded into AutoCAD, and they must be balanced correctly to ensure the functions of the program can be executed.

Visual LISP includes a number of tools to help detect unbalanced or unmatched parentheses. When formatting code in the text editor, the Visual LISP code formatter searches for unbalanced parentheses. If allowed, the code formatter will add parentheses where it thinks they are missing. Under most conditions, the code formatter adds the missing parentheses at the end of the code being formatted.

Note: If you do not allow the formatter to add the missing parentheses, it will not format the code either.

The following code demonstrates missing parentheses and what happens when using the Match Forward tool:

1  (defun yinyang (/ origin radius i-radius half-r origin-x origin-y)
2  (setq half-r (/ radius 2))
3  (setq origin-x (car origin))
4  (setq origin-y (cadr origin))
5  (command "._circle" 
6           origin 
7           radius
8           (command "._arc"
9                    "_c"
10                   (list origin-x (+ origin-y half-r)) 
11                   (list origin-x (+ origin-y radius)) 
12                   origin                             
13          )
14          (command "._arc"
15                   "_c"
16                  (list origin-x (- origin-y half-r)) 
17                  (list origin-x (- origin-y radius)) 
18                  origin                             
19          )
20 )

(The line numbers are not part of the text; they are used to help explain the example.)

Here is what happens if you load the previous code in Visual LISP and continually use the Match Forward tool, starting with the insertion point at the beginning of line 1.

The reason why the cursor moves to the last line is because the close parenthesis that matches the open parenthesis on line 5 is the last parenthesis in the program. This is an error because the last close parenthesis in an AutoLISP program should match the open parenthesis of the program's defun function. Notice also that all the statements after line 5 are indented in a manner unlike in the preceding program code. These two clues indicate something is amiss at this point in the program. In fact, the close parenthesis to the command function that begins on line 5 is missing. The missing parenthesis should be placed between line 7 and 8.