-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbf.js
More file actions
191 lines (180 loc) · 4.56 KB
/
Copy pathbf.js
File metadata and controls
191 lines (180 loc) · 4.56 KB
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
function* filter(source) {
for (let char of source) {
if (/[><\.,\+\-\[\]]/.exec(char)) {
yield char;
}
}
}
class Operation {
constructor(type) {
this.type = type;
}
append(op) {
return false;
}
execute({pc, dp, data, inp, out, maxMemory}) {
throw "[BUG] unimplemented operation!";
}
}
class DataOp extends Operation {
constructor() {
super("data");
this.amount = 0;
}
append(op) {
if (op === "+") {
this.amount++;
} else if (op === "-") {
this.amount--;
} else {
return false;
}
return true;
}
execute({pc, dp, data, inp, out, maxMemory}) {
data[dp] = ((data[dp]|0) + this.amount)&255;
}
}
class CellOp extends Operation {
constructor() {
super("cell");
this.amount = 0;
}
append(op) {
if (op === ">") {
this.amount++;
} else if (op === "<") {
this.amount--;
} else {
return false;
}
return true;
}
execute(context) {
let dp = context.dp += this.amount;
if (dp < 0) {
throw "invalid adress: " + dp;
} else if (dp >= context.maxMemory && context.maxMemory !== -1) {
throw "out of memory";
}
}
}
class InputOp extends Operation {
constructor() {
super("IN");
}
execute({pc, dp, data, inp, out, maxMemory}) {
data[dp] = inp();
}
}
class OutputOp extends Operation {
constructor() {
super("OUT");
}
execute({pc, dp, data, inp, out, maxMemory}) {
out(data[dp] |= 0);
}
}
class JumpOp extends Operation {
constructor(target = -1) {
super("jump");
this.target = target;
}
execute(context) {
if (context.pc < this.target === (context.data[context.dp] === 0)) {
context.pc = this.target;
}
}
}
function compress(stream) {
let brackets = [];
let code = [];
let partialOp = null;
for (op of stream) {
if (partialOp) {
if (partialOp.append(op)) {
continue;
} else {
code.push(partialOp);
partialOp = null;
}
}
if (op === "[") {
let jump = new JumpOp();
brackets.push({ op : jump, loc : code.length});
code.push(jump);
}
else if (op === "]") {
if (!brackets.length) {
throw "unmatched closing bracket";
}
let {op : prev, loc : ploc} = brackets.pop();
prev.target = code.length;
let back = new JumpOp(ploc);
code.push(back);
}
else if (op === "+" || op === "-") {
partialOp = new DataOp();
partialOp.append(op)
}
else if (op === ">" || op === "<") {
partialOp = new CellOp();
partialOp.append(op)
}
else if (op === ",") {
code.push(new InputOp());
} else if (op === ".") {
code.push(new OutputOp())
}
}
if (brackets.length) {
throw "unmatched opening brackets"
}
if (partialOp) {
code.push(partialOp);
}
return code
}
function compile(source) {
let tokenstream = filter(source);
let operations = compress(tokenstream);
return operations;
}
function execute(code, input, output, executionSteps, maxMemory) {
let context = {pc : 0, dp : 0, data : [], inp : input, out : output, maxMemory : maxMemory};
for (; context.pc < code.length && (executionSteps > 0 || executionSteps === -1); --executionSteps, context.pc++) {
code[context.pc].execute(context);
}
if (context.pc < code.length) {
throw "used up computation time"
}
}
function runWithText(source, inputText, steps, memoryLimit, outputLimit) {
let code = compile(source);
let inputLoader;
{
let ind = 0;
inputLoader = x => {
if (ind >= inputText.length) {
return 0;
} else {
return inputText.charCodeAt(ind++);
}
}
}
let output = "";
let outputWriter = x => {
if (output.length >= outputLimit) {
throw "out of string output";
} else {
output += String.fromCharCode(x);
}
}
execute(code, inputLoader, outputWriter, steps, memoryLimit);
return output;
}
module.exports = {
compile : compile,
execute : execute,
runWithText : runWithText,
}