xlisp - ClassicCMP

advertisement
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
XLISP Command Loop
Page 2
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
remain unclosed.
of
parens
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
Another EOF will exit from XLISP.
typing
the
EOF
key.
XLISP: An Experimental Object Oriented Language
DATA TYPES AND THE EVALUATOR
Page 3
XLISP data types
There are several different data types
programmers.
o
symbols
o
strings
o
integers
o
objects
o
file pointers
o
lists
o
subrs (builtin functions)
available
to
XLISP
The XLISP evaluator
The process of evaluation in XLISP:
o
Integers, strings, objects,
subrs evaluate to themselves
file
pointers,
and
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
LEXICAL CONVENTIONS
Page 4
XLISP lexical conventions:
The following conventions are followed when
programs:
entering
XLISP
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
non-blank printable characters except the following:
of
( ) . ' " ;
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:
\\
\n
\t
\r
\e
\nnn
means
means
means
means
means
means
the character '\'
newline
tab
return
escape
the character whose octal code is nnn
The single quote character can be used as a shorthand for
call on the function 'quote':
'foo
is equivalent to:
(quote foo)
a
XLISP: An Experimental Object Oriented Language
OBJECTS
Page 5
Objects:
Definitions:
o
selector - a symbol used to select
method
an
appropriate
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
OBJECTS
Page 6
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 <sel>
<sel>
<args>
returns
Class
[<args>...] SEND SUPERCLASS A MESSAGE
the message selector
the message arguments
the result of sending the message
THE CLASS OF ALL OBJECT CLASSES (including itself)
Messages:
new
CREATE A NEW INSTANCE OF A CLASS
returns
the new class object
isnew [<scls>]
<scls>
returns
INITIALIZE A NEW CLASS
the superclass
the new class object
answer <msg> <fargs> <code>
ADD A MESSAGE TO A CLASS
<msg>
the message symbol
<fargs>
the formal argument list
this list is of the form:
(<farg>... [/ <local>...])
where
<farg>
a formal argument
<local>
a local variable
<code>
a list of executable expressions
returns
the object
ivars <vars>
<vars>
returns
DEFINE THE LIST OF INSTANCE VARIABLES
the list of instance variable symbols
the object
cvars <vars>
<vars>
returns
DEFINE THE LIST OF CLASS VARIABLES
the list of class variable symbols
the object
XLISP: An Experimental Object Oriented Language
OBJECTS
Page 7
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,
and methods from its super-class.
class
variables,
XLISP: An Experimental Object Oriented Language
OBJECTS
Page 8
The 'Keymap' Class:
A keymap is data structure that
keystrokes into a message.
translates
a
sequence
of
In order to create a keymap:
(setq km (Keymap 'new))
In order to add a key definition to a keymap (km):
(km
(km
(km
(km
'key
'key
'key
'key
"\eA"
"\eB"
"\eC"
"\eD"
'up)
'down)
'right)
'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 <kstr> <ksym>
ADD A KEY DEFINITION TO A KEYMAP
<kstr>
the string defining the key
<ksym>
the symbol for the message
returns
the keymap
process <envlist>
PROCESS INPUT USING A KEYMAP
<envlist>
list of active objects
returns
the keymap when a message evaluates to
nil
XLISP: An Experimental Object Oriented Language
SYMBOLS
Page 9
Symbols:
o
self - the
context)
current
object
(within
o
msgclass - the class in which
was found
o
currentenv - the environment list for
invocation of kmprocess
o
oblist - the object list
the
a
current
the
message
method
current
XLISP: An Experimental Object Oriented Language
FUNCTIONS
Page 10
Utility functions:
(load <fname>)
<fname>
returns
LOAD AN XLISP SOURCE FILE
the filename string
the filename
(mem)
SHOW MEMORY ALLOCATION STATISTICS
returns
nil
(gc)
FORCE GARBAGE COLLECTION
returns
nil
(alloc <num>)
CHANGE NUMBER OF NODES TO ALLOCATE IN EACH
SEGMENT
<num>
returns
(expand <num>)
<num>
returns
the number of nodes to allocate
the old number of nodes to allocate
EXPAND MEMORY BY ADDING SEGMENTS
the number of segments to add
the number of segments added
XLISP: An Experimental Object Oriented Language
FUNCTIONS
Page 11
Functions:
(eval <expr>)
<expr>
returns
EVALUATE AN XLISP EXPRESSION
the expression to be evaluated
the result of evaluating the expression
(set <sym> <expr>)
SET THE VALUE OF A SYMBOL
<sym>
the symbol being set
<expr>
the new value
returns
the new value
(setq <qsym> <expr>)
SET THE VALUE OF A SYMBOL
<qsym>
the symbol being set (quoted)
<expr>
the new value
returns
the new value
(print <expr>...)
PRINT A LIST OF VALUES
<expr>
the expressions to be printed
returns
nil
(princ <expr>...)
PRINT A LIST OF VALUES WITHOUT QUOTING
<expr>
the expressions to be printed
returns
nil
(quote <expr>)
or
'<expr>
<expr>
returns
RETURN AN EXPRESSION UNEVALUATED
the expression to be quoted (quoted)
<expr> unevaluated
(if <texpr> <expr1> [ <expr2> ])
EXECUTE EXPRESSIONS
CONDITIONALLY
<texpr>
test expression
<expr1>
expression evaluated if texpr is non-nil or nonzero
<expr2>
expression evaluated if texpr is nil or zero
returns
the value of the expression evaluated
(while <texpr> <expr>...)
ITERATE WHILE AN EXPRESSION IS
TRUE
<texpr>
test expression evaluated at start of each
<expr>
expressions evaluated as long as <texpr>
returns
non-nil or non-zero
the result of the last expression evaluated
iteration
evaluates to
(repeat <iexpr> <expr>...)
ITERATE USING A REPEAT COUNT
<iexpr>
integer expression indicating the repeat count
<expr>
expressions evaluated <iexpr> times
returns
the result of the last expression evaluated
(foreach <qsym> <list> <expr>...) ITERATE FOR EACH ELEMENT IN A
LIST
<qsym>
<list>
<expr>
symbol to assign each list element to (quoted)
list to iterate through
expressions evaluated for each element in the
returns
the result of the last expression evaluated
list
XLISP: An Experimental Object Oriented Language
FUNCTIONS
Page 12
(defun <qsym> <qfargs> <expr>...)
DEFINE A NEW FUNCTION
<qsym>
symbol to be defined (quoted)
<qfargs>
list of formal arguments (quoted)
this list is of the form:
(<farg>... [/ <local>...])
where
<farg>
is a formal argument
<local>
is a local variable
<expr>
expressions constituting the body of the
function (quoted)
returns
the function symbol
(cond <pair>...)
EVALUATE CONDITIONALLY
<pair>
pair consisting of:
(<pred> <expr>)
where
<pred>
is a predicate expression
<expr>
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
FUNCTIONS
I/O Functions:
(fopen <fname> <mode>) OPEN A FILE
<fname>
the file name string
<mode>
the open mode string
returns
a file pointer
(fclose <fp>)
<fp>
returns
CLOSE A FILE
the file pointer
nil
(getc [<fp>])
<fp>
returns
GET A CHARACTER FROM A FILE
the file pointer (default is stdin)
the character (integer)
(putc <ch> [<fp>])
PUT A CHARACTER TO A FILE
<ch>
the character to put (integer)
<fp>
the file pointer (default is stdout)
returns
the character (integer)
(fgets [<fp>])
<fp>
returns
GET A STRING FROM A FILE
the file pointer (default is stdin)
the input string
(fputs <str> [<fp>]) PUT A STRING TO A FILE
<str>
the string to output
<fp>
the file pointer (default is stdout)
returns
the string
Page 13
XLISP: An Experimental Object Oriented Language
FUNCTIONS
Page 14
String Functions:
(strcat <expr>...) CONCATENATE STRINGS
<expr>
string expressions
returns
result of concatenating the strings
(strlen <expr>) COMPUTE THE LENGTH OF A STRING
<expr>
the string expression
returns
the length of the string
(substr <expr> <sexpr> [<lexpr>]) RETURN SUBSTRING
<expr>
string expression
<sexpr>
starting position
<lexpr>
optional length (default is rest of string)
returns
substring starting at <sexpr> for <lexpr>
(ascii <expr>)
<expr>
returns
NUMERIC VALUE OF CHARACTER
string expression
numeric value of first character (according to
(chr <expr>)
<expr>
returns
CHARACTER EQUIVALENT OF ASCII VALUE
numeric expression
one character string with ASCII equivalent of
(atoi <expr>)
<expr>
returns
CONVERT AN ASCII STRING TO AN INTEGER
string expression
the integer value of the string expression
(itoa <expr>)
<expr>
returns
CONVERT AN INTEGER TO AN ASCII STRING
integer expression
the string representation of the integer value
ASCII)
<expr>
XLISP: An Experimental Object Oriented Language
FUNCTIONS
Page 15
List Functions:
(head <expr>)
or
(car <expr)
<expr>
returns
RETURN THE HEAD ELEMENT OF A LIST
(tail <expr>)
or
(cdr <expr>)
<expr>
returns
RETURN THE TAIL ELEMENTS OF A LIST
the list
the first element of the list
the list
the list minus the first element
(list <expr>...)
CREATE A LIST OF VALUES
<expr>
evaluated expressions to be combined into a list
returns
the new list
(nth <n> <list>)
RETURN THE NTH ELEMENT OF A LIST
<n>
the number of the element to return
<list>
the list to return the nth element of
returns
the nth element or nil if the list isn't that
long
(append <expr>...)
APPEND LISTS
<expr>
lists whose elements are to be appended
returns
the new list
(cons <e1> <e2>)
<e1>
becomes
<e2>
becomes
returns
the new
CONSTRUCT A NEW LIST ELEMENT
the head (car) of the new list
the tail (cdr) of the new list
list
(null <expr>)
<expr>
returns
CHECKS FOR AN EMPTY LIST
the list to check
t if the list is empty, nil otherwise
(atom <expr>)
<expr>
returns
CHECKS FOR AN ATOM (ANYTHING THAT ISN'T A LIST)
the expression to check
t if the value is an atom, nil otherwise
(listp <expr>)
<expr>
returns
CHECKS FOR A LIST
the expression to check
t if the value is a list, nil otherwise
XLISP: An Experimental Object Oriented Language
FUNCTIONS
(type <expr>)
<expr>
returns
Page 16
RETURNS THE TYPE OF THE EXPRESSION
the expression to return the type of
nil if the value is nil otherwise one of the
symbols:
SYM
OBJ
LIST
KMAP
SUBR
STR
INT
FPTR
for
for
for
for
for
for
for
for
symbols
objects
list nodes
keymap nodes
internal subroutine nodes
string nodes
integer nodes
file pointer nodes
(eq <expr1> <expr2>)
CHECKS FOR THE EXPRESSIONS BEING THE SAME
<expr1>
the first expression
<expr2>
the second expression
returns
t if they are equal, nil otherwise
(equal <expr1> <expr2>) CHECKS FOR THE EXPRESSIONS BEING EQUAL
<expr1>
the first expression
<expr2>
the second expression
returns
t if they are equal, nil otherwise
(read [ <str> ])
READ AN XLISP EXPRESSION
<str>
the string to use as input (optional)
returns
the expression read
(reverse <expr>)
REVERSE A LIST
<expr>
the list to reverse
returns
a new list in the reverse order
(length <expr>) FIND THE LENGTH OF A LIST
<expr>
the list to find the length of
returns
the length
XLISP: An Experimental Object Oriented Language
FUNCTIONS
Page 17
Arithmetic Functions:
(+ <expr>...)
<expr>
returns
ADD A LIST OF VALUES
expressions to be added
the result of the addition
(- <expr>...)
<expr>
returns
SUBTRACT A LIST OF VALUES
expressions to be subtracted
the result of the subtraction
(* <expr>...)
<expr>
returns
MULTIPLY A LIST OF VALUES
expressions to be multiplied
the result of the multiplication
(/ <expr>...)
<expr>
returns
DIVIDE A LIST OF VALUES
expressions to be divided
the result of the division
(% <expr>...)
<expr>
returns
MODulus A LIST OF VALUES
expressions to be MODulused
the result of mod
(& <expr>...)
<expr>
returns
THE BITWISE AND OF A LIST OF VALUES
expressions to be ANDed
the bit by bit ANDing of expressions
(| <expr...)
<expr>
returns
THE BITWISE OR OF A LIST OF VALUES
expressions to be ORed
the bit by bit ORing of expressions
(~ <expr>)
<expr>
returns
THE BITWISE NOT OF A VALUE
expression to be NOTed
the bit by bit inversion of expression
(min <expr>...) THE SMALLEST OF A LIST OF VALUES
<expr>
expressions to be checked
returns
the smallest value of the list
(max <expr>...) THE LARGEST OF A LIST OF VALUES
<expr>
expressions to be checked
returns
the largest value of the list
(abs <expr>)
<expr>
returns
THE ABSOLUTE VALUE OF AN EXPRESSION
integer expression
the absolute value of the expression
XLISP: An Experimental Object Oriented Language
FUNCTIONS
Page 18
Boolean Functions:
(&& <expr>...)
<expr>
returns
THE LOGICAL AND OF A LIST OF VALUES
expressions to be ANDed
the result of anding the expressions
(evaluation of expressions stops after the first
expression that evaluates to false)
(|| <expr>...)
<expr>
returns
THE LOGICAL OR OF A LIST OF VALUES
expressions to be ORed
the result of oring the expressions
(evaluation of expressions stops after the first
expression that evaluates to true)
(! <expr>)
<expr>
return
THE LOGICAL NOT OF A VALUE
expression to be NOTed
logical not of <expr>
XLISP: An Experimental Object Oriented Language
FUNCTIONS
Page 19
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'.
(< <e1> <e2>)
<e1>
<e2>
returns
TEST FOR LESS THAN
the left operand of the comparison
the right operand of the comparison
the result of comparing <e1> with <e2>
(<= <e1> <e2>)
<e1>
<e2>
returns
TEST FOR LESS THAN OR EQUAL TO
the left operand of the comparison
the right operand of the comparison
the result of comparing <e1> with <e2>
(== <e1> <e2>)
<e1>
<e2>
returns
TEST FOR EQUAL TO
the left operand of the comparison
the right operand of the comparison
the result of comparing <e1> with <e2>
(!= <e1> <e2>)
<e1>
<e2>
returns
TEST FOR NOT EQUAL TO
the left operand of the comparison
the right operand of the comparison
the result of comparing <e1> with <e2>
(>= <e1> <e2>)
<e1>
<e2>
returns
TEST FOR GREATER THAN OR EQUAL TO
the left operand of the comparison
the right operand of the comparison
the result of comparing <e1> with <e2>
(> <e1> <e2>)
<e1>
<e2>
returns
he comparison
returns
(==
TEST FOR GREATER THAN
the left operand of the comparison
the right operand of the comparison
the result of comparing <e1> with <e2>
the result of comparing <e1> with <e2>
Download