Dernière activité 2 weeks ago

A sketch of a small virtual machine that operates on multiple stacks of bits.

vita.nv Brut Playground
1||:: dispatch
2
3|:: dispatch| :: evaluate :text: "
4 initial state
5 01 11 11 11
6 rules
7 rule
8 and if
9 1 match
10 else
11 0 fail
12 end
13 end rule
14 end rules
15"
16
17|:: parse? :text: "rules" | :text: "<1[1"
18|:: parse? :text: "end rules" | :text: "not]"
19|:: parse? :text: "rule" | :text: "[>"
20|:: parse? :text: "end rule" | :text: "<0]"
21|:: parse? :text: "initial state"| :text: '>'
22|:: parse? :text: "match" | :text: "<0>"
23|:: parse? :text: "fail" | :text: "<1>"
24
25|:: select| :: evaluate :text: "
26 1111111 >0<[>1<]0>[0][>0<[>1<]1>] <[<]
27"
28
29|:: equality| :: evaluate :text: "
30 111> 111< unary equal
31"
32
33|:: seek| :: evaluate :text: "
34 01111 seek right
35 011 seek left
36"
37
38|:: constant equality| :: evaluate :text: "
39 1001 if
40 if
41 01
42 >0<
43 else
44 if
45 001
46 >0<
47 else
48 if
49 1001
50 >1<
51 else
52 0001
53 >0<
54 end
55 end
56 end
57 else
58 0
59 >0<
60 end
61"
62
63
64
65
66|############################################|
67|# Here be dragons of the friendliest kind. #|
68|############################################|
69
70
71|:step: :@math: $x|
72 :steps: $x
73|:step:? :steps: $x|
74 :@math: add $x 1
75|:step:|
76 :steps: 1
77
78|:: evaluate|
79 :: parse
80 :: run :stack: 1
81 :number of stacks: 10
82
83|:: parse? :text: "halt" | :text: '@'
84|:: parse? :text: "begin" | :text: '['
85|:: parse? :text: "loop" | :text: ']'
86|:: parse? :text: "end" | :text: ">0]<"
87|:: parse? :text: "if" | :text: ">1<[>[0]<"
88|:: parse? :text: "else" | :text: ">0<0]>[<"
89
90|:: parse? :text: "drop" | :text: "[0]"
91|:: parse? :text: "dup" | :text: "
92 if
93 1 1
94 else
95 0 0
96 end
97"
98|:: parse? :text: "swap"| :text: "
99 if
100 if
101 1 1
102 else
103 1 0
104 end
105 else
106 if
107 0 1
108 else
109 0 0
110 end
111 end
112"
113|:: parse? :text: "and"| :text: "
114 if
115 else
116 drop 0
117 end
118"
119
120|:: parse? :text: "or"| :text: "
121 if
122 drop 1
123 else
124 end
125"
126
127|:: parse? :text: "not"| :text: "
128 if
129 0
130 else
131 1
132 end
133"
134
135|:: parse? :text: "move unary value left"| :text: "
136 <0> begin
137 <1>
138 loop
139"
140
141|:: parse? :text: "move unary value right"| :text: "
142 >0< begin
143 >1<
144 loop
145"
146
147|:: parse? :text: "seek left"| :text: "[move unary value left<]"
148|:: parse? :text: "seek right"| :text: "[move unary value right>]"
149
150|:: parse? :text: "unary equal"| :text: "
151 >>1<<1[
152 if
153 > if
154 <1
155 else
156 <[]>> drop 0<<0
157 end
158 else
159 > if
160 []> drop 0<<0
161 else
162 <0
163 end
164 end
165 ]
166 >> if
167 << 1
168 else
169 << 0
170 end
171"
172
173|:: parse? :text: '<'| :parsed: <
174|:: parse? :text: '>'| :parsed: >
175|:: parse? :text: '0'| :parsed: 0
176|:: parse? :text: '1'| :parsed: 1
177|:: parse? :text: '['| :parsed: [
178|:: parse? :text: ']'| :parsed: ]
179|:: parse? :text: '@'| :parsed: @
180|:: parse? :text: $x |
181|:: parse? :parsed: $x| :code: $x
182|:: parse|
183
184|:: push to $x :value: $y| :stacks: $x $y
185|:: push $x to $y| :stacks: $y $x
186
187|:: peek at $x :value: $y?|
188 :: push $y to $x
189|:: peek at $x?|
190 :: pop from $x
191
192|:: pop from $x :stacks: $x $y|
193 :value: $y
194 :: reset stacks
195|:: pop from $x? :stacks: $z $y|
196 :seen: $z $y
197|:: pop from $x :default symbol: $y?|
198 :value: $y
199 :: reset stacks
200|:: pop from $x|
201 :value: 0
202 :: reset stacks
203
204|:: reset stacks? :seen: $x $y|
205 :stacks: $x $y
206|:: reset stacks|
207
208|:: move $direction :@math: $x|
209 :stack: $x
210|:: move left :stack: 1 :number of stacks: $x?|
211 :stack: $x
212|:: move left? :stack: $x|
213 :@math: subtract $x 1
214|:: move right :stack: $x :number of stacks: $x?|
215 :stack: 1
216|:: move right? :stack: $x|
217 :@math: add $x 1
218|:: move $direction?|
219 :stack: 1
220
221|:: compare and branch :value: 0|
222 :: seek forward :depth:
223|:: compare and branch :value: $x|
224
225|:: seek forward? :code: [|
226 :executed: [
227 :depth:
228|:: seek forward? :code: ] :depth:? :depth:|
229 :executed: ]
230|:: seek forward :code: ] :depth:|
231 :executed: ]
232|:: seek forward? :code: $x|
233 :executed: $x
234|:: seek forward|
235 :: stop execution :error: unbalanced loops
236
237|:: seek backward? :executed: ]|
238 :code: ]
239 :depth:
240|:: seek backward? :executed: [ :depth:? :depth:|
241 :code: [
242|:: seek backward :executed: [ :depth:|
243 :code: [
244|:: seek backward? :executed: $x|
245 :code: $x
246|:: seek backward|
247 :: stop execution :error: unbalanced loops
248
249|:: step :code: <|
250 :executed: <
251 :: move left
252|:: step :code: >|
253 :executed: >
254 :: move right
255|:: step :code: 0 :stack: $x?|
256 :executed: 0
257 :: push 0 to $x
258|:: step :code: 1 :stack: $x?|
259 :executed: 1
260 :: push 1 to $x
261|:: step :code: [ :stack: $x?|
262 :executed: [
263 :: pop from $x
264 :: compare and branch
265|:: step :code: ]?|
266 :: seek backward :depth:
267|:: step :code: @| :halt:
268|:: step :code: $x|
269|:: step| :halt:
270
271|:: run :halt:|
272 :: clean up
273|:: run?|
274 :: step :step:
275
276|:: clean up? :executed: $x|
277|:: clean up? :code: $x|
278|:: clean up|
279