aboutsummaryrefslogtreecommitdiff
path: root/matlab/printer.m
blob: 5200a6ee69577eb48cf1c2bda4991233ee3beaed (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
% this is just being used as a namespace
classdef printer
    methods (Static = true)
        function str = pr_str(obj, print_readably)
            switch class(obj)
            case 'types.Symbol'
                str = obj.name;
            case 'double'
                str = num2str(obj);
            case 'char'
                if types.keyword_Q(obj)
                    str = sprintf(':%s', obj(2:end));
                else
                    if print_readably
                        str = strrep(obj, '\', '\\');
                        str = strrep(str, '"', '\"');
                        str = strrep(str, char(10), '\n');
                        str = sprintf('"%s"', str);
                    else
                        str = obj;
                    end
                end
            case 'types.List'
                strs = cellfun(@(x) printer.pr_str(x, print_readably), ...
                               obj.data, 'UniformOutput', false);
                str = sprintf('(%s)', strjoin(strs, ' '));
            case 'types.Vector'
                strs = cellfun(@(x) printer.pr_str(x, print_readably), ...
                               obj.data, 'UniformOutput', false);
                str = sprintf('[%s]', strjoin(strs, ' '));
            case 'types.HashMap'
                strs = {};
                ks = obj.keys();
                for i=1:length(ks)
                    k = ks{i};
                    strs{end+1} = printer.pr_str(k, print_readably);
                    strs{end+1} = printer.pr_str(obj.get(k), print_readably);
                end
                str = sprintf('{%s}', strjoin(strs, ' '));
            case 'types.Nil'
                str = 'nil';
            case 'logical'
                if eq(obj, true)
                    str = 'true';
                else
                    str = 'false';
                end
            case 'types.Atom'
                str = sprintf('(atom %s)', printer.pr_str(obj.val,true));
            otherwise
                str = '#<unknown>';
            end
        end
    end
end