-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbf2lua.lua
executable file
·60 lines (55 loc) · 1.41 KB
/
bf2lua.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
#!/usr/bin/env lua
indent = ' '
nest = 0
print([[
data = {0}
index = 1
]])
while true do
local line = io.stdin:read('*l')
if not line then break end
for char in line:gmatch(".") do
if char == '>' then
print(indent:rep(nest) .. '-- >')
print(indent:rep(nest) .. 'index = index + 1')
print(indent:rep(nest) .. 'if #data <= index then')
nest = nest + 1
print(indent:rep(nest) .. 'data[#data+1]=0')
nest = nest - 1
print(indent:rep(nest) .. 'end')
print()
elseif char == '<' then
print(indent:rep(nest) .. '-- <')
print(indent:rep(nest) .. 'index = index - 1')
print()
elseif char == '+' then
print(indent:rep(nest) .. '-- +')
print(indent:rep(nest) .. 'data[index] = data[index] + 1')
print()
elseif char == '-' then
print(indent:rep(nest) .. '-- -')
print(indent:rep(nest) .. 'data[index] = data[index] - 1')
print()
elseif char == '.' then
print(indent:rep(nest) .. '-- .')
print(indent:rep(nest) .. 'io.write(string.char(data[index]))')
print()
elseif char == ',' then
print(indent:rep(nest) .. '-- ,')
-- ToDo
print(indent:rep(nest) .. '-- NOT IMPLEMENTED')
print()
elseif char == '[' then
print(indent:rep(nest) .. '-- [')
print(indent:rep(nest) .. 'while data[index] ~= 0 do')
nest = nest + 1
print()
elseif char == ']' then
print(indent:rep(nest) .. '-- ]')
nest = nest - 1
print("end")
print()
end
end
print()
end