matlab: step2
authorJoel Martin <github@martintribe.org>
Sun, 8 Feb 2015 03:03:43 +0000 (21:03 -0600)
committerJoel Martin <github@martintribe.org>
Sun, 8 Feb 2015 03:03:43 +0000 (21:03 -0600)
matlab/step0_repl.m
matlab/step1_read_print.m
matlab/step2_eval.m [new file with mode: 0644]

index 22025c0..3b4e7ca 100644 (file)
@@ -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
-
index 486b725..8b63b36 100644 (file)
@@ -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 (file)
index 0000000..418bb84
--- /dev/null
@@ -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