Skip to content

Commit

Permalink
matlab: all step4 except optional.
Browse files Browse the repository at this point in the history
  • Loading branch information
kanaka committed Feb 9, 2015
1 parent d662415 commit 6d12aff
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 6 deletions.
8 changes: 8 additions & 0 deletions matlab/+types/Nil.m
Original file line number Diff line number Diff line change
@@ -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
3 changes: 3 additions & 0 deletions matlab/+types/Symbol.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@
function sym = Symbol(name)
sym.name = name;
end
function ret = eq(a,b)
ret = strcmp(a.name, b.name);
end
end
end
38 changes: 33 additions & 5 deletions matlab/core.m
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
9 changes: 8 additions & 1 deletion matlab/printer.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,14 @@
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);
Expand Down
2 changes: 2 additions & 0 deletions matlab/reader.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
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')
Expand Down
5 changes: 5 additions & 0 deletions matlab/step4_if_fn_do.m
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,17 @@ function step4_if_fn_do(varargin), main(varargin), 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');
Expand Down
26 changes: 26 additions & 0 deletions matlab/types.m
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,31 @@
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

0 comments on commit 6d12aff

Please sign in to comment.