diff --git a/README.bf b/README.bf index b4903a8..c005fe3 100644 --- a/README.bf +++ b/README.bf @@ -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: @@ -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.]] diff --git a/meta.in b/meta.in index b0a4101..1d9c4ca 100644 --- a/meta.in +++ b/meta.in @@ -36,20 +36,44 @@ 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 @@ -57,24 +81,51 @@ interpreter switch/loop 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)