\ bnf 00.11.26 8:25 pm NAB
\ Based on Brad Rodriguez' BNF code.
.( Loading BNF module...) cr
needs core-ext
variable success
create buffer 160 allot
variable bp buffer bp !
: b, ( c -- ) bp @ c! 1 bp +! ;
: ballot ( n -- ) bp +! ;
: <bnf ( -- )
success @ if
2r> bp @ >r >in @ >r 2>r
else 2r> 2drop then ;
: bnf> ( -- )
success @ if
2r> 2r> 2drop 2>r
else 2r> r> >in ! r> bp ! 2>r
then ;
: | ( -- )
success @ if
2r> 2r> 2drop 2drop
else
2r> 2r@ >in ! bp ! 2>r 1 success !
then ;
: bnf: ( -- sys ) : postpone <bnf ;
: nop ( -- ) ;
: ;bnf ( sys -- )
postpone bnf>
\ Compensate for tail-optimization:
postpone nop
postpone ;
; immediate
: @token ( -- c | 0 )
source >in @ tuck
1+ < if 2drop 0 else + c@ then ;
: +token ( bool -- ) if 1 >in +! then ;
: update-pointer ( bool -- )
dup success ! +token ;
: =token ( c -- )
success @ if
@token = update-pointer
else drop then ;
: token ( c "name" -- ) create c,
does> ( addq -- ) c@ =token ;