matlab: fix do/slice, strings. Self-hosting!
[jackhill/mal.git] / matlab / stepA_interop.m
CommitLineData
7f567f36
JM
1function stepA_interop(varargin), main(varargin), end
2
3% read
4function ret = READ(str)
5 ret = reader.read_str(str);
6end
7
8% eval
9function ret = is_pair(ast)
6a572dff 10 ret = types.sequential_Q(ast) && length(ast) > 0;
7f567f36
JM
11end
12
13function ret = quasiquote(ast)
14 if ~is_pair(ast)
6a572dff
JM
15 ret = types.List(types.Symbol('quote'), ast);
16 elseif isa(ast.get(1),'types.Symbol') && ...
17 strcmp(ast.get(1).name, 'unquote')
18 ret = ast.get(2);
19 elseif is_pair(ast.get(1)) && ...
20 isa(ast.get(1).get(1),'types.Symbol') && ...
21 strcmp(ast.get(1).get(1).name, 'splice-unquote')
22 ret = types.List(types.Symbol('concat'), ...
23 ast.get(1).get(2), ...
24 quasiquote(ast.slice(2)));
7f567f36 25 else
6a572dff
JM
26 ret = types.List(types.Symbol('cons'), ...
27 quasiquote(ast.get(1)), ...
28 quasiquote(ast.slice(2)));
7f567f36
JM
29 end
30end
31
32function ret = is_macro_call(ast, env)
6a572dff
JM
33 if types.list_Q(ast) && isa(ast.get(1), 'types.Symbol') && ...
34 ~islogical(env.find(ast.get(1)))
35 f = env.get(ast.get(1));
7f567f36
JM
36 ret = isa(f,'types.Function') && f.is_macro;
37 else
38 ret = false;
39 end
40end
41
42function ret = macroexpand(ast, env)
43 while is_macro_call(ast, env)
6a572dff
JM
44 mac = env.get(ast.get(1));
45 args = ast.slice(2);
46 ast = mac.fn(args.data{:});
7f567f36
JM
47 end
48 ret = ast;
49end
50
51function ret = eval_ast(ast, env)
52 switch class(ast)
53 case 'types.Symbol'
54 ret = env.get(ast);
6a572dff
JM
55 case 'types.List'
56 ret = types.List();
7f567f36 57 for i=1:length(ast)
6a572dff
JM
58 ret.append(EVAL(ast.get(i), env));
59 end
60 case 'types.Vector'
61 ret = types.Vector();
62 for i=1:length(ast)
63 ret.append(EVAL(ast.get(i), env));
64 end
65 case 'types.HashMap'
66 ret = types.HashMap();
67 ks = ast.keys();
68 for i=1:length(ks)
69 k = ks{i};
70 ret.set(EVAL(k, env), EVAL(ast.get(k), env));
7f567f36
JM
71 end
72 otherwise
73 ret = ast;
74 end
75end
76
77function ret = EVAL(ast, env)
78 while true
0b234e13 79 %fprintf('EVAL: %s\n', printer.pr_str(ast, true));
6a572dff 80 if ~types.list_Q(ast)
7f567f36
JM
81 ret = eval_ast(ast, env);
82 return;
83 end
84
85 % apply
86 ast = macroexpand(ast, env);
6a572dff 87 if ~types.list_Q(ast)
7f567f36
JM
88 ret = ast;
89 return;
90 end
91
6a572dff
JM
92 if isa(ast.get(1),'types.Symbol')
93 a1sym = ast.get(1).name;
7f567f36
JM
94 else
95 a1sym = '_@$fn$@_';
96 end
97 switch (a1sym)
98 case 'def!'
6a572dff 99 ret = env.set(ast.get(2), EVAL(ast.get(3), env));
7f567f36
JM
100 return;
101 case 'let*'
102 let_env = Env(env);
6a572dff
JM
103 for i=1:2:length(ast.get(2))
104 let_env.set(ast.get(2).get(i), EVAL(ast.get(2).get(i+1), let_env));
7f567f36
JM
105 end
106 env = let_env;
6a572dff 107 ast = ast.get(3); % TCO
7f567f36 108 case 'quote'
6a572dff 109 ret = ast.get(2);
7f567f36
JM
110 return;
111 case 'quasiquote'
6a572dff 112 ast = quasiquote(ast.get(2)); % TCO
7f567f36 113 case 'defmacro!'
6a572dff 114 ret = env.set(ast.get(2), EVAL(ast.get(3), env));
7f567f36
JM
115 ret.is_macro = true;
116 return;
117 case 'macroexpand'
6a572dff 118 ret = macroexpand(ast.get(2), env);
7f567f36
JM
119 return;
120 case 'try*'
121 try
6a572dff 122 ret = EVAL(ast.get(2), env);
7f567f36
JM
123 return;
124 catch e
6a572dff 125 if length(ast) > 2 && strcmp(ast.get(3).get(1).name, 'catch*')
7f567f36
JM
126 if isa(e, 'types.MalException')
127 exc = e.obj;
128 else
129 exc = e.message;
130 end
6a572dff
JM
131 catch_env = Env(env, types.List(ast.get(3).get(2)), ...
132 types.List(exc));
133 ret = EVAL(ast.get(3).get(3), catch_env);
7f567f36
JM
134 return;
135 else
136 throw(e);
137 end
138 end
139 case 'do'
6a572dff
JM
140 el = eval_ast(ast.slice(2,length(ast)-1), env);
141 ast = ast.get(length(ast)); % TCO
7f567f36 142 case 'if'
6a572dff 143 cond = EVAL(ast.get(2), env);
7f567f36
JM
144 if strcmp(class(cond), 'types.Nil') || ...
145 (islogical(cond) && cond == false)
146 if length(ast) > 3
6a572dff 147 ast = ast.get(4); % TCO
7f567f36
JM
148 else
149 ret = types.nil;
150 return;
151 end
152 else
6a572dff 153 ast = ast.get(3); % TCO
7f567f36
JM
154 end
155 case 'fn*'
6a572dff
JM
156 fn = @(varargin) EVAL(ast.get(3), Env(env, ast.get(2), ...
157 types.List(varargin{:})));
158 ret = types.Function(fn, ast.get(3), env, ast.get(2));
7f567f36
JM
159 return;
160 otherwise
161 el = eval_ast(ast, env);
6a572dff
JM
162 f = el.get(1);
163 args = el.slice(2);
7f567f36
JM
164 if isa(f, 'types.Function')
165 env = Env(f.env, f.params, args);
166 ast = f.ast; % TCO
167 else
6a572dff 168 ret = f(args.data{:});
7f567f36
JM
169 return
170 end
171 end
172 end
173end
174
175% print
176function ret = PRINT(ast)
177 ret = printer.pr_str(ast, true);
178end
179
180% REPL
181function ret = rep(str, env)
182 ret = PRINT(EVAL(READ(str), env));
183end
184
185function main(args)
186 repl_env = Env(false);
187
188 % core.m: defined using matlab
189 ns = core.ns(); ks = ns.keys();
190 for i=1:length(ks)
191 k = ks{i};
192 repl_env.set(types.Symbol(k), ns(k));
193 end
194 repl_env.set(types.Symbol('eval'), @(a) EVAL(a, repl_env));
6a572dff
JM
195 rest_args = args(2:end);
196 repl_env.set(types.Symbol('*ARGV*'), types.List(rest_args{:}));
7f567f36
JM
197
198 % core.mal: defined using the langauge itself
199 rep('(def! *host-language* "matlab")', repl_env);
200 rep('(def! not (fn* (a) (if a false true)))', repl_env);
201 rep('(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) ")")))))"', repl_env);
202 rep('(defmacro! cond (fn* (& xs) (if (> (count xs) 0) (list ''if (first xs) (if (> (count xs) 1) (nth xs 1) (throw "odd number of forms to cond")) (cons ''cond (rest (rest xs)))))))', repl_env);
203 rep('(defmacro! or (fn* (& xs) (if (empty? xs) nil (if (= 1 (count xs)) (first xs) `(let* (or_FIXME ~(first xs)) (if or_FIXME or_FIXME (or ~@(rest xs))))))))', repl_env);
204
205 if ~isempty(args)
0b234e13 206 rep(sprintf('(load-file "%s")', args{1}), repl_env);
7f567f36
JM
207 quit;
208 end
209
210 %cleanObj = onCleanup(@() disp('*** here1 ***'));
211 rep('(println (str "Mal [" *host-language* "]"))', repl_env);
212 while (true)
213 line = input('user> ', 's');
214 if strcmp(strtrim(line),''), continue, end
215 try
216 fprintf('%s\n', rep(line, repl_env));
217 catch err
218 if isa(err, 'types.MalException')
219 fprintf('Error: %s\n', printer.pr_str(err.obj, true));
220 else
221 fprintf('Error: %s\n', err.message);
222 end
223 fprintf('%s\n', getReport(err, 'extended'));
224 end
225 end
226end