Última actividad 2 weeks ago

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

Revisión 859505eb610774614b36dfbcd3cf2293740e302a

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