diff --git a/README.md b/README.md index 9cdc5c9..c8781ef 100644 --- a/README.md +++ b/README.md @@ -89,9 +89,9 @@ Intended deviations from the behaviour of the real Lisp reader are as follows: 1. It reads the meta-expression language `MEXPR` in addition to the symbolic expression language `SEXPR`, which I do not believe the Lisp 1.5 reader ever did; -2. It treats everything between a semi-colon and an end of line as a comment, - as most modern Lisps do; but I do not believe Lisp 1.5 had this feature. - +2. It treats everything between a double semi-colon and an end of line as + a comment, as most modern Lisps do; but I do not believe Lisp 1.5 had + this feature. ### BUT WHY?!!?! @@ -116,7 +116,6 @@ implementations. I'm convinced you could still use Lisp 1.5 for interesting and useful software (which isn't to say that some modern Lisps aren't better, but this is software which is almost sixty years old). - ## Installation At present, clone the source and build it using diff --git a/docs/codox/beowulf.bootstrap.html b/docs/codox/beowulf.bootstrap.html index 4bd0a94..e2883df 100644 --- a/docs/codox/beowulf.bootstrap.html +++ b/docs/codox/beowulf.bootstrap.html @@ -1,12 +1,20 @@ -beowulf.bootstrap documentation

beowulf.bootstrap

Lisp as defined in Chapter 1 (pages 1-14) of the Lisp 1.5 Programmer's Manual; that is to say, a very simple Lisp language, which should, I believe, be sufficient in conjunction with the functions provided by beowulf.host, be sufficient to bootstrap the full Lisp 1.5 interpreter..

-

The convention is adopted that functions in this file with names in ALLUPPERCASE are Lisp 1.5 functions (although written in Clojure) and that therefore all arguments must be numbers, symbols or beowulf.cons_cell.ConsCell objects.

*options*

dynamic

Command line options from invocation.

APPEND

(APPEND x y)

Append the the elements of y to the elements of x.

-

All args are assumed to be beowulf.cons-cell/ConsCell objects. See page 11 of the Lisp 1.5 Programmers Manual.

APPLY

(APPLY function args environment)

For bootstrapping, at least, a version of APPLY written in Clojure. All args are assumed to be symbols or beowulf.cons-cell/ConsCell objects. See page 13 of the Lisp 1.5 Programmers Manual.

ASSOC

(ASSOC x a)

If a is an association list such as the one formed by PAIRLIS in the above example, then assoc will produce the first pair whose first term is x. Thus it is a table searching function.

-

All args are assumed to be beowulf.cons-cell/ConsCell objects. See page 12 of the Lisp 1.5 Programmers Manual.

ATOM

macro

(ATOM x)

Returns T if and only is the argument x is bound to and atom; else F. It is not clear to me from the documentation whether (ATOM 7) should return T or F. I’m going to assume T.

ATOM?

macro

(ATOM? x)

The convention of returning F from predicates, rather than NIL, is going to tie me in knots. This is a variant of ATOM which returns NIL on failure.

CAAAAR

(CAAAAR x)

TODO: write docs

CAAADR

(CAAADR x)

TODO: write docs

CAAAR

(CAAAR x)

TODO: write docs

CAADAR

(CAADAR x)

TODO: write docs

CAADDR

(CAADDR x)

TODO: write docs

CAADR

(CAADR x)

TODO: write docs

CAAR

(CAAR x)

TODO: write docs

CADAAR

(CADAAR x)

TODO: write docs

CADADR

(CADADR x)

TODO: write docs

CADAR

(CADAR x)

TODO: write docs

CADDAR

(CADDAR x)

TODO: write docs

CADDDR

(CADDDR x)

TODO: write docs

CADDR

(CADDR x)

TODO: write docs

CADR

(CADR x)

TODO: write docs

CAR

(CAR x)

Return the item indicated by the first pointer of a pair. NIL is treated specially: the CAR of NIL is NIL.

CDAAAR

(CDAAAR x)

TODO: write docs

CDAADR

(CDAADR x)

TODO: write docs

CDAAR

(CDAAR x)

TODO: write docs

CDADAR

(CDADAR x)

TODO: write docs

CDADDR

(CDADDR x)

TODO: write docs

CDADR

(CDADR x)

TODO: write docs

CDAR

(CDAR x)

TODO: write docs

CDDAAR

(CDDAAR x)

TODO: write docs

CDDADR

(CDDADR x)

TODO: write docs

CDDAR

(CDDAR x)

TODO: write docs

CDDDAR

(CDDDAR x)

TODO: write docs

CDDDDR

(CDDDDR x)

TODO: write docs

CDDDR

(CDDDR x)

TODO: write docs

CDDR

(CDDR x)

TODO: write docs

CDR

(CDR x)

Return the item indicated by the second pointer of a pair. NIL is treated specially: the CDR of NIL is NIL.

EQ

(EQ x y)

Returns T if and only if both x and y are bound to the same atom, else F.

EQUAL

(EQUAL x y)

This is a predicate that is true if its two arguments are identical S-expressions, and false if they are different. (The elementary predicate EQ is defined only for atomic arguments.) The definition of EQUAL is an example of a conditional expression inside a conditional expression.

-

NOTE: returns F on failure, not NIL

EVAL

(EVAL expr env)

For bootstrapping, at least, a version of EVAL written in Clojure. All args are assumed to be symbols or beowulf.cons-cell/ConsCell objects. See page 13 of the Lisp 1.5 Programmers Manual.

MEMBER

(MEMBER x y)

This predicate is true if the S-expression x occurs among the elements of the list y.

-

All args are assumed to be symbols or beowulf.cons-cell/ConsCell objects. See page 11 of the Lisp 1.5 Programmers Manual.

NULL

macro

(NULL x)

Returns T if and only if the argument x is bound to NIL; else F.

oblist

The default environment.

PAIRLIS

(PAIRLIS x y a)

This function gives the list of pairs of corresponding elements of the lists x and y, and APPENDs this to the list a. The resultant list of pairs, which is like a table with two columns, is called an association list.

+beowulf.bootstrap documentation

beowulf.bootstrap

Lisp as defined in Chapter 1 (pages 1-14) of the Lisp 1.5 Programmer's Manual; that is to say, a very simple Lisp language, which should, I believe, be sufficient in conjunction with the functions provided by beowulf.host, be sufficient to bootstrap the full Lisp 1.5 interpreter..

+

The convention is adopted that functions in this file with names in ALLUPPERCASE are Lisp 1.5 functions (although written in Clojure) and that therefore all arguments must be numbers, symbols or beowulf.cons_cell.ConsCell objects.

*options*

dynamic

Command line options from invocation.

APPEND

(APPEND x y)

Append the the elements of y to the elements of x.

+

All args are assumed to be beowulf.cons-cell/ConsCell objects. See page 11 of the Lisp 1.5 Programmers Manual.

APPLY

(APPLY function args environment)

For bootstrapping, at least, a version of APPLY written in Clojure. All args are assumed to be symbols or beowulf.cons-cell/ConsCell objects. See page 13 of the Lisp 1.5 Programmers Manual.

ASSOC

(ASSOC x a)

If a is an association list such as the one formed by PAIRLIS in the above example, then assoc will produce the first pair whose first term is x. Thus it is a table searching function.

+

All args are assumed to be beowulf.cons-cell/ConsCell objects. See page 12 of the Lisp 1.5 Programmers Manual.

ATOM

macro

(ATOM x)

Returns T if and only if the argument x is bound to an atom; else F. It is not clear to me from the documentation whether (ATOM 7) should return T or F. I’m going to assume T.

ATOM?

macro

(ATOM? x)

The convention of returning F from predicates, rather than NIL, is going to tie me in knots. This is a variant of ATOM which returns NIL on failure.

CAAAAR

macro

(CAAAAR x)

TODO: write docs

CAAADR

macro

(CAAADR x)

TODO: write docs

CAAAR

macro

(CAAAR x)

TODO: write docs

CAADAR

macro

(CAADAR x)

TODO: write docs

CAADDR

macro

(CAADDR x)

TODO: write docs

CAADR

macro

(CAADR x)

TODO: write docs

CAAR

macro

(CAAR x)

TODO: write docs

CADAAR

macro

(CADAAR x)

TODO: write docs

CADADR

macro

(CADADR x)

TODO: write docs

CADAR

macro

(CADAR x)

TODO: write docs

CADDAR

macro

(CADDAR x)

TODO: write docs

CADDDR

macro

(CADDDR x)

TODO: write docs

CADDR

macro

(CADDR x)

TODO: write docs

CADR

macro

(CADR x)

TODO: write docs

CAR

(CAR x)

Return the item indicated by the first pointer of a pair. NIL is treated specially: the CAR of NIL is NIL.

CDAAAR

macro

(CDAAAR x)

TODO: write docs

CDAADR

macro

(CDAADR x)

TODO: write docs

CDAAR

macro

(CDAAR x)

TODO: write docs

CDADAR

macro

(CDADAR x)

TODO: write docs

CDADDR

macro

(CDADDR x)

TODO: write docs

CDADR

macro

(CDADR x)

TODO: write docs

CDAR

macro

(CDAR x)

TODO: write docs

CDDAAR

macro

(CDDAAR x)

TODO: write docs

CDDADR

macro

(CDDADR x)

TODO: write docs

CDDAR

macro

(CDDAR x)

TODO: write docs

CDDDAR

macro

(CDDDAR x)

TODO: write docs

CDDDDR

macro

(CDDDDR x)

TODO: write docs

CDDDR

macro

(CDDDR x)

TODO: write docs

CDDR

macro

(CDDR x)

TODO: write docs

CDR

(CDR x)

Return the item indicated by the second pointer of a pair. NIL is treated specially: the CDR of NIL is NIL.

CONS

(CONS car cdr)

Construct a new instance of cons cell with this car and cdr.

DEFINE

(DEFINE args)

Bootstrap-only version of DEFINE which, post boostrap, can be overwritten in LISP.

+

The single argument to DEFINE should be an assoc list which should be nconc’ed onto the front of the oblist. Broadly, (SETQ OBLIST (NCONC ARG1 OBLIST))

EQ

(EQ x y)

Returns T if and only if both x and y are bound to the same atom, else NIL.

EQUAL

(EQUAL x y)

This is a predicate that is true if its two arguments are identical S-expressions, and false if they are different. (The elementary predicate EQ is defined only for atomic arguments.) The definition of EQUAL is an example of a conditional expression inside a conditional expression.

+

NOTE: returns F on failure, not NIL

EVAL

(EVAL expr)(EVAL expr env)

For bootstrapping, at least, a version of EVAL written in Clojure. All args are assumed to be symbols or beowulf.cons-cell/ConsCell objects. See page 13 of the Lisp 1.5 Programmers Manual.

eval-internal

(eval-internal expr env)

Common guts for both EVAL and traced-eval

INTEROP

(INTEROP & args__3196__auto__)

Clojure (or other host environment) interoperation API. fn-symbol is expected to be either

+
    +
  1. a symbol bound in the host environment to a function; or
  2. +
  3. a sequence (list) of symbols forming a qualified path name bound to a function.
  4. +
+

Lower case characters cannot normally be represented in Lisp 1.5, so both the upper case and lower case variants of fn-symbol will be tried. If the function you’re looking for has a mixed case name, that is not currently accessible.

+

args is expected to be a Lisp 1.5 list of arguments to be passed to that function. Return value must be something acceptable to Lisp 1.5, so either a symbol, a number, or a Lisp 1.5 list.

+

If fn-symbol is not found (even when cast to lower case), or is not a function, or the value returned cannot be represented in Lisp 1.5, an exception is thrown with :cause bound to :interop and :detail set to a value representing the actual problem.

interop-interpret-q-name

(interop-interpret-q-name & args__3196__auto__)

For interoperation with Clojure, it will often be necessary to pass qualified names that are not representable in Lisp 1.5. This function takes a sequence in the form (PART PART PART... NAME) and returns a symbol in the form PART.PART.PART/NAME. This symbol will then be tried in both that form and lower-cased. Names with hyphens or underscores cannot be represented with this scheme.

MEMBER

(MEMBER x y)

This predicate is true if the S-expression x occurs among the elements of the list y.

+

All args are assumed to be symbols or beowulf.cons-cell/ConsCell objects. See page 11 of the Lisp 1.5 Programmers Manual.

NILP

macro

(NILP x)

Not part of LISP 1.5: T if o is NIL, else NIL.

NULL

macro

(NULL x)

Returns T if and only if the argument x is bound to NIL; else F.

OBLIST

(OBLIST)

Not certain whether or not this is part of LISP 1.5; adapted from PSL. return the current value of the object list. Note that in PSL this function returns a list of the symbols bound, not the whole association list.

oblist

The default environment.

PAIRLIS

(PAIRLIS x y a)

This function gives the list of pairs of corresponding elements of the lists x and y, and APPENDs this to the list a. The resultant list of pairs, which is like a table with two columns, is called an association list.

Eessentially, it builds the environment on the stack, implementing shallow binding.

-

All args are assumed to be beowulf.cons-cell/ConsCell objects. See page 12 of the Lisp 1.5 Programmers Manual.

SUBLIS

(SUBLIS a y)

Here a is assumed to be an association list of the form ((ul . vl)...(un . vn)), where the us are atomic, and y is any S-expression. What SUBLIS does, is to treat the us as variables when they occur in y, and to SUBSTitute the corresponding vs from the pair list.

+

All args are assumed to be beowulf.cons-cell/ConsCell objects. See page 12 of the Lisp 1.5 Programmers Manual.

QUOTE

macro

(QUOTE f)

Quote, but in upper case for LISP 1.5

SET

(SET symbol val)

Implementation of SET in Clojure. Add to the oblist a binding of the value of var to the value of val. NOTE WELL: this is not SETQ!

SUBLIS

(SUBLIS a y)

Here a is assumed to be an association list of the form ((ul . vl)...(un . vn)), where the us are atomic, and y is any S-expression. What SUBLIS does, is to treat the us as variables when they occur in y, and to SUBSTitute the corresponding vs from the pair list.

My interpretation is that this is variable binding in the stack frame.

-

All args are assumed to be beowulf.cons-cell/ConsCell objects. See page 12 of the Lisp 1.5 Programmers Manual.

SUBST

(SUBST x y z)

This function gives the result of substituting the S-expression x for all occurrences of the atomic symbol y in the S-expression z.

traced-eval

(traced-eval & args__2885__auto__)

Essentially, identical to EVAL except traced.

uaf

(uaf l path)

Universal access function; l is expected to be an arbitrary list, path a (clojure) list of the characters a and d. Intended to make declaring all those fiddly #'c[ad]+r' functions a bit easier

\ No newline at end of file +

All args are assumed to be beowulf.cons-cell/ConsCell objects. See page 12 of the Lisp 1.5 Programmers Manual.

SUBST

(SUBST x y z)

This function gives the result of substituting the S-expression x for all occurrences of the atomic symbol y in the S-expression z.

to-beowulf

(to-beowulf o)

Return a beowulf-native representation of the Clojure object o. Numbers and symbols are unaffected. Collections have to be converted; strings must be converted to symbols.

to-clojure

(to-clojure l)

If l is a beowulf.cons_cell.ConsCell, return a Clojure list having the same members in the same order.

traced-eval

(traced-eval & args__3196__auto__)

Essentially, identical to EVAL except traced.

uaf

(uaf l path)

Universal access function; l is expected to be an arbitrary LISP list, path a (clojure) list of the characters a and d. Intended to make declaring all those fiddly #'c[ad]+r' functions a bit easier

\ No newline at end of file diff --git a/docs/codox/beowulf.cons-cell.html b/docs/codox/beowulf.cons-cell.html index 5db7b53..df217e8 100644 --- a/docs/codox/beowulf.cons-cell.html +++ b/docs/codox/beowulf.cons-cell.html @@ -1,3 +1,3 @@ -beowulf.cons-cell documentation

beowulf.cons-cell

The fundamental cons cell on which all Lisp structures are built. Lisp 1.5 lists do not necessarily have a sequence as their CDR, so cannot be implemented on top of Clojure lists.

F

The canonical false value - different from NIL, which is not canonically false in Lisp 1.5.

make-beowulf-list

(make-beowulf-list x)

Construct a linked list of cons cells with the same content as the sequence x.

make-cons-cell

macro

(make-cons-cell car cdr)

Construct a new instance of cons cell with this car and cdr.

NIL

The canonical empty list symbol.

pretty-print

(pretty-print cell)(pretty-print cell width level)

This isn’t the world’s best pretty printer but it sort of works.

T

The canonical true value.

\ No newline at end of file +beowulf.cons-cell documentation

beowulf.cons-cell

The fundamental cons cell on which all Lisp structures are built. Lisp 1.5 lists do not necessarily have a sequence as their CDR, and must have both CAR and CDR mutable, so cannot be implemented on top of Clojure lists.

cons-cell?

(cons-cell? o)

Is this object o a beowulf cons-cell?

F

The canonical false value - different from NIL, which is not canonically false in Lisp 1.5.

make-beowulf-list

(make-beowulf-list x)

Construct a linked list of cons cells with the same content as the sequence x.

make-cons-cell

(make-cons-cell car cdr)

Construct a new instance of cons cell with this car and cdr.

MutableSequence

protocol

Like a sequence, but mutable.

members

getCar

(getCar this)

Return the first element of this sequence.

getCdr

(getCdr this)

like more, q.v., but returns List NIL not Clojure nil when empty.

getUid

(getUid this)

Returns a unique identifier for this object

rplaca

(rplaca this value)

replace the first element of this sequence with this value

rplacd

(rplacd this value)

replace the rest (but-first; cdr) of this sequence with this value

NIL

The canonical empty list symbol.

pretty-print

(pretty-print cell)(pretty-print cell width level)

This isn’t the world’s best pretty printer but it sort of works.

T

The canonical true value.

\ No newline at end of file diff --git a/docs/codox/beowulf.core.html b/docs/codox/beowulf.core.html index f5e11fe..26270d3 100644 --- a/docs/codox/beowulf.core.html +++ b/docs/codox/beowulf.core.html @@ -1,3 +1,3 @@ -beowulf.core documentation

beowulf.core

Essentially, the -main function and the bootstrap read-eval-print loop.

-main

(-main & opts)

Parse options, print the banner, read the init file if any, and enter the read/eval/print loop.

cli-options

TODO: write docs

repl

(repl prompt)

Read/eval/print loop.

\ No newline at end of file +beowulf.core documentation

beowulf.core

Essentially, the -main function and the bootstrap read-eval-print loop.

-main

(-main & opts)

Parse options, print the banner, read the init file if any, and enter the read/eval/print loop.

cli-options

TODO: write docs

repl

(repl prompt)

Read/eval/print loop.

stop-word

TODO: write docs

\ No newline at end of file diff --git a/docs/codox/beowulf.host.html b/docs/codox/beowulf.host.html index 946857b..ee61b9b 100644 --- a/docs/codox/beowulf.host.html +++ b/docs/codox/beowulf.host.html @@ -1,3 +1,3 @@ -beowulf.host documentation

beowulf.host

provides Lisp 1.5 functions which can’t be (or can’t efficiently be) implemented in Lisp 1.5, which therefore need to be implemented in the host language, in this case Clojure.

\ No newline at end of file +beowulf.host documentation

beowulf.host

provides Lisp 1.5 functions which can’t be (or can’t efficiently be) implemented in Lisp 1.5, which therefore need to be implemented in the host language, in this case Clojure.

ADD1

(ADD1 x)

TODO: write docs

DIFFERENCE

(DIFFERENCE x y)

TODO: write docs

FIXP

(FIXP x)

TODO: write docs

LIST

(LIST & args)

TODO: write docs

NUMBERP

(NUMBERP x)

TODO: write docs

PLUS2

(PLUS2 x y)

Lisp 1.5 PLUS is varargs, and implementing varargs functions in Clojure is not an added complexity I want. So this is a two arg PLUS, on which a varargs PLUS can be built in the Lisp 1.5 layer using REDUCE.

QUOTIENT

(QUOTIENT x y)

I’m not certain from the documentation whether Lisp 1.5 QUOTIENT returned the integer part of the quotient, or a realnum representing the whole quotient. I am for now implementing the latter.

REMAINDER

(REMAINDER x y)

TODO: write docs

RPLACA

(RPLACA cell value)

Replace the CAR pointer of this cell with this value. Dangerous, should really not exist, but does in Lisp 1.5 (and was important for some performance hacks in early Lisps)

RPLACD

(RPLACD cell value)

Replace the CDR pointer of this cell with this value. Dangerous, should really not exist, but does in Lisp 1.5 (and was important for some performance hacks in early Lisps)

SUB1

(SUB1 x)

TODO: write docs

TIMES2

(TIMES2 x y)

TODO: write docs

\ No newline at end of file diff --git a/docs/codox/beowulf.read.html b/docs/codox/beowulf.read.html index 3deec4d..e5d4d8b 100644 --- a/docs/codox/beowulf.read.html +++ b/docs/codox/beowulf.read.html @@ -1,9 +1,9 @@ -beowulf.read documentation

beowulf.read

This provides the reader required for boostrapping. It’s not a bad reader - it provides feedback on errors found in the input - but it isn’t the real Lisp reader.

+beowulf.read documentation

beowulf.read

This provides the reader required for boostrapping. It’s not a bad reader - it provides feedback on errors found in the input - but it isn’t the real Lisp reader.

Intended deviations from the behaviour of the real Lisp reader are as follows:

    -
  1. It reads the meta-expression language MEXPR in addition to the symbolic expression language SEXPR, which I do not believe the Lisp 1.5 reader ever did;
  2. +
  3. It reads the meta-expression language MEXPR in addition to fLAMthe symbolic expression language SEXPR, which I do not believe the Lisp 1.5 reader ever did;
  4. It treats everything between a semi-colon and an end of line as a comment, as most modern Lisps do; but I do not believe Lisp 1.5 had this feature.
-

Both these extensions can be disabled by using the --strict command line switch.

gen-cond

(gen-cond p)

Generate a cond statement from this simplified parse tree fragment p; returns nil if p does not represent a (MEXPR) cond statement.

gen-cond-clause

(gen-cond-clause p)

Generate a cond clause from this simplified parse tree fragment p; returns nil if p does not represent a cond clause.

gen-dot-terminated-list

(gen-dot-terminated-list p)

Generate a list, which may be dot-terminated, from this partial parse tree ‘p’. Note that the function acts recursively and progressively decapitates its argument, so that the argument will not always be a valid parse tree.

gen-fn-call

(gen-fn-call p)

Generate a function call from this simplified parse tree fragment p; returns nil if p does not represent a (MEXPR) function call.

generate

(generate p)

Generate lisp structure from this parse tree p. It is assumed that p has been simplified.

gsp

macro

(gsp s)

Shortcut macro - the internals of read; or, if you like, read-string. Argument s should be a string representation of a valid Lisp expression.

parse

Parse a string presented as argument into a parse tree which can then be operated upon further.

READ

(READ input)

An implementation of a Lisp reader sufficient for bootstrapping; not necessarily the final Lisp reader.

simplify

(simplify p)(simplify p context)

Simplify this parse tree p. If p is an instaparse failure object, throw an ex-info, with p as the value of its :failure key.

strip-leading-zeros

(strip-leading-zeros s)(strip-leading-zeros s prefix)

read-string interprets strings with leading zeros as octal; strip any from this string s. If what’s left is empty (i.e. there were only zeros, return "0".

\ No newline at end of file +

Both these extensions can be disabled by using the --strict command line switch.

gsp

(gsp s)

Shortcut macro - the internals of read; or, if you like, read-string. Argument s should be a string representation of a valid Lisp expression.

number-lines

(number-lines s)(number-lines s e)

TODO: write docs

READ

(READ)(READ input)

An implementation of a Lisp reader sufficient for bootstrapping; not necessarily the final Lisp reader. input should be either a string representation of a LISP expression, or else an input stream. A single form will be read.

read-from-console

(read-from-console)

Attempt to read a complete lisp expression from the console.

strip-line-comments

(strip-line-comments s)

Strip blank lines and comment lines from this string s, expected to be Lisp source.

\ No newline at end of file diff --git a/docs/codox/beowulf.reader.generate.html b/docs/codox/beowulf.reader.generate.html new file mode 100644 index 0000000..3788098 --- /dev/null +++ b/docs/codox/beowulf.reader.generate.html @@ -0,0 +1,3 @@ + +beowulf.reader.generate documentation

beowulf.reader.generate

TODO: write docs

gen-cond

(gen-cond p)

Generate a cond statement from this simplified parse tree fragment p; returns nil if p does not represent a (MEXPR) cond statement.

gen-cond-clause

(gen-cond-clause p)

Generate a cond clause from this simplified parse tree fragment p; returns nil if p does not represent a cond clause.

gen-dot-terminated-list

(gen-dot-terminated-list p)

Generate a list, which may be dot-terminated, from this partial parse tree ‘p’. Note that the function acts recursively and progressively decapitates its argument, so that the argument will not always be a valid parse tree.

gen-fn-call

(gen-fn-call p)

Generate a function call from this simplified parse tree fragment p; returns nil if p does not represent a (MEXPR) function call.

gen-iexpr

(gen-iexpr tree)

TODO: write docs

generate

(generate p)

Generate lisp structure from this parse tree p. It is assumed that p has been simplified.

generate-assign

(generate-assign tree)

Generate an assignment statement based on this tree. If the thing being assigned to is a function signature, then we have to do something different to if it’s an atom.

generate-defn

(generate-defn tree)

TODO: write docs

generate-set

(generate-set tree)

Actually not sure what the mexpr representation of set looks like

strip-leading-zeros

(strip-leading-zeros s)(strip-leading-zeros s prefix)

read-string interprets strings with leading zeros as octal; strip any from this string s. If what’s left is empty (i.e. there were only zeros, return "0".

\ No newline at end of file diff --git a/docs/codox/beowulf.reader.macros.html b/docs/codox/beowulf.reader.macros.html new file mode 100644 index 0000000..5534744 --- /dev/null +++ b/docs/codox/beowulf.reader.macros.html @@ -0,0 +1,3 @@ + +beowulf.reader.macros documentation

beowulf.reader.macros

Can I implement reader macros? let’s see!

*readmacros*

dynamic

TODO: write docs

expand-macros

(expand-macros form)

TODO: write docs

\ No newline at end of file diff --git a/docs/codox/beowulf.reader.parser.html b/docs/codox/beowulf.reader.parser.html new file mode 100644 index 0000000..43b0526 --- /dev/null +++ b/docs/codox/beowulf.reader.parser.html @@ -0,0 +1,3 @@ + +beowulf.reader.parser documentation

beowulf.reader.parser

The actual parser, supporting both S-expression and M-expression syntax.

parse

Parse a string presented as argument into a parse tree which can then be operated upon further.

\ No newline at end of file diff --git a/docs/codox/beowulf.reader.simplify.html b/docs/codox/beowulf.reader.simplify.html new file mode 100644 index 0000000..11b5d77 --- /dev/null +++ b/docs/codox/beowulf.reader.simplify.html @@ -0,0 +1,3 @@ + +beowulf.reader.simplify documentation

beowulf.reader.simplify

Simplify parse trees. Be aware that this is very tightly coupled with the parser.

remove-nesting

(remove-nesting tree context)

TODO: write docs

remove-optional-space

(remove-optional-space tree)

TODO: write docs

simplify

(simplify p)(simplify p context)

Simplify this parse tree p. If p is an instaparse failure object, throw an ex-info, with p as the value of its :failure key.

\ No newline at end of file diff --git a/docs/codox/index.html b/docs/codox/index.html index 7dfb2d3..d77bafa 100644 --- a/docs/codox/index.html +++ b/docs/codox/index.html @@ -1,3 +1,3 @@ -Beowulf 0.2.0

Beowulf 0.2.0

Released under the GPL-2.0-or-later

An implementation of LISP 1.5 in Clojure.

Installation

To install, add the following dependency to your project or build file:

[beowulf "0.2.0"]

Topics

Namespaces

beowulf.bootstrap

Lisp as defined in Chapter 1 (pages 1-14) of the Lisp 1.5 Programmer's Manual; that is to say, a very simple Lisp language, which should, I believe, be sufficient in conjunction with the functions provided by beowulf.host, be sufficient to bootstrap the full Lisp 1.5 interpreter..

beowulf.cons-cell

The fundamental cons cell on which all Lisp structures are built. Lisp 1.5 lists do not necessarily have a sequence as their CDR, so cannot be implemented on top of Clojure lists.

Public variables and functions:

beowulf.core

Essentially, the -main function and the bootstrap read-eval-print loop.

Public variables and functions:

beowulf.host

provides Lisp 1.5 functions which can’t be (or can’t efficiently be) implemented in Lisp 1.5, which therefore need to be implemented in the host language, in this case Clojure.

Public variables and functions:

    beowulf.read

    This provides the reader required for boostrapping. It’s not a bad reader - it provides feedback on errors found in the input - but it isn’t the real Lisp reader.

    \ No newline at end of file +Beowulf 0.2.1-SNAPSHOT

    Beowulf 0.2.1-SNAPSHOT

    Released under the GPL-2.0-or-later

    An implementation of LISP 1.5 in Clojure.

    Installation

    To install, add the following dependency to your project or build file:

    [beowulf "0.2.1-SNAPSHOT"]

    Topics

    Namespaces

    beowulf.bootstrap

    Lisp as defined in Chapter 1 (pages 1-14) of the Lisp 1.5 Programmer's Manual; that is to say, a very simple Lisp language, which should, I believe, be sufficient in conjunction with the functions provided by beowulf.host, be sufficient to bootstrap the full Lisp 1.5 interpreter..

    beowulf.cons-cell

    The fundamental cons cell on which all Lisp structures are built. Lisp 1.5 lists do not necessarily have a sequence as their CDR, and must have both CAR and CDR mutable, so cannot be implemented on top of Clojure lists.

    beowulf.core

    Essentially, the -main function and the bootstrap read-eval-print loop.

    Public variables and functions:

    beowulf.host

    provides Lisp 1.5 functions which can’t be (or can’t efficiently be) implemented in Lisp 1.5, which therefore need to be implemented in the host language, in this case Clojure.

    beowulf.read

    This provides the reader required for boostrapping. It’s not a bad reader - it provides feedback on errors found in the input - but it isn’t the real Lisp reader.

    Public variables and functions:

    beowulf.reader.generate

    TODO: write docs

    beowulf.reader.macros

    Can I implement reader macros? let’s see!

    Public variables and functions:

    beowulf.reader.parser

    The actual parser, supporting both S-expression and M-expression syntax.

    Public variables and functions:

    beowulf.reader.simplify

    Simplify parse trees. Be aware that this is very tightly coupled with the parser.

    Public variables and functions:

    \ No newline at end of file diff --git a/docs/codox/intro.html b/docs/codox/intro.html index 17bdf0c..0598d94 100644 --- a/docs/codox/intro.html +++ b/docs/codox/intro.html @@ -1,4 +1,4 @@ -Introduction to beowulf

    Introduction to beowulf

    +Introduction to beowulf

    Introduction to beowulf

    TODO: write great documentation

    \ No newline at end of file diff --git a/resources/img/screenshot.png b/resources/img/screenshot.png new file mode 100644 index 0000000..d8e5fb4 Binary files /dev/null and b/resources/img/screenshot.png differ diff --git a/resources/img/screenshot.xcf b/resources/img/screenshot.xcf new file mode 100644 index 0000000..18488bd Binary files /dev/null and b/resources/img/screenshot.xcf differ diff --git a/src/beowulf/bootstrap.clj b/src/beowulf/bootstrap.clj index 4e54623..1b01322 100644 --- a/src/beowulf/bootstrap.clj +++ b/src/beowulf/bootstrap.clj @@ -61,10 +61,10 @@ [x] `(if (or (symbol? ~x) (number? ~x)) T NIL)) -(defmacro CONS +(defn CONS "Construct a new instance of cons cell with this `car` and `cdr`." [car cdr] - `(beowulf.cons_cell.ConsCell. ~car ~cdr)) + (beowulf.cons_cell.ConsCell. car cdr (gensym "c"))) (defn CAR "Return the item indicated by the first pointer of a pair. NIL is treated @@ -406,12 +406,12 @@ "Implementation of SET in Clojure. Add to the `oblist` a binding of the value of `var` to the value of `val`. NOTE WELL: this is not SETQ!" [symbol val] - (doall + (when (swap! oblist (fn [ob s v] (make-cons-cell (make-cons-cell s v) ob)) symbol val) - NIL)) + NIL)) (defn APPLY "For bootstrapping, at least, a version of APPLY written in Clojure. @@ -426,7 +426,7 @@ (ATOM? function) T) (cond ;; (fn? (eval function)) (apply (eval function) args) - (not= + (not= (ASSOC function environment) NIL) (APPLY (CDR (ASSOC function environment)) args environment) (= function 'ATOM) (if (ATOM? (CAR args)) T NIL) @@ -436,15 +436,15 @@ (= function 'DEFINE) (DEFINE args) (= function 'EQ) (apply EQ args) (= function 'INTEROP) (INTEROP (CAR args) (CDR args)) - (= function 'SET) (SET (CAR args) (CADR args)) - (EVAL function environment)(APPLY - (EVAL function environment) - args - environment) + (= function 'SET) (SET (CAR args) (CADR args)) + (EVAL function environment) (APPLY + (EVAL function environment) + args + environment) :else (throw (ex-info "No function found" {:context "APPLY" - :function function - :args args}))) + :function function + :args args}))) (fn? function) ;; i.e., it's a Clojure function (apply function (to-clojure args)) (= (first function) 'LAMBDA) (EVAL diff --git a/src/beowulf/host.clj b/src/beowulf/host.clj index 38737a5..8992bb0 100644 --- a/src/beowulf/host.clj +++ b/src/beowulf/host.clj @@ -2,7 +2,7 @@ "provides Lisp 1.5 functions which can't be (or can't efficiently be) implemented in Lisp 1.5, which therefore need to be implemented in the host language, in this case Clojure." - (:require [beowulf.cons-cell :refer [T NIL F]] + (:require [beowulf.cons-cell :refer [F make-beowulf-list NIL T]] ;; note hyphen - this is Clojure... ) (:import [beowulf.cons_cell ConsCell] @@ -13,7 +13,6 @@ ;; those which can be implemented in Lisp should be, since that aids ;; portability. - (defn RPLACA "Replace the CAR pointer of this `cell` with this `value`. Dangerous, should really not exist, but does in Lisp 1.5 (and was important for some @@ -109,3 +108,7 @@ (defn NUMBERP [x] (if (number? x) T F)) + +(defn LIST + [& args] + (make-beowulf-list args)) \ No newline at end of file diff --git a/src/beowulf/read.clj b/src/beowulf/read.clj index 5757252..44cf163 100644 --- a/src/beowulf/read.clj +++ b/src/beowulf/read.clj @@ -15,7 +15,7 @@ switch." (:require [beowulf.reader.generate :refer [generate]] [beowulf.reader.parser :refer [parse]] - [beowulf.reader.simplify :refer [simplify]] + [beowulf.reader.simplify :refer [remove-optional-space simplify]] [clojure.string :refer [join split starts-with? trim]]) (:import [java.io InputStream] [instaparse.gll Failure])) @@ -61,7 +61,7 @@ (if (instance? Failure parse-tree) (doall (println (number-lines source parse-tree)) (throw (ex-info "Parse failed" (assoc parse-tree :source source)))) - (generate (simplify parse-tree))))) + (generate (simplify (remove-optional-space parse-tree)))))) (defn read-from-console "Attempt to read a complete lisp expression from the console." diff --git a/src/beowulf/reader/generate.clj b/src/beowulf/reader/generate.clj index bd0d38f..81f461e 100644 --- a/src/beowulf/reader/generate.clj +++ b/src/beowulf/reader/generate.clj @@ -1,5 +1,6 @@ (ns beowulf.reader.generate (:require [beowulf.cons-cell :refer [make-beowulf-list make-cons-cell NIL]] + [beowulf.reader.macros :refer [expand-macros]] [clojure.math.numeric-tower :refer [expt]] [clojure.string :refer [upper-case]])) @@ -119,6 +120,15 @@ (cons (generate (nth (second tree) 2)) (map generate (-> tree rest rest rest)))))))) +(defn gen-iexpr + [tree] + (let [bundle (reduce #(assoc %1 (first %2) (nth %2 1)) + {} + (rest tree))] + (list (generate (:iop bundle)) + (generate (:lhs bundle)) + (generate (:rhs bundle))))) + (defn generate-set "Actually not sure what the mexpr representation of set looks like" [tree] @@ -152,46 +162,51 @@ `p` has been simplified." [p] (try - (if - (coll? p) - (case (first p) - :λ "LAMBDA" - :λexpr (make-cons-cell - (generate (nth p 1)) - (make-cons-cell (generate (nth p 2)) - (generate (nth p 3)))) - :args (make-beowulf-list (map generate (rest p))) - :atom (symbol (second p)) - :bindings (generate (second p)) - :body (make-beowulf-list (map generate (rest p))) - :cond (gen-cond p) - :cond-clause (gen-cond-clause p) - (:decimal :integer) (read-string (strip-leading-zeros (second p))) - :defn (generate-assign p) - :dotted-pair (make-cons-cell - (generate (nth p 1)) - (generate (nth p 2))) - :exponent (generate (second p)) - :fncall (gen-fn-call p) - :list (gen-dot-terminated-list (rest p)) - :mvar (symbol (upper-case (second p))) - :octal (let [n (read-string (strip-leading-zeros (second p) "0")) - scale (generate (nth p 2))] - (* n (expt 8 scale))) + (expand-macros + (if + (coll? p) + (case (first p) + ">" 'GREATERP + :λ "LAMBDA" + :λexpr (make-cons-cell + (generate (nth p 1)) + (make-cons-cell (generate (nth p 2)) + (generate (nth p 3)))) + :args (make-beowulf-list (map generate (rest p))) + :atom (symbol (second p)) + :bindings (generate (second p)) + :body (make-beowulf-list (map generate (rest p))) + :cond (gen-cond p) + :cond-clause (gen-cond-clause p) + (:decimal :integer) (read-string (strip-leading-zeros (second p))) + :defn (generate-assign p) + :dotted-pair (make-cons-cell + (generate (nth p 1)) + (generate (nth p 2))) + :exponent (generate (second p)) + :fncall (gen-fn-call p) + :iexpr (gen-iexpr p) + :list (gen-dot-terminated-list (rest p)) + (:lhs :rhs) (generate (second p)) + :mexpr (generate (second p)) + :mvar (symbol (upper-case (second p))) + :octal (let [n (read-string (strip-leading-zeros (second p) "0")) + scale (generate (nth p 2))] + (* n (expt 8 scale))) ;; the quote read macro (which probably didn't exist in Lisp 1.5, but...) - :quoted-expr (make-beowulf-list (list 'QUOTE (generate (second p)))) - :scale-factor (if - (empty? (second p)) 0 - (read-string (strip-leading-zeros (second p)))) - :scientific (let [n (generate (second p)) - exponent (generate (nth p 2))] - (* n (expt 10 exponent))) + :quoted-expr (make-beowulf-list (list 'QUOTE (generate (second p)))) + :scale-factor (if + (empty? (second p)) 0 + (read-string (strip-leading-zeros (second p)))) + :scientific (let [n (generate (second p)) + exponent (generate (nth p 2))] + (* n (expt 10 exponent))) ;; default - (throw (ex-info (str "Unrecognised head: " (first p)) - {:generating p}))) - p) + (throw (ex-info (str "Unrecognised head: " (first p)) + {:generating p}))) + p)) (catch Throwable any (throw (ex-info "Could not generate" {:generating p} diff --git a/src/beowulf/reader/macros.clj b/src/beowulf/reader/macros.clj new file mode 100644 index 0000000..2fc4214 --- /dev/null +++ b/src/beowulf/reader/macros.clj @@ -0,0 +1,36 @@ +(ns beowulf.reader.macros + "Can I implement reader macros? let's see!" + (:require [beowulf.bootstrap :refer [CADR CADDR CDDR CONS]] + [beowulf.cons-cell :refer [make-beowulf-list]] + [beowulf.host :refer [LIST]] + [clojure.string :refer [join]]) + (:import [beowulf.cons_cell ConsCell])) + +;; We don't need (at least, in the Clojure reader) to rewrite forms like +;; "'FOO", because that's handled by the parser. But we do need to rewrite +;; things which don't evaluate their arguments, like `SETQ`, because (unless +;; LABEL does it, which I'm not yet sure of) we're not yet able to implement +;; things which don't evaluate arguments. + +(def ^:dynamic *readmacros* + {:car {'DEFUN (fn [f] + (LIST 'SET (LIST 'QUOTE (CADR f)) + (CONS 'LAMBDA (CDDR f)))) + 'SETQ (fn [f] (LIST 'SET (LIST 'QUOTE (CADR f)) (CADDR f)))}}) + +(defn expand-macros + [form] + (try + (if-let [car (when (and (coll? form) (symbol? (first form))) + (first form))] + (if-let [macro (-> *readmacros* :car car)] + (make-beowulf-list (apply macro (list form))) + form) + form) + (catch Exception any + (println (join "\n" + ["# ERROR while expanding macro:" + (str "# Form: " form) + (str "# Error class: " (.getName (.getClass any))) + (str "# Message: " (.getMessage any))])) + form))) \ No newline at end of file diff --git a/src/beowulf/reader/parser.clj b/src/beowulf/reader/parser.clj index 91acde0..f248a3b 100644 --- a/src/beowulf/reader/parser.clj +++ b/src/beowulf/reader/parser.clj @@ -46,7 +46,7 @@ ;; Infix operators appear in mexprs, e.g. on page 7. Ooops! ;; I do not know what infix operators are considered legal. "iexpr := iexp iop iexp; - iexp := mexpr | mvar | number | mexpr | opt-space iexp opt-space; + iexp := mexpr | number | opt-space iexp opt-space; iop := '>' | '<' | '+' | '-' | '/' | '=' ;" ;; comments. I'm pretty confident Lisp 1.5 did NOT have these. diff --git a/src/beowulf/reader/simplify.clj b/src/beowulf/reader/simplify.clj index 48ed5d0..1cf525a 100644 --- a/src/beowulf/reader/simplify.clj +++ b/src/beowulf/reader/simplify.clj @@ -2,8 +2,9 @@ "Simplify parse trees. Be aware that this is very tightly coupled with the parser." (:require [beowulf.bootstrap :refer [*options*]] + [clojure.tools.trace :refer [deftrace]] [instaparse.failure :as f]) - (:import [instaparse.gll Failure])) + (:import [instaparse.gll Failure])) (declare simplify) @@ -12,83 +13,90 @@ (if (vector? tree) (if (= :opt-space (first tree)) nil - (remove nil? - (map remove-optional-space tree))) + (let [v (remove nil? + (map remove-optional-space tree))] + (if (seq v) + (apply vector v) + v))) tree)) (defn remove-nesting - [tree] + [tree context] (let [tree' (remove-optional-space tree)] - (if-let [key (when (and (vector? tree') (keyword? (first tree'))) (first tree'))] + (if-let [key (when (and (vector? tree') + (keyword? (first tree'))) + (first tree'))] (loop [r tree'] (if (and r (vector? r) (keyword? (first r))) (if (= (first r) key) - (recur (simplify (second r) :foo)) + (recur (simplify (second r) context)) r) r)) tree'))) - (defn simplify "Simplify this parse tree `p`. If `p` is an instaparse failure object, throw an `ex-info`, with `p` as the value of its `:failure` key." ([p] (if (instance? Failure p) - (throw (ex-info (str "Ic ne behæfd: " (f/pprint-failure p)) {:cause :parse-failure - :phase :simplify - :failure p})) + (throw (ex-info + (str "Ic ne behæfd: " (f/pprint-failure p)) + {:cause :parse-failure + :phase :simplify + :failure p})) (simplify p :expr))) ([p context] - (if - (coll? p) - (apply - vector - (remove - #(when (coll? %) (empty? %)) - (case (first p) - (:λexpr - :args :bindings :body :cond :cond-clause :defn :dot-terminal - :fncall :lhs :octal :quoted-expr :rhs :scientific) (map #(simplify % context) p) - (:arg :expr :coefficient :fn-name :number) (simplify (second p) context) - (:arrow :dot :e :lpar :lsqb :opt-comment :opt-space :q :quote :rpar :rsqb - :semi-colon :sep :space) nil - :atom (if - (= context :mexpr) - [:quoted-expr p] - p) - :comment (when - (:strict *options*) - (throw - (ex-info "Cannot parse comments in strict mode" - {:cause :strict}))) - :dotted-pair (if - (= context :mexpr) - [:fncall - [:mvar "cons"] - [:args - (simplify (nth p 1) context) - (simplify (nth p 2) context)]] - (map simplify p)) - :iexp (second (remove-nesting p)) - :iexpr [:iexpr - [:lhs (simplify (second p) context)] - (simplify (nth p 2) context) ;; really should be the operator - [:rhs (simplify (nth p 3) context)]] - :mexpr (if - (:strict *options*) - (throw - (ex-info "Cannot parse meta expressions in strict mode" - {:cause :strict})) - (simplify (second p) :mexpr)) - :list (if - (= context :mexpr) - [:fncall - [:mvar "list"] - [:args (apply vector (map simplify (rest p)))]] - (map #(simplify % context) p)) - :raw (first (remove empty? (map simplify (rest p)))) - :sexpr (simplify (second p) :sexpr) + (cond + (string? p) p + (coll? p) (apply + vector + (remove + #(when (coll? %) (empty? %)) + (case (first p) + (:λexpr + :args :bindings :body :cond :cond-clause :defn :dot-terminal + :fncall :lhs :octal :quoted-expr :rhs :scientific) (map #(simplify % context) p) + (:arg :expr :coefficient :fn-name :number) (simplify (second p) context) + (:arrow :dot :e :lpar :lsqb :opt-comment :opt-space :q :quote :rpar :rsqb + :semi-colon :sep :space) nil + :atom (if + (= context :mexpr) + [:quoted-expr p] + p) + :comment (when + (:strict *options*) + (throw + (ex-info "Cannot parse comments in strict mode" + {:cause :strict}))) + :decimal p + :dotted-pair (if + (= context :mexpr) + [:fncall + [:mvar "cons"] + [:args + (simplify (nth p 1) context) + (simplify (nth p 2) context)]] + (map #(simplify % context) p)) + :iexp (simplify (second p) context) + :iexpr [:iexpr + [:lhs (simplify (second p) context)] + (simplify (nth p 2) context) ;; really should be the operator + [:rhs (simplify (nth p 3) context)]] + :mexpr (if + (:strict *options*) + (throw + (ex-info "Cannot parse meta expressions in strict mode" + {:cause :strict})) + (simplify (second p) :mexpr)) + :list (if + (= context :mexpr) + [:fncall + [:mvar "list"] + [:args (apply vector (map simplify (rest p)))]] + (map #(simplify % context) p)) + :raw (first (remove empty? (map simplify (rest p)))) + :sexpr (simplify (second p) :sexpr) ;;default - p))) - p))) + p))) + :else p))) diff --git a/test/beowulf/reader_macro_test.clj b/test/beowulf/reader_macro_test.clj new file mode 100644 index 0000000..228a6a9 --- /dev/null +++ b/test/beowulf/reader_macro_test.clj @@ -0,0 +1,11 @@ +(ns beowulf.reader-macro-test + (:require [clojure.test :refer [deftest is testing]] + [beowulf.read :refer [gsp]] + [beowulf.reader.macros :refer [expand-macros]])) + +(deftest macro-expansion + (testing "Expanding DEFUN" + (let [expected "(SET (QUOTE FACT) (LAMBDA (X) (COND ((ZEROP X) 1) (T (TIMES X (FACT (SUB1 X)))))))" + source "(DEFUN FACT (X) (COND ((ZEROP X) 1) (T (TIMES X (FACT (SUB1 X))))))" + actual (print-str (gsp source))] + (is (= actual expected))))) \ No newline at end of file diff --git a/test/beowulf/sexpr_test.clj b/test/beowulf/sexpr_test.clj index e8e0892..a175eb5 100644 --- a/test/beowulf/sexpr_test.clj +++ b/test/beowulf/sexpr_test.clj @@ -114,6 +114,6 @@ (deftest list-tests (testing "Reading arbitrarily structured lists" - (let [expected "(DEFUN FACT (X) (COND ((ZEROP X) 1) (T (TIMES X (FACT (SUB1 X))))))" + (let [expected "(SET (QUOTE FACT) (LAMBDA (X) (COND ((ZEROP X) 1) (T (TIMES X (FACT (SUB1 X)))))))" actual (print-str (gsp expected))] (is (= actual expected)))))