Racing Dog's Kennel PL/0 IDE









The PL/0 IDE is an interactive environment for Niklaus Wirth's teaching language PL/0 (see his book Algorithms + Data Structures = Programs). I'm assuming you've read it!

When PL/0 was designed, computers had memory resources which were miniscule by today's standards, hence there were very tight restrictions on memory usage. Also, many only worked in batch mode. PL/0 was thus created to work in batch mode and with constraints on the sizes of its internal data structures. None of this relates to today's computers and how users expect to able to use them. Given that there still seems to some interest in using PL/0 as a beginners language it seemed to me that a modern interactive IDE would be a good idea, hence PL/0 IDE. As the original language compiler and built in interpreter were implemented in Pascal, this IDE is implemented in Lazarus/Free Pascal, thus the compiler and interpreter parts are still recognisably based on the original code, if slightly different in places.

There are several implementations of PL/0 which are true to Wirth's original work to varying extents. Whilst I wanted to remain faithful to the original, some things just had to change, simply due to the change to a modern environment and, internally, the availability generic dynamic structures which assist in removimg size restrictions.

The syntax of the language is as follows....

        program -> block '.' .

        block -> [ 'const' identifier '=' number { ',' identifier '=' number} ';' ]
                 [ 'var' identifier {',' identifier} ';' ]
                 { 'procedure' identifier ';' block ';' } 
                 statement .

        statement -> [ identifier ':=' expression |
                       'call' identifier |
                       'begin' statement { ';' statement } 'end' |
                       'if' condition 'then' statement |
                       'while' condition 'do' statement |
                       'read' identifier |
                       'write' expression 
					 ] .

        condition -> 'odd' expression |
                     expression ( '=' | '#' | '<' | '>' | '<=' | '>=' ) expression .

        expression -> [ '+' | '-' ] term { ( '+' | '-' ) term } .

        term -> factor {( '*' | '/' | '%' ) factor} .

        factor -> identifier | number | '(' expression ')' . 
	  

-> indicates that the syntax rule named to the left is defined by the item(s) to the right
. indicates the end of a syntax rule
| indicates that the items to either side are alternatives only one of which should be used
' ' encloses characters to be used literally as shown
( ) indicates grouping
[ ] indicates an optional item
{ } indicates an optional item which may be repeated as required
Identifiers must start with an alphabetic character which may be followed by any number of alphanumeric characters
Numbers are 32 bit integers in the range -2147483648..2147483647

Like I said, basics for beginners.

An interactive environment requires some means of user input, that is provided by the new read statement. The original provided output by simply printing the result of assignments to variables. Here only the value of the expression contained in the new write statement is output to the "Edit / Run" mode Output text box,(sse the slide show below). Individual assignments can still be viewed by using the IDE's "PL/0 Machine" display. As virtually all modern languages have some form of modulus operator, and it is a trivial addition, that has been provided via the symbol % as used in other languages.

One further addition is in response to the modern usage that indicates that programs should be commented. It thus seemed wrong to me that beginners should be taught to program without comments. The simplest form, and most trivial to implement, is the Visual Basic "single quote to line end" schema, so that has also been done.

The virtual machine used to execute a program is as per Wirth with the following exceptions ("levels" means "number of stack frames")....

  1. Every procedure and the main program has its own stack frame. The structure is
                   Dynamic Link
                   Static Link
                   Return Address
                   Locally Declared Data
                   Temporary Data
    		    
    The Dynamic Link allows the previous frame to become the current frame after the procedure exits. The Static Link is used to limit data access to only statically "in scope" data, thus allowing nested procedures to recurse correctly. The structure differs from Wirth in that the two links are adjacent. In PL/0 it doesn't matter what order these items are in as the overheads of stack clean up on procedure exit are hidden inside a single instruction, OPR 0,0. I just felt the links belong together, rather than being separated by the Return Address.
  2. In Wirth's book, for some reason, the instruction OPR 0,6 was implementing odd operator and OPR 0,7 was unused, despite the book's earlier example of generated code which indicated that odd should be implemented by OPR 0,7! As I needed an extra OPR to implement the % operator I went with the example and used OPR 0,6 for % and OPR 0,7 for odd.
  3. The read statement is implemented as the instruction INP l,a which is, get a user value and store it at saddress a, l levels down. The user is presented with a simple dialog inviting the input of a value. The header of the dialog is set to the name of the variable for which input is required. It therefore helps the user if a long variable name is used.
  4. The write statement is implemented as the instruction WRI 0,a which causes the current top of the virtual machine's stack to be popped and displayed in the Output text box. The address a is interpreted as the entry for the Write statement in the Code grid in the "PL/0 Machine" display to allow that statement to be used as annotation in the Output text box.

This slide show shows what the IDE looks like...

The first slide illustrates "Edit /Run" mode, note the simple colour highlighting. The second illustrates a program being executed and the read dialog. The third is the same program at the same point but in the "PL/0 Machine" view. The fourth illustrates how the Links change due to recursion. The fifth illustrates the Help file for the IDE. The Help is also provided in pdf form in the intallation folder.