-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbrainfuck.js
83 lines (82 loc) · 2.4 KB
/
brainfuck.js
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
function check(str) {
var bracket = []
var char = ['+', '-', '>', '<', '[', ']', ',', '.']
for (let index = 0; index < str.length; index++) {
const element = str[index]
if (char.includes(element) == false) {
return false
}
if (element == '[') {
bracket.push(element)
} else if (element == ']') {
if (bracket.length == 0) {
return false
}
bracket.pop()
}
}
if (bracket.length == 0) {
return true
} else {
return false
}
}
function parser(str) {
var memory = []
var output = []
var bracketpos = []
var pos = 0
var index = 0
while (index < str.length) {
// setTimeout(() => {
switch (str[index]) {
case '>':
pos++
break
case '<':
pos--
break
case '+':
if (memory[pos] == undefined) {
memory[pos] = 0
}
memory[pos]++
break
case '-':
if (memory[pos] == undefined) {
memory[pos] = 0
}
memory[pos]--
break
case '.':
output.push(String.fromCharCode(memory[pos]))
break
case ',':
// 咕咕咕
break
case '[':
if (memory[pos] == undefined || memory[pos] == 0) {
memory[pos] = 0
while (index < str.length && str[index] != ']') {
index++
}
} else {
bracketpos.push(index)
}
break
case ']':
if (memory[pos] != 0) {
index = bracketpos[bracketpos.length-1]
} else {
bracketpos.pop()
}
break
default:
break
}
// }, 1)
index++
}
alert(output.join(''))
}
// ++++++++[>++++[>++>+++>+++>+<<<<-]>+>+>->>+[<]<-]>>.>---.+++++++..+++.>>.<-.<.+++.------.--------.>>+.>++.