aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2015-02-08 19:23:49 -0600
committerJoel Martin <github@martintribe.org>2015-02-08 23:51:45 -0600
commitc3023f2642064bb416bc6b817ab5ea80fec29fce (patch)
treedfaf85ae10920e89f5cd2c6f8e6ea758f76fff30
parent9831bce71071b1db9391ef77d450199c6bc76990 (diff)
downloadmal-c3023f2642064bb416bc6b817ab5ea80fec29fce.tar.gz
mal-c3023f2642064bb416bc6b817ab5ea80fec29fce.zip
matlab: step7, fix reader bug.
-rw-r--r--matlab/core.m11
-rw-r--r--matlab/reader.m14
-rw-r--r--matlab/step7_quote.m151
3 files changed, 175 insertions, 1 deletions
diff --git a/matlab/core.m b/matlab/core.m
index fb6e456..4115b2c 100644
--- a/matlab/core.m
+++ b/matlab/core.m
@@ -23,6 +23,14 @@ classdef core
ret = types.nil;
end
+ function ret = concat(varargin)
+ if nargin == 0
+ ret = {};
+ else
+ ret = cat(2,varargin{:});
+ end
+ end
+
function n = ns()
n = containers.Map();
n('=') = @types.equal;
@@ -45,6 +53,9 @@ classdef core
n('list') = @(varargin) varargin;
n('list?') = @iscell;
+
+ n('cons') = @(a,b) [{a}, b];
+ n('concat') = @core.concat;
n('empty?') = @(a) length(a) == 0;
n('count') = @(a) length(a);
end
diff --git a/matlab/reader.m b/matlab/reader.m
index 7053aef..968e5a1 100644
--- a/matlab/reader.m
+++ b/matlab/reader.m
@@ -49,7 +49,19 @@ classdef reader
function ast = read_form(rdr)
%fprintf('in read_form\n');
token = rdr.peek();
- switch token(1)
+ switch token
+ case ''''
+ rdr.next();
+ ast = {types.Symbol('quote'), reader.read_form(rdr)};
+ case '`'
+ rdr.next();
+ ast = {types.Symbol('quasiquote'), reader.read_form(rdr)};
+ case '~'
+ rdr.next();
+ ast = {types.Symbol('unquote'), reader.read_form(rdr)};
+ case '~@'
+ rdr.next();
+ ast = {types.Symbol('splice-unquote'), reader.read_form(rdr)};
case ')'
error('unexpected '')''');
case '('
diff --git a/matlab/step7_quote.m b/matlab/step7_quote.m
new file mode 100644
index 0000000..eaace83
--- /dev/null
+++ b/matlab/step7_quote.m
@@ -0,0 +1,151 @@
+function step7_quote(varargin), main(varargin), end
+
+% read
+function ret = READ(str)
+ ret = reader.read_str(str);
+end
+
+% eval
+function ret = is_pair(ast)
+ ret = isa(ast, 'cell') && 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 = 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
+ 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 '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);
+
+ 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