classdef Nil
+ methods
+ function len = length(obj)
+ len = 0;
+ end
+ function ret = eq(a,b)
+ ret = strcmp(class(b),'types.Nil');
+ end
+ end
end
function sym = Symbol(name)
sym.name = name;
end
+ function ret = eq(a,b)
+ ret = strcmp(a.name, b.name);
+ end
end
end
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;
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);
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')
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');
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