-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
121 lines (108 loc) · 3.08 KB
/
main.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
const CELLS = 30000;
// deno-fmt-ignore
enum OP {
INC_VAL = 0x2B, // +
DEC_VAL = 0x2D, // -
INC_PTR = 0x3E, // >
DEC_PTR = 0x3C, // <
JMP_FWD = 0x5B, // [
JMP_BWD = 0x5D, // ]
OUTPUT = 0x2E, // .
INPUT = 0x2C, // ,
}
interface Instruction {
op: OP;
matching?: number;
}
function lexer(buf: Uint8Array): Instruction[] {
const program: Instruction[] = [];
const jumps: number[] = [];
for (let i = 0; i < buf.length; ++i) {
switch (buf[i]) {
case OP.INC_VAL:
case OP.DEC_VAL:
case OP.INC_PTR:
case OP.DEC_PTR:
case OP.INPUT:
case OP.OUTPUT:
program.push({ op: buf[i] });
break;
case OP.JMP_FWD:
jumps.push(program.length);
program.push({ op: OP.JMP_FWD });
break;
case OP.JMP_BWD: {
const matching = jumps.pop();
if (matching === undefined) {
throw new SyntaxError("Unmatching '[' command");
}
program[matching].matching = program.length;
program.push({ op: OP.JMP_BWD, matching });
break;
}
}
}
if (jumps.length > 0) {
throw new SyntaxError("Unmatching ']' command");
}
return program;
}
async function execute(program: Instruction[]) {
const data = new Uint8Array(CELLS);
let ptr = 0;
for (let i = 0; i < program.length; ++i) {
switch (program[i].op) {
case OP.INC_VAL:
++data[ptr];
break;
case OP.DEC_VAL:
--data[ptr];
break;
case OP.INC_PTR: {
if (ptr === CELLS) {
throw new Error("Overflowed");
}
++ptr;
break;
}
case OP.DEC_PTR: {
if (ptr < 0) {
throw new Error("Overflowed");
}
--ptr;
break;
}
case OP.JMP_FWD: {
const matching = program[i].matching!;
i = data[ptr] === 0 ? matching : program[matching].matching!;
break;
}
case OP.JMP_BWD: {
const matching = program[i].matching!;
i = data[ptr] !== 0 ? matching : program[matching].matching!;
break;
}
case OP.OUTPUT: {
const out = new Uint8Array(1);
out[0] = data[ptr];
await Deno.stdout.write(out);
break;
}
case OP.INPUT: {
const inp = new Uint8Array(1);
await Deno.stdin.read(inp);
data[ptr] = inp[0];
break;
}
}
}
}
async function main() {
const file = Deno.args[0];
const content = await Deno.readFile(file);
const program = lexer(content);
await execute(program);
}
if (import.meta.main) {
await main();
}