Última atividade 2 weeks ago

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

Revisão 27fff849186fcd2278f5c8e7e6b85dca1bc4c17f

vita.nv Bruto 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|:: parse? :text: "or"| :text: "
54 if
55 drop 1
56 else
57 end
58"
59|:: parse? :text: "not"| :text: "
60 if
61 0
62 else
63 1
64 end
65"
66
67|:: parse? :text: '<'| :parsed: <
68|:: parse? :text: '>'| :parsed: >
69|:: parse? :text: '0'| :parsed: 0
70|:: parse? :text: '1'| :parsed: 1
71|:: parse? :text: '['| :parsed: [
72|:: parse? :text: ']'| :parsed: ]
73|:: parse? :text: '@'| :parsed: @
74|:: parse? :text: $x |
75|:: parse? :parsed: $x| :code: $x
76|:: parse|
77
78|:: push to $x :value: $y| :stacks: $x $y
79|:: push $x to $y| :stacks: $y $x
80
81|:: peek at $x :value: $y?|
82 :: push $y to $x
83|:: peek at $x?|
84 :: pop from $x
85
86|:: pop from $x :stacks: $x $y|
87 :value: $y
88 :: reset stacks
89|:: pop from $x? :stacks: $z $y|
90 :seen: $z $y
91|:: pop from $x :default symbol: $y?|
92 :value: $y
93 :: reset stacks
94|:: pop from $x|
95 :: stop execution :error: pop from an empty or nonexistent stack $x
96
97|:: reset stacks? :seen: $x $y|
98 :stacks: $x $y
99|:: reset stacks|
100
101|:: move $direction :@math: $x|
102 :stack: $x
103|:: move left :stack: 1 :number of stacks: $x?|
104 :stack: $x
105|:: move left? :stack: $x|
106 :@math: subtract $x 1
107|:: move right :stack: $x :number of stacks: $x?|
108 :stack: 1
109|:: move right? :stack: $x|
110 :@math: add $x 1
111|:: move $direction?|
112 :stack: 1
113
114|:: compare and branch :value: 0|
115 :: seek forward :depth:
116|:: compare and branch :value: $x|
117
118|:: seek forward? :code: [|
119 :executed: [
120 :depth:
121|:: seek forward? :code: ] :depth:? :depth:|
122 :executed: ]
123|:: seek forward :code: ] :depth:|
124 :executed: ]
125|:: seek forward? :code: $x|
126 :executed: $x
127|:: seek forward|
128 :: stop execution :error: unbalanced loops
129
130|:: seek backward? :executed: ]|
131 :code: ]
132 :depth:
133|:: seek backward? :executed: [ :depth:? :depth:|
134 :code: [
135|:: seek backward :executed: [ :depth:|
136 :code: [
137|:: seek backward? :executed: $x|
138 :code: $x
139|:: seek backward|
140 :: stop execution :error: unbalanced loops
141
142|:: step :code: <|
143 :executed: <
144 :: move left
145|:: step :code: >|
146 :executed: >
147 :: move right
148|:: step :code: 0 :stack: $x?|
149 :executed: 0
150 :: push 0 to $x
151|:: step :code: 1 :stack: $x?|
152 :executed: 1
153 :: push 1 to $x
154|:: step :code: [ :stack: $x?|
155 :executed: [
156 :: pop from $x
157 :: compare and branch
158|:: step :code: ]?|
159 :: seek backward :depth:
160|:: step :code: @| :halt:
161|:: step :code: $x|
162|:: step| :halt:
163
164|:: run :halt:|
165 :: clean up
166|:: run?|
167 :: step
168
169|:: clean up? :executed: $x|
170|:: clean up? :code: $x|
171|:: clean up|
172