1 ;; -*- lexical-binding: t; -*-
3 (defun load-relative (file)
4 (let* ((current-file (or load-file-name buffer-file-name
))
5 (current-file-directory (file-name-directory current-file
)))
6 (load (expand-file-name file current-file-directory
) nil t
)))
8 (load-relative "types.el")
9 (load-relative "env.el")
10 (load-relative "func.el")
11 (load-relative "reader.el")
12 (load-relative "printer.el")
13 (load-relative "core.el")
15 (defvar repl-env
(mal-env))
17 (dolist (binding core-ns
)
18 (let ((symbol (car binding
))
20 (mal-env-set repl-env symbol fn
)))
22 (defun mal-pair-p (mal-object)
23 (let ((type (mal-type mal-object
))
24 (value (mal-value mal-object
)))
25 (if (and (or (eq type
'list
) (eq type
'vector
))
26 (not (zerop (length value
))))
30 (defun quasiquote (ast)
31 (if (not (mal-pair-p ast
))
32 (mal-list (list (mal-symbol 'quote
) ast
))
33 (let* ((a (mal-value (mal-listify ast
)))
38 ((eq (mal-value a0
) 'unquote
)
41 (eq (mal-value (car (mal-value a0
)))
43 (mal-list (list (mal-symbol 'concat
)
45 (quasiquote (mal-list a0...
)))))
47 (mal-list (list (mal-symbol 'cons
)
49 (quasiquote (mal-list a0...
)))))))))
58 (let* ((a (mal-value ast
))
66 (let ((identifier (mal-value a1
))
67 (value (EVAL a2 env
)))
68 (throw 'return
(mal-env-set env identifier value
))))
70 (let* ((env* (mal-env env
))
71 (bindings (mal-value a1
))
73 (when (vectorp bindings
)
74 (setq bindings
(append bindings nil
)))
76 (let ((key (mal-value (pop bindings
)))
77 (value (EVAL (pop bindings
) env
*)))
78 (mal-env-set env
* key value
)))
84 (setq ast
(quasiquote a1
))) ; TCO
86 (let* ((a0...
(cdr a
))
87 (butlast (butlast a0...
))
88 (last (car (last a0...
))))
90 (eval-ast (mal-list butlast
) env
))
91 (setq ast last
))) ; TCO
93 (let* ((condition (EVAL a1 env
))
94 (condition-type (mal-type condition
))
97 (if (and (not (eq condition-type
'false
))
98 (not (eq condition-type
'nil
)))
101 (setq ast else
) ; TCO
102 (throw 'return
(mal-nil))))))
104 (let* ((binds (mapcar 'mal-value
(mal-value a1
)))
108 (let ((env* (mal-env env binds args
)))
109 (EVAL body env
*))))))
110 (throw 'return
(mal-func body binds env fn
))))
112 ;; not a special form
113 (let* ((ast* (mal-value (eval-ast ast env
)))
117 (let ((env* (mal-env (mal-func-env fn
)
121 ast
(mal-func-ast fn
))) ; TCO
123 (let ((fn* (mal-value fn
)))
124 (throw 'return
(apply fn
* args
))))))))
125 (throw 'return
(eval-ast ast env
))))))
127 (defun eval-ast (ast env
)
128 (let ((type (mal-type ast
))
129 (value (mal-value ast
)))
132 (let ((definition (mal-env-get env value
)))
133 (or definition
(error "Definition not found"))))
135 (mal-list (mapcar (lambda (item) (EVAL item env
)) value
)))
137 (mal-vector (vconcat (mapcar (lambda (item) (EVAL item env
)) value
))))
139 (let ((map (copy-hash-table value
)))
140 (maphash (lambda (key value
)
141 (puthash key
(EVAL value env
) map
))
148 (mal-env-set repl-env
'eval
(mal-fn (let ((env repl-env
)) (lambda (form) (EVAL form env
)))))
149 (mal-env-set repl-env
'*ARGV
* (mal-list (mapcar 'mal-string argv
)))
155 (PRINT (EVAL (READ input
) repl-env
)))
157 (rep "(def! not (fn* (a) (if a false true)))")
158 (rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))")
160 (defun readln (prompt)
161 ;; C-d throws an error
162 (ignore-errors (read-from-minibuffer prompt
)))
164 (defun println (format-string &rest args
)
166 (princ format-string
)
167 (princ (apply 'format format-string args
)))
170 (defmacro with-error-handling
(&rest body
)
174 ;; empty input, carry on
176 (unterminated-sequence
177 (let* ((type (cadr err
))
180 ((eq type
'string
) ?
\")
181 ((eq type
'list
) ?\
))
182 ((eq type
'vector
) ?\
])
183 ((eq type
'map
) ?
}))))
184 (princ (format "Expected '%c', got EOF\n" end
))))
186 (println (error-message-string err
)))))
191 (rep (format "(load-file \"%s\")" (car argv
))))
194 (let ((input (readln "user> ")))
197 (println (rep input
)))
199 ;; print final newline