Ostatnio aktywny 2 weeks ago

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

Rewizja f5ac4cccc13a63113b0242203e94cbdfac1735c6

vita.nv Surowy Playground
1||:: evaluate :text: "
2 1 0 swap dup and not drop
3"
4
5
6
7
8|############################################|
9|# Here be dragons of the friendliest kind. #|
10|############################################|
11
12|:: evaluate|
13 :: parse
14 :: run :stack: 1
15 :number of stacks: 10
16
17|:: parse? :text: "halt" | :text: '@'
18|:: parse? :text: "begin" | :text: '['
19|:: parse? :text: "loop" | :text: ']'
20|:: parse? :text: "end" | :text: ">0]<"
21|:: parse? :text: "if" | :text: ">1<[>[0]<"
22|:: parse? :text: "else" | :text: ">0<0]>[<"
23
24|:: parse? :text: "drop" | :text: "[0]"
25|:: parse? :text: "dup" | :text: "
26 if
27 1 1
28 else
29 0 0
30 end
31"
32|:: parse? :text: "swap"| :text: "
33 if
34 if
35 1 1
36 else
37 1 0
38 end
39 else
40 if
41 0 1
42 else
43 0 0
44 end
45 end
46"
47|:: parse? :text: "and"| :text: "
48 if
49 else
50 drop 0
51 end
52"
53
54|:: parse? :text: "or"| :text: "
55 if
56 drop 1
57 else
58 end
59"
60
61|:: parse? :text: "not"| :text: "
62 if
63 0
64 else
65 1
66 end
67"
68
69|:: parse? :text: '<'| :parsed: <
70|:: parse? :text: '>'| :parsed: >
71|:: parse? :text: '0'| :parsed: 0
72|:: parse? :text: '1'| :parsed: 1
73|:: parse? :text: '['| :parsed: [
74|:: parse? :text: ']'| :parsed: ]
75|:: parse? :text: '@'| :parsed: @
76|:: parse? :text: $x |
77|:: parse? :parsed: $x| :code: $x
78|:: parse|
79
80|:: push to $x :value: $y| :stacks: $x $y
81|:: push $x to $y| :stacks: $y $x
82
83|:: peek at $x :value: $y?|
84 :: push $y to $x
85|:: peek at $x?|
86 :: pop from $x
87
88|:: pop from $x :stacks: $x $y|
89 :value: $y
90 :: reset stacks
91|:: pop from $x? :stacks: $z $y|
92 :seen: $z $y
93|:: pop from $x :default symbol: $y?|
94 :value: $y
95 :: reset stacks
96|:: pop from $x|
97 :: stop execution :error: pop from an empty or nonexistent stack $x
98
99|:: reset stacks? :seen: $x $y|
100 :stacks: $x $y
101|:: reset stacks|
102
103|:: move $direction :@math: $x|
104 :stack: $x
105|:: move left :stack: 1 :number of stacks: $x?|
106 :stack: $x
107|:: move left? :stack: $x|
108 :@math: subtract $x 1
109|:: move right :stack: $x :number of stacks: $x?|
110 :stack: 1
111|:: move right? :stack: $x|
112 :@math: add $x 1
113|:: move $direction?|
114 :stack: 1
115
116|:: compare and branch :value: 0|
117 :: seek forward :depth:
118|:: compare and branch :value: $x|
119
120|:: seek forward? :code: [|
121 :executed: [
122 :depth:
123|:: seek forward? :code: ] :depth:? :depth:|
124 :executed: ]
125|:: seek forward :code: ] :depth:|
126 :executed: ]
127|:: seek forward? :code: $x|
128 :executed: $x
129|:: seek forward|
130 :: stop execution :error: unbalanced loops
131
132|:: seek backward? :executed: ]|
133 :code: ]
134 :depth:
135|:: seek backward? :executed: [ :depth:? :depth:|
136 :code: [
137|:: seek backward :executed: [ :depth:|
138 :code: [
139|:: seek backward? :executed: $x|
140 :code: $x
141|:: seek backward|
142 :: stop execution :error: unbalanced loops
143
144|:: step :code: <|
145 :executed: <
146 :: move left
147|:: step :code: >|
148 :executed: >
149 :: move right
150|:: step :code: 0 :stack: $x?|
151 :executed: 0
152 :: push 0 to $x
153|:: step :code: 1 :stack: $x?|
154 :executed: 1
155 :: push 1 to $x
156|:: step :code: [ :stack: $x?|
157 :executed: [
158 :: pop from $x
159 :: compare and branch
160|:: step :code: ]?|
161 :: seek backward :depth:
162|:: step :code: @| :halt:
163|:: step :code: $x|
164|:: step| :halt:
165
166|:: run :halt:|
167 :: clean up
168|:: run?|
169 :: step
170
171|:: clean up? :executed: $x|
172|:: clean up? :code: $x|
173|:: clean up|
174