matlab: add metadata and atom support.
[jackhill/mal.git] / matlab / step6_file.m
1 function step6_file(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);
13 case 'types.List'
14 ret = types.List();
15 for i=1:length(ast)
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));
29 end
30 otherwise
31 ret = ast;
32 end
33 end
34
35 function ret = EVAL(ast, env)
36 while true
37 if ~types.list_Q(ast)
38 ret = eval_ast(ast, env);
39 return;
40 end
41
42 % apply
43 if isa(ast.get(1),'types.Symbol')
44 a1sym = ast.get(1).name;
45 else
46 a1sym = '_@$fn$@_';
47 end
48 switch (a1sym)
49 case 'def!'
50 ret = env.set(ast.get(2), EVAL(ast.get(3), env));
51 return;
52 case 'let*'
53 let_env = Env(env);
54 for i=1:2:length(ast.get(2))
55 let_env.set(ast.get(2).get(i), EVAL(ast.get(2).get(i+1), let_env));
56 end
57 env = let_env;
58 ast = ast.get(3); % TCO
59 case 'do'
60 el = eval_ast(ast.slice(2,length(ast)-1), env);
61 ast = ast.get(length(ast)); % TCO
62 case 'if'
63 cond = EVAL(ast.get(2), env);
64 if strcmp(class(cond), 'types.Nil') || ...
65 (islogical(cond) && cond == false)
66 if length(ast) > 3
67 ast = ast.get(4); % TCO
68 else
69 ret = types.nil;
70 return;
71 end
72 else
73 ast = ast.get(3); % TCO
74 end
75 case 'fn*'
76 fn = @(varargin) EVAL(ast.get(3), Env(env, ast.get(2), ...
77 types.List(varargin{:})));
78 ret = types.Function(fn, ast.get(3), env, ast.get(2));
79 return;
80 otherwise
81 el = eval_ast(ast, env);
82 f = el.get(1);
83 args = el.slice(2);
84 if isa(f, 'types.Function')
85 env = Env(f.env, f.params, args);
86 ast = f.ast; % TCO
87 else
88 ret = f(args.data{:});
89 return
90 end
91 end
92 end
93 end
94
95 % print
96 function ret = PRINT(ast)
97 ret = printer.pr_str(ast, true);
98 end
99
100 % REPL
101 function ret = rep(str, env)
102 ret = PRINT(EVAL(READ(str), env));
103 end
104
105 function main(args)
106 repl_env = Env(false);
107
108 % core.m: defined using matlab
109 ns = core.ns(); ks = ns.keys();
110 for i=1:length(ks)
111 k = ks{i};
112 repl_env.set(types.Symbol(k), ns(k));
113 end
114 repl_env.set(types.Symbol('eval'), @(a) EVAL(a, repl_env));
115 rest_args = args(2:end);
116 repl_env.set(types.Symbol('*ARGV*'), types.List(rest_args{:}));
117
118 % core.mal: defined using the langauge itself
119 rep('(def! not (fn* (a) (if a false true)))', repl_env);
120 rep('(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) ")")))))"', repl_env);
121
122 if ~isempty(args)
123 rep(strcat('(load-file "', args{1}, '")'), repl_env);
124 quit;
125 end
126
127 %cleanObj = onCleanup(@() disp('*** here1 ***'));
128 while (true)
129 line = input('user> ', 's');
130 if strcmp(strtrim(line),''), continue, end
131 try
132 fprintf('%s\n', rep(line, repl_env));
133 catch err
134 fprintf('Error: %s\n', err.message);
135 fprintf('%s\n', getReport(err, 'extended'));
136 end
137 end
138 end