Annotation of gforth/kernel/recognizer.fs, revision 1.21

1.1       pazsan      1: \ recognizer-based interpreter                       05oct2011py
                      2: 
1.20      pazsan      3: \ Copyright (C) 2012 Free Software Foundation, Inc.
                      4: 
                      5: \ This file is part of Gforth.
                      6: 
                      7: \ Gforth is free software; you can redistribute it and/or
                      8: \ modify it under the terms of the GNU General Public License
                      9: \ as published by the Free Software Foundation, either version 3
                     10: \ of the License, or (at your option) any later version.
                     11: 
                     12: \ This program is distributed in the hope that it will be useful,
                     13: \ but WITHOUT ANY WARRANTY; without even the implied warranty of
                     14: \ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     15: \ GNU General Public License for more details.
                     16: 
                     17: \ You should have received a copy of the GNU General Public License
                     18: \ along with this program. If not, see http://www.gnu.org/licenses/.
                     19: 
1.1       pazsan     20: \ Recognizer are words that take a string and try to figure out
                     21: \ what to do with it.  I want to separate the parse action from
                     22: \ the interpret/compile/postpone action, so that recognizers
                     23: \ are more general than just be used for the interpreter.
                     24: 
                     25: \ The "design pattern" used here is the *factory*, even though
                     26: \ the recognizer does not return a full-blown object.
                     27: \ A recognizer has the stack effect
1.21    ! pazsan     28: \ ( addr u -- token table | addr u r:fail )
1.1       pazsan     29: \ where the token is the result of the parsing action (can be more than
                     30: \ one stack or live on other stacks, e.g. on the FP stack)
1.21    ! pazsan     31: \ and the table contains three actions (as array of three xts):
        !            32: \ interpret it, compile it, compile it as literal.
1.1       pazsan     33: 
1.7       pazsan     34: : recognizer: ( xt1 xt2 xt3 -- ) Create rot , swap , , ;
1.1       pazsan     35: 
1.7       pazsan     36: (field) r>int      ( r-addr -- addr )  0 cells ,
1.13      pazsan     37: (field) r>comp     ( r-addr -- addr )  1 cells ,
                     38: (field) r>lit      ( r-addr -- addr )  2 cells ,
1.1       pazsan     39: 
1.14      pazsan     40: ' no.extensions dup dup Create r:fail A, A, A,
                     41: 
1.15      pazsan     42: : lit, ( n -- ) postpone Literal ;
                     43: : nt, ( nt -- ) name>comp execute ;
1.18      pazsan     44: : nt-ex ( nt -- )
                     45:     [ cell 1 floats - dup [IF] ] lp+!# [ dup , [THEN] drop ]
                     46:     r> >l name>int execute @local0 >r lp+ ;
1.15      pazsan     47: 
1.16      pazsan     48: ' nt-ex
                     49: ' nt,
                     50: ' lit,
1.10      pazsan     51: Create r:word rot A, swap A, A,
1.1       pazsan     52: 
1.14      pazsan     53: : word-recognizer ( addr u -- nt r:word | addr u r:fail )
1.1       pazsan     54:     2dup find-name [ [IFDEF] prelude-mask ] run-prelude [ [THEN] ] dup
1.14      pazsan     55:     IF  nip nip r:word  ELSE  drop r:fail  THEN ;
1.1       pazsan     56: 
1.16      pazsan     57: ' noop
                     58: ' lit,
1.1       pazsan     59: dup
1.10      pazsan     60: Create r:num rot A, swap A, A,
1.1       pazsan     61: 
1.16      pazsan     62: ' noop
                     63: :noname ( n -- ) postpone 2Literal ;
1.1       pazsan     64: dup
1.10      pazsan     65: Create r:2num rot A, swap A, A,
1.7       pazsan     66: 
                     67: \ snumber? should be implemented as recognizer stack
1.1       pazsan     68: 
1.14      pazsan     69: : num-recognizer ( addr u -- n/d table | addr u r:fail )
1.1       pazsan     70:     2dup 2>r snumber?  dup
                     71:     IF
1.14      pazsan     72:        2rdrop 0> IF  r:2num   ELSE  r:num  THEN  EXIT
1.1       pazsan     73:     THEN
1.14      pazsan     74:     drop 2r> r:fail ;
1.3       pazsan     75: 
1.1       pazsan     76: \ recognizer stack
                     77: 
                     78: $10 Constant max-rec#
                     79: 
                     80: : get-recognizers ( rec-addr -- xt1 .. xtn n )
1.8       pazsan     81:     dup swap @ dup >r cells bounds swap ?DO
1.1       pazsan     82:        I @
1.8       pazsan     83:     cell -LOOP  r> ;
1.1       pazsan     84: 
                     85: : set-recognizers ( xt1 .. xtn n rec-addr -- )
                     86:     over max-rec# u>= abort" Too many recognizers"
1.8       pazsan     87:     2dup ! cell+ swap cells bounds ?DO
1.1       pazsan     88:        I !
1.8       pazsan     89:     cell +LOOP ;
1.1       pazsan     90: 
1.7       pazsan     91: Variable forth-recognizer
                     92: 
1.10      pazsan     93: ' word-recognizer A, ' num-recognizer A, max-rec# 2 - cells allot
1.7       pazsan     94: 2 forth-recognizer !
1.10      pazsan     95: \ ' num-recognizer ' word-recognizer 2 forth-recognizer set-recognizers
1.1       pazsan     96: 
                     97: \ recognizer loop
                     98: 
                     99: : do-recognizer ( addr u rec-addr -- token table )
                    100:     dup cell+ swap @ cells bounds ?DO
1.14      pazsan    101:        I perform dup r:fail <>  IF  UNLOOP  EXIT  THEN  drop
1.1       pazsan    102:     cell +LOOP
1.3       pazsan    103:     r:fail ;
1.1       pazsan    104: 
1.6       pazsan    105: \ nested recognizer helper
                    106: 
1.14      pazsan    107: \ : nest-recognizer ( addr u -- token table | addr u r:fail )
                    108: \   xxx-recognizer do-recognizer ;
1.6       pazsan    109: 
1.1       pazsan    110: : interpreter-r ( addr u -- ... xt )
1.16      pazsan    111:     forth-recognizer do-recognizer r>int @ ;
1.1       pazsan    112: 
1.7       pazsan    113: ' interpreter-r IS parser1
                    114: 
1.1       pazsan    115: : compiler-r ( addr u -- ... xt )
1.16      pazsan    116:     forth-recognizer do-recognizer r>comp @ ;
1.1       pazsan    117: 
                    118: : [ ( -- ) \  core     left-bracket
                    119:     \G Enter interpretation state. Immediate word.
                    120:     ['] interpreter-r  IS parser1 state off ; immediate
                    121: 
                    122: : ] ( -- ) \ core      right-bracket
                    123:     \G Enter compilation state.
                    124:     ['] compiler-r     IS parser1 state on  ;
                    125: 
1.16      pazsan    126: : >int      ( token table -- )  r>int perform ;
                    127: : >comp     ( token table -- )  r>comp perform ;
1.7       pazsan    128: : >postpone ( token table -- )
1.16      pazsan    129:     >r r@ r>lit perform r> r>comp @ compile, ;
1.7       pazsan    130: 
1.4       pazsan    131: : postpone ( "name" -- ) \ core
                    132:     \g Compiles the compilation semantics of @i{name}.
1.19      pazsan    133:     parse-name forth-recognizer do-recognizer >postpone
                    134: ; immediate restrict
1.4       pazsan    135: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>