forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
step9_try.mal
181 lines (145 loc) · 4.97 KB
/
step9_try.mal
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
(load-file "../mal/env.mal")
(load-file "../mal/core.mal")
;; read
(def! READ (fn* [strng]
(read-string strng)))
;; eval
(def! is-pair (fn* [x]
(if (sequential? x)
(if (> (count x) 0)
true))))
(def! QUASIQUOTE (fn* [ast]
(cond
(not (is-pair ast))
(list 'quote ast)
(= 'unquote (first ast))
(nth ast 1)
(if (is-pair (first ast))
(if (= 'splice-unquote (first (first ast)))
true))
(list 'concat (nth (first ast) 1) (QUASIQUOTE (rest ast)))
"else"
(list 'cons (QUASIQUOTE (first ast)) (QUASIQUOTE (rest ast))))))
(def! is-macro-call (fn* [ast env]
(if (list? ast)
(let* [a0 (first ast)]
(if (symbol? a0)
(if (env-find env a0)
(let* [m (meta (env-get env a0))]
(if m
(if (get m "ismacro")
true)))))))))
(def! MACROEXPAND (fn* [ast env]
(if (is-macro-call ast env)
(let* [mac (env-get env (first ast))]
(MACROEXPAND (apply mac (rest ast)) env))
ast)))
(def! eval-ast (fn* [ast env] (do
;;(do (prn "eval-ast" ast "/" (keys env)) )
(cond
(symbol? ast) (env-get env ast)
(list? ast) (map (fn* [exp] (EVAL exp env)) ast)
(vector? ast) (apply vector (map (fn* [exp] (EVAL exp env)) ast))
(map? ast) (apply hash-map
(apply concat
(map (fn* [k] [k (EVAL (get ast k) env)])
(keys ast))))
"else" ast))))
(def! LET (fn* [env args]
(if (> (count args) 0)
(do
(env-set env (nth args 0) (EVAL (nth args 1) env))
(LET env (rest (rest args)))))))
(def! EVAL (fn* [ast env] (do
;;(do (prn "EVAL" ast "/" (keys @env)) )
(if (not (list? ast))
(eval-ast ast env)
;; apply list
(let* [ast (MACROEXPAND ast env)]
(if (not (list? ast))
(eval-ast ast env)
(let* [a0 (first ast)]
(cond
(nil? a0)
ast
(= 'def! a0)
(env-set env (nth ast 1) (EVAL (nth ast 2) env))
(= 'let* a0)
(let* [let-env (new-env env)]
(do
(LET let-env (nth ast 1))
(EVAL (nth ast 2) let-env)))
(= 'quote a0)
(nth ast 1)
(= 'quasiquote a0)
(let* [a1 (nth ast 1)]
(EVAL (QUASIQUOTE a1) env))
(= 'defmacro! a0)
(let* [a1 (nth ast 1)
a2 (nth ast 2)
f (EVAL a2 env)
m (or (meta f) {})
mac (with-meta f (assoc m "ismacro" true))]
(env-set env a1 mac))
(= 'macroexpand a0)
(let* [a1 (nth ast 1)]
(MACROEXPAND a1 env))
(= 'try* a0)
(if (= 'catch* (nth (nth ast 2) 0))
(try*
(EVAL (nth ast 1) env)
(catch* exc
(EVAL (nth (nth ast 2) 2)
(new-env env
[(nth (nth ast 2)1)]
[exc]))))
(EVAL (nth ast 1) env))
(= 'do a0)
(let* [el (eval-ast (rest ast) env)]
(nth el (- (count el) 1)))
(= 'if a0)
(let* [cond (EVAL (nth ast 1) env)]
(if (or (= cond nil) (= cond false))
(if (> (count ast) 3)
(EVAL (nth ast 3) env)
nil)
(EVAL (nth ast 2) env)))
(= 'fn* a0)
(fn* [& args]
(EVAL (nth ast 2) (new-env env (nth ast 1) args)))
"else"
(let* [el (eval-ast ast env)
f (first el)
args (rest el)]
(apply f args))))))))))
;; print
(def! PRINT (fn* [exp] (pr-str exp)))
;; repl
(def! repl-env (new-env))
(def! rep (fn* [strng]
(PRINT (EVAL (READ strng) repl-env))))
;; core.mal: defined directly using mal
(map (fn* [data] (env-set repl-env (nth data 0) (nth data 1))) core_ns)
(env-set repl-env 'eval (fn* [ast] (EVAL ast repl-env)))
(env-set repl-env '*ARGV* (rest *ARGV*))
;; core.mal: defined using the new language itself
(rep "(def! not (fn* [a] (if a false true)))")
(rep "(def! load-file (fn* (f) (eval (read-string (str \"(do \" (slurp f) \")\")))))")
(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)))))))")
(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))))))))")
;; repl loop
(def! repl-loop (fn* []
(let* [line (readline "mal-user> ")]
(if line
(do
(if (not (= "" line))
(try*
(println (rep line))
(catch* exc
(println "Uncaught exception:" exc))))
(repl-loop))))))
(def! -main (fn* [& args]
(if (> (count args) 0)
(rep (str "(load-file \"" (first args) "\")"))
(repl-loop))))
(apply -main *ARGV*)