XLISP: An Experimental Object Oriented Language by David Betz 114 Davenport Ave. Manchester, NH 03103 (603) 625-4691 XLISP is an experimental programming language combining some of the features of LISP with an object oriented extension capability. It was implemented to allow experimentation with object oriented programming on small computers. There are currently implementations running on the PDP-11 under RSX-11, RT-11, and UNIX V7, on the VAX-11 under VAX/VMS and Berkeley VAX/UNIX and on the Z-80 running CP/M-80. It is completely written in the programming language 'C' and is believed to be easily extended with user written builtin functions and classes. It is available free of charge and is in the public domain. Many traditional LISP functions are built into XLISP. In addition, XLISP defines the object classes 'Object', 'Class', and 'Keymap' as primitives. 'Object' is the only class that has no superclass and hence is the root of the class heirarchy tree. 'Class' is the class of which all classes are instances (it is the only object that is an instance of itself). 'Keymap' is a class whose instances are mappings from input key sequences to messages. This document is intended to be a brief description of XLISP. It assumes some knowledge of LISP and some understanding of the concepts of object oriented programming. XLISP: An Experimental Object Oriented Language Page 2 XLISP Command Loop When XLISP is started, it issues the following prompt: > This indicates that XLISP is waiting for an expression to be typed. When an incomplete expression has been typed (one where the left and right parens don't match) XLISP changes its prompt to: n> where n is an integer indicating how many levels of parens remain unclosed. When a complete expression has been entered, XLISP attempts to evaluate that expression. If the expression evaluates successfully, XLISP prints the result of the evaluation and then returns to the initial prompt waiting for another expression to be typed. Input can be aborted at any time by typing the EOF key. Another EOF will exit from XLISP. XLISP: An Experimental Object Oriented Language Page 3 DATA TYPES AND THE EVALUATOR XLISP data types There are several different data types available to XLISP programmers. o symbols o strings o integers o objects o file pointers o lists o subrs (builtin functions) The XLISP evaluator The process of evaluation in XLISP: o Integers, strings, objects, file pointers, and subrs evaluate to themselves o Symbols evaluate to the value associated with their current binding o Lists are evaluated by evaluating the first element of the list o If it evaluates to a subr, the builtin function is executed using the remaining list elements as arguments (they are evaluated by the subr itself) o If it evaluates to a list, the list is assumed to be a function definition and the function is evaluated using the values of the remaining list elements as arguments o If it evaluates to an object, the second list element is evaluated and used as a message selector. The message formed by combining the selector with the values of the remaining list elements is sent to the object. XLISP: An Experimental Object Oriented Language Page 4 LEXICAL CONVENTIONS XLISP lexical conventions: The following conventions are followed when entering XLISP programs: Comments in XLISP code begin with a semi-colon character and continue to the end of the line. Symbol names in XLISP can consist of any sequence of non-blank printable characters except the following: ( ) . ' " ; Symbol names must not begin with a digit. Integer literals consist of a sequence of digits optionally beginning with a '+' or '-'. The range of values an integer can represent is limited by the size of a C 'int' on the machine that XLISP is running on. Literal strings are sequences of characters surrounded by double quotes. Within quoted strings the '\' character is used to allow non-printable characters to be included. The codes recognized are: \\ means the character '\' \n means newline \t means tab \r means return \e means escape \nnn means the character whose octal code is nnn The single quote character can be used as a shorthand for a call on the function 'quote': 'foo is equivalent to: (quote foo) XLISP: An Experimental Object Oriented Language Page 5 OBJECTS Objects: Definitions: o selector - a symbol used to select an appropriate method o message - a selector and a list of actual arguments o method - the code that implements a message Since XLISP was created to provide a simple basis for experimenting with object oriented programming, one of the primitive data types included was 'object'. In XLISP, an object consists of a data structure containing a pointer to the object's class as well as a list containing the values of the object's instance variables. Officially, there is no way to see inside an object (look at the values of its instance variables). The only way to communicate with an object is by sending it a message. When the XLISP evaluator evaluates a list the value of whose first element is an object, it interprets the value of the second element of the list (which must be a symbol) as the message selector. The evaluator determines the class of the receiving object and attempts to find a method corresponding to the message selector in the set of messages defined for that class. If the message is not found in the object's class and the class has a super-class, the search continues by looking at the messages defined for the super-class. This process continues from one super-class to the next until a method for the message is found. If no method is found, an error occurs. When a method is found, the evaluator binds the receiving object to the symbol 'self', binds the class in which the method was found to the symbol 'msgclass', and evaluates the method using the remaining elements of the original list as arguments to the method. These arguments are always evaluated prior to being bound to their corresponding formal arguments. The result of evaluating the method becomes the result of the expression. XLISP: An Experimental Object Oriented Language Page 6 OBJECTS Classes: Object THE TOP OF THE CLASS HEIRARCHY Messages: print THE DEFAULT OBJECT PRINT ROUTINE returns the object show SHOW AN OBJECT'S INSTANCE VARIABLES returns the object class RETURN THE CLASS OF AN OBJECT returns the class of the object isnew THE DEFAULT OBJECT INITIALIZATION ROUTINE returns the object sendsuper [...] SEND SUPERCLASS A MESSAGE the message selector the message arguments returns the result of sending the message Class THE CLASS OF ALL OBJECT CLASSES (including itself) Messages: new CREATE A NEW INSTANCE OF A CLASS returns the new class object isnew [] INITIALIZE A NEW CLASS the superclass returns the new class object answer ADD A MESSAGE TO A CLASS the message symbol the formal argument list this list is of the form: (... [/ ...]) where a formal argument a local variable a list of executable expressions returns the object ivars DEFINE THE LIST OF INSTANCE VARIABLES the list of instance variable symbols returns the object cvars DEFINE THE LIST OF CLASS VARIABLES the list of class variable symbols returns the object XLISP: An Experimental Object Oriented Language Page 7 OBJECTS When a new instance of a class is created by sending the message 'new' to an existing class, the message 'isnew' followed by whatever parameters were passed to the 'new' message is sent to the newly created object. When a new class is created by sending the 'new' message to the object 'Class', an optional parameter may be specified indicating of which class the newly generated class is to be a subclass. If this parameter is omitted, the new class will be a subclass of 'Object'. Example: ; create 'Foo' as a subclass of 'Object' (setq Foo (Class 'new)) ; create 'Bar' as a subclass of 'Foo' (setq Bar (Class 'new Foo)) A class inherits all instance variables, class variables, and methods from its super-class. XLISP: An Experimental Object Oriented Language Page 8 OBJECTS The 'Keymap' Class: A keymap is data structure that translates a sequence of keystrokes into a message. In order to create a keymap: (setq km (Keymap 'new)) In order to add a key definition to a keymap (km): (km 'key "\eA" 'up) (km 'key "\eB" 'down) (km 'key "\eC" 'right) (km 'key "\eD" 'left) Executing a keymap: (setq env (list ob1 ob2 ob3 ob4)) (km 'process env) When the process message is sent, its method enters a character input loop calling kbin to get single unechoed characters from the keyboard. When a sequence of characters is found that matches one of the sequences defined in a key function call, the corresponding message is sent. The method tries to send the message to each of the objects in the environment list. It stops when it finds an object that knows how to answer the message. Along with the message selector given in the key definition, the sequence of matched characters is passed as a single string parameter. Keymap new CREATE A NEW KEYMAP returns a new keymap isnew INITIALIZE THE NEW KEYMAP returns the keymap key ADD A KEY DEFINITION TO A KEYMAP the string defining the key the symbol for the message returns the keymap process PROCESS INPUT USING A KEYMAP list of active objects returns the keymap when a message evaluates to nil XLISP: An Experimental Object Oriented Language Page 9 SYMBOLS Symbols: o self - the current object (within a message context) o msgclass - the class in which the current method was found o currentenv - the environment list for the current invocation of kmprocess o oblist - the object list XLISP: An Experimental Object Oriented Language Page 10 FUNCTIONS Utility functions: (load ) LOAD AN XLISP SOURCE FILE the filename string returns the filename (mem) SHOW MEMORY ALLOCATION STATISTICS returns nil (gc) FORCE GARBAGE COLLECTION returns nil (alloc ) CHANGE NUMBER OF NODES TO ALLOCATE IN EACH SEGMENT the number of nodes to allocate returns the old number of nodes to allocate (expand ) EXPAND MEMORY BY ADDING SEGMENTS the number of segments to add returns the number of segments added XLISP: An Experimental Object Oriented Language Page 11 FUNCTIONS Functions: (eval ) EVALUATE AN XLISP EXPRESSION the expression to be evaluated returns the result of evaluating the expression (set ) SET THE VALUE OF A SYMBOL the symbol being set the new value returns the new value (setq ) SET THE VALUE OF A SYMBOL the symbol being set (quoted) the new value returns the new value (print ...) PRINT A LIST OF VALUES the expressions to be printed returns nil (princ ...) PRINT A LIST OF VALUES WITHOUT QUOTING the expressions to be printed returns nil (quote ) RETURN AN EXPRESSION UNEVALUATED or ' the expression to be quoted (quoted) returns unevaluated (if [ ]) EXECUTE EXPRESSIONS CONDITIONALLY test expression expression evaluated if texpr is non-nil or non-zero expression evaluated if texpr is nil or zero returns the value of the expression evaluated (while ...) ITERATE WHILE AN EXPRESSION IS TRUE test expression evaluated at start of each iteration expressions evaluated as long as evaluates to non-nil or non-zero returns the result of the last expression evaluated (repeat ...) ITERATE USING A REPEAT COUNT integer expression indicating the repeat count expressions evaluated times returns the result of the last expression evaluated (foreach ...) ITERATE FOR EACH ELEMENT IN A LIST symbol to assign each list element to (quoted) list to iterate through expressions evaluated for each element in the list returns the result of the last expression evaluated XLISP: An Experimental Object Oriented Language Page 12 FUNCTIONS (defun ...) DEFINE A NEW FUNCTION symbol to be defined (quoted) list of formal arguments (quoted) this list is of the form: (... [/ ...]) where is a formal argument is a local variable expressions constituting the body of the function (quoted) returns the function symbol (cond ...) EVALUATE CONDITIONALLY pair consisting of: ( ) where is a predicate expression is evaluated if the predicate is not nil returns the value of the first expression whose predicate is not nil (exit) EXIT XLISP returns never returns XLISP: An Experimental Object Oriented Language Page 13 FUNCTIONS I/O Functions: (fopen ) OPEN A FILE the file name string the open mode string returns a file pointer (fclose ) CLOSE A FILE the file pointer returns nil (getc []) GET A CHARACTER FROM A FILE the file pointer (default is stdin) returns the character (integer) (putc []) PUT A CHARACTER TO A FILE the character to put (integer) the file pointer (default is stdout) returns the character (integer) (fgets []) GET A STRING FROM A FILE the file pointer (default is stdin) returns the input string (fputs []) PUT A STRING TO A FILE the string to output the file pointer (default is stdout) returns the string XLISP: An Experimental Object Oriented Language Page 14 FUNCTIONS String Functions: (strcat ...) CONCATENATE STRINGS string expressions returns result of concatenating the strings (strlen ) COMPUTE THE LENGTH OF A STRING the string expression returns the length of the string (substr []) RETURN SUBSTRING string expression starting position optional length (default is rest of string) returns substring starting at for (ascii ) NUMERIC VALUE OF CHARACTER string expression returns numeric value of first character (according to ASCII) (chr ) CHARACTER EQUIVALENT OF ASCII VALUE numeric expression returns one character string with ASCII equivalent of (atoi ) CONVERT AN ASCII STRING TO AN INTEGER string expression returns the integer value of the string expression (itoa ) CONVERT AN INTEGER TO AN ASCII STRING integer expression returns the string representation of the integer value XLISP: An Experimental Object Oriented Language Page 15 FUNCTIONS List Functions: (head ) RETURN THE HEAD ELEMENT OF A LIST or (car the list returns the first element of the list (tail ) RETURN THE TAIL ELEMENTS OF A LIST or (cdr ) the list returns the list minus the first element (list ...) CREATE A LIST OF VALUES evaluated expressions to be combined into a list returns the new list (nth ) RETURN THE NTH ELEMENT OF A LIST the number of the element to return the list to return the nth element of returns the nth element or nil if the list isn't that long (append ...) APPEND LISTS lists whose elements are to be appended returns the new list (cons ) CONSTRUCT A NEW LIST ELEMENT becomes the head (car) of the new list becomes the tail (cdr) of the new list returns the new list (null ) CHECKS FOR AN EMPTY LIST the list to check returns t if the list is empty, nil otherwise (atom ) CHECKS FOR AN ATOM (ANYTHING THAT ISN'T A LIST) the expression to check returns t if the value is an atom, nil otherwise (listp ) CHECKS FOR A LIST the expression to check returns t if the value is a list, nil otherwise XLISP: An Experimental Object Oriented Language Page 16 FUNCTIONS (type ) RETURNS THE TYPE OF THE EXPRESSION the expression to return the type of returns nil if the value is nil otherwise one of the symbols: SYM for symbols OBJ for objects LIST for list nodes KMAP for keymap nodes SUBR for internal subroutine nodes STR for string nodes INT for integer nodes FPTR for file pointer nodes (eq ) CHECKS FOR THE EXPRESSIONS BEING THE SAME the first expression the second expression returns t if they are equal, nil otherwise (equal ) CHECKS FOR THE EXPRESSIONS BEING EQUAL the first expression the second expression returns t if they are equal, nil otherwise (read [ ]) READ AN XLISP EXPRESSION the string to use as input (optional) returns the expression read (reverse ) REVERSE A LIST the list to reverse returns a new list in the reverse order (length ) FIND THE LENGTH OF A LIST the list to find the length of returns the length XLISP: An Experimental Object Oriented Language Page 17 FUNCTIONS Arithmetic Functions: (+ ...) ADD A LIST OF VALUES expressions to be added returns the result of the addition (- ...) SUBTRACT A LIST OF VALUES expressions to be subtracted returns the result of the subtraction (* ...) MULTIPLY A LIST OF VALUES expressions to be multiplied returns the result of the multiplication (/ ...) DIVIDE A LIST OF VALUES expressions to be divided returns the result of the division (% ...) MODulus A LIST OF VALUES expressions to be MODulused returns the result of mod (& ...) THE BITWISE AND OF A LIST OF VALUES expressions to be ANDed returns the bit by bit ANDing of expressions (| expressions to be ORed returns the bit by bit ORing of expressions (~ ) THE BITWISE NOT OF A VALUE expression to be NOTed returns the bit by bit inversion of expression (min ...) THE SMALLEST OF A LIST OF VALUES expressions to be checked returns the smallest value of the list (max ...) THE LARGEST OF A LIST OF VALUES expressions to be checked returns the largest value of the list (abs ) THE ABSOLUTE VALUE OF AN EXPRESSION integer expression returns the absolute value of the expression XLISP: An Experimental Object Oriented Language Page 18 FUNCTIONS Boolean Functions: (&& ...) THE LOGICAL AND OF A LIST OF VALUES expressions to be ANDed returns the result of anding the expressions (evaluation of expressions stops after the first expression that evaluates to false) (|| ...) THE LOGICAL OR OF A LIST OF VALUES expressions to be ORed returns the result of oring the expressions (evaluation of expressions stops after the first expression that evaluates to true) (! ) THE LOGICAL NOT OF A VALUE expression to be NOTed return logical not of XLISP: An Experimental Object Oriented Language Page 19 FUNCTIONS Relational Functions: The relational functions can be used to compare integers and strings. The functions '==' and '!=' can also be used to compare other types. The result of these comparisons is computed the same way as for 'eq'. (< ) TEST FOR LESS THAN the left operand of the comparison the right operand of the comparison returns the result of comparing with (<= ) TEST FOR LESS THAN OR EQUAL TO the left operand of the comparison the right operand of the comparison returns the result of comparing with (== ) TEST FOR EQUAL TO the left operand of the comparison the right operand of the comparison returns the result of comparing with (!= ) TEST FOR NOT EQUAL TO the left operand of the comparison the right operand of the comparison returns the result of comparing with (>= ) TEST FOR GREATER THAN OR EQUAL TO the left operand of the comparison the right operand of the comparison returns the result of comparing with (> ) TEST FOR GREATER THAN the left operand of the comparison the right operand of the comparison returns the result of comparing with