-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.lua
97 lines (73 loc) · 1.71 KB
/
main.lua
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
-- this is a brainfuck interpreter in luau, a dialect of lua. ( https://github.com/Roblox/luau )
local bf = { }
function bf.New ()
local arr = {
memory = {[0]=0};
pointer = 0;
}
function arr:execute (code: string)
local skip: boolean = false
local characters = string.split(code, "")
local index: number = 0
local print_buffer = ""
local last_lb_index: number = nil
local function store ()
if self.memory[self.pointer] == nil then
self.memory[self.pointer] = 0
end
end
coroutine.wrap(function()
while true do
index += 1
store()
if index > #characters then break end
local character: string = characters[index]
if character == "]" then skip = false end
if skip and not character == "]" then continue end
if character == ">" then
self.pointer += 1
continue
end
if character == "<" then
self.pointer -= 1
continue
end
if character == "+" then
self.memory[self.pointer] += 1
continue
end
if character == "-" then
self.memory[self.pointer] -= 1
continue
end
if character == "." then
print_buffer ..= string.char(self.memory[self.pointer])
continue
end
if character == "[" then
last_lb_index = index
if self.memory[self.pointer] == 0 then
skip = true
continue
end
end
if character == "]" then
if self.memory[self.pointer] == 0 then
continue
end
index = last_lb_index
continue
end
if character == "?" then -- debug tool
print(self.memory[self.pointer])
end
if character == ";" then -- wait
wait()
end
end
print(print_buffer)
end)()
end
return arr
end
return bf