1
1
mirror of https://github.com/kanaka/mal.git synced 2024-08-18 02:00:40 +03:00
mal/impls/chuck/printer.ck
Joel Martin 8a19f60386 Move implementations into impls/ dir
- Reorder README to have implementation list after "learning tool"
  bullet.

- This also moves tests/ and libs/ into impls. It would be preferrable
  to have these directories at the top level.  However, this causes
  difficulties with the wasm implementations which need pre-open
  directories and have trouble with paths starting with "../../". So
  in lieu of that, symlink those directories to the top-level.

- Move the run_argv_test.sh script into the tests directory for
  general hygiene.
2020-02-10 23:50:16 -06:00

77 lines
1.9 KiB
Plaintext

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) + ")";
}
else if( type == "subr" )
{
return "#<Subr>";
}
else if( type == "func" )
{
return "#<Func>";
}
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, "{", "}");
}
else
{
return "Unknown type";
}
}
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;
}
}