Formatting and indentation in ZIL?

How to format and indent ZIL-code? I found this page about formatting Lisp and put together a small list of “rules” for my ZIL-code.

   ABOUT FORMATTING AND INDENTATION OF ZIL
   =======================================
   
   0. DON'T put closing brackets on their own line.
   
   1. Routine arguments on single line or left-align with 1st argument.
   
   <ROUTINE TEST-F (ARG1 ARG2 ARG3...)
   
   Or
   
   <ROUTINE TEST-F (ARG1 ARG2 ARG3 "AUX" VAR1
                    VAR2 VAR3 ...)
	
	2. Indent routine-body with 4 spaces.
	
	<ROUTINE TEST-F ()
	    <ROUTINE-BODY>>
	
	3. Conditions
	
	3.1 Align Conditions on the left parantheses.
	
	<COND (<WHEN-THIS> <DO-THIS>)
	      (<WHEN-THIS> <DO-THIS>)>
	
	3.2 Align body in conditions on left bracket.
	
	<COND (<WHEN-THIS> 
	       <DO-THIS>
		   <AND-THIS>)
	      (<WHEN-THIS> 
		   <DO-THIS>
		   <AND-THIS>)>
	
	3.3 Example of nested conditions

	<COND (<WHEN-THIS> 
	       <COND (<WHEN-THIS> 
		          <DO-THIS>
		          <AND-THIS>)>)
	      (<WHEN-THIS> 
	       <COND (<WHEN-THIS> <DO-THIS>)
				 (<WHEN-THIS> <DO-THIS>)>)
		  (ELSE
		   <DO-THIS>
		   <AND-THIS>)>
	
	4. Align nested forms that requires multiline on left bracket.
	   (The routine below returns TRUE when taking object (OBJ) is
	   not invisible and not held and have either TAKEBIT or TRYTAKEBIT.
	   It also returns TRUE when dropping object (OBJ) and it's held.)
	   
	<ROUTINE ALL-INCLUDES? (OBJ)
		<NOT <OR <FSET? .OBJ ,INVISIBLE>
				 <AND <VERB? TAKE> <HELD? .OBJ>>
				 <AND <VERB? DROP> <NOT <HELD? .OBJ>>>
				 <=? .OBJ ,WINNER>
				 <AND <VERB? TAKE DROP>
					  <NOT <OR <FSET? .OBJ ,TAKEBIT>
							   <FSET? .OBJ ,TRYTAKEBIT>>>>>>>
   
	5. Text that requires multiline in, for example, TELL or LDESC
	   should begin in column 1 with the opening double-quote.
	   
	<ROOM SMALL-ROOM
		(DESC "Small Room")
		(IN ROOMS)
		(LDESC 
	"You are in a small room containing a vending machine. 
	Beside the stairway leading down stands a huge troll,
	brandishing a large axe. You can also go up through a 
	hole in the ceiling.")
		(UP TO INSIDE-HOUSE)
		(DOWN PER SMALL-ROOM-EXIT)
		(ACTION SMALL-ROOM-F)
		(FLAGS LIGHTBIT)>
	
	Or
	
	<ROUTINE SMALL-ROOM-F ()
		<COND (<WHEN-THIS> 
			   <TELL
	"You are in a small room containing a vending machine. 
	Beside the stairway leading down stands a huge troll,
	brandishing a large axe. You can also go up through a 
	hole in the ceiling.">)
			  (<WHEN-THIS> 
			   <DO-THIS>
			   <AND-THIS>)>>

Any suggestions for revisions, additions and removals for this set of “rules”?

6 Likes

FWIW, some of the MDL documentation mentions a pretty-printing library called PPRINT. Historical source code for it can be found here, although it doesn’t quite line up with what’s documented.

I also started writing my own (incompatible) pretty-printer here a while back, before the original PPRINT code surfaced.

If one of these were fully implemented and usable, I’d recommend just adopting it as the official formatter, but sadly they aren’t. So…


The rules you’ve described are sensible, consistent with all the formatters I’ve seen, and (I think) mostly consistent with the historical Infocom code as well as the ZILF library and samples.

Point 0 can be generalized: a closing bracket should never be the first thing on a line (indented or not), and its horizontal position should always be to the right of the matching opening bracket.

Point 2 applies to all multiline statements that consist of a “header” and “body”: ROUTINE, DEFINE, OBJECT, REPEAT, DEFSTRUCT, etc. The “header” should be written on one line (if possible) or compactly wrapped, and the “body” should be indented a standard amount rather than lining up with anything in the header:

<REPEAT ((X 1))
    <FOO>
    <BAR>>

<OBJECT CHAIR
    (DESC "chair")
    (SYNONYM CHAIR)>

This creates a visual hierarchy, vertically, between the header and body.

Forms that don’t consist of a header and body instead create a visual hierarchy horizontally, by lining up the arguments to the right of the function name:

<COND (.FOO <BAR>)
      (.BAZ <QUUX>)
      (T <ZORK>)>

<+ .A
   .B
   <* .C .D>>
2 Likes