DISABLE FDs (REMOVE ME).
[jackhill/mal.git] / picolisp / step2_eval.l
1 (de load-relative (Path)
2 (load (pack (car (file)) Path)) )
3
4 (load-relative "readline.l")
5 (load-relative "types.l")
6 (load-relative "reader.l")
7 (load-relative "printer.l")
8
9 (de READ (String)
10 (read-str String) )
11
12 (def '*ReplEnv
13 '((+ . ((A B) (MAL-number (+ (MAL-value A) (MAL-value B)))))
14 (- . ((A B) (MAL-number (- (MAL-value A) (MAL-value B)))))
15 (* . ((A B) (MAL-number (* (MAL-value A) (MAL-value B)))))
16 (/ . ((A B) (MAL-number (/ (MAL-value A) (MAL-value B))))) ) )
17
18 (de EVAL (Ast Env)
19 (if (= (MAL-type Ast) 'list)
20 (if (not (MAL-value Ast))
21 Ast
22 (let Value (MAL-value (eval-ast Ast Env))
23 (apply (car Value) (cdr Value)) ) )
24 (eval-ast Ast Env) ) )
25
26 (de eval-ast (Ast Env)
27 (let Value (MAL-value Ast)
28 (case (MAL-type Ast)
29 (symbol
30 (if (assoc Value Env)
31 (cdr @)
32 (throw 'err (MAL-error (MAL-string (pack "'" Value "' not found")))) ) )
33 (list (MAL-list (mapcar '((Form) (EVAL Form Env)) Value)))
34 (vector (MAL-vector (mapcar '((Form) (EVAL Form Env)) Value)))
35 (map (MAL-map (mapcar '((Form) (EVAL Form Env)) Value)))
36 (T Ast) ) ) )
37
38 (de PRINT (Ast)
39 (pr-str Ast T) )
40
41 (de rep (String)
42 (PRINT (EVAL (READ String) *ReplEnv)) )
43
44 (load-history ".mal_history")
45
46 (use Eof
47 (until Eof
48 (let Input (readline "user> ")
49 (if (=0 Input)
50 (setq Eof T)
51 (let Output (catch 'err (rep Input))
52 (if (isa '+MALError Output)
53 (let Message (MAL-value Output)
54 (unless (= (MAL-value Message) "end of token stream")
55 (prinl "[error] " (pr-str Message)) ) )
56 (prinl Output) ) ) ) ) ) )
57
58 (prinl)
59 (bye)