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

76 lines
1.8 KiB
Plaintext
Raw Permalink 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.intValue);
2016-04-28 10:20:09 +03:00
}
else if( type == "string" )
{
if( print_readably )
{
return String.repr(m.stringValue);
2016-04-28 10:20:09 +03:00
}
else
{
return m.stringValue;
2016-04-28 10:20:09 +03:00
}
}
else if( type == "symbol" )
{
return m.stringValue;
2016-04-28 10:20:09 +03:00
}
else if( type == "keyword" )
{
return ":" + m.stringValue;
2016-04-28 10:20:09 +03:00
}
else if( type == "atom" )
{
return "(atom " + pr_str(m.malObjectValue(), print_readably) + ")";
2016-04-28 10:20:09 +03:00
}
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.malObjectValues(), print_readably, "(", ")");
2016-04-28 10:20:09 +03:00
}
else if( type == "vector" )
{
return pr_list(m.malObjectValues(), print_readably, "[", "]");
2016-04-28 10:20:09 +03:00
}
else if( type == "hashmap" )
{
return pr_list(m.malObjectValues(), print_readably, "{", "}");
2016-04-28 10:20:09 +03:00
}
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;
}
}