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 | |
9870acb6 JM |
43 | if length(ast) == 0 |
44 | ret = ast; | |
45 | return; | |
46 | end | |
6a572dff JM |
47 | if isa(ast.get(1),'types.Symbol') |
48 | a1sym = ast.get(1).name; | |
d6624158 JM |
49 | else |
50 | a1sym = '_@$fn$@_'; | |
51 | end | |
52 | switch (a1sym) | |
53 | case 'def!' | |
6a572dff | 54 | ret = env.set(ast.get(2), EVAL(ast.get(3), env)); |
d6624158 | 55 | case 'let*' |
47699629 | 56 | let_env = Env({env}); |
6a572dff JM |
57 | for i=1:2:length(ast.get(2)) |
58 | let_env.set(ast.get(2).get(i), EVAL(ast.get(2).get(i+1), let_env)); | |
d6624158 | 59 | end |
6a572dff | 60 | ret = EVAL(ast.get(3), let_env); |
d6624158 | 61 | case 'do' |
6a572dff JM |
62 | el = eval_ast(ast.slice(2), env); |
63 | ret = el.get(length(el)); | |
d6624158 | 64 | case 'if' |
6a572dff | 65 | cond = EVAL(ast.get(2), env); |
d6624158 JM |
66 | if strcmp(class(cond), 'types.Nil') || ... |
67 | (islogical(cond) && cond == false) | |
68 | if length(ast) > 3 | |
6a572dff | 69 | ret = EVAL(ast.get(4), env); |
d6624158 | 70 | else |
47699629 | 71 | ret = type_utils.nil; |
d6624158 JM |
72 | end |
73 | else | |
6a572dff | 74 | ret = EVAL(ast.get(3), env); |
d6624158 JM |
75 | end |
76 | case 'fn*' | |
47699629 | 77 | ret = @(varargin) EVAL(ast.get(3), Env({env}, ast.get(2), ... |
6a572dff | 78 | types.List(varargin{:}))); |
d6624158 JM |
79 | otherwise |
80 | el = eval_ast(ast, env); | |
6a572dff JM |
81 | f = el.get(1); |
82 | args = el.data(2:end); | |
d6624158 JM |
83 | ret = f(args{:}); |
84 | end | |
85 | end | |
86 | ||
87 | ||
88 | function ret = PRINT(ast) | |
89 | ret = printer.pr_str(ast, true); | |
90 | end | |
91 | ||
92 | % REPL | |
93 | function ret = rep(str, env) | |
94 | ret = PRINT(EVAL(READ(str), env)); | |
95 | end | |
96 | ||
97 | function main(args) | |
47699629 | 98 | repl_env = Env(); |
6d12affa JM |
99 | |
100 | % core.m: defined using matlab | |
d6624158 JM |
101 | ns = core.ns(); ks = ns.keys(); |
102 | for i=1:length(ks) | |
103 | k = ks{i}; | |
104 | repl_env.set(types.Symbol(k), ns(k)); | |
105 | end | |
106 | ||
6d12affa JM |
107 | % core.mal: defined using the langauge itself |
108 | rep('(def! not (fn* (a) (if a false true)))', repl_env); | |
109 | ||
d6624158 JM |
110 | %cleanObj = onCleanup(@() disp('*** here1 ***')); |
111 | while (true) | |
47699629 JM |
112 | try |
113 | line = input('user> ', 's'); | |
114 | catch err | |
115 | return | |
116 | end | |
d6624158 JM |
117 | if strcmp(strtrim(line),''), continue, end |
118 | try | |
119 | fprintf('%s\n', rep(line, repl_env)); | |
120 | catch err | |
121 | fprintf('Error: %s\n', err.message); | |
47699629 | 122 | type_utils.print_stack(err); |
d6624158 JM |
123 | end |
124 | end | |
125 | end |