1
1
mirror of https://github.com/kanaka/mal.git synced 2024-11-09 18:06:35 +03:00
mal/impls/chuck/printer.ck

77 lines
1.9 KiB
Plaintext
Raw Normal View History

2016-04-28 10:20:09 +03:00
public class Printer
{
fun static string pr_str(MalObject m, int print_readably)
{
m.type => string type;
if( type == "true" || type == "false" || type == "nil" )
{
return type;
}
else if( type == "int" )
{
return Std.itoa((m$MalInt).value());
}
else if( type == "string" )
{
(m$MalString).value() => string value;
if( print_readably )
{
return String.repr(value);
}
else
{
return value;
}
}
else if( type == "symbol" )
{
return (m$MalSymbol).value();
}
else if( type == "keyword" )
{
return ":" + (m$MalKeyword).value();
}
else if( type == "atom" )
{
return "(atom " + pr_str((m$MalAtom).value(), print_readably) + ")";
}
2016-04-29 21:40:11 +03:00
else if( type == "subr" )
{
return "#<Subr>";
}
2016-05-12 11:01:09 +03:00
else if( type == "func" )
{
return "#<Func>";
}
2016-04-28 10:20:09 +03:00
else if( type == "list" )
{
return pr_list((m$MalList).value(), print_readably, "(", ")");
}
else if( type == "vector" )
{
return pr_list((m$MalVector).value(), print_readably, "[", "]");
}
else if( type == "hashmap" )
{
return pr_list((m$MalHashMap).value(), print_readably, "{", "}");
}
2016-04-29 21:40:11 +03:00
else
{
return "Unknown type";
}
2016-04-28 10:20:09 +03:00
}
fun static string pr_list(MalObject m[], int print_readably, string start, string end)
{
string parts[m.size()];
for( 0 => int i; i < m.size(); i++ )
{
pr_str(m[i], print_readably) => parts[i];
}
return start + String.join(parts, " ") + end;
}
}