Merge pull request #377 from asarhaddon/fix-runtests-pre-eval
[jackhill/mal.git] / common-lisp / src / step4_if_fn_do.lisp
CommitLineData
82b73d0b
IA
1(defpackage :mal
2 (:use :common-lisp
3 :types
4 :env
5 :reader
6 :printer
7 :core)
774d5cf8
IA
8 (:import-from :cl-readline
9 :readline
10 :register-function)
0795349b
IA
11 (:import-from :genhash
12 :hashref
13 :hashmap)
14 (:import-from :utils
15 :listify
774d5cf8
IA
16 :getenv
17 :common-prefix)
82b73d0b
IA
18 (:export :main))
19
20(in-package :mal)
21
22(defvar *repl-env* (env:create-mal-env))
23
24(dolist (binding core:ns)
baa3c3af 25 (env:set-env *repl-env* (car binding) (cdr binding)))
82b73d0b
IA
26
27(defvar mal-def! (make-mal-symbol "def!"))
28(defvar mal-let* (make-mal-symbol "let*"))
29(defvar mal-do (make-mal-symbol "do"))
30(defvar mal-if (make-mal-symbol "if"))
31(defvar mal-fn* (make-mal-symbol "fn*"))
32
33(defun eval-sequence (sequence env)
34 (map 'list
35 (lambda (ast) (mal-eval ast env))
36 (mal-data-value sequence)))
37
38(defun eval-hash-map (hash-map env)
baa3c3af
IA
39 (let ((hash-map-value (mal-data-value hash-map))
40 (new-hash-table (make-mal-value-hash-table)))
82b73d0b
IA
41 (genhash:hashmap (lambda (key value)
42 (setf (genhash:hashref (mal-eval key env) new-hash-table)
43 (mal-eval value env)))
44 hash-map-value)
baa3c3af 45 (make-mal-hash-map new-hash-table)))
82b73d0b
IA
46
47(defun eval-ast (ast env)
48 (switch-mal-type ast
49 (types:symbol (env:get-env env ast))
50 (types:list (eval-sequence ast env))
51 (types:vector (make-mal-vector (apply 'vector (eval-sequence ast env))))
52 (types:hash-map (eval-hash-map ast env))
53 (types:any ast)))
54
55(defun eval-let* (forms env)
56 (let ((new-env (env:create-mal-env :parent env))
baa3c3af 57 (bindings (utils:listify (mal-data-value (second forms)))))
82b73d0b
IA
58
59 (mapcar (lambda (binding)
60 (env:set-env new-env
61 (car binding)
62 (mal-eval (or (cdr binding)
baa3c3af 63 mal-nil)
82b73d0b
IA
64 new-env)))
65 (loop
66 for (symbol value) on bindings
67 by #'cddr
68 collect (cons symbol value)))
69
70 (mal-eval (third forms) new-env)))
71
72(defun eval-list (ast env)
73 (let ((forms (mal-data-value ast)))
74 (cond
75 ((mal-data-value= mal-def! (first forms))
76 (env:set-env env (second forms) (mal-eval (third forms) env)))
77 ((mal-data-value= mal-let* (first forms))
78 (eval-let* forms env))
79 ((mal-data-value= mal-do (first forms))
80 (car (last (mapcar (lambda (form) (mal-eval form env))
81 (cdr forms)))))
82 ((mal-data-value= mal-if (first forms))
83 (let ((predicate (mal-eval (second forms) env)))
baa3c3af
IA
84 (mal-eval (if (or (mal-data-value= predicate mal-nil)
85 (mal-data-value= predicate mal-false))
82b73d0b
IA
86 (fourth forms)
87 (third forms))
88 env)))
89 ((mal-data-value= mal-fn* (first forms))
baa3c3af 90 (make-mal-fn (let ((arglist (second forms))
82b73d0b
IA
91 (body (third forms)))
92 (lambda (&rest args)
93 (mal-eval body (env:create-mal-env :parent env
baa3c3af 94 :binds (listify (mal-data-value arglist))
82b73d0b
IA
95 :exprs args))))))
96 (t (let* ((evaluated-list (eval-ast ast env))
97 (function (car evaluated-list)))
98 ;; If first element is a mal function unwrap it
99 (apply (mal-data-value function)
100 (cdr evaluated-list)))))))
101
102(defun mal-read (string)
103 (reader:read-str string))
104
105(defun mal-eval (ast env)
106 (cond
baa3c3af
IA
107 ((null ast) mal-nil)
108 ((not (mal-list-p ast)) (eval-ast ast env))
82b73d0b
IA
109 ((zerop (length (mal-data-value ast))) ast)
110 (t (eval-list ast env))))
111
112(defun mal-print (expression)
113 (printer:pr-str expression))
114
115(defun rep (string)
116 (handler-case
baa3c3af 117 (mal-print (mal-eval (mal-read string) *repl-env*))
82b73d0b 118 (error (condition)
baa3c3af 119 (format nil "~a" condition))))
82b73d0b
IA
120
121(rep "(def! not (fn* (a) (if a false true)))")
122
123(defvar *use-readline-p* nil)
124
774d5cf8
IA
125(defun complete-toplevel-symbols (input &rest ignored)
126 (declare (ignorable ignored))
127
128 (let (candidates)
129 (loop for key being the hash-keys of (env:mal-env-bindings *repl-env*)
130 when (let ((pos (search input key))) (and pos (zerop pos)))
131 do (push key candidates))
132
133 (if (= 1 (length candidates))
134 (cons (car candidates) candidates)
135 (cons (apply #'utils:common-prefix candidates) candidates))))
136
82b73d0b
IA
137(defun raw-input (prompt)
138 (format *standard-output* prompt)
139 (force-output *standard-output*)
140 (read-line *standard-input* nil))
141
142(defun mal-readline (prompt)
143 (if *use-readline-p*
774d5cf8 144 (rl:readline :prompt prompt :add-history t :novelty-check #'string/=)
82b73d0b
IA
145 (raw-input prompt)))
146
147(defun mal-writeline (string)
148 (when string
149 (write-line string)
150 (force-output *standard-output*)))
151
152(defun main (&optional (argv nil argv-provided-p))
153 (declare (ignorable argv argv-provided-p))
89676a9f 154
0795349b
IA
155 (setf *use-readline-p* (not (or (string= (utils:getenv "PERL_RL") "false")
156 (string= (utils:getenv "TERM") "dumb"))))
89676a9f
IA
157
158 ;; In GNU CLISP's batch mode the standard-input seems to be set to some sort
159 ;; of input string-stream, this interacts wierdly with the PERL_RL enviroment
160 ;; variable which the test runner sets causing `read-line' on *standard-input*
161 ;; to fail with an empty stream error. The following reinitializes the
162 ;; standard streams
163 ;;
164 ;; See http://www.gnu.org/software/clisp/impnotes/streams-interactive.html
165 #+clisp (setf *standard-input* (ext:make-stream :input)
166 *standard-output* (ext:make-stream :output :buffered t)
167 *error-output* (ext:make-stream :error :buffered t))
168
774d5cf8
IA
169 ;; CCL fails with a error while registering completion function
170 ;; See also https://github.com/mrkkrp/cl-readline/issues/5
171 #-ccl (rl:register-function :complete #'complete-toplevel-symbols)
172
82b73d0b
IA
173 (loop do (let ((line (mal-readline "user> ")))
174 (if line (mal-writeline (rep line)) (return)))))
033f64c4
IA
175
176;;; Workaround for CMUCL's printing of "Reloaded library ... " messages when an
177;;; image containing foreign libraries is restored. The extra messages cause the
178;;; MAL testcases to fail
179
180#+cmucl (progn
181 (defvar *old-standard-output* *standard-output*
182 "Keep track of current value standard output, this is restored after image restore completes")
183
184 (defun muffle-output ()
185 (setf *standard-output* (make-broadcast-stream)))
186
187 (defun restore-output ()
188 (setf *standard-output* *old-standard-output*))
189
190 (pushnew #'muffle-output ext:*after-save-initializations*)
191 (setf ext:*after-save-initializations*
192 (append ext:*after-save-initializations* (list #'restore-output))))