matlab: all step4 except optional.
authorJoel Martin <github@martintribe.org>
Sun, 8 Feb 2015 19:18:08 +0000 (13:18 -0600)
committerJoel Martin <github@martintribe.org>
Mon, 9 Feb 2015 05:51:22 +0000 (23:51 -0600)
matlab/+types/Nil.m
matlab/+types/Symbol.m
matlab/core.m
matlab/printer.m
matlab/reader.m
matlab/step4_if_fn_do.m
matlab/types.m

index d7573f0..7aa8130 100644 (file)
@@ -1,2 +1,10 @@
 classdef Nil
+    methods
+        function len = length(obj)
+            len = 0;
+        end
+        function ret = eq(a,b)
+            ret = strcmp(class(b),'types.Nil');
+        end
+    end
 end
index 1a06037..5da6b7a 100644 (file)
@@ -6,5 +6,8 @@ classdef Symbol
         function sym = Symbol(name)
             sym.name = name;
         end
+        function ret = eq(a,b)
+            ret = strcmp(a.name, b.name);
+        end
     end
 end
index 968b9a4..8a64934 100644 (file)
@@ -1,16 +1,44 @@
 classdef core
     methods(Static)
+        function str = pr_str(varargin)
+            strs = cellfun(@(s) printer.pr_str(s,true), varargin, ...
+                    'UniformOutput', false);
+            str = strjoin(strs, ' ');
+        end
+        function str = do_str(varargin)
+            strs = cellfun(@(s) printer.pr_str(s,false), varargin, ...
+                    'UniformOutput', false);
+            str = strjoin(strs, '');
+        end
+        function ret = prn(varargin)
+            strs = cellfun(@(s) printer.pr_str(s,true), varargin, ...
+                    'UniformOutput', false);
+            fprintf('%s\n', strjoin(strs, ' '));
+            ret = types.nil;
+        end
+        function ret = println(varargin)
+            strs = cellfun(@(s) printer.pr_str(s,false), varargin, ...
+                    'UniformOutput', false);
+            fprintf('%s\n', strjoin(strs, ' '));
+            ret = types.nil;
+        end
+
         function n = ns()
             n = containers.Map();
-            n('=') =  @(a,b) a==b;
+            n('=') =  @types.equal;
+
+            n('pr-str') = @core.pr_str;
+            n('str') = @core.do_str;
+            n('prn') = @core.prn;
+            n('println') = @core.println;
             n('<') =  @(a,b) a<b;
             n('<=') = @(a,b) a<=b;
             n('>') =  @(a,b) a>b;
             n('>=') = @(a,b) a>=b;
-            n('+') = @(a,b) a+b;
-            n('-') = @(a,b) a-b;
-            n('*') = @(a,b) a*b;
-            n('/') = @(a,b) floor(a/b);
+            n('+') =  @(a,b) a+b;
+            n('-') =  @(a,b) a-b;
+            n('*') =  @(a,b) a*b;
+            n('/') =  @(a,b) floor(a/b);
 
             n('list') = @(varargin) varargin;
             n('list?') = @iscell;
index 9308a96..11e22a8 100644 (file)
@@ -8,7 +8,14 @@ classdef printer
             case 'double'
                 str = num2str(obj);
             case 'char'
-                str = strcat('"', obj, '"');
+                if print_readably
+                    str = strrep(obj, '\', '\\');
+                    str = strrep(str, '"', '\"');
+                    str = strrep(str, char(10), '\n');
+                    str = strcat('"', str, '"');
+                else
+                    str = obj;
+                end
             case 'cell'
                 strs = cellfun(@(x) printer.pr_str(x, print_readably), ...
                                obj, 'UniformOutput', false);
index 77dae07..7053aef 100644 (file)
@@ -14,6 +14,8 @@ classdef reader
                 atm = str2double(token);
             elseif strcmp(token(1), '"')
                 atm = token(2:length(token)-1);
+                atm = strrep(atm, '\"', '"');
+                atm = strrep(atm, '\n', char(10));
             elseif strcmp(token, 'nil')
                 atm = types.nil;
             elseif strcmp(token, 'true')
index 4f73da7..fe3ca2b 100644 (file)
@@ -78,12 +78,17 @@ 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
 
+    % core.mal: defined using the langauge itself
+    rep('(def! not (fn* (a) (if a false true)))', repl_env);
+
     %cleanObj = onCleanup(@() disp('*** here1 ***'));
     while (true)
         line = input('user> ', 's');
index cb3bf8f..7263fb2 100644 (file)
@@ -2,5 +2,31 @@ classdef types
     properties (Constant = true)
         nil = types.Nil();
     end
+
+    methods(Static)
+        function ret = equal(a,b)
+            ret = false;
+            ota = class(a); otb = class(b);
+            if ~(strcmp(ota,otb) || (iscell(a) && iscell(b)))
+                return;
+            end
+            switch (ota)
+            case 'cell'
+                if ~(length(a) == length(b))
+                    return
+                end
+                for i=1:length(a)
+                    if ~(types.equal(a{i}, b{i}))
+                        return
+                    end
+                end
+                ret = true;
+            case 'char'
+                ret = strcmp(a,b);
+            otherwise
+                ret = a == b;
+            end
+        end
+    end
 end