4 (%compile-defmacro 'defmacro
5 (lambda (name args &rest body)
7 (%compile-defmacro ',name (lambda ,args ,@body))))))
9 (defmacro defvar (name value)
12 (%compile-defvar ',name))
18 (defmacro defun (name args &rest body)
21 (%compile-defun ',name))
22 (fsetq ,name (lambda ,args ,@body))))
24 (defmacro when (condition &rest body)
25 `(if ,condition (progn ,@body)))
27 (defmacro unless (condition &rest body)
28 `(if ,condition nil (progn ,@body)))
30 (defun = (x y) (= x y))
31 (defun + (x y) (+ x y))
32 (defun - (x y) (- x y))
33 (defun * (x y) (* x y))
34 (defun / (x y) (/ x y))
35 (defun 1+ (x) (+ x 1))
36 (defun 1- (x) (- x 1))
37 (defun zerop (x) (= x 0))
38 (defun not (x) (if x nil t))
40 (defun truncate (x y) (floor (/ x y)))
42 (defun cons (x y ) (cons x y))
43 (defun car (x) (car x))
44 (defun caar (x) (car (car x)))
45 (defun cadr (x) (car (cdr x)))
46 (defun cdr (x) (cdr x))
47 (defun cdar (x) (cdr (car x)))
48 (defun cddr (x) (cdr (cdr x)))
50 (defun append (list1 list2)
54 (append (cdr list1) list2))))
56 (defun reverse-aux (list acc)
59 (reverse-aux (cdr list) (cons (car list) acc))))
62 (reverse-aux list '()))
72 (while (not (null list))
74 (setq list (cdr list)))
77 (defun mapcar (func list)
80 (cons (funcall func (car list))
81 (mapcar func (cdr list)))))
83 (defmacro push (x place)
84 `(setq ,place (cons ,x ,place)))
86 (defvar *package* (new))
89 (let ((s (get *package* name)))
92 (set *package* name (make-symbol name)))))
94 (defun find-symbol (name)
98 (defmacro cond (&rest clausules)
101 (if (eq (caar clausules) t)
102 `(progn ,@(cdar clausules))
103 `(if ,(caar clausules)
104 (progn ,@(cdar clausules))
105 (cond ,@(cdr clausules))))))
107 (defun !reduce (func list initial)
112 (funcall func initial (car list)))))
115 (defun code-char (x) x)
116 (defun char-code (x) x)
117 (defvar *newline* (string (code-char 10)))
119 (defun concat (&rest strs)
120 (!reduce (lambda (s1 s2) (concat-two s1 s2))
124 ;;; Concatenate a list of strings, with a separator
125 (defun join (list separator)
134 (join (cdr list) separator)))))
136 (defun join-trailing (list separator)
139 (concat (car list) separator (join-trailing (cdr list) separator))))
141 (defun integer-to-string (x)
145 (while (not (zerop x 0))
146 (push (mod x 10) digits)
147 (setq x (truncate x 10)))
148 (join (mapcar (lambda (d) (string (char "0123456789" d)))
152 (defmacro and (&rest forms)
164 (defmacro or (&rest forms)
173 (or ,@(cdr forms))))))
176 (defmacro prog1 (form &rest body)
177 (let ((value (make-symbol "VALUE")))
178 `(let ((,value ,form))
183 (defun char= (x y) (= x y))
188 ;;; It is a basic Lisp reader. It does not use advanced stuff
189 ;;; intentionally, because we want to use it to bootstrap a simple
190 ;;; Lisp. The main entry point is the function `ls-read', which
191 ;;; accepts a strings as argument and return the Lisp expression.
192 (defun make-string-stream (string)
195 (defun %peek-char (stream)
196 (and (< (cdr stream) (length (car stream)))
197 (char (car stream) (cdr stream))))
199 (defun %read-char (stream)
200 (and (< (cdr stream) (length (car stream)))
201 (prog1 (char (car stream) (cdr stream))
202 (setcdr stream (1+ (cdr stream))))))
204 (defun whitespacep (ch)
205 (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
207 (defun skip-whitespaces (stream)
209 (setq ch (%peek-char stream))
210 (while (and ch (whitespacep ch))
212 (setq ch (%peek-char stream)))))
214 (defun terminalp (ch)
215 (or (null ch) (whitespacep ch) (char= #\) ch) (char= #\( ch)))
218 (defun read-until (stream func)
221 (setq ch (%peek-char stream))
222 (while (not (funcall func ch))
223 (setq string (concat string (string ch)))
225 (setq ch (%peek-char stream)))
228 (defun skip-whitespaces-and-comments (stream)
230 (skip-whitespaces stream)
231 (setq ch (%peek-char stream))
232 (while (and ch (char= ch #\;))
233 (read-until stream (lambda (x) (char= x #\newline)))
234 (skip-whitespaces stream)
235 (setq ch (%peek-char stream)))))
237 (defun %read-list (stream)
238 (skip-whitespaces-and-comments stream)
239 (let ((ch (%peek-char stream)))
246 (skip-whitespaces-and-comments stream)
247 (prog1 (ls-read stream)
248 (unless (char= (%read-char stream) #\))
249 (error "')' was expected."))))
251 (cons (ls-read stream) (%read-list stream))))))