Commit | Line | Data |
---|---|---|
31690700 | 1 | (load-file "../mal/env.mal") |
ea81a808 | 2 | (load-file "../mal/core.mal") |
31690700 JM |
3 | |
4 | ;; read | |
5 | (def! READ (fn* [strng] | |
6 | (read-string strng))) | |
7 | ||
8 | ||
9 | ;; eval | |
10 | (def! eval-ast (fn* [ast env] (do | |
11 | ;;(do (prn "eval-ast" ast "/" (keys env)) ) | |
12 | (cond | |
13 | (symbol? ast) (env-get env ast) | |
14 | ||
15 | (list? ast) (map (fn* [exp] (EVAL exp env)) ast) | |
16 | ||
17 | (vector? ast) (apply vector (map (fn* [exp] (EVAL exp env)) ast)) | |
18 | ||
19 | (map? ast) (apply hash-map | |
20 | (apply concat | |
21 | (map (fn* [k] [k (EVAL (get ast k) env)]) | |
22 | (keys ast)))) | |
23 | ||
24 | "else" ast)))) | |
25 | ||
26 | (def! LET (fn* [env args] | |
27 | (if (> (count args) 0) | |
28 | (do | |
29 | (env-set env (nth args 0) (EVAL (nth args 1) env)) | |
30 | (LET env (rest (rest args))))))) | |
31 | ||
32 | (def! EVAL (fn* [ast env] (do | |
33 | ;;(do (prn "EVAL" ast "/" (keys @env)) ) | |
34 | (if (not (list? ast)) | |
35 | (eval-ast ast env) | |
36 | ||
37 | ;; apply list | |
38 | (let* [a0 (first ast)] | |
39 | (cond | |
40 | (= 'def! a0) | |
41 | (env-set env (nth ast 1) (EVAL (nth ast 2) env)) | |
42 | ||
43 | (= 'let* a0) | |
44 | (let* [let-env (new-env env)] | |
45 | (do | |
46 | (LET let-env (nth ast 1)) | |
47 | (EVAL (nth ast 2) let-env))) | |
48 | ||
49 | (= 'do a0) | |
50 | (let* [el (eval-ast (rest ast) env)] | |
51 | (nth el (- (count el) 1))) | |
52 | ||
53 | (= 'if a0) | |
54 | (let* [cond (EVAL (nth ast 1) env)] | |
55 | (if (or (= cond nil) (= cond false)) | |
56 | (if (> (count ast) 3) | |
57 | (EVAL (nth ast 3) env) | |
58 | nil) | |
59 | (EVAL (nth ast 2) env))) | |
8cb5cda4 | 60 | |
31690700 JM |
61 | (= 'fn* a0) |
62 | (fn* [& args] | |
63 | (EVAL (nth ast 2) (new-env env (nth ast 1) args))) | |
64 | ||
65 | "else" | |
66 | (let* [el (eval-ast ast env) | |
67 | f (first el) | |
68 | args (rest el)] | |
69 | (apply f args)))))))) | |
70 | ||
71 | ||
72 | ||
73 | (def! PRINT (fn* [exp] (pr-str exp))) | |
74 | ||
75 | ;; repl | |
76 | (def! repl-env (new-env)) | |
77 | (def! rep (fn* [strng] | |
1617910a | 78 | (PRINT (EVAL (READ strng) repl-env)))) |
31690700 | 79 | |
8cb5cda4 JM |
80 | ;; core.mal: defined directly using mal |
81 | (map (fn* [data] (env-set repl-env (nth data 0) (nth data 1))) core_ns) | |
31690700 | 82 | |
8cb5cda4 | 83 | ;; core.mal: defined using the new language itself |
31690700 JM |
84 | (rep "(def! not (fn* [a] (if a false true)))") |
85 | ||
86b689f3 JM |
86 | ;; repl loop |
87 | (def! repl-loop (fn* [] | |
31690700 JM |
88 | (let* [line (readline "mal-user> ")] |
89 | (if line | |
90 | (do | |
91 | (if (not (= "" line)) | |
92 | (try* | |
86b689f3 | 93 | (println (rep line)) |
31690700 JM |
94 | (catch* exc |
95 | (println "Uncaught exception:" exc)))) | |
86b689f3 JM |
96 | (repl-loop)))))) |
97 | ||
98 | (def! -main (fn* [& args] | |
99 | (repl-loop))) | |
31690700 | 100 | (-main) |