1 | #Written Sept 30 2009
|
---|
2 | #This program raped the mind of Adam Domurad; It is not completely efficient for sure but I was more concerned with making a working interpreter
|
---|
3 | #You can use as many as 256 nested loops on a 8 bit cell interpreter
|
---|
4 | #NOTE code will be parsed a lot faster without comment characters
|
---|
5 | #[first place 0 at start and place code input, working out if its a comment and correcting [ -> 1, ] -> 2, + -> 3, - -> 4, > -> 5, < -> 6, . -> 7, , -> 8]
|
---|
6 | #[Ascii: + is 43, , is 44, - is 45, . is 46, < is 60, > is 62, [ is 91, ] is 93]
|
---|
7 | #[Data used is 8 + Data used by subprogram * 2 + non-comment instructions in subprogram.]
|
---|
8 | #[It is possible but a good deal more complex to not use double the data the subprogram uses, the data pointer would have to be stored]
|
---|
9 | #[numerically and then shifted through the data area, and then restored.]
|
---|
10 | ->->>>-
|
---|
11 | [
|
---|
12 | , #input char and see if its a valid command
|
---|
13 | #if its minus 1 make it 0 to handle various EOF standards
|
---|
14 | >+<
|
---|
15 | +[->-]
|
---|
16 | >[->]<<
|
---|
17 | #if it is '%' we end the code processing and treat the rest as input
|
---|
18 | >+<
|
---|
19 | --- --- --- --- --- ---
|
---|
20 | --- --- --- --- --- --- - #minus 37#
|
---|
21 | [ +++ +++ +++ +++ +++ +++
|
---|
22 | +++ +++ +++ +++ +++ +++ + > - ]
|
---|
23 | >[->]<<
|
---|
24 | [ #make sure it is not = 0
|
---|
25 | >+++++ +++ #set cell plus 1 to 8
|
---|
26 | [
|
---|
27 | <----- #minus 40 in total
|
---|
28 | >-
|
---|
29 | ] #cell plus 1 is 0
|
---|
30 |
|
---|
31 | <--- #minus 43 in total
|
---|
32 |
|
---|
33 | [ #was it NOT 43?
|
---|
34 | -#minus 44 in total
|
---|
35 | [#was it NOT 44
|
---|
36 | -
|
---|
37 | [#was it NOT 45?
|
---|
38 | -
|
---|
39 | [#was it NOT 46?
|
---|
40 | --- --- --- --- --
|
---|
41 | [#was it NOT 60 ?
|
---|
42 | --
|
---|
43 | [#was it NOT 62?
|
---|
44 | >+++++ ++
|
---|
45 | [
|
---|
46 | <----#minusing 7*4 = 28 total minus of 90
|
---|
47 | >-
|
---|
48 | ]
|
---|
49 | <
|
---|
50 | -
|
---|
51 | [#was it NOT 91?
|
---|
52 | --
|
---|
53 | [#was it NOT 93?
|
---|
54 | [+]-#set cell to minus 1 NOTE: implementation must support decreasing 0
|
---|
55 | > #increment cell pointer so that we can exit the loop
|
---|
56 | ]
|
---|
57 | <+
|
---|
58 | [ # is the cell before us minus 1? If so the value was a comment and this loop is skipped
|
---|
59 | -# here we know we have a 93
|
---|
60 | >++ #set to 2
|
---|
61 | > # go to a 0 cell to exit the loop
|
---|
62 | ]
|
---|
63 | ->#Either way we end up one space past a minus 1 cell
|
---|
64 | ]#END 91 We are either on the current cell plus 1 = 0 or the cell plus 2 = 0; the value before us is minus 1 signalling that we entered this loop
|
---|
65 | <+
|
---|
66 | [ # is the cell before us minus 1? If so skip this loop if not handle 91 case
|
---|
67 | -#here we know we have a 91
|
---|
68 | >+ #set to 1
|
---|
69 | > # go to a 0 cell to exit the loop
|
---|
70 | ]
|
---|
71 | ->
|
---|
72 | ]#END 62 If there was a comment or a set so far the cell behind us is minus 1
|
---|
73 | <+ [ - > +++++ #set to 5# >]
|
---|
74 | ->
|
---|
75 | ]#END 60
|
---|
76 | <+ [- > +++ +++ #set to 6# >]
|
---|
77 | ->
|
---|
78 | ]#END 46
|
---|
79 | <+ [- > +++ +++ + #set to 7# >]
|
---|
80 | ->
|
---|
81 | ]#END 45
|
---|
82 | <+ [- > +++ + #set to 4# >]
|
---|
83 | ->
|
---|
84 | ]#END 44
|
---|
85 | <+ [- > +++ +++ ++ #set to 8#>]
|
---|
86 | ->
|
---|
87 | ]#END 42
|
---|
88 | <+ [- > +++ #set to 3#>]
|
---|
89 | ->
|
---|
90 | ]#END 0
|
---|
91 | +
|
---|
92 | < #minus 1 if not at EOF the last instruction if EOF (value 1 to 8)
|
---|
93 | + [->->]
|
---|
94 | >[<->-]<
|
---|
95 | ] #continue if it was negative one do not continue if it was EOF
|
---|
96 | <+++>>#add a useless plus at the end which makes sure next instruction is never 0 but is not executed
|
---|
97 | #past the code immediately is a 0; then a 0; then a minus 1; then a 0; then the data
|
---|
98 | #before the current instruction is two 0s; when the simulated instruction pointer moves the two 0s are pushed before the next instruction
|
---|
99 | #the second 0 holds a fetched instruction the others are simply reference points
|
---|
100 | >->>-
|
---|
101 |
|
---|
102 |
|
---|
103 | <<<<<[<]>
|
---|
104 | [ #Main loop
|
---|
105 |
|
---|
106 | -[-[-[-[-[-[-[-
|
---|
107 | #8 INPUT CASE
|
---|
108 | <<+++ +++ ++ #Move the current instruction into a 0 that was before it
|
---|
109 | >>>[>]>>>> +[->>+]- #Find the start of the data
|
---|
110 | >, #Read input into the pointedto data
|
---|
111 | < << +[-<<+]- #Find the register area
|
---|
112 | <<<[<]< #Stop at the first 0 in the instruction pointer
|
---|
113 | ]
|
---|
114 |
|
---|
115 | #7 OUTPUT CASE
|
---|
116 | >
|
---|
117 | [
|
---|
118 | <<< +++ +++ + #Move the current instruction into a 0 that was before it
|
---|
119 | >>>[>]>>>> +[->>+]- #Find the start of the data
|
---|
120 | >. #Output the pointedto data
|
---|
121 | < << +[-<<+]- #Find the register area
|
---|
122 | <<<[<] #Stop at the last 0 in the instruction pointer
|
---|
123 | ]
|
---|
124 | <
|
---|
125 | ]
|
---|
126 |
|
---|
127 | #6 DECREMENT POINTER CASE
|
---|
128 | >
|
---|
129 | [
|
---|
130 | <<<+++ +++ #Move the current instruction into a 0 that was before it
|
---|
131 | >>>[>]>>>> +[->>+] #Find the start of the data and delete the marker
|
---|
132 | <<- #Add a marker before the data before us
|
---|
133 | << +[-<<+]- #Find the register area
|
---|
134 | <<<[<] #Stop at the last 0 in the instruction pointer
|
---|
135 | ]
|
---|
136 | <
|
---|
137 | ]
|
---|
138 |
|
---|
139 | #5 INCREMENT POINTER CASE
|
---|
140 | >
|
---|
141 | [
|
---|
142 | <<<+++ ++ #Move the current instruction into a 0 that was before it
|
---|
143 | >>>[>]>>>> +[->>+] #Find the start of the data and delete the marker
|
---|
144 | >>- #Add a marker before the data after us
|
---|
145 | << +[-<<+]- #Find the register area
|
---|
146 | <<<[<] #Stop at the last 0 in the instruction pointer
|
---|
147 | ]
|
---|
148 | <
|
---|
149 | ]
|
---|
150 |
|
---|
151 | #4 DECREMENT CASE
|
---|
152 | >
|
---|
153 | [
|
---|
154 | <<<+++ + #Move the current instruction into the 0 that was before it
|
---|
155 | >>>[>]>>>> +[->>+]- #Find the start of the data
|
---|
156 | >- #Decrement the pointedto data
|
---|
157 | < << +[-<<+]- #Find the register area
|
---|
158 | <<<[<] #Stop at the last 0 in the instruction pointer
|
---|
159 | ]
|
---|
160 | <
|
---|
161 | ]
|
---|
162 |
|
---|
163 | #3 INCREMENT CASE
|
---|
164 | >
|
---|
165 | [
|
---|
166 | <<<+++ #Move the current instruction into the 0 that was before it
|
---|
167 | >>>[>]>>>> +[->>+]- #Find the start of the data
|
---|
168 | >+ #Increment the pointedto data
|
---|
169 | < << +[-<<+]- #Find the register area
|
---|
170 | <<<[<] #Stop at the last 0 in the instruction pointer
|
---|
171 | ]
|
---|
172 | <
|
---|
173 | ]
|
---|
174 |
|
---|
175 | #2 RIGHT BRACKET CASE
|
---|
176 | >
|
---|
177 | [
|
---|
178 | <++ #Repair this instruction in case we will be jumping back
|
---|
179 | [>] >> >> +[->>+]- #Find the start of the data
|
---|
180 | >[ #IF the data is not 0 we must find the matching left bracket and set the instruction one after it as the current instruction
|
---|
181 | < << +[-<<+]- #Go to register area
|
---|
182 | < << [<] #Move to the instruction pointer second 0
|
---|
183 | - #Set to negative 1 since we already have a right bracket; we will now call this the bracket count
|
---|
184 | [
|
---|
185 | #set up: instruction 0 bracketcount
|
---|
186 | << #The instruction before our instruction pointer
|
---|
187 | -[ #If it is NOT a 1
|
---|
188 | >>- #Decrement the bracket pointer
|
---|
189 | <-> #Set the 0 cell to negative 1 in case we have a 0 bracket pointer
|
---|
190 | [<+] #Point to the 0 cell and 0 it if we did not have a 0 bracket pointer
|
---|
191 | ]
|
---|
192 | <+[ #This happens if the bracket pointer was not set to 0
|
---|
193 | ->
|
---|
194 | >[<] #We are now at the 0 cell
|
---|
195 | ]
|
---|
196 | < #We are now back to the instruction
|
---|
197 |
|
---|
198 | -[ #If it is NOT a 2
|
---|
199 | >>+ #Increment the bracket pointer
|
---|
200 | <--> #Set the 0 cell to negative 2 in case we have a 0 bracket pointer
|
---|
201 | [<++] #Point to the 0 cell and 0 it if we did not have a 0 bracket pointer
|
---|
202 | ]
|
---|
203 | <++[ #This happens if the bracket pointer was not set to 0
|
---|
204 | -->
|
---|
205 | >[<] #We are now at the 0 cell
|
---|
206 | ]
|
---|
207 | < #We are now back to the instruction
|
---|
208 | ++ #Restore instruction
|
---|
209 | >> #Move to bracket count
|
---|
210 | [ #Only enter if we have a bracket count != 0 since we do not need to move behind the last right bracket
|
---|
211 | [<+>-] #We will decrement the instruction pointer: first we move the bracket count into our 0
|
---|
212 | <<[>>+<<-] #Transfer the instruction into the prior bracket count cell pointing at the 0 cell
|
---|
213 | ]
|
---|
214 | <[ #Will only be entered if above loop was entered
|
---|
215 | >
|
---|
216 | ]>
|
---|
217 | ]
|
---|
218 | ]
|
---|
219 | <[#If we are still in the current data this will be minus one else it will be the 0 cell and will not enter
|
---|
220 | << +[-<<+]- #Go to register area
|
---|
221 | < << [<] #Move to the instruction pointer second 0
|
---|
222 | <++ #Create the instruction in the first 0
|
---|
223 | >>-- #Destroy the original instruction making it the second 0
|
---|
224 | < #Go to first 0 cell
|
---|
225 | ]
|
---|
226 | > #End at second 0 cell
|
---|
227 | ]
|
---|
228 | <
|
---|
229 | ]
|
---|
230 |
|
---|
231 | >
|
---|
232 | #1 LEFT BRACKET CASE
|
---|
233 | [
|
---|
234 | <<<+>>> #Move the instruction pointer one back
|
---|
235 | [>] >> >> +[->>+]- #Find the start of the data
|
---|
236 | >[ #IF the data is not 0 we go about our business
|
---|
237 | <<<+[-<<+]- #Go to register area
|
---|
238 | < << [<] #Move to the instruction pointer second 0
|
---|
239 | ]
|
---|
240 | <[#If we are still in the current data this will be minus one else it will be the 0 cell and will not enter
|
---|
241 | #Here we need to jump forward
|
---|
242 | << +[-<<+]- #Go to register area
|
---|
243 | < << [<] #Move to the instruction pointer second 0
|
---|
244 | + #Set to 1 since we already have a left bracket; we will now call this the bracket count
|
---|
245 | [
|
---|
246 | #set up: instruction 0 bracketcount
|
---|
247 | > #The instruction after our instruction pointer
|
---|
248 | -[ #If it is NOT a 1
|
---|
249 | <- #Decrement the bracket pointer
|
---|
250 | < #Point to the 0 cell
|
---|
251 | ]
|
---|
252 | <<[>>] #If we are at 0 cell there will always be a non zero 2 away while if we are at instruction we will now be at 0 cell
|
---|
253 | >> #Move again to instruction
|
---|
254 |
|
---|
255 | -[ #If it is NOT a 2
|
---|
256 | <+ #Increment the bracket pointer
|
---|
257 | < #Point to the 0 cell
|
---|
258 | ]
|
---|
259 | <<[>>] #If we are at 0 cell there will always be a non zero 2 away while if we are at instruction we will now be at 0 cell
|
---|
260 | >> #Move again to instruction
|
---|
261 |
|
---|
262 | ++ #Restore instruction
|
---|
263 | < #Move to bracket count
|
---|
264 | [ #Only enter if we have a bracket count != 0 since we do not need to move behind the last right bracket
|
---|
265 | >[<<+>>-] #Transfer the instruction to the 0 cell
|
---|
266 | <[>+<-] #Transfer our bracket count to where the instruction was making this the new 0 cell
|
---|
267 | ]
|
---|
268 | <[ #Will only be entered if above loop was entered
|
---|
269 | >
|
---|
270 | ]>
|
---|
271 | ]
|
---|
272 | ]
|
---|
273 | >[
|
---|
274 | [<<+>>-] #Transfer the instruction to the 0 cell
|
---|
275 | <[>+<-] #Transfer our bracket count to where the instruction was making this the new 0 cell
|
---|
276 | > #Point to new second 0 cell
|
---|
277 | ] #End at second 0 cell
|
---|
278 | ]
|
---|
279 | > #Point to next instruction
|
---|
280 | ]
|
---|