SIGN IN SIGN UP
2015-02-07 20:32:06 -06:00
% 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 type_utils.keyword_Q(obj)
str = sprintf(':%s', obj(2:end));
2015-02-08 13:18:08 -06:00
else
if print_readably
str = strrep(obj, '\', '\\');
str = strrep(str, '"', '\"');
str = strrep(str, char(10), '\n');
str = sprintf('"%s"', str);
else
str = obj;
end
2015-02-08 13:18:08 -06:00
end
case 'types.List'
2015-02-07 20:32:06 -06:00
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, ' '));
2015-02-08 01:13:41 -06:00
case 'types.Nil'
str = 'nil';
2015-02-07 20:32:06 -06:00
case 'logical'
if eq(obj, true)
str = 'true';
else
str = 'false';
end
2015-02-10 01:34:56 -06:00
case 'types.Atom'
str = sprintf('(atom %s)', printer.pr_str(obj.val,true));
2015-02-07 20:32:06 -06:00
otherwise
str = '#<unknown>';
end
end
end
end