matlab: all step4 except optional.
[jackhill/mal.git] / matlab / printer.m
CommitLineData
9a54ea18
JM
1% this is just being used as a namespace
2classdef printer
3 methods (Static = true)
4 function str = pr_str(obj, print_readably)
5 switch class(obj)
6 case 'types.Symbol'
7 str = obj.name;
8 case 'double'
9 str = num2str(obj);
10 case 'char'
6d12affa
JM
11 if print_readably
12 str = strrep(obj, '\', '\\');
13 str = strrep(str, '"', '\"');
14 str = strrep(str, char(10), '\n');
15 str = strcat('"', str, '"');
16 else
17 str = obj;
18 end
9a54ea18
JM
19 case 'cell'
20 strs = cellfun(@(x) printer.pr_str(x, print_readably), ...
21 obj, 'UniformOutput', false);
22 str = strcat('(', strjoin(strs, ' '), ')');
d6624158
JM
23 case 'types.Nil'
24 str = 'nil';
9a54ea18
JM
25 case 'logical'
26 if eq(obj, true)
27 str = 'true';
28 else
29 str = 'false';
30 end
31 otherwise
32 str = '#<unknown>';
33 end
34 end
35 end
36end