forked from kanaka/mal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.st
208 lines (165 loc) · 4.19 KB
/
types.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
Object subclass: MALObject [
| type value meta |
type [ ^type ]
value [ ^value ]
meta [ ^meta ]
value: aValue [
value := aValue.
]
meta: aMeta [
meta := aMeta.
]
MALObject class >> new: type value: value meta: meta [
| object |
object := super new.
object init: type value: value meta: meta.
^object
]
init: aType value: aValue meta: aMeta [
type := aType.
value := aValue.
meta := aMeta.
]
withMeta: meta [
| object |
object := self deepCopy.
object meta: meta.
^object
]
isPair [
^(self type = #list or: [ self type = #vector ]) and:
[ self value notEmpty ]
]
printOn: stream [
stream nextPutAll: '<';
nextPutAll: self class printString;
nextPutAll: ': ';
nextPutAll: value printString.
meta notNil ifTrue: [
stream nextPutAll: ' | '
nextPutAll: meta printString.
].
stream nextPutAll: '>'.
]
= x [
self type ~= x type ifTrue: [ ^false ].
^self value = x value
]
hash [
^self value hash
]
]
MALObject subclass: MALTrue [
MALTrue class >> new [
^super new: #true value: true meta: nil.
]
]
MALObject subclass: MALFalse [
MALFalse class >> new [
^super new: #false value: false meta: nil.
]
]
MALObject subclass: MALNil [
MALNil class >> new [
^super new: #nil value: nil meta: nil.
]
]
MALObject class extend [
True := MALTrue new.
False := MALFalse new.
Nil := MALNil new.
True [ ^True ]
False [ ^False ]
Nil [ ^Nil ]
]
MALObject subclass: MALNumber [
MALNumber class >> new: value [
^super new: #number value: value meta: nil.
]
]
MALObject subclass: MALString [
MALString class >> new: value [
^super new: #string value: value meta: nil.
]
]
MALObject subclass: MALSymbol [
MALSymbol class >> new: value [
^super new: #symbol value: value meta: nil.
]
]
MALObject subclass: MALKeyword [
MALKeyword class >> new: value [
^super new: #keyword value: value meta: nil.
]
]
MALObject subclass: MALList [
MALList class >> new: value [
^super new: #list value: value meta: nil.
]
= x [
(x type ~= #list and: [ x type ~= #vector ]) ifTrue: [ ^false ].
^self value = x value
]
]
MALObject subclass: MALVector [
MALVector class >> new: value [
^super new: #vector value: value meta: nil.
]
= x [
(x type ~= #vector and: [ x type ~= #list ]) ifTrue: [ ^false ].
^self value = x value
]
]
MALObject subclass: MALMap [
MALMap class >> new: value [
^super new: #map value: value meta: nil.
]
]
MALObject subclass: MALAtom [
MALAtom class >> new: value [
^super new: #atom value: value meta: nil.
]
]
MALObject subclass: Fn [
| fn |
fn [ ^fn ]
Fn class >> new: fn [
| f |
f := super new: #fn value: fn meta: nil.
f init: fn.
^f
]
init: f [
fn := f.
]
]
Error subclass: MALError [
description [ ^'A MAL-related error' ]
isResumable [ ^true ]
data [ ^self messageText ]
]
MALError subclass: MALUnterminatedSequence [
MALUnterminatedSequence class >> new [ ^super new ]
messageText [ ^'expected ''', self basicMessageText, ''', got EOF' ]
]
MALError subclass: MALUnexpectedToken [
MALUnexpectedToken class >> new [ ^super new ]
messageText [ ^'unexpected token: ''', self basicMessageText, '''']
]
MALError subclass: MALEmptyInput [
MALEmptyInput class >> new [ ^super new ]
messageText [ ^'Empty input' ]
]
MALError subclass: MALUnknownSymbol [
MALUnknownSymbol class >> new [ ^super new ]
messageText [ ^'''', self basicMessageText, ''' not found']
]
MALError subclass: MALOutOfBounds [
MALOutOfBounds class >> new [ ^super new ]
messageText [ ^'Out of bounds' ]
]
MALError subclass: MALCustomError [
MALCustomError class >> new [ ^super new ]
messageText [ ^Printer prStr: self basicMessageText printReadably: true ]
data [ ^self basicMessageText ]
]