diff options
| author | Joel Martin <github@martintribe.org> | 2015-02-08 20:35:44 -0600 |
|---|---|---|
| committer | Joel Martin <github@martintribe.org> | 2015-02-08 23:51:47 -0600 |
| commit | c4033aab513035999bb322b2063b6fbf57bedc97 (patch) | |
| tree | 6000856d742db0a89328096a4142df09f3e29cb8 | |
| parent | c3023f2642064bb416bc6b817ab5ea80fec29fce (diff) | |
| download | mal-c4033aab513035999bb322b2063b6fbf57bedc97.tar.gz mal-c4033aab513035999bb322b2063b6fbf57bedc97.zip | |
matlab: add step8
| -rw-r--r-- | Makefile | 7 | ||||
| -rw-r--r-- | matlab/Function.m | 1 | ||||
| -rw-r--r-- | matlab/core.m | 19 | ||||
| -rw-r--r-- | matlab/step2_eval.m | 2 | ||||
| -rw-r--r-- | matlab/step3_env.m | 2 | ||||
| -rw-r--r-- | matlab/step4_if_fn_do.m | 2 | ||||
| -rw-r--r-- | matlab/step5_tco.m | 2 | ||||
| -rw-r--r-- | matlab/step6_file.m | 2 | ||||
| -rw-r--r-- | matlab/step7_quote.m | 2 | ||||
| -rw-r--r-- | matlab/step8_macros.m | 184 |
10 files changed, 216 insertions, 7 deletions
@@ -74,6 +74,10 @@ rust_STEP_TO_PROG = rust/target/$($(1)) scala_STEP_TO_PROG = scala/$($(1)).scala vb_STEP_TO_PROG = vb/$($(1)).exe +# Needed some argument munging +COMMA = , +noop = +SPACE = $(noop) $(noop) bash_RUNSTEP = bash ../$(2) $(3) c_RUNSTEP = ../$(2) $(3) @@ -88,7 +92,8 @@ lua_RUNSTEP = ../$(2) $(3) make_RUNSTEP = make -f ../$(2) $(3) mal_RUNSTEP = $(call $(MAL_IMPL)_RUNSTEP,$(1),$(call $(MAL_IMPL)_STEP_TO_PROG,stepA),../$(2),") #" ocaml_RUNSTEP = ../$(2) $(3) -matlab_RUNSTEP = matlab -nodisplay -nosplash -nodesktop -nojvm -r "run('../$(2)');quit;" +matlab_args = $(subst $(SPACE),$(COMMA),$(patsubst %,'%',$(strip $(1)))) +matlab_RUNSTEP = matlab -nodisplay -nosplash -nodesktop -nojvm -r "$($(1))($(call matlab_args,$(4)));quit;" perl_RUNSTEP = perl ../$(2) --raw $(3) php_RUNSTEP = php ../$(2) $(3) ps_RUNSTEP = $(4)gs -q -I./ -dNODISPLAY -- ../$(2) $(3)$(4) diff --git a/matlab/Function.m b/matlab/Function.m index 407fe39..2f28368 100644 --- a/matlab/Function.m +++ b/matlab/Function.m @@ -4,6 +4,7 @@ classdef Function < handle ast env params + is_macro = false end methods function f = Function(fn, ast, env, params) diff --git a/matlab/core.m b/matlab/core.m index 4115b2c..ef0b019 100644 --- a/matlab/core.m +++ b/matlab/core.m @@ -31,6 +31,22 @@ classdef core end end + function ret = first(seq) + if length(seq) < 1 + ret = types.nil; + else + ret = seq{1}; + end + end + + function ret = nth(seq, idx) + if idx+1 > length(seq) + throw(MException('Range:nth', ... + 'nth: index out of range')) + end + ret = seq{idx+1}; + end + function n = ns() n = containers.Map(); n('=') = @types.equal; @@ -56,6 +72,9 @@ classdef core n('cons') = @(a,b) [{a}, b]; n('concat') = @core.concat; + n('nth') = @core.nth; + n('first') = @core.first; + n('rest') = @(a) a(2:end); n('empty?') = @(a) length(a) == 0; n('count') = @(a) length(a); end diff --git a/matlab/step2_eval.m b/matlab/step2_eval.m index 4ce13fb..a388145 100644 --- a/matlab/step2_eval.m +++ b/matlab/step2_eval.m @@ -21,7 +21,7 @@ function ret = eval_ast(ast, env) end function ret = EVAL(ast, env) - if ~iscell(ast), + if ~iscell(ast) ret = eval_ast(ast, env); return; end diff --git a/matlab/step3_env.m b/matlab/step3_env.m index 6ac6426..f5b953b 100644 --- a/matlab/step3_env.m +++ b/matlab/step3_env.m @@ -21,7 +21,7 @@ function ret = eval_ast(ast, env) end function ret = EVAL(ast, env) - if ~iscell(ast), + if ~iscell(ast) ret = eval_ast(ast, env); return; end diff --git a/matlab/step4_if_fn_do.m b/matlab/step4_if_fn_do.m index fe3ca2b..dc9f28b 100644 --- a/matlab/step4_if_fn_do.m +++ b/matlab/step4_if_fn_do.m @@ -21,7 +21,7 @@ function ret = eval_ast(ast, env) end function ret = EVAL(ast, env) - if ~iscell(ast), + if ~iscell(ast) ret = eval_ast(ast, env); return; end diff --git a/matlab/step5_tco.m b/matlab/step5_tco.m index c63e7e4..7f6ecd7 100644 --- a/matlab/step5_tco.m +++ b/matlab/step5_tco.m @@ -22,7 +22,7 @@ end function ret = EVAL(ast, env) while true - if ~iscell(ast), + if ~iscell(ast) ret = eval_ast(ast, env); return; end diff --git a/matlab/step6_file.m b/matlab/step6_file.m index 63fd84a..674a53a 100644 --- a/matlab/step6_file.m +++ b/matlab/step6_file.m @@ -22,7 +22,7 @@ end function ret = EVAL(ast, env) while true - if ~iscell(ast), + if ~iscell(ast) ret = eval_ast(ast, env); return; end diff --git a/matlab/step7_quote.m b/matlab/step7_quote.m index eaace83..266751a 100644 --- a/matlab/step7_quote.m +++ b/matlab/step7_quote.m @@ -7,7 +7,7 @@ end % eval function ret = is_pair(ast) - ret = isa(ast, 'cell') && length(ast) > 0; + ret = iscell(ast) && length(ast) > 0; end function ret = quasiquote(ast) diff --git a/matlab/step8_macros.m b/matlab/step8_macros.m new file mode 100644 index 0000000..61a86f4 --- /dev/null +++ b/matlab/step8_macros.m @@ -0,0 +1,184 @@ +function step8_macros(varargin), main(varargin), end + +% read +function ret = READ(str) + ret = reader.read_str(str); +end + +% eval +function ret = is_pair(ast) + ret = iscell(ast) && length(ast) > 0; +end + +function ret = quasiquote(ast) + if ~is_pair(ast) + ret = {types.Symbol('quote'), ast}; + elseif isa(ast{1},'types.Symbol') && ... + strcmp(ast{1}.name, 'unquote') + ret = ast{2}; + elseif is_pair(ast{1}) && isa(ast{1}{1},'types.Symbol') && ... + strcmp(ast{1}{1}.name, 'splice-unquote') + ret = {types.Symbol('concat'), ... + ast{1}{2}, ... + quasiquote(ast(2:end))}; + else + ret = {types.Symbol('cons'), ... + quasiquote(ast{1}), ... + quasiquote(ast(2:end))}; + end +end + +function ret = is_macro_call(ast, env) + if iscell(ast) && isa(ast{1}, 'types.Symbol') && ... + ~islogical(env.find(ast{1})) + f = env.get(ast{1}); + ret = isa(f,'Function') && f.is_macro; + else + ret = false; + end +end + +function ret = macroexpand(ast, env) + while is_macro_call(ast, env) + mac = env.get(ast{1}); + ast = mac.fn(ast{2:end}); + end + ret = ast; +end + +function ret = eval_ast(ast, env) + switch class(ast) + case 'types.Symbol' + ret = env.get(ast); + case 'cell' + ret = {}; + for i=1:length(ast) + ret{end+1} = EVAL(ast{i}, env); + end + otherwise + ret = ast; + end +end + +function ret = EVAL(ast, env) + while true + if ~iscell(ast) + ret = eval_ast(ast, env); + return; + end + + % apply + ast = macroexpand(ast, env); + if ~iscell(ast) + ret = ast; + return; + end + + if isa(ast{1},'types.Symbol') + a1sym = ast{1}.name; + else + a1sym = '_@$fn$@_'; + end + switch (a1sym) + case 'def!' + ret = env.set(ast{2}, EVAL(ast{3}, env)); + return; + case 'let*' + let_env = Env(env); + for i=1:2:length(ast{2}) + let_env.set(ast{2}{i}, EVAL(ast{2}{i+1}, let_env)); + end + env = let_env; + ast = ast{3}; % TCO + case 'quote' + ret = ast{2}; + return; + case 'quasiquote' + ast = quasiquote(ast{2}); % TCO + case 'defmacro!' + ret = env.set(ast{2}, EVAL(ast{3}, env)); + ret.is_macro = true; + return; + case 'macroexpand' + ret = macroexpand(ast{2}, env); + return; + case 'do' + el = eval_ast(ast(2:end-1), env); + ast = ast{end}; % TCO + case 'if' + cond = EVAL(ast{2}, env); + if strcmp(class(cond), 'types.Nil') || ... + (islogical(cond) && cond == false) + if length(ast) > 3 + ast = ast{4}; % TCO + else + ret = types.nil; + return; + end + else + ast = ast{3}; % TCO + end + case 'fn*' + fn = @(varargin) EVAL(ast{3}, Env(env, ast{2}, varargin)); + ret = Function(fn, ast{3}, env, ast{2}); + return; + otherwise + el = eval_ast(ast, env); + f = el{1}; + args = el(2:end); + if isa(f, 'Function') + env = Env(f.env, f.params, args); + ast = f.ast; % TCO + else + ret = f(args{:}); + return + end + end + end +end + +% print +function ret = PRINT(ast) + ret = printer.pr_str(ast, true); +end + +% REPL +function ret = rep(str, env) + ret = PRINT(EVAL(READ(str), env)); +end + +function main(args) + repl_env = Env(false); + + % core.m: defined using matlab + ns = core.ns(); ks = ns.keys(); + for i=1:length(ks) + k = ks{i}; + repl_env.set(types.Symbol(k), ns(k)); + end + repl_env.set(types.Symbol('eval'), @(a) EVAL(a, repl_env)); + repl_env.set(types.Symbol('*ARGV*'), args(2:end)); + + % core.mal: defined using the langauge itself + rep('(def! not (fn* (a) (if a false true)))', repl_env); + rep('(def! load-file (fn* (f) (eval (read-string (str "(do " (slurp f) ")")))))"', repl_env); + 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); + 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); + + if ~isempty(args) + rep(strcat('(load-file "', args{1}, '")'), repl_env); + quit; + end + + %cleanObj = onCleanup(@() disp('*** here1 ***')); + while (true) + line = input('user> ', 's'); + if strcmp(strtrim(line),''), continue, end + try + fprintf('%s\n', rep(line, repl_env)); + catch err + fprintf('Error: %s\n', err.message); + fprintf('%s\n', getReport(err, 'extended')); + end + end +end |
