Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/nested-loops'
Browse files Browse the repository at this point in the history
  • Loading branch information
aartaka committed Jul 1, 2023
2 parents 5d16b35 + 6f0b33b commit e42de47
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 31 deletions.
19 changes: 17 additions & 2 deletions README.bf
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,23 @@
to
[N] [0] [P...] [PN] [0] [0] [F] [T...]
Thus, meta.bf uses a three byte long shifting evaluation window.]]
Thus, meta.bf uses a three byte long shifting evaluation window.]
[2.3 BRACKET SEARCH MEMORY LAYOUT
During the nested brackets traversal (both left- and right-hand),
meta.bf uses all the three bytes to keep search information. The
allocation of these bytes is:
[P...] [0] [B] [F] [T...]
Where:
- P and T are the familiar "processed" and "to be processed"
sectors.
- B is the bracket count. It's set to 1 initially, and increased
with every nested brackets level added.
- F is the case flag used to dispatch the opening bracket, closing
bracket, and everything else.]]
[3 META.BF VERSIONS/FILES:
Expand Down Expand Up @@ -128,6 +144,5 @@
meta.repl.*.bf) minified REPL code.]
[4 TO DO:
- Make it support nested loops.
- Optimize layout and memory even more. Or revert all the
optimizations for the sake of readability.]]
109 changes: 80 additions & 29 deletions meta.in
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,96 @@ interpreter switch/loop
to the opening bracket
tovalue() value cell
[fromvaluecell()
< move to the last instruction
check it for being an opening bracket
----- ----- ----- ----- ----- -----
----- ----- ----- ----- ----- -----
[ restore the value if it is not
+++++ +++++ +++++ +++++ +++++ +++++
+++++ +++++ +++++ +++++ +++++ +++++
[>>>+<<<-] copy it to the sector for further evaluation
subtract 91 from the next value
< ----- ----- ----- ----- -----
----- ----- ----- ----- ----- ----- -----] until opening bracket
restore the bracket in place once hit
+++++ +++++ +++++ +++++ +++++ +++++
+++++ +++++ +++++ +++++ +++++ +++++
<< move to the last instruction
[>>>+<<<-] copy it to be processed
>+ set up the bracket count
[<+ set up the case flag
<[switch:
opening bracket
----- ----- ----- ----- ----- -----
----- ----- ----- ----- ----- -----
[-- closing bracket
[default:
>-< kill flag
restore the character
++ +++++ +++++ +++++ +++++ +++++ +++++
+++++ +++++ +++++ +++++ +++++ +++++
[>>>+<<<-] copy to be processed later
]
>[ case closing bracket:
- kill the flag
>+ increase bracket count
> ++ +++++ +++++ +++++ +++++ +++++ +++++
+++++ +++++ +++++ +++++ +++++ +++++ restore bracket
<<
]<
]
>[ case opening bracket:
- kill the flag
>- decrease the bracket count
restore opening bracket
> +++++ +++++ +++++ +++++ +++++ +++++
+++++ +++++ +++++ +++++ +++++ +++++
<< back to the flag
]<
]
>>[<+>-] move the bracket count
<] and loop while it's positive
we're at [P] [0] [B(0)^] [0] [opening bracket] [T]
>>[<<<+>>>-] copy the opening bracket to move inside it
<<< to the copied bracket
move to the value cell
tovaluecell()
tovalue()[fromvalue()+tovalue()-]] backup the value cell to N and exit
fromvalue()[tovalue()+fromvalue()-]tovalue() copy the value if it was backed up
fromvaluecell()
]>]
< case opening bracket
[- kill the flag
[[-] kill the flag
+ and set the bracket count instead of it
restore 91 (opening bracket)
<< +++++ +++++ +++++ +++++ +++++
+++++ +++++ +++++ +++++ +++++ +++++ +++++
check next instruction for being a closing bracket
>>>> ----- ----- ----- ----- ----- --
----- ----- ----- ----- ----- ----- -----
[restore the value if it is not
+++++ +++++ +++++ +++++ +++++ +++++
+++++ +++++ +++++ +++++ +++++ +++++ ++
[<<<+>>>-] copy it to the processed sector
subtract closing bracket (93 minus 31) from the next value
> -- ----- ----- ----- ----- -----
----- ----- ----- ----- ----- ----- -----]
restore the closing bracket in place to be processed next
+++++ +++++ +++++ +++++ +++++ +++++
+++++ +++++ +++++ +++++ +++++ +++++ ++
<<]>]
>> back to the bracket count
[ matching bracket search
>+ set case flag
>[switch:
opening bracket (91 minus 31)
----- ----- ----- ----- ----- -----
----- ----- ----- ----- ----- -----
[ closing bracket (93 minus 31)
--
[ default:
<-> kill the flag
restore the character
++ +++++ +++++ +++++ +++++ +++++ +++++
+++++ +++++ +++++ +++++ +++++ +++++
[<<<+>>>-] copy the character to the processed sector
]
<[ case closing bracket:
- kill the flag
<- subtract one from the bracket count
restore closing bracket in the processed sector
< ++ +++++ +++++ +++++ +++++ +++++ +++++
+++++ +++++ +++++ +++++ +++++ +++++
>> back to the case flag
]>
]
<[ case opening bracket:
- kill the flag
<+ add one to the bracket count
restore opening bracket in the processed sector
< +++++ +++++ +++++ +++++ +++++
+++++ +++++ +++++ +++++ +++++ +++++ +++++
>> back to the case flag
]>
]
<<[>+<-]> copy the bracket count closer and loop
] matching bracket search ends
we're at [P] [closing bracket] [0] [B(0)^] [0] [T]
<<[>>>+<<<-] copy the closing bracket to be processed next
> back to the new fake case flag
]>]
< case greater than
[- kill the flag
restore the value (62 minus 31 or greater than)
Expand Down

0 comments on commit e42de47

Please sign in to comment.