-
Notifications
You must be signed in to change notification settings - Fork 0
/
command.ts
363 lines (305 loc) · 10.2 KB
/
command.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
export {parse};
import { Token, TokenType, TokenIterator } from './tokenise.ts';
import { Joss, Result, Step } from './joss.ts';
import { expect } from './parse_helpers.ts';
import { Expression, VariableExpression, ValueRange } from './expression.ts';
class Command implements Step {
verb: Verb;
ifmodifier: If|null;
constructor(verb: Verb, ifmodifier: If|null = null) {
this.verb = verb;
this.ifmodifier = ifmodifier;
}
eval(joss: Joss): void {
if (!this.ifmodifier || this.ifmodifier.eval(joss)) {
this.verb.eval(joss);
}
}
static parse(tokens: TokenIterator<Token>): Command {
const token = tokens.next();
let verb;
switch (token.type) {
case TokenType.END:
verb = new NoOp();
break;
case TokenType.ID:
switch (token.raw) {
case 'Type':
verb = Type.parse(tokens);
break;
case 'Set':
verb = Set.parse(tokens);
break;
case 'Let':
verb = Let.parse(tokens);
break;
case 'Do':
verb = Do.parse(tokens);
break;
default:
throw new Error(`${token.raw} is not a command`);
}
break;
default:
throw new Error(`Expecting verb to start command, got ${token.raw}`);
}
return new Command(verb, tokens.peek().raw === 'if' ? If.parse(tokens) : null);
}
}
interface StringExpression {
eval(joss: Joss): string;
}
class If {
expression: Expression;
constructor(expression: Expression) {
this.expression = expression;
}
eval(joss: Joss): boolean {
// TODO boolean types...
return Boolean(this.expression.eval(joss, {}));
}
static parse(tokens: TokenIterator<Token>): If {
expect('', tokens.next(), TokenType.ID, 'if');
// TODO distinguish types properly.
return new If(Expression.parse(tokens));
}
}
abstract class Verb {
/**
*
* @param joss
* @returns line location to go to next (or null if not a goto)
*/
abstract eval(joss: Joss): void;
}
class NoOp implements Verb {
eval(_joss: Joss): void {}
}
class Maths implements StringExpression {
expression: Expression;
constructor(expr: Expression) {
this.expression = expr;
}
eval(joss: Joss): string {
return this.expression.eval(joss, {}).toString();
}
static parse(tokens: TokenIterator<Token>): Maths {
return new Maths(Expression.parse(tokens));
}
}
class QuotedString implements StringExpression {
val: string;
constructor(val: string) {
this.val = val;
}
eval(joss: Joss): string {
return this.val;
}
static parse(tokens: TokenIterator<Token>): QuotedString {
const token = tokens.next();
// assert type === TokenType.STR
return new QuotedString(token.raw.slice(1, -1));
}
}
class Set implements Verb {
target: VariableExpression;
expression: Expression;
constructor(target: VariableExpression, expression: Expression) {
this.expression = expression;
this.target = target;
}
static parse(tokens: TokenIterator<Token>): Set {
const var_expression = VariableExpression.parse(tokens);
expect('after set variable', tokens.next(), TokenType.OP, '=');
return new Set(var_expression, Expression.parse(tokens));
}
eval(joss: Joss): void {
this.target.eval_set(joss, this.expression.eval(joss, {}));
}
}
class Let implements Verb {
target: VariableExpression;
argNames: string[];
expression: Expression;
constructor(target: VariableExpression, argNames: string[], expression: Expression) {
this.expression = expression;
this.target = target;
this.argNames = argNames;
}
eval(joss: Joss): void {
this.target.eval_set(joss, (...args: any[]) => {
if (args.length !== this.argNames.length) {
throw new Error('Invalid arity on function call');
}
const fnArgs = args.reduce((o, arg, i) => {
o[this.argNames[i]] = arg;
return o;
}, {});
return this.expression.eval(joss, fnArgs);
});
}
static parse(tokens: TokenIterator<Token>): Set {
const v = tokens.next().raw;
let token = tokens.peek();
const argNames: string[] = [];
if (token.type === TokenType.OPEN_BRACKET) {
tokens.next();
const expectedBracket = token.raw === '[' ? ']' : ')';
while (true) {
token = expect('variable name', tokens.next(), TokenType.VAR);
argNames.push(token.raw);
if (tokens.peek().type === TokenType.CLOSE_BRACKET) {
break;
}
expect('next variable argument', token, TokenType.COMMA);
}
expect('end of function arguments', tokens.next(), TokenType.CLOSE_BRACKET, expectedBracket);
}
expect('after set variable', tokens.next(), TokenType.OP, '=');
return new Let(new VariableExpression(v), argNames, Expression.parse(tokens));
}
}
class Type implements Verb {
expressions: StringExpression[];
constructor(expressions: StringExpression[]) {
this.expressions = expressions;
}
// ? Can't use TokenType here, because then we have to define _all_ token types for the object...
static parseDecision: Record<number, (tokens: TokenIterator<Token>) => StringExpression> = {
[TokenType.VAR]: Maths.parse,
[TokenType.NUM]: Maths.parse,
[TokenType.OP]: Maths.parse,
[TokenType.STR]: QuotedString.parse,
[TokenType.OPEN_BRACKET]: Maths.parse,
};
static parse(tokens: TokenIterator<Token>): Type {
const expressions: StringExpression[] = [];
let token;
do {
token = tokens.peek();
const parseFn = this.parseDecision[token.type];
if (parseFn === undefined) {
throw new Error(`Can\'t type ${token.raw}`)
}
expressions.push(parseFn(tokens));
token = tokens.peek();
if (token.type !== TokenType.COMMA) {
break;
}
token = tokens.next();
} while (true);
return new Type(expressions);
}
eval(joss: Joss): void {
for (const e of this.expressions) {
joss.output(e.eval(joss));
joss.output('\n');
}
}
}
class Do implements Verb {
part: string;
step: string | null;
times: Expression | null;
for: {s: string, range: ValueRange} | null;
constructor(part: string, step: string | null = null) {
this.part = part;
this.step = step;
this.times = null;
this.for = null;
}
// i.e. eval without modifier.
evalDo(joss: Joss): void {
if (this.step) {
joss.getStep(this.part, this.step).eval(joss);
} else {
for (const step of joss.getPartSteps(this.part)) {
step.eval(joss);
}
}
}
eval(joss: Joss): void {
if (this.times) {
for (let i = 0; i < Number(this.times.eval(joss, {})); ++i) {
this.evalDo(joss);
}
} else if (this.for) {
for (const v of this.for.range.eval(joss, {})) {
joss.setVariable(this.for.s, v);
this.evalDo(joss);
}
} else {
this.evalDo(joss);
}
}
static parse(tokens: TokenIterator<Token>): Do {
let token = tokens.next();
switch (token.raw) {
case 'step':
token = tokens.next();
if (!token.raw.includes('.')) {
throw new Error('Invalid step (i.e. must be 1.1, not 1)');
}
break;
case 'part':
token = tokens.next();
if (token.raw.includes('.')) {
throw new Error('Invalid part (i.e. must be 1, not 1.1)');
}
break;
default:
throw new Error('Expecting step or part after Do');
}
const [part, step] = token.raw.split('.');
const doVerb = new Do(part, step || null);
// Add possible modifier.
switch (tokens.peek().raw) {
case 'for':
tokens.next();
token = expect('variable for range', tokens.next(), TokenType.VAR);
expect('= for range', tokens.next(), TokenType.OP, '=');
doVerb.for = {s: token.raw, range: ValueRange.parse(tokens)};
break;
case ',':
tokens.next();
doVerb.times = Expression.parse(tokens);
expect('expecting times after expression following for', tokens.next(), TokenType.ID, 'times');
break;
default:
break;
}
return doVerb;
}
}
class StoredCommand {
part: string;
step: string;
command: Command;
constructor(part: string, step: string, command: Command) {
this.part = part;
this.step = step;
this.command = command;
}
eval(joss: Joss): void {
joss.setStep(this.part, this.step, this.command);
}
static parse(tokens: TokenIterator<Token>): StoredCommand {
const token = tokens.next();
if (!token.raw.includes('.')) {
throw new Error('Line number without step (i.e. must be 1.1, not 1)');
}
const [part, step] = token.raw.split('.');
return new StoredCommand(part, step, Command.parse(tokens));
}
}
function parse(tokens: TokenIterator<Token>): Command|StoredCommand {
if (tokens.peek().type === TokenType.NUM) {
const sc = StoredCommand.parse(tokens);
expect('End of command', tokens.next(), TokenType.PERIOD);
return sc;
} else {
const command = Command.parse(tokens);
expect('End of command', tokens.next(), TokenType.PERIOD);
return command;
}
}