aboutsummaryrefslogtreecommitdiff
path: root/matlab/core.m
blob: 2f9e1f3b644d22cb12afb536c4ec90646411e7aa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
classdef core
    methods(Static)
        function ret = throw(obj)
            ret = types.nil;
            throw(types.MalException(obj));
        end

        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 ret = time_ms()
            secs = now-repmat(datenum('1970-1-1 00:00:00'),size(now));
            ret = floor(secs.*repmat(24*3600.0*1000,size(now)));
        end

        function new_hm = assoc(hm, varargin)
            new_hm = clone(hm);
            for i=1:2:length(varargin)
                new_hm.set(varargin{i}, varargin{i+1});
            end
        end

        function new_hm = dissoc(hm, varargin)
            new_hm = clone(hm);
            ks = intersect(hm.keys(),varargin);
            remove(new_hm.data, ks);
        end

        function ret = get(hm, key)
            if hm == types.nil
                ret = types.nil;
            else
                if hm.data.isKey(key)
                    ret = hm.data(key);
                else
                    ret = types.nil;
                end
            end
        end

        function ret = keys(hm)
            ks = hm.keys();
            ret = types.List(ks{:});
        end

        function ret = vals(hm)
            vs = hm.values();
            ret = types.List(vs{:});
        end

        function ret = cons(a, seq)
            cella = [{a}, seq.data];
            ret = types.List(cella{:});
        end

        function ret = concat(varargin)
            if nargin == 0
                cella = {};
            else
                cells = cellfun(@(x) x.data, varargin, ...
                                'UniformOutput', false);
                cella = cat(2,cells{:});
            end
            ret = types.List(cella{:});
        end

        function ret = first(seq)
            if length(seq) < 1
                ret = types.nil;
            else
                ret = seq.get(1);
            end
        end

        function ret = rest(seq)
            cella = seq.data(2:end);
            ret = types.List(cella{:});
        end

        function ret = nth(seq, idx)
            if idx+1 > length(seq)
                throw(MException('Range:nth', ...
                                 'nth: index out of range'))
            end
            ret = seq.get(idx+1);
        end

        function ret = apply(varargin)
            f = varargin{1};
            if isa(f, 'types.Function')
                f = f.fn;
            end
            first_args = varargin(2:end-1);
            rest_args = varargin{end}.data;
            args = [first_args rest_args];
            ret = f(args{:});
        end

        function ret = map(f, lst)
            if isa(f, 'types.Function')
                f = f.fn;
            end
            cells = cellfun(@(x) f(x), lst.data, 'UniformOutput', false);
            ret = types.List(cells{:});
        end

        function new_obj = with_meta(obj, meta)
            new_obj = clone(obj);
            new_obj.meta = meta;
        end

        function meta = meta(obj)
            switch class(obj)
            case {'types.List', 'types.Vector',
                  'types.HashMap', 'types.Function'}
                meta = obj.meta;
            otherwise
                meta = types.nil;
            end
        end

        function ret = reset_BANG(atm, val)
            atm.val = val;
            ret = val;
        end

        function ret = swap_BANG(atm, f, varargin)
            args = [{atm.val} varargin];
            if isa(f, 'types.Function')
                f = f.fn;
            end
            atm.val = f(args{:});
            ret = atm.val;
        end

        function n = ns()
            n = containers.Map();
            n('=') =  @types.equal;
            n('throw') = @core.throw;
            n('nil?') = @(a) isa(a, 'types.Nil');
            n('true?') = @(a) isa(a, 'logical') && a == true;
            n('false?') = @(a) isa(a, 'logical') && a == false;
            n('symbol') = @(a) types.Symbol(a);
            n('symbol?') = @(a) isa(a, 'types.Symbol');
            n('keyword') = @types.keyword;
            n('keyword?') = @types.keyword_Q;

            n('pr-str') = @core.pr_str;
            n('str') = @core.do_str;
            n('prn') = @core.prn;
            n('println') = @core.println;
            n('read-string') = @reader.read_str;
            n('readline') = @(p) input(p, 's');
            n('slurp') = @fileread;

            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('time-ms') = @core.time_ms;

            n('list') = @(varargin) types.List(varargin{:});
            n('list?') = @types.list_Q;
            n('vector') = @(varargin) types.Vector(varargin{:});
            n('vector?') = @types.vector_Q;
            n('hash-map') = @(varargin) types.HashMap(varargin{:});
            n('map?') = @types.hash_map_Q;
            n('assoc') = @core.assoc;
            n('dissoc') = @core.dissoc;
            n('get') = @core.get;
            n('contains?') = @(a,b) a.data.isKey(b);
            n('keys') = @core.keys;
            n('vals') = @core.vals;

            n('sequential?') = @types.sequential_Q;
            n('cons') = @core.cons;
            n('concat') = @core.concat;
            n('nth') = @core.nth;
            n('first') = @core.first;
            n('rest') = @core.rest;
            n('empty?') = @(a) length(a) == 0;
            n('count') = @(a) length(a);
            n('apply') = @core.apply;
            n('map') = @core.map;
            n('conj') = @(x) disp('not implemented yet');

            n('with-meta') = @core.with_meta;
            n('meta') = @core.meta;
            n('atom') = @types.Atom;
            n('atom?') = @(a) isa(a, 'types.Atom');
            n('deref') = @(a) a.val;
            n('reset!') = @core.reset_BANG;
            n('swap!') = @core.swap_BANG;
        end
    end
end