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" )
|
|
|
|
{
|
2024-08-28 23:12:06 +03:00
|
|
|
return Std.itoa(m.intValue);
|
2016-04-28 10:20:09 +03:00
|
|
|
}
|
|
|
|
else if( type == "string" )
|
|
|
|
{
|
|
|
|
if( print_readably )
|
|
|
|
{
|
2024-08-28 23:12:06 +03:00
|
|
|
return String.repr(m.stringValue);
|
2016-04-28 10:20:09 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2024-08-28 23:12:06 +03:00
|
|
|
return m.stringValue;
|
2016-04-28 10:20:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if( type == "symbol" )
|
|
|
|
{
|
2024-08-28 23:12:06 +03:00
|
|
|
return m.stringValue;
|
2016-04-28 10:20:09 +03:00
|
|
|
}
|
|
|
|
else if( type == "keyword" )
|
|
|
|
{
|
2024-08-28 23:12:06 +03:00
|
|
|
return ":" + m.stringValue;
|
2016-04-28 10:20:09 +03:00
|
|
|
}
|
|
|
|
else if( type == "atom" )
|
|
|
|
{
|
2024-08-28 23:12:06 +03:00
|
|
|
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" )
|
|
|
|
{
|
2024-08-28 23:12:06 +03:00
|
|
|
return pr_list(m.malObjectValues(), print_readably, "(", ")");
|
2016-04-28 10:20:09 +03:00
|
|
|
}
|
|
|
|
else if( type == "vector" )
|
|
|
|
{
|
2024-08-28 23:12:06 +03:00
|
|
|
return pr_list(m.malObjectValues(), print_readably, "[", "]");
|
2016-04-28 10:20:09 +03:00
|
|
|
}
|
|
|
|
else if( type == "hashmap" )
|
|
|
|
{
|
2024-08-28 23:12:06 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|