aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoel Martin <github@martintribe.org>2015-02-08 13:18:08 -0600
committerJoel Martin <github@martintribe.org>2015-02-08 23:51:22 -0600
commit6d12affa8bc91a6662e8d4bdcc66a6963b77e947 (patch)
tree644d5a482081600faafc09e679fb1006796de659
parentd6624158bdf41e047ad8d0a9942238dc80649901 (diff)
downloadmal-6d12affa8bc91a6662e8d4bdcc66a6963b77e947.tar.gz
mal-6d12affa8bc91a6662e8d4bdcc66a6963b77e947.zip
matlab: all step4 except optional.
-rw-r--r--matlab/+types/Nil.m8
-rw-r--r--matlab/+types/Symbol.m3
-rw-r--r--matlab/core.m38
-rw-r--r--matlab/printer.m9
-rw-r--r--matlab/reader.m2
-rw-r--r--matlab/step4_if_fn_do.m5
-rw-r--r--matlab/types.m26
7 files changed, 85 insertions, 6 deletions
diff --git a/matlab/+types/Nil.m b/matlab/+types/Nil.m
index d7573f0..7aa8130 100644
--- a/matlab/+types/Nil.m
+++ b/matlab/+types/Nil.m
@@ -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
diff --git a/matlab/+types/Symbol.m b/matlab/+types/Symbol.m
index 1a06037..5da6b7a 100644
--- a/matlab/+types/Symbol.m
+++ b/matlab/+types/Symbol.m
@@ -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
diff --git a/matlab/core.m b/matlab/core.m
index 968b9a4..8a64934 100644
--- a/matlab/core.m
+++ b/matlab/core.m
@@ -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;
diff --git a/matlab/printer.m b/matlab/printer.m
index 9308a96..11e22a8 100644
--- a/matlab/printer.m
+++ b/matlab/printer.m
@@ -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);
diff --git a/matlab/reader.m b/matlab/reader.m
index 77dae07..7053aef 100644
--- a/matlab/reader.m
+++ b/matlab/reader.m
@@ -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')
diff --git a/matlab/step4_if_fn_do.m b/matlab/step4_if_fn_do.m
index 4f73da7..fe3ca2b 100644
--- a/matlab/step4_if_fn_do.m
+++ b/matlab/step4_if_fn_do.m
@@ -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');
diff --git a/matlab/types.m b/matlab/types.m
index cb3bf8f..7263fb2 100644
--- a/matlab/types.m
+++ b/matlab/types.m
@@ -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