aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2015-02-07 21:03:43 -0600
committerJoel Martin <github@martintribe.org>2015-02-07 21:03:43 -0600
commita8d58c9360834ce70472f5719227f76fcf8c52d7 (patch)
tree2cc231fe8ba1e0aa8fdaa98e8ac20b006bfe6269
parent9a54ea186c2c7646d014b97774adfc418e9ab9c9 (diff)
downloadmal-a8d58c9360834ce70472f5719227f76fcf8c52d7.tar.gz
mal-a8d58c9360834ce70472f5719227f76fcf8c52d7.zip
matlab: step2
-rw-r--r--matlab/step0_repl.m11
-rw-r--r--matlab/step1_read_print.m10
-rw-r--r--matlab/step2_eval.m62
3 files changed, 76 insertions, 7 deletions
diff --git a/matlab/step0_repl.m b/matlab/step0_repl.m
index 22025c0..3b4e7ca 100644
--- a/matlab/step0_repl.m
+++ b/matlab/step0_repl.m
@@ -1,25 +1,28 @@
function step0_repl(varargin), main(varargin), end
+% read
function ret = READ(str)
ret = str;
end
+% eval
function ret = EVAL(ast, env)
ret = ast;
end
+% print
function ret = PRINT(ast)
ret = ast;
end
-function ret = rep(str)
- ret = PRINT(EVAL(READ(str), ''));
+% REPL
+function ret = rep(str, env)
+ ret = PRINT(EVAL(READ(str), env));
end
function main(args)
while (true)
line = input('user> ', 's');
- fprintf('%s\n', rep(line));
+ fprintf('%s\n', rep(line, ''));
end
end
-
diff --git a/matlab/step1_read_print.m b/matlab/step1_read_print.m
index 486b725..8b63b36 100644
--- a/matlab/step1_read_print.m
+++ b/matlab/step1_read_print.m
@@ -1,19 +1,23 @@
function step1_read_print(varargin), main(varargin), end
+% read
function ret = READ(str)
ret = reader.read_str(str);
end
+% eval
function ret = EVAL(ast, env)
ret = ast;
end
+% print
function ret = PRINT(ast)
ret = printer.pr_str(ast, true);
end
-function ret = rep(str)
- ret = PRINT(EVAL(READ(str), ''));
+% REPL
+function ret = rep(str, env)
+ ret = PRINT(EVAL(READ(str), env));
end
function main(args)
@@ -22,7 +26,7 @@ function main(args)
line = input('user> ', 's');
if strcmp(strtrim(line),''), continue, end
try
- fprintf('%s\n', rep(line));
+ fprintf('%s\n', rep(line, ''));
catch err
fprintf('Error: %s\n', err.message);
fprintf('%s\n', getReport(err, 'extended'));
diff --git a/matlab/step2_eval.m b/matlab/step2_eval.m
new file mode 100644
index 0000000..418bb84
--- /dev/null
+++ b/matlab/step2_eval.m
@@ -0,0 +1,62 @@
+function step1_read_print(varargin), main(varargin), end
+
+% read
+function ret = READ(str)
+ ret = reader.read_str(str);
+end
+
+% eval
+function ret = eval_ast(ast, env)
+ switch class(ast)
+ case 'types.Symbol'
+ ret = env(ast.name);
+ 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)
+ if ~iscell(ast),
+ ret = eval_ast(ast, env);
+ return;
+ end
+
+ % apply
+ el = eval_ast(ast, env);
+ f = el{1};
+ args = el(2:end);
+ ret = f(args{:});
+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 = containers.Map( ...
+ {'+', '-', '*', '/'}, ...
+ {@(a,b) a+b, @(a,b) a-b, @(a,b) a*b, @(a,b) floor(a/b)});
+
+ %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