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 (defun = (x y) (= x y))
25 (defun + (x y) (+ x y))
26 (defun - (x y) (- x y))
27 (defun * (x y) (* x y))
28 (defun / (x y) (/ x y))
29 (defun 1+ (x) (+ x 1))
30 (defun 1- (x) (- x 1))
31 (defun zerop (x) (= x 0))
32 (defun not (x) (if x nil t))
34 (defun truncate (x y) (floor (/ x y)))
36 (defun cons (x y ) (cons x y))
37 (defun car (x) (car x))
38 (defun caar (x) (car (car x)))
39 (defun cadr (x) (car (cdr x)))
40 (defun cdr (x) (cdr x))
41 (defun cdar (x) (cdr (car x)))
42 (defun cddr (x) (cdr (cdr x)))
44 (defun append (list1 list2)
48 (append (cdr list1) list2))))
50 (defun reverse-aux (list acc)
53 (reverse-aux (cdr list) (cons (car list) acc))))
56 (reverse-aux list '()))
66 (while (not (null list))
68 (setq list (cdr list)))
71 (defun mapcar (func list)
74 (cons (funcall func (car list))
75 (mapcar func (cdr list)))))
77 (defmacro push (x place)
78 `(setq ,place (cons ,x ,place)))
80 (defvar *package* (new))
83 (let ((s (get *package* name)))
86 (set *package* name (make-symbol name)))))
88 (defun find-symbol (name)
92 (defmacro cond (&rest clausules)
95 (if (eq (caar clausules) t)
96 `(progn ,@(cdar clausules))
97 `(if ,(caar clausules)
98 (progn ,@(cdar clausules))
99 (cond ,@(cdr clausules))))))
101 (defun !reduce (func list initial)
106 (funcall func initial (car list)))))
109 (defun code-char (x) x)
110 (defun char-code (x) x)
111 (defvar *newline* (string (code-char 10)))
113 (defun concat (&rest strs)
114 (!reduce (lambda (s1 s2) (concat-two s1 s2))
118 ;;; Concatenate a list of strings, with a separator
119 (defun join (list separator)
128 (join (cdr list) separator)))))
130 (defun join-trailing (list separator)
133 (concat (car list) separator (join-trailing (cdr list) separator))))
135 (defun integer-to-string (x)
139 (while (not (zerop x 0))
140 (push (mod x 10) digits)
141 (setq x (truncate x 10)))
142 (join (mapcar (lambda (d) (string (char "0123456789" d)))
146 (defmacro and (&rest forms)
158 (defmacro or (&rest forms)
167 (or ,@(cdr forms))))))
170 (defmacro prog1 (form &rest body)
171 (let ((value (make-symbol "VALUE")))
172 `(let ((,value ,form))
177 (defun char= (x y) (= x y))
182 ;;; It is a basic Lisp reader. It does not use advanced stuff
183 ;;; intentionally, because we want to use it to bootstrap a simple
184 ;;; Lisp. The main entry point is the function `ls-read', which
185 ;;; accepts a strings as argument and return the Lisp expression.
186 (defun make-string-stream (string)
189 (defun %peek-char (stream)
190 (and (< (cdr stream) (length (car stream)))
191 (char (car stream) (cdr stream))))
193 (defun %read-char (stream)
194 (and (< (cdr stream) (length (car stream)))
195 (prog1 (char (car stream) (cdr stream))
196 (setcdr stream (1+ (cdr stream))))))
198 (defun whitespacep (ch)
199 (or (char= ch #\space) (char= ch #\newline) (char= ch #\tab)))
201 (defun skip-whitespaces (stream)
203 (setq ch (%peek-char stream))
204 (while (and ch (whitespacep ch))
206 (setq ch (%peek-char stream)))))
208 (defun terminalp (ch)
209 (or (null ch) (whitespacep ch) (char= #\) ch) (char= #\( ch)))