-
Notifications
You must be signed in to change notification settings - Fork 0
/
bf_fun.lpp.lua
87 lines (75 loc) · 1.95 KB
/
bf_fun.lpp.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
|>
bf = {}
bf.pointer = 0
function bf.move_ptr_from_to(current, target)
if target > current then
return (">"):rep(target - current)
else
return ("<"):rep(current - target)
end
end
function bf.move_ptr(target)
local mov = bf.move_ptr_from_to(bf.pointer, target)
bf.pointer = target
return mov
end
function bf.inc_by(by)
if by > 0 then
return ("+"):rep(by)
else
return ("-"):rep(-by)
end
end
function bf.dec_by(by)
return bf.inc_by(-by)
end
function bf.move_to(...)
local prev_target = bf.pointer
local increment_cells = {}
for i, target in ipairs({...}) do
increment_cells[i] = bf.move_ptr_from_to(prev_target, target) .. "+"
prev_target = target
end
return table.concat {
"[-",
table.concat(increment_cells),
bf.move_ptr_from_to(prev_target, bf.pointer),
"]",
}
end
--TODO copy to multiple
function bf.copy_to(temporary, target)
local _pointer = bf.pointer
return table.concat {
bf.move_to(target, temporary),
bf.move_ptr(temporary),
bf.move_to(_pointer),
bf.move_ptr(_pointer),
}
end
---reset the cell to zero
bf.set_0 = "[-]"
---prints string (needs to be zero terminated on *both* ends!)
bf.print_zero_terminated = "[.>]<[<]>"
---increment/decrement double-cell integer, requres one free cell to the right
bf.inc_double = "+>+[<->[->+<]]>[-<+>]<<"
bf.dec_double = ">[<+>[->+<]]>[-<+>]<-<-"
function bf.inc_double_by(by)
if by > 0 then
return (bf.inc_double):rep(by)
else
return (bf.dec_double):rep(-by)
end
end
function bf.dec_double_by(by)
return bf.inc_double_by(-by)
end
function bf.add_single_to_double(double_ptr)
return table.concat {
"[",
bf.move_ptr_from_to(pointer, target),
bf.move_ptr_from_to(target, pointer),
"]",
}
end
<|