-
Notifications
You must be signed in to change notification settings - Fork 3
/
bf-interpreter.spwn
259 lines (232 loc) · 7.28 KB
/
bf-interpreter.spwn
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
RIGHT = 1b
LEFT = 2b
INCR = 3b
DECR = 4b
DOT = 5b
OPEN = 6b //opening bracket
CLOSE = 7b //closing bracket
spacing = 10
insert_bf_script = (str: @string, offset: [@number]) {
let index = 0
for ch in str {
blockid = switch ch {
case ">": RIGHT,//increment the data pointer (to point to the next cell to the right).
case "<": LEFT, //decrement the data pointer (to point to the next cell to the left).
case "+": INCR, //increment (increase by one) the byte at the data pointer.
case "-": DECR, //decrement (decrease by one) the byte at the data pointer.
case ".": DOT //output the byte at the data pointer.
//case ",": //accept one byte of input, storing its value in the byte at the data pointer.
case "[": OPEN,
case "]": CLOSE,
else: null
}
if blockid != null {
extract obj_props
$.add(obj {
OBJ_ID: 1816,
X: offset[0] + index * spacing * 3,
Y: offset[1],
BLOCK_A: blockid,
SCALING: 0.2,
})
$.add(obj {
OBJ_ID: 914,
X: offset[0] + index * spacing * 3,
Y: offset[1] - 30,
TEXT: $.b64encrypt(ch),
COLOR: 1c,
})
index++
}
}
}
type @bytedisplay
impl @bytedisplay {
new: (offset: [@number]) {
1c.set(255, 255, 255)
//order = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[ ]^_`abcdefghijklmnopqrstuvwxyz{|}~"
order = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
list = []
for ch in order {
color = ?c
extract obj_props
$.add(obj {
OBJ_ID: 914,
X: offset[0],
Y: offset[1],
TEXT: $.b64encrypt(ch),
COLOR: color,
})
color.set(0,0,0, blending = true)
list.push(color)
}
return @bytedisplay::{list: list, chars: order.length}
},
display: (self, index: @number) {
for i in ..self.list.length {
if i == index {
-> self.list[index].pulse(0,255,0, fade_out = 0.5)
-> self.list[index].set(255,255,255, 0.5)
} else {
self.list[i].set(0,0,0, blending = true)
}
}
}
}
type @bfreader
layers = counter(0)
//layers.display(150, 300)
impl @bfreader {
new: (script_offset: [@number], cell_count: @number) {
extract obj_props
block = ?b
group = ?g
$.add(obj {
OBJ_ID: 1816,
X: script_offset[0] - spacing * 6,
Y: script_offset[1],
GROUPS: group,
BLOCK_A: block,
SCALING: 0.5,
DYNAMIC_BLOCK: true,
})
for b in [RIGHT,LEFT,INCR,DECR,DOT,OPEN,CLOSE] {
$.add(obj {
OBJ_ID: 1816,
X: script_offset[0] - spacing * 3,
Y: script_offset[1],
SCALING: 0.5,
BLOCK_A: b,
})
}
// CREATE CELLS
let cells = []
for i in ..cell_count {
c = counter()
cells.push(c)
c.display(script_offset[0] + i * 45, script_offset[1] + 90)
}
ptr = counter(0)
ptr.display(script_offset[0], script_offset[1] + 60)
std_out = @bytedisplay::new([script_offset[0], script_offset[1] + 120])
out = @bfreader::{
group: group,
right: counter(block.create_tracker_item(RIGHT)),
left: counter(block.create_tracker_item(LEFT)),
incr: counter(block.create_tracker_item(INCR)),
dot: counter(block.create_tracker_item(DOT)),
decr: counter(block.create_tracker_item(DECR)),
open: counter(block.create_tracker_item(OPEN)),
close: counter(block.create_tracker_item(CLOSE)),
std_out: std_out,
ptr: ptr,
cells: cells,
}
group.move(spacing * 2, 0, 0.3)
return out
},
currently_on: (self) {
if self.right == 1 {
return RIGHT
} else if self.left == 1 {
return LEFT
} else if self.incr == 1 {
return INCR
} else if self.decr == 1 {
return DECR
} else if self.dot == 1 {
return DOT
} else if self.open == 1 {
return OPEN
} else if self.close == 1 {
return CLOSE
}
},
current_cell: (self) => self.cells[self.ptr.to_const(..self.cells.length)],
interpret: (self) {
ret = !{
self.group.move(spacing, 0, 0)
-> return
}
current = self.currently_on()
if current == RIGHT {
self.ptr += 1
ret!
}
else if current == LEFT {
self.ptr -= 1
ret!
}
else if current == INCR {
-> self.current_cell() += 1
ret!
}
else if current == DECR {
-> self.current_cell() -= 1
ret!
}
else if current == DOT {
std_out = counter(0)
() {
std_out.reset(5)
cell = self.current_cell()
wait()
cell.copy_to([std_out], speed = 6)
} ()
self.std_out.display(std_out.to_const(..self.std_out.chars))
ret!
}
else if current == OPEN {
move_to = !{
condition = () => !(layers == 0 && self.close == 1)
while_loop(condition, delay = 0.03, (){
wait(0.02)
-> if self.open == 1{
layers += 1
}
-> if self.close == 1 {
layers -= 1
}
wait(0.01)
self.group.move(spacing, 0)
})
ret!
}
if self.current_cell() == 0 {
layers -= 1
call_with_delay(0.05, move_to)
} else {
ret!
}
}
else if current == CLOSE {
move_back = !{
condition = ()=> !(layers == 0 && self.open == 1)
while_loop(condition, delay = 0.03, (){
wait(0.02)
-> if self.close == 1 {
layers += 1
}
-> if self.open == 1 {
layers -= 1
}
wait(0.01)
self.group.move(-spacing, 0)
})
ret!
}
if self.current_cell() != 0 {
layers -= 1
call_with_delay(0.05, move_back)
} else {
ret!
}
}
}
}
offset = [300, 300]
insert_bf_script("><+-[].", offset)
reader = @bfreader::new(offset, 20)
while_loop(()=>true, (){
reader.interpret()
})