Commit | Line | Data |
---|---|---|
e83d6df7 VS |
1 | public class Printer |
2 | { | |
3 | fun static string pr_str(MalObject m, int print_readably) | |
4 | { | |
5 | m.type => string type; | |
6 | ||
7 | if( type == "true" || type == "false" || type == "nil" ) | |
8 | { | |
9 | return type; | |
10 | } | |
11 | else if( type == "int" ) | |
12 | { | |
13 | return Std.itoa((m$MalInt).value()); | |
14 | } | |
15 | else if( type == "string" ) | |
16 | { | |
17 | (m$MalString).value() => string value; | |
18 | if( print_readably ) | |
19 | { | |
20 | return String.repr(value); | |
21 | } | |
22 | else | |
23 | { | |
24 | return value; | |
25 | } | |
26 | } | |
27 | else if( type == "symbol" ) | |
28 | { | |
29 | return (m$MalSymbol).value(); | |
30 | } | |
31 | else if( type == "keyword" ) | |
32 | { | |
33 | return ":" + (m$MalKeyword).value(); | |
34 | } | |
35 | else if( type == "atom" ) | |
36 | { | |
37 | return "(atom " + pr_str((m$MalAtom).value(), print_readably) + ")"; | |
38 | } | |
80a2a738 VS |
39 | else if( type == "subr" ) |
40 | { | |
41 | return "#<Subr>"; | |
42 | } | |
674e1c56 VS |
43 | else if( type == "func" ) |
44 | { | |
45 | return "#<Func>"; | |
46 | } | |
e83d6df7 VS |
47 | else if( type == "list" ) |
48 | { | |
49 | return pr_list((m$MalList).value(), print_readably, "(", ")"); | |
50 | } | |
51 | else if( type == "vector" ) | |
52 | { | |
53 | return pr_list((m$MalVector).value(), print_readably, "[", "]"); | |
54 | } | |
55 | else if( type == "hashmap" ) | |
56 | { | |
57 | return pr_list((m$MalHashMap).value(), print_readably, "{", "}"); | |
58 | } | |
80a2a738 VS |
59 | else |
60 | { | |
61 | return "Unknown type"; | |
62 | } | |
e83d6df7 VS |
63 | } |
64 | ||
65 | fun static string pr_list(MalObject m[], int print_readably, string start, string end) | |
66 | { | |
67 | string parts[m.size()]; | |
68 | ||
69 | for( 0 => int i; i < m.size(); i++ ) | |
70 | { | |
71 | pr_str(m[i], print_readably) => parts[i]; | |
72 | } | |
73 | ||
74 | return start + String.join(parts, " ") + end; | |
75 | } | |
76 | } |