Merge pull request #406 from chr15m/lib-alias-hacks
[jackhill/mal.git] / matlab / step2_eval.m
index 418bb84..cb426d2 100644 (file)
@@ -1,4 +1,4 @@
-function step1_read_print(varargin), main(varargin), end
+function step2_eval(varargin), main(varargin), end
 
 % read
 function ret = READ(str)
@@ -10,10 +10,22 @@ function ret = eval_ast(ast, env)
     switch class(ast)
     case 'types.Symbol'
         ret = env(ast.name);
-    case 'cell'
-        ret = {};
+    case 'types.List'
+        ret = types.List();
         for i=1:length(ast)
-            ret{end+1} = EVAL(ast{i}, env);
+            ret.append(EVAL(ast.get(i), env));
+        end
+    case 'types.Vector'
+        ret = types.Vector();
+        for i=1:length(ast)
+            ret.append(EVAL(ast.get(i), env));
+        end
+    case 'types.HashMap'
+        ret = types.HashMap();
+        ks = ast.keys();
+        for i=1:length(ks)
+            k = ks{i};
+            ret.set(EVAL(k, env), EVAL(ast.get(k), env));
         end
     otherwise
         ret = ast;
@@ -21,15 +33,20 @@ function ret = eval_ast(ast, env)
 end
 
 function ret = EVAL(ast, env)
-    if ~iscell(ast),
+    %fprintf('EVAL: %s\n', printer.pr_str(ast, true));
+    if ~type_utils.list_Q(ast)
         ret = eval_ast(ast, env);
         return;
     end
 
     % apply
+    if length(ast) == 0
+        ret = ast;
+        return;
+    end
     el = eval_ast(ast, env);
-    f = el{1};
-    args = el(2:end);
+    f = el.get(1);
+    args = el.data(2:end);
     ret = f(args{:});
 end
 
@@ -44,19 +61,29 @@ function ret = rep(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)});
+    if exist('OCTAVE_VERSION', 'builtin') ~= 0
+        repl_env = Dict();
+    else
+        repl_env = containers.Map();
+    end
+    repl_env('+') = @(a,b) a+b;
+    repl_env('-') = @(a,b) a-b;
+    repl_env('*') = @(a,b) a*b;
+    repl_env('/') = @(a,b) floor(a/b);
 
     %cleanObj = onCleanup(@() disp('*** here1 ***'));
     while (true)
-        line = input('user> ', 's');
+        try
+            line = input('user> ', 's');
+        catch err
+            return
+        end
         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'));
+            type_utils.print_stack(err);
         end
     end
 end