forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep4_if_fn_do.el
144 lines (130 loc) · 4.36 KB
/
step4_if_fn_do.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
;; -*- lexical-binding: t; -*-
(defun load-relative (file)
(let* ((current-file (or load-file-name buffer-file-name))
(current-file-directory (file-name-directory current-file)))
(load (expand-file-name file current-file-directory) nil t)))
(load-relative "types.el")
(load-relative "env.el")
(load-relative "reader.el")
(load-relative "printer.el")
(load-relative "core.el")
(defvar repl-env (mal-env))
(dolist (binding core-ns)
(let ((symbol (car binding))
(fn (cdr binding)))
(mal-env-set repl-env symbol fn)))
(defun READ (input)
(read-str input))
(defun EVAL (ast env)
(if (and (mal-list-p ast) (mal-value ast))
(let* ((a (mal-value ast))
(a0 (car a))
(a0* (mal-value a0))
(a1 (cadr a))
(a2 (nth 2 a))
(a3 (nth 3 a)))
(cond
((eq a0* 'def!)
(let ((identifier (mal-value a1))
(value (EVAL a2 env)))
(mal-env-set env identifier value)))
((eq a0* 'let*)
(let* ((env* (mal-env env))
(a1* (mal-value a1))
(bindings (if (vectorp a1*) (append a1* nil) a1*))
(form a2))
(while bindings
(let ((key (mal-value (pop bindings)))
(value (EVAL (pop bindings) env*)))
(mal-env-set env* key value)))
(EVAL form env*)))
((eq a0* 'do)
(car (last (mal-value (eval-ast (mal-list (cdr a)) env)))))
((eq a0* 'if)
(let* ((condition (EVAL a1 env))
(condition-type (mal-type condition))
(then a2)
(else a3))
(if (and (not (eq condition-type 'false))
(not (eq condition-type 'nil)))
(EVAL then env)
(if else
(EVAL else env)
mal-nil))))
((eq a0* 'fn*)
(let ((binds (mapcar 'mal-value (mal-value a1)))
(body a2))
(mal-fn
(lambda (&rest args)
(let ((env* (mal-env env binds args)))
(EVAL body env*))))))
(t
;; not a special form
(let* ((ast* (mal-value (eval-ast ast env)))
(fn (car ast*))
(fn* (cond
((functionp fn)
fn)
((mal-fn-p fn)
(mal-value fn))))
(args (cdr ast*)))
(apply fn* args)))))
(eval-ast ast env)))
(defun eval-ast (ast env)
(let ((type (mal-type ast))
(value (mal-value ast)))
(cond
((eq type 'symbol)
(let ((definition (mal-env-get env value)))
(or definition (error "Definition not found"))))
((eq type 'list)
(mal-list (mapcar (lambda (item) (EVAL item env)) value)))
((eq type 'vector)
(mal-vector (vconcat (mapcar (lambda (item) (EVAL item env)) value))))
((eq type 'map)
(let ((map (copy-hash-table value)))
(maphash (lambda (key value)
(puthash key (EVAL value env) map))
map)
(mal-map map)))
(t
;; return as is
ast))))
(defun PRINT (input)
(pr-str input t))
(defun rep (input)
(PRINT (EVAL (READ input) repl-env)))
(rep "(def! not (fn* (a) (if a false true)))")
(defun readln (prompt)
;; C-d throws an error
(ignore-errors (read-from-minibuffer prompt)))
(defun println (format-string &rest args)
(if (not args)
(princ format-string)
(princ (apply 'format format-string args)))
(terpri))
(defun main ()
(let (eof)
(while (not eof)
(let ((input (readln "user> ")))
(if input
(condition-case err
(println (rep input))
(end-of-token-stream
;; empty input, carry on
)
(unterminated-sequence
(let* ((type (cadr err))
(end
(cond
((eq type 'string) ?\")
((eq type 'list) ?\))
((eq type 'vector) ?\])
((eq type 'map) ?}))))
(princ (format "Expected '%c', got EOF\n" end))))
(error ; catch-all
(println (error-message-string err))))
(setq eof t)
;; print final newline
(terpri))))))
(main)