-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalues.ts
292 lines (241 loc) · 5.6 KB
/
values.ts
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
type Value
= StringValue
| IntValue
| BoolValue
| DoubleValue
| Quote
| VStack
| Scope
;
type ValueTypeTags = {
'string': StringValue,
'int': IntValue,
'boolean': BoolValue,
'double': DoubleValue,
'quote': Quote,
'stack': VStack,
'scope': Scope
};
function typecheck<T extends Value['type']>(v: Value, typeTag: T): ValueTypeTags[T] {
return (v.type === typeTag || ((v.type as string) === 'js_object' && typeTag === 'scope'))
? v as ValueTypeTags[T]
: _ERROR.wrongType(v.type, typeTag);
}
class StringValue {
public readonly type: 'string' = 'string';
public constructor(public readonly v: string) {}
public repr(): string {
let singleQuotes: number = 0;
let doubleQuotes: number = 0;
for(let i: number = 0; i < this.v.length; ++i) {
switch(this.v[i]) {
case '"':
++doubleQuotes;
break;
case "'":
++singleQuotes;
break;
}
}
let d: string = (singleQuotes <= doubleQuotes ? "'" : '"');
let sb: Array<string> = [d];
for(let i: number = 0; i < this.v.length; ++i) {
let c = this.v[i];
switch(c) {
case '\n':
sb.push('\\n');
break
case '\r':
sb.push("\\r");
break;
case '\t':
sb.push("\\t");
break;
case '\b':
sb.push("\\b");
break;
case '\f':
sb.push("\\f");
break;
case '\\':
sb.push("\\\\");
break;
case '"':
case "'":
if(c === d) {
sb.push("\\");
}
sb.push(c);
break;
default:
let cc: number = c.charCodeAt(i);
if(cc < 0x20 || cc > 0x7F) {
sb.push('\\u', ('0000' + cc.toString(16)).substr(-4));
} else {
sb.push(c);
}
}
}
sb.push(d);
return sb.join('');
}
public toString(): string {
return this.v;
}
}
class IntValue {
public readonly type: 'int' = 'int';
public readonly v: number;
public constructor(v: number) { this.v = v|0; }
public repr(): string {
return this.v.toString();
}
public toString(): string {
return this.v.toString();
}
}
class BoolValue {
public readonly type: 'boolean' = 'boolean';
public static readonly TRUE: BoolValue = new BoolValue(true);
public static readonly FALSE: BoolValue = new BoolValue(false);
private constructor(public readonly v: boolean) {}
public repr(): string {
return this.v ? 'true' : 'false';
}
public toString(): string {
return this.v ? 'true' : 'false';
}
}
class DoubleValue {
public readonly type: 'double' = 'double';
public constructor(readonly v: number) {}
public repr(): string {
return this.v.toString();
}
public toString(): string {
return this.v.toString();
}
}
class Quote {
public readonly type: 'quote' = 'quote';
readonly ops: Array<Op> = [];
private isStringing: boolean = false;
public repr(): string {
if(this.isStringing) {
return "(...)";
} else {
this.isStringing = true;
let sb: Array<string> = ['('];
let glue: string = '';
for(let i: number = 0; i < this.ops.length; ++i) {
sb.push(glue, this.ops[i].repr());
glue = ' ';
}
sb.push(')');
this.isStringing = false;
return sb.join('');
}
}
public toString(): string {
return this.repr();
}
}
class VStack {
public readonly type: 'stack' = 'stack';
readonly v: Array<Value> = [];
public push(value: Value): void {
this.v.push(value);
}
public peek<T extends Value['type']>(typeTag: T): ValueTypeTags[T] {
return typecheck(this.v.peek(), typeTag);
}
public pop<T extends Value['type']>(typeTag: T): ValueTypeTags[T] {
return typecheck(this.popAny(), typeTag);
}
public peekAny(): Value {
return this.v.peek();
}
public popAny(): Value {
return this.v.pop() as Value || _ERROR.emptyStack();
}
public getValue(index: number): Value {
if(index < 0 || index >= this.v.length) {
_ERROR.indexOutOfBounds(index, this.v.length);
}
return this.v[index];
}
public length(): number {
return this.v.length;
}
private isStringing: boolean = false;
public repr(): string {
if(this.isStringing) {
return '[...].';
} else {
this.isStringing = true;
let sb: Array<string> = ['['];
let glue: string = '';
for(let i: number = 0; i < this.v.length; ++i) {
sb.push(glue, this.v[i].repr());
glue = ' ';
}
sb.push('].');
this.isStringing = false;
return sb.join('');
}
}
public toString(): string {
return this.repr();
}
}
class Scope {
public readonly type: 'scope' = 'scope';
private readonly v: { [k: string]: PushOp|QuotedOp } = Object.create(null);
public store(name: string, op: PushOp|QuotedOp): void {
this.v[name] = op;
}
public load(name: string): PushOp|QuotedOp|null {
if(Object.prototype.hasOwnProperty.call(this.v, name)) {
return this.v[name];
} else {
return null;
}
}
public doAssignment(op: AssignOp, val: Value): void {
if(op.doNow) {
if(!(val instanceof Quote)) { throw new Error('Illegal state: store immediate must be quote'); }
this.store(op.name, new QuotedOp(val));
} else {
this.store(op.name, new PushOp(val));
}
}
isStringing: boolean = false;
public repr(): string {
if(this.isStringing) {
return '{...}.';
} else {
this.isStringing = true;
let sb: Array<string> = ['{'];
let glue: string = '';
for(let k in this.v) {
if(Object.prototype.hasOwnProperty.call(this.v, k)) {
sb.push(glue);
glue = ' ';
let op: Op = this.v[k];
if(op instanceof PushOp) {
sb.push(op.v.repr(), '>');
} else {
sb.push(op.q.repr(), '>!');
}
sb.push(k);
}
}
sb.push('}.');
this.isStringing = false;
return sb.join('');
}
}
public toString(): string {
return this.repr();
}
}