matlab: fix do/slice, strings. Self-hosting!
[jackhill/mal.git] / matlab / types.m
1 classdef types
2 properties (Constant = true)
3 nil = types.Nil();
4 end
5
6 methods(Static)
7 function ret = equal(a,b)
8 ret = false;
9 ota = class(a); otb = class(b);
10 if ~(strcmp(ota,otb) || ...
11 (types.sequential_Q(a) && types.sequential_Q(b)))
12 return;
13 end
14 switch (ota)
15 case {'types.List', 'types.Vector'}
16 if ~(length(a) == length(b))
17 return;
18 end
19 for i=1:length(a)
20 if ~(types.equal(a.get(i), b.get(i)))
21 return;
22 end
23 end
24 ret = true;
25 case 'char'
26 ret = strcmp(a,b);
27 otherwise
28 ret = a == b;
29 end
30 end
31
32 function ret = sequential_Q(obj)
33 ret = strcmp(class(obj), 'types.List') || ...
34 strcmp(class(obj), 'types.Vector');
35 end
36
37 function ret = list_Q(obj)
38 ret = strcmp(class(obj), 'types.List');
39 end
40 function ret = vector_Q(obj)
41 ret = strcmp(class(obj), 'types.Vector');
42 end
43 function ret = hash_map_Q(obj)
44 ret = strcmp(class(obj), 'types.HashMap');
45 end
46
47 function ret = keyword(str)
48 ret = sprintf('%s%s', native2unicode(hex2dec('029e'),'UTF-8'), ...
49 str(2:end));
50 end
51 function ret = keyword_Q(obj)
52 ret = length(obj) > 1 && ...
53 strcmp(obj(1), native2unicode(hex2dec('029e'),'UTF-8'));
54 end
55 end
56 end
57