-
Notifications
You must be signed in to change notification settings - Fork 1
/
commands.ts
414 lines (397 loc) · 10.6 KB
/
commands.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
import { ParseHelper } from "./parseHelper";
import { RamState } from "./ram";
/**
* Abstract class that all commands inherit from.
*
* Provides static `matchAndConstruct` method that constructs the appropriate command to your input.
*/
export abstract class RamCommand {
/**
* Finds a command that matches the supplied input and constructs that command.
*
* Commands inheriting from this super class should overwrite this method to match and construct just that specific command.
*
* @param words Sequence of strings and numbers that represent the command.
* @returns Constructed matching command, if any. False otherwise.
*/
public static matchAndConstruct(
words: (string | number)[]
): false | RamCommand {
const match = Object.keys(commands)
.map((key) => {
const command: typeof RamCommand = commands[key];
const match = command.matchAndConstruct(words);
if (match) {
return match;
}
return false;
})
.find((val) => !!val);
return match || false;
}
param: number;
execute(state: RamState): void {
throw new Error("Method not implemented.");
}
}
/**
* Command `dload {value}` that loads supplied value into the accumulator.
*/
class RamDirectLoad extends RamCommand {
constructor(param: number) {
super();
this.param = param;
}
execute(state: RamState) {
state.accumulator = this.param;
}
static matchAndConstruct(words: (string | number)[]): false | RamDirectLoad {
const match = ParseHelper.match(words, ["dload", ParseHelper.int]);
if (match) {
const num = match[0];
if (typeof num === "number") {
return new RamDirectLoad(num);
}
}
return false;
}
}
/**
* Command `load {index}` that loads the value located in the memory at supplied index into the accumulator.
*/
class RamLoad extends RamCommand {
constructor(param: number) {
super();
this.param = param;
}
execute(state: RamState) {
state.accumulator = state.memory[this.param];
}
static matchAndConstruct(words: (string | number)[]): false | RamLoad {
const match = ParseHelper.match(words, ["load", ParseHelper.int]);
if (match) {
const num = match[0];
if (typeof num === "number") {
return new RamLoad(num);
}
}
return false;
}
}
/**
* Command `iload {index}` that loads the value referenced in the memory at supplied index into the accumulator.
* (It loads `memory[memory[index]]`.)
*/
class RamILoad extends RamCommand {
constructor(param: number) {
super();
this.param = param;
}
execute(state: RamState) {
const index = state.memory[this.param];
if (typeof index === "number") {
state.accumulator = state.memory[index];
}
}
static matchAndConstruct(words: (string | number)[]): false | RamILoad {
const match = ParseHelper.match(words, ["iload", ParseHelper.int]);
if (match) {
const num = match[0];
if (typeof num === "number") {
return new RamILoad(num);
}
}
return false;
}
}
/**
* Command `goto {line}` that jumps to supplied line in the program. The next command to be executed will be the one located at supplied line.
*/
class RamGoto extends RamCommand {
constructor(param: number) {
super();
this.param = param;
}
execute(state: RamState) {
state.line = this.param;
}
static matchAndConstruct(words: (string | number)[]): false | RamGoto {
const match = ParseHelper.match(words, ["goto", ParseHelper.int]);
if (match) {
const num = match[0];
if (typeof num === "number") {
return new RamGoto(num);
}
}
return false;
}
}
/**
* Command `if {condition} then {line}` that jumps to supplied line in the program if the accumulator value equals supplied condition value, and does nothing otherwise.
*/
class RamIfThen extends RamCommand {
condition: string | number;
constructor(condition: string | number, param: number) {
super();
this.condition = condition;
this.param = param;
}
execute(state: RamState) {
if (state.accumulator === this.condition) {
state.line = this.param;
}
}
static matchAndConstruct(words: (string | number)[]): false | RamIfThen {
const match =
ParseHelper.match(words, [
"if",
ParseHelper.int,
"then",
ParseHelper.int
]) ||
ParseHelper.match(words, [
"if",
ParseHelper.char,
"then",
ParseHelper.int
]);
if (match) {
const condition = match[0];
const goto = match[1];
if (typeof goto === "number") {
return new RamIfThen(condition, goto);
}
}
return false;
}
}
/**
* Command `store {index}` that stores the accumulator value into the memory at supplied index.
*/
class RamStore extends RamCommand {
constructor(param: number) {
super();
this.param = param;
}
execute(state: RamState) {
state.memory[this.param] = state.accumulator;
}
static matchAndConstruct(words: (string | number)[]): false | RamStore {
const match = ParseHelper.match(words, ["store", ParseHelper.int]);
if (match) {
const num = match[0];
if (typeof num === "number") {
return new RamStore(num);
}
}
return false;
}
}
/**
* Command `istore {index}` that stores the accumulator value into the memory at the position referenced by supplied index.
* (It stores into `memory[memory[index]]`.)
*/
class RamIStore extends RamCommand {
constructor(param: number) {
super();
this.param = param;
}
execute(state: RamState) {
const index = state.memory[this.param];
if (typeof index === "number") {
state.memory[index] = state.accumulator;
}
}
static matchAndConstruct(words: (string | number)[]): false | RamIStore {
const match = ParseHelper.match(words, ["istore", ParseHelper.int]);
if (match) {
const num = match[0];
if (typeof num === "number") {
return new RamIStore(num);
}
}
return false;
}
}
/**
* Command `add {index}` that increases the accumulator value by the value located in memory at supplied index.
*/
class RamAdd extends RamCommand {
constructor(param: number) {
super();
this.param = param;
}
execute(state: RamState) {
const toAdd = state.memory[this.param];
if (typeof toAdd === "number" && typeof state.accumulator === "number") {
state.accumulator += toAdd;
}
}
static matchAndConstruct(words: (string | number)[]): false | RamAdd {
const match = ParseHelper.match(words, ["add", ParseHelper.int]);
if (match) {
const num = match[0];
if (typeof num === "number") {
return new RamAdd(num);
}
}
return false;
}
}
/**
* Command `sub {index}` that decreases the accumulator value by the value located in memory at supplied index.
*
* Note that negative values are not supported. If this command would decrease the accumulator value below 0, it is set to 0 instead.
*/
class RamSub extends RamCommand {
constructor(param: number) {
super();
this.param = param;
}
execute(state: RamState) {
const toAdd = state.memory[this.param];
if (typeof toAdd === "number" && typeof state.accumulator === "number") {
const result = Math.max(0, state.accumulator - toAdd);
state.accumulator = result;
}
}
static matchAndConstruct(words: (string | number)[]): false | RamSub {
const match = ParseHelper.match(words, ["sub", ParseHelper.int]);
if (match) {
const num = match[0];
if (typeof num === "number") {
return new RamSub(num);
}
}
return false;
}
}
/**
* Command `read` that reads the next character of the input into the accumulator.
*/
class RamRead extends RamCommand {
constructor() {
super();
}
execute(state: RamState) {
state.accumulator = state.input[state.inputAt];
state.inputAt++;
}
static matchAndConstruct(words: (string | number)[]): false | RamRead {
const match = ParseHelper.match(words, ["read"]);
if (match) {
return new RamRead();
}
return false;
}
}
/**
* Commnad `write` that appends the accumulator character to the output.
*/
class RamWrite extends RamCommand {
char: string;
constructor(char: string) {
super();
this.char = char;
}
execute(state: RamState) {
state.output += this.char;
}
static matchAndConstruct(words: (string | number)[]): false | RamWrite {
const match = ParseHelper.match(words, ["write", ParseHelper.char]);
if (match) {
const char = match[0];
if (typeof char === "string") {
return new RamWrite(char);
}
}
return false;
}
}
/**
* Command `mul {index}` that multiplies the accumulator value by the value located in memory at supplied index.
*/
class RamMul extends RamCommand {
constructor(param: number) {
super();
this.param = param;
}
execute(state: RamState) {
const toMul = state.memory[this.param];
if (typeof toMul === "number" && typeof state.accumulator === "number") {
state.accumulator *= toMul;
}
}
static matchAndConstruct(words: (string | number)[]): false | RamMul {
const match = ParseHelper.match(words, ["mul", ParseHelper.int]);
if (match) {
const num = match[0];
if (typeof num === "number") {
return new RamMul(num);
}
}
return false;
}
}
/**
* Command `div {index}` that divides the accumulator value by the value located in memory at supplied index.
*
* Note that only integers are supported. If the result of the division doesn't result in a whole number, it is rounded down.
*/
class RamDiv extends RamCommand {
constructor(param: number) {
super();
this.param = param;
}
execute(state: RamState) {
const toDiv = state.memory[this.param];
if (typeof toDiv === "number" && typeof state.accumulator === "number") {
const result = Math.floor(state.accumulator / toDiv);
state.accumulator = result;
}
}
static matchAndConstruct(words: (string | number)[]): false | RamDiv {
const match = ParseHelper.match(words, ["div", ParseHelper.int]);
if (match) {
const num = match[0];
if (typeof num === "number") {
return new RamDiv(num);
}
}
return false;
}
}
/**
* Command `end` that ends the execution of the program.
*/
class RamEnd extends RamCommand {
constructor() {
super();
}
execute(state: RamState) {
state.done = true;
}
static matchAndConstruct(words: (string | number)[]): false | RamEnd {
const match = ParseHelper.match(words, ["end"]);
if (match) {
return new RamEnd();
}
return false;
}
}
export const commands = {
load: RamLoad,
store: RamStore,
dload: RamDirectLoad,
goto: RamGoto,
ifthen: RamIfThen,
add: RamAdd,
sub: RamSub,
read: RamRead,
write: RamWrite,
end: RamEnd,
mul: RamMul,
div: RamDiv,
iload: RamILoad,
istore: RamIStore
};