8 (:import-from
:cl-readline
11 (:import-from
:genhash
22 (define-condition invalid-function
(mal-runtime-exception)
23 ((form :initarg
:form
:reader form
)
24 (context :initarg
:context
:reader context
))
25 (:report
(lambda (condition stream
)
27 "Invalid function '~a' provided while ~a"
28 (printer:pr-str
(form condition
))
29 (if (string= (context condition
) "apply")
34 (defvar *repl-env
* (env:create-mal-env
))
36 (dolist (binding core
:ns
)
37 (env:set-env
*repl-env
* (car binding
) (cdr binding
)))
39 (defvar mal-def
! (make-mal-symbol "def!"))
40 (defvar mal-let
* (make-mal-symbol "let*"))
41 (defvar mal-do
(make-mal-symbol "do"))
42 (defvar mal-if
(make-mal-symbol "if"))
43 (defvar mal-fn
* (make-mal-symbol "fn*"))
44 (defvar mal-quote
(make-mal-symbol "quote"))
45 (defvar mal-quasiquote
(make-mal-symbol "quasiquote"))
46 (defvar mal-unquote
(make-mal-symbol "unquote"))
47 (defvar mal-splice-unquote
(make-mal-symbol "splice-unquote"))
48 (defvar mal-cons
(make-mal-symbol "cons"))
49 (defvar mal-concat
(make-mal-symbol "concat"))
50 (defvar mal-defmacro
! (make-mal-symbol "defmacro!"))
51 (defvar mal-macroexpand
(make-mal-symbol "macroexpand"))
52 (defvar mal-try
* (make-mal-symbol "try*"))
53 (defvar mal-catch
* (make-mal-symbol "catch*"))
54 (defvar mal-throw
(make-mal-symbol "throw"))
56 (defun eval-sequence (sequence env
)
58 (lambda (ast) (mal-eval ast env
))
59 (mal-data-value sequence
)))
61 (defun eval-hash-map (hash-map env
)
62 (let ((hash-map-value (mal-data-value hash-map
))
63 (new-hash-table (make-mal-value-hash-table)))
64 (genhash:hashmap
(lambda (key value
)
65 (setf (genhash:hashref
(mal-eval key env
) new-hash-table
)
66 (mal-eval value env
)))
68 (make-mal-hash-map new-hash-table
)))
70 (defun eval-ast (ast env
)
72 (types:symbol
(env:get-env env ast
))
73 (types:list
(eval-sequence ast env
))
74 (types:vector
(make-mal-vector (apply 'vector
(eval-sequence ast env
))))
75 (types:hash-map
(eval-hash-map ast env
))
78 (defun is-pair (value)
79 (and (or (mal-list-p value
)
81 (< 0 (length (mal-data-value value
)))))
83 (defun quasiquote (ast)
84 (if (not (is-pair ast
))
85 (make-mal-list (list mal-quote ast
))
86 (let ((forms (map 'list
#'identity
(mal-data-value ast
))))
88 ((mal-data-value= mal-unquote
(first forms
))
91 ((and (is-pair (first forms
))
92 (mal-data-value= mal-splice-unquote
93 (first (mal-data-value (first forms
)))))
94 (make-mal-list (list mal-concat
95 (second (mal-data-value (first forms
)))
96 (quasiquote (make-mal-list (cdr forms
))))))
98 (t (make-mal-list (list mal-cons
99 (quasiquote (first forms
))
100 (quasiquote (make-mal-list (cdr forms
))))))))))
102 (defun is-macro-call (ast env
)
103 (when (mal-list-p ast
)
104 (let* ((func-symbol (first (mal-data-value ast
)))
105 (func (when (mal-symbol-p func-symbol
)
106 (env:find-env env func-symbol
))))
109 (cdr (assoc :is-macro
(mal-data-attrs func
)))))))
111 (defun mal-macroexpand (ast env
)
113 while
(is-macro-call ast env
)
114 do
(let* ((forms (mal-data-value ast
))
115 (func (env:get-env env
(first forms
))))
116 (setf ast
(apply (mal-data-value func
)
120 (defun mal-read (string)
121 (reader:read-str string
))
123 (defun mal-eval (ast env
)
125 do
(setf ast
(mal-macroexpand ast env
))
127 ((null ast
) (return mal-nil
))
128 ((not (mal-list-p ast
)) (return (eval-ast ast env
)))
129 ((zerop (length (mal-data-value ast
))) (return ast
))
130 (t (let ((forms (mal-data-value ast
)))
132 ((mal-data-value= mal-quote
(first forms
))
133 (return (second forms
)))
135 ((mal-data-value= mal-quasiquote
(first forms
))
136 (setf ast
(quasiquote (second forms
))))
138 ((mal-data-value= mal-macroexpand
(first forms
))
139 (return (mal-macroexpand (second forms
) env
)))
141 ((mal-data-value= mal-def
! (first forms
))
142 (return (env:set-env env
(second forms
) (mal-eval (third forms
) env
))))
144 ((mal-data-value= mal-defmacro
! (first forms
))
145 (let ((value (mal-eval (third forms
) env
)))
146 (return (if (mal-fn-p value
)
150 (setf (cdr (assoc :is-macro
(mal-data-attrs value
))) t
)
152 (error 'invalid-function
154 :context
"macro")))))
156 ((mal-data-value= mal-let
* (first forms
))
157 (let ((new-env (env:create-mal-env
:parent env
))
158 (bindings (utils:listify
(mal-data-value (second forms
)))))
160 (mapcar (lambda (binding)
163 (mal-eval (or (cdr binding
)
167 for
(symbol value
) on bindings
169 collect
(cons symbol value
)))
170 (setf ast
(third forms
)
173 ((mal-data-value= mal-do
(first forms
))
174 (mapc (lambda (form) (mal-eval form env
))
175 (butlast (cdr forms
)))
176 (setf ast
(car (last forms
))))
178 ((mal-data-value= mal-if
(first forms
))
179 (let ((predicate (mal-eval (second forms
) env
)))
180 (setf ast
(if (or (mal-data-value= predicate mal-nil
)
181 (mal-data-value= predicate mal-false
))
185 ((mal-data-value= mal-fn
* (first forms
))
186 (return (let ((arglist (second forms
))
187 (body (third forms
)))
188 (make-mal-fn (lambda (&rest args
)
189 (mal-eval body
(env:create-mal-env
:parent env
190 :binds
(listify (mal-data-value arglist
))
192 :attrs
(list (cons :params arglist
)
195 (cons :is-macro nil
))))))
197 ((mal-data-value= mal-try
* (first forms
))
198 (if (not (third forms
))
199 (return (mal-eval (second forms
) env
))
201 (return (mal-eval (second forms
) env
))
203 (let ((catch-forms (mal-data-value (third forms
))))
204 (when (mal-data-value= mal-catch
*
206 (return (mal-eval (third catch-forms
)
207 (env:create-mal-env
:parent env
208 :binds
(list (second catch-forms
))
209 :exprs
(list (if (typep condition
'mal-user-exception
)
210 (mal-exception-data condition
)
211 (make-mal-string (format nil
"~a" condition
)))))))))))))
213 (t (let* ((evaluated-list (eval-ast ast env
))
214 (function (car evaluated-list
)))
215 ;; If first element is a mal function unwrap it
216 (cond ((mal-fn-p function
)
217 (let* ((attrs (mal-data-attrs function
)))
218 (setf ast
(cdr (assoc :ast attrs
))
219 env
(env:create-mal-env
:parent
(cdr (assoc :env attrs
))
222 (mal-data-value (cdr (assoc :params attrs
))))
223 :exprs
(cdr evaluated-list
)))))
224 ((mal-builtin-fn-p function
)
225 (return (apply (mal-data-value function
)
226 (cdr evaluated-list
))))
227 (t (error 'invalid-function
229 :context
"apply")))))))))))
231 (defun mal-print (expression)
232 (printer:pr-str expression
))
236 (mal-print (mal-eval (mal-read string
) *repl-env
*))
237 (mal-error (condition)
238 (format nil
"Error: ~a" condition
))
239 (mal-runtime-exception (condition)
240 (format nil
"Exception: ~a" condition
))
241 (mal-user-exception (condition)
242 (format nil
"Exception: ~a" (pr-str (mal-exception-data condition
))))
244 (format nil
"Internal error: ~a" condition
))))
246 (env:set-env
*repl-env
*
247 (make-mal-symbol "eval")
248 (make-mal-builtin-fn (lambda (ast)
249 (mal-eval ast
*repl-env
*))))
251 (rep "(def! not (fn* (a) (if a false true)))")
252 (rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))")
253 (rep "(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list 'if (first xs) (if (> (count xs) 1) (nth xs 1) (throw \"odd number of forms to cond\")) (cons 'cond (rest (rest xs)))))))")
254 (rep "(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))")
256 (defvar *use-readline-p
* nil
)
258 (defun complete-toplevel-symbols (input &rest ignored
)
259 (declare (ignorable ignored
))
262 (loop for key being the hash-keys of
(env:mal-env-bindings
*repl-env
*)
263 when
(let ((pos (search input key
))) (and pos
(zerop pos
)))
264 do
(push key candidates
))
266 (if (= 1 (length candidates
))
267 (cons (car candidates
) candidates
)
268 (cons (apply #'utils
:common-prefix candidates
) candidates
))))
270 (defun raw-input (prompt)
271 (format *standard-output
* prompt
)
272 (force-output *standard-output
*)
273 (read-line *standard-input
* nil
))
275 (defun mal-readline (prompt)
277 (rl:readline
:prompt prompt
:add-history t
:novelty-check
#'string
/=)
280 (defun mal-writeline (string)
283 (force-output *standard-output
*)))
286 (loop do
(let ((line (mal-readline "user> ")))
288 (mal-writeline (rep line
))
291 (defun run-file (file)
292 (rep (format nil
"(load-file \"~a\")" file
)))
294 (defun main (&optional
(argv nil argv-provided-p
))
296 (setf *use-readline-p
* (not (or (string= (uiop:getenv
"PERL_RL") "false")
297 (string= (uiop:getenv
"TERM") "dumb"))))
299 ;; In GNU CLISP's batch mode the standard-input seems to be set to some sort
300 ;; of input string-stream, this interacts wierdly with the PERL_RL enviroment
301 ;; variable which the test runner sets causing `read-line' on *standard-input*
302 ;; to fail with an empty stream error. The following reinitializes the
305 ;; See http://www.gnu.org/software/clisp/impnotes/streams-interactive.html
306 #+clisp
(setf *standard-input
* (ext:make-stream
:input
)
307 *standard-output
* (ext:make-stream
:output
:buffered t
)
308 *error-output
* (ext:make-stream
:error
:buffered t
))
310 ;; CCL fails with a error while registering completion function
311 ;; See also https://github.com/mrkkrp/cl-readline/issues/5
312 #-ccl
(rl:register-function
:complete
#'complete-toplevel-symbols
)
315 (let ((args (if argv-provided-p
317 (cdr (utils:raw-command-line-arguments
)))))
318 (env:set-env
*repl-env
*
319 (make-mal-symbol "*ARGV*")
320 (make-mal-list (mapcar #'make-mal-string
(cdr args
))))
323 (run-file (car args
)))))
325 ;;; Workaround for CMUCL's printing of "Reloaded library ... " messages when an
326 ;;; image containing foreign libraries is restored. The extra messages cause the
327 ;;; MAL testcases to fail.
330 (defvar *old-standard-output
* *standard-output
*
331 "Keep track of current value standard output, this is restored after image restore completes")
333 (defun muffle-output ()
334 (setf *standard-output
* (make-broadcast-stream)))
336 (defun restore-output ()
337 (setf *standard-output
* *old-standard-output
*))
339 (pushnew #'muffle-output ext
:*after-save-initializations
*)
340 (setf ext
:*after-save-initializations
*
341 (append ext
:*after-save-initializations
* (list #'restore-output
))))