Última actividad 2 weeks ago

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

Revisión 9b7a400cc8afa744e9221d26270483df260bc4b8

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