Commit | Line | Data |
---|---|---|
d6624158 JM |
1 | classdef core |
2 | methods(Static) | |
b550d8b7 JM |
3 | function ret = throw(obj) |
4 | ret = types.nil; | |
5 | throw(types.MalException(obj)); | |
6 | end | |
7 | ||
6d12affa JM |
8 | function str = pr_str(varargin) |
9 | strs = cellfun(@(s) printer.pr_str(s,true), varargin, ... | |
10 | 'UniformOutput', false); | |
11 | str = strjoin(strs, ' '); | |
12 | end | |
13 | function str = do_str(varargin) | |
14 | strs = cellfun(@(s) printer.pr_str(s,false), varargin, ... | |
15 | 'UniformOutput', false); | |
16 | str = strjoin(strs, ''); | |
17 | end | |
18 | function ret = prn(varargin) | |
19 | strs = cellfun(@(s) printer.pr_str(s,true), varargin, ... | |
20 | 'UniformOutput', false); | |
21 | fprintf('%s\n', strjoin(strs, ' ')); | |
22 | ret = types.nil; | |
23 | end | |
24 | function ret = println(varargin) | |
25 | strs = cellfun(@(s) printer.pr_str(s,false), varargin, ... | |
26 | 'UniformOutput', false); | |
27 | fprintf('%s\n', strjoin(strs, ' ')); | |
28 | ret = types.nil; | |
29 | end | |
30 | ||
7f567f36 JM |
31 | function ret = time_ms() |
32 | secs = now-repmat(datenum('1970-1-1 00:00:00'),size(now)); | |
33 | ret = floor(secs.*repmat(24*3600.0*1000,size(now))); | |
34 | end | |
35 | ||
c3023f26 JM |
36 | function ret = concat(varargin) |
37 | if nargin == 0 | |
38 | ret = {}; | |
39 | else | |
40 | ret = cat(2,varargin{:}); | |
41 | end | |
42 | end | |
43 | ||
c4033aab JM |
44 | function ret = first(seq) |
45 | if length(seq) < 1 | |
46 | ret = types.nil; | |
47 | else | |
48 | ret = seq{1}; | |
49 | end | |
50 | end | |
51 | ||
52 | function ret = nth(seq, idx) | |
53 | if idx+1 > length(seq) | |
54 | throw(MException('Range:nth', ... | |
55 | 'nth: index out of range')) | |
56 | end | |
57 | ret = seq{idx+1}; | |
58 | end | |
59 | ||
b550d8b7 JM |
60 | function ret = apply(varargin) |
61 | f = varargin{1}; | |
62 | if isa(f, 'types.Function') | |
63 | f = f.fn; | |
64 | end | |
65 | first_args = varargin(2:end-1); | |
66 | rest_args = varargin{end}; | |
67 | args = [first_args rest_args]; | |
68 | ret = f(args{:}); | |
69 | end | |
70 | ||
71 | function ret = map(f, lst) | |
72 | if isa(f, 'types.Function') | |
73 | f = f.fn; | |
74 | end | |
75 | ret = cellfun(@(x) f(x), lst, 'UniformOutput', false); | |
76 | end | |
77 | ||
d6624158 JM |
78 | function n = ns() |
79 | n = containers.Map(); | |
6d12affa | 80 | n('=') = @types.equal; |
b550d8b7 JM |
81 | n('throw') = @core.throw; |
82 | n('nil?') = @(a) isa(a, 'types.Nil'); | |
83 | n('true?') = @(a) isa(a, 'logical') && a == true; | |
84 | n('false?') = @(a) isa(a, 'logical') && a == false; | |
85 | n('symbol') = @(a) types.Symbol(a); | |
86 | n('symbol?') = @(a) isa(a, 'types.Symbol'); | |
6d12affa JM |
87 | |
88 | n('pr-str') = @core.pr_str; | |
89 | n('str') = @core.do_str; | |
90 | n('prn') = @core.prn; | |
91 | n('println') = @core.println; | |
9831bce7 | 92 | n('read-string') = @reader.read_str; |
b550d8b7 | 93 | n('readline') = @(p) input(p, 's'); |
9831bce7 JM |
94 | n('slurp') = @fileread; |
95 | ||
d6624158 JM |
96 | n('<') = @(a,b) a<b; |
97 | n('<=') = @(a,b) a<=b; | |
98 | n('>') = @(a,b) a>b; | |
99 | n('>=') = @(a,b) a>=b; | |
6d12affa JM |
100 | n('+') = @(a,b) a+b; |
101 | n('-') = @(a,b) a-b; | |
102 | n('*') = @(a,b) a*b; | |
103 | n('/') = @(a,b) floor(a/b); | |
7f567f36 | 104 | n('time-ms') = @core.time_ms; |
d6624158 JM |
105 | |
106 | n('list') = @(varargin) varargin; | |
107 | n('list?') = @iscell; | |
c3023f26 JM |
108 | |
109 | n('cons') = @(a,b) [{a}, b]; | |
110 | n('concat') = @core.concat; | |
c4033aab JM |
111 | n('nth') = @core.nth; |
112 | n('first') = @core.first; | |
113 | n('rest') = @(a) a(2:end); | |
d6624158 JM |
114 | n('empty?') = @(a) length(a) == 0; |
115 | n('count') = @(a) length(a); | |
b550d8b7 JM |
116 | n('apply') = @core.apply; |
117 | n('map') = @core.map; | |
d6624158 JM |
118 | end |
119 | end | |
120 | end | |
121 |