TEACH BRACKETS A.Sloman Feb 82 === BRACKETS IN POP11 ================================================== POP11 makes use of several different kinds of brackets to indicate the beginnings, and ends, and sometimes middles, of bits of program. For instance here are some opening and closing brackets, arranged as matched pairs: ( ) (see HELP * ROUNDBRA) [ ] (see HELP * SQUAREBRA) { } (see HELP * SQUAREBRA) If you are using a terminal with upper case only, some of these will not print properly. If you don't balance brackets properly, POP11 complains. E.g. Try typing the following unmatched pair of brackets to POP11: ( ] You'll get a mishap message: POP11 complains about a MISPLACED SYNTAX WORD. Similarly if you give it a closing bracket without an opener, e.g. ) or } Try the following too: [ } { ] Here the opening brackets indicate that a structure is to be built, ie. a list (indicated by "[") or a vector indicated by "{". If the wrong sort of closing bracket is found you'll get a 'MISSING CLOSING BRACKET' error. Compare [a list with {a vector} in it] => {a vector with [a list] in it } => The situation is further complicated by the fact that the list brackets [ ..... ] can enclose words, including some of the other brackets, e.g. "(" and ")". So the following will not give an error: [ ) a list of seven items ( ] => Though this will: ( ] not legal POP11 [ ) => Try that. If you want to have a list or vector bracket included in a list or vector, you'll have to 'unquote' it with '% .... %' and then quote it with "..", e.g. [a bracket % "}" % and another % "[" % ] which creates a list of six elements. Besides the 'obvious' brackets, there are several unobvious ones. For instance, the word "define" is a sort of opening bracket. That is, when POP11 reads it in, it has definite expectations about what is coming next, namely a procedure heading, terminated by a semi-colon. e.g. define double(x) -> result; But if you leave out the "(", you'll get a complaint. Try: define double x) This is not a case of unexpected closing bracket. Rather, POP11 expects a list of 'input variables' to follow the procedure name, and if it meets ")" without its opener, it gets confused. If everything is OK up to the semi-colon, then POP11, expects the closing bracket of a procedure definition, and if it first meets another closer, before its opener, then there's trouble. E.g. try define double(x); ] Compare define double(x); enddefine; define double(x); endif ENDIF is the final closing bracket for IF, just as ENDUNTIL is the closing bracket for UNTIL, ENDREPEAT the closing bracket for REPEAT, etc. POP11 doesn't like to find 'ENDIF' when it's looking for 'ENDDEFINE'. Similarly, it doesn't like to find ENDDEFINE when its looking for something else. Try the following: if enddefine if ) The first 'closing bracket' expected by IF is 'THEN'. It doesn't like finding a different closing bracket, or even a semi-colon before then try: if ; if 3 > 2 ; After IF (and several other 'openers', like WHILE and UNTIL), POP11 treats ";" as an unacceptable closing bracket. After THEN, things are different. What do you think will be 'expected' in the following mishaps: if then ) if then enddefine What about if then else enddefine if then elseif ; Getting used to all these different cases where mishaps occur takes a little time. If you have a program in a file and you try to load it the mishap message will include a line number telling you how far the file had been read in before the mishap occurred. If your mishap involves unmatched brackets, the actual cause of the mishap could be a long way further back in the file. Try putting the following in a file, making sure you put in an exact copy: define silly(x); if islist(x) then length(([this is a long and silly list of words]) endif enddefine; Note the unmatched "(". When you load the file, you'll get a line number in the mishap. This does not correspond to the actual line in which the extra '(' was inserted, but to the last line read in before the mishap was detected. The 'doing' line of the mishap message also helps you to see which 'opening brackets' had not yet met their final closing brackets, e.g. 'DEFINE', 'IF', and '(' The looping brackets 'UNTIL' and 'WHILE' are a bit like 'IF', in that you have a middle bracket 'DO', like 'THEN', and a final closing bracket e.g. 'ENDUNTIL'. At present the POP11 system accepts 'THEN' instead of 'DO', and 'ENDDO' instead of 'ENDUNTIL'. For more on POP11 syntax, see TEACH * POPSUMMARY UNLESS is much like IF, except that it does not permit ELSE or ELSEIF clauses. Try: unless x > 3 then x=> else 3 => endunless; --- C.all/teach/brackets ----------------------------------------------- --- Copyright University of Sussex 1988. All rights reserved. ----------