-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbf-interp-orig
executable file
·163 lines (149 loc) · 3.48 KB
/
bf-interp-orig
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
#!/usr/local/bin/node
// Usage: node bf
// BF interpreter
'use strict';
var fs = require('fs');
function read_stdinSync() {
var b = new Buffer(1024)
var data = ''
while (true) {
var n = fs.readSync(process.stdin.fd, b, 0, b.length)
if (!n) break
data += b.toString('utf8', 0, n)
}
return data
}
function main() {
let argv = process.argv.slice(2)
if (argv.length < 1) {
throw "bad usage - no program file specified"
}
let progpath = argv[0]
let input = argv[1] || ''
let memsize = parseInt(argv[2]) || 10000
let code
if (progpath === "-") {
code = read_stdinSync()
} else {
code = fs.readFileSync(progpath, "utf8");
}
new BF(memsize, code, input).run()
}
function compute_jumps(program) {
let jumps = Array(program.length).fill(0)
let stack = []
for (let i = 0; i < program.length; ++i) {
if (program[i] == '[') {
stack.push(i)
} else if (program[i] == ']') {
if (stack.length > 0) {
let x = stack.pop()
jumps[i] = x+1
jumps[x] = i+1
} else {
throw "bad [] pairing"
}
}
}
return jumps
}
class BF {
constructor(memsize, program, input) {
this.memsize = memsize
this.memory = Array(memsize).fill(0)
this.program = program + "\0" // a string
this.input = input
this.jumps = compute_jumps(program)
this.dp = 0
this.pc = 0
this.inp = 0
this.halted = 0
}
run() {
while (!this.halted) { this.step() }
}
step() {
let ch = this.program[this.pc]
switch (ch) {
case '!':
{ let msg = ''
while (1) {
let x = this.program[++this.pc]
if (x == '!') break
if (x != '\n') msg += x
}
this.pc++
process.stdout.write("DEBUG " + msg + " at " + this.dp + " = " + this.memory[this.dp]+"\n")
}
break
case '\0':
this.halted = 1
break
case '>':
this.dp++
if (this.dp >= this.memsize) {
throw ("memory fault: " + this.dp)
}
this.pc++
break
case '<':
this.dp--
if (this.dp < 0) {
throw ("memory fault: " + this.dp)
}
this.pc++
break
case '+':
{ let x = ++this.memory[this.dp]
if (x > 255) this.memory[this.dp] = 0
this.pc++
}
break
case '-':
{ let x = --this.memory[this.dp]
if (x < 0) this.memory[this.dp] = 255
this.pc++
}
break
case '[':
if (this.memory[ this.dp ]) {
this.pc++;
} else {
this.pc = this.jumps[ this.pc ];
}
break
case ']':
if (this.memory[ this.dp ]) {
this.pc = this.jumps[ this.pc ];
} else {
this.pc++;
}
break
case '.':
{ let ch = this.memory[this.dp]
process.stdout.write(String.fromCharCode(ch))
// console.log("output: " + ch + " " + String.fromCharCode(ch))
}
this.pc++
break
case ',':
if (this.inp < this.input.length) {
this.memory[ this.dp ] = this.input.charCodeAt(this.inp++)
} else {
this.memory[ this.dp ] = 0
}
this.pc++
break
case '@':
{ let ch = this.memory[this.dp]
console.log("byte at " + this.dp + ": " + ch)
}
this.pc++
break
default:
this.pc++
break
}
}
}
main()