Commit | Line | Data |
---|---|---|
d6624158 JM |
1 | function step4_if_fn_do(varargin), main(varargin), end |
2 | ||
3 | % read | |
4 | function ret = READ(str) | |
5 | ret = reader.read_str(str); | |
6 | end | |
7 | ||
8 | % eval | |
9 | function ret = eval_ast(ast, env) | |
10 | switch class(ast) | |
11 | case 'types.Symbol' | |
12 | ret = env.get(ast); | |
6a572dff JM |
13 | case 'types.List' |
14 | ret = types.List(); | |
d6624158 | 15 | for i=1:length(ast) |
6a572dff JM |
16 | ret.append(EVAL(ast.get(i), env)); |
17 | end | |
18 | case 'types.Vector' | |
19 | ret = types.Vector(); | |
20 | for i=1:length(ast) | |
21 | ret.append(EVAL(ast.get(i), env)); | |
22 | end | |
23 | case 'types.HashMap' | |
24 | ret = types.HashMap(); | |
25 | ks = ast.keys(); | |
26 | for i=1:length(ks) | |
27 | k = ks{i}; | |
28 | ret.set(EVAL(k, env), EVAL(ast.get(k), env)); | |
d6624158 JM |
29 | end |
30 | otherwise | |
31 | ret = ast; | |
32 | end | |
33 | end | |
34 | ||
35 | function ret = EVAL(ast, env) | |
0b234e13 | 36 | %fprintf('EVAL: %s\n', printer.pr_str(ast, true)); |
47699629 | 37 | if ~type_utils.list_Q(ast) |
d6624158 JM |
38 | ret = eval_ast(ast, env); |
39 | return; | |
40 | end | |
41 | ||
42 | % apply | |
6a572dff JM |
43 | if isa(ast.get(1),'types.Symbol') |
44 | a1sym = ast.get(1).name; | |
d6624158 JM |
45 | else |
46 | a1sym = '_@$fn$@_'; | |
47 | end | |
48 | switch (a1sym) | |
49 | case 'def!' | |
6a572dff | 50 | ret = env.set(ast.get(2), EVAL(ast.get(3), env)); |
d6624158 | 51 | case 'let*' |
47699629 | 52 | let_env = Env({env}); |
6a572dff JM |
53 | for i=1:2:length(ast.get(2)) |
54 | let_env.set(ast.get(2).get(i), EVAL(ast.get(2).get(i+1), let_env)); | |
d6624158 | 55 | end |
6a572dff | 56 | ret = EVAL(ast.get(3), let_env); |
d6624158 | 57 | case 'do' |
6a572dff JM |
58 | el = eval_ast(ast.slice(2), env); |
59 | ret = el.get(length(el)); | |
d6624158 | 60 | case 'if' |
6a572dff | 61 | cond = EVAL(ast.get(2), env); |
d6624158 JM |
62 | if strcmp(class(cond), 'types.Nil') || ... |
63 | (islogical(cond) && cond == false) | |
64 | if length(ast) > 3 | |
6a572dff | 65 | ret = EVAL(ast.get(4), env); |
d6624158 | 66 | else |
47699629 | 67 | ret = type_utils.nil; |
d6624158 JM |
68 | end |
69 | else | |
6a572dff | 70 | ret = EVAL(ast.get(3), env); |
d6624158 JM |
71 | end |
72 | case 'fn*' | |
47699629 | 73 | ret = @(varargin) EVAL(ast.get(3), Env({env}, ast.get(2), ... |
6a572dff | 74 | types.List(varargin{:}))); |
d6624158 JM |
75 | otherwise |
76 | el = eval_ast(ast, env); | |
6a572dff JM |
77 | f = el.get(1); |
78 | args = el.data(2:end); | |
d6624158 JM |
79 | ret = f(args{:}); |
80 | end | |
81 | end | |
82 | ||
83 | ||
84 | function ret = PRINT(ast) | |
85 | ret = printer.pr_str(ast, true); | |
86 | end | |
87 | ||
88 | % REPL | |
89 | function ret = rep(str, env) | |
90 | ret = PRINT(EVAL(READ(str), env)); | |
91 | end | |
92 | ||
93 | function main(args) | |
47699629 | 94 | repl_env = Env(); |
6d12affa JM |
95 | |
96 | % core.m: defined using matlab | |
d6624158 JM |
97 | ns = core.ns(); ks = ns.keys(); |
98 | for i=1:length(ks) | |
99 | k = ks{i}; | |
100 | repl_env.set(types.Symbol(k), ns(k)); | |
101 | end | |
102 | ||
6d12affa JM |
103 | % core.mal: defined using the langauge itself |
104 | rep('(def! not (fn* (a) (if a false true)))', repl_env); | |
105 | ||
d6624158 JM |
106 | %cleanObj = onCleanup(@() disp('*** here1 ***')); |
107 | while (true) | |
47699629 JM |
108 | try |
109 | line = input('user> ', 's'); | |
110 | catch err | |
111 | return | |
112 | end | |
d6624158 JM |
113 | if strcmp(strtrim(line),''), continue, end |
114 | try | |
115 | fprintf('%s\n', rep(line, repl_env)); | |
116 | catch err | |
117 | fprintf('Error: %s\n', err.message); | |
47699629 | 118 | type_utils.print_stack(err); |
d6624158 JM |
119 | end |
120 | end | |
121 | end |