forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
printer.st
56 lines (49 loc) · 1.87 KB
/
printer.st
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
Object subclass: Printer [
Printer class >> prStr: sexp printReadably: printReadably [
sexp type = #fn ifTrue: [ ^'#<fn>' ].
sexp type = #func ifTrue: [ ^'#<func>' ].
sexp type = #true ifTrue: [ ^'true' ].
sexp type = #false ifTrue: [ ^'false' ].
sexp type = #nil ifTrue: [ ^'nil' ].
sexp type = #number ifTrue: [ ^sexp value asString ].
sexp type = #symbol ifTrue: [ ^sexp value asString ].
sexp type = #keyword ifTrue: [ ^':', sexp value ].
sexp type = #string ifTrue: [
printReadably ifTrue: [
^sexp value repr
] ifFalse: [
^sexp value
]
].
sexp type = #list ifTrue: [
^self prList: sexp printReadably: printReadably
starter: '(' ender: ')'
].
sexp type = #vector ifTrue: [
^self prList: sexp printReadably: printReadably
starter: '[' ender: ']'
].
sexp type = #map ifTrue: [
^self prMap: sexp printReadably: printReadably
].
sexp type = #atom ifTrue: [
^'(atom ', (self prStr: sexp value printReadably: printReadably), ')'
].
Error halt: 'unimplemented type'
]
Printer class >> prList: sexp printReadably: printReadably
starter: starter ender: ender [
| items |
items := sexp value collect:
[ :item | self prStr: item printReadably: printReadably ].
^starter, (items join: ' ') , ender
]
Printer class >> prMap: sexp printReadably: printReadably [
| items |
items := sexp value associations collect:
[ :item |
(self prStr: item key printReadably: printReadably), ' ',
(self prStr: item value printReadably: printReadably) ].
^'{', (items join: ' '), '}'
]
]