-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathrec-sequence.fs
53 lines (42 loc) · 1.99 KB
/
rec-sequence.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
\ recognizer-based interpreter, sequence
\ Authors: Bernd Paysan
\ Copyright (C) 2022,2023,2024 Free Software Foundation, Inc.
\ This file is part of Gforth.
\ Gforth is free software; you can redistribute it and/or
\ modify it under the terms of the GNU General Public License
\ as published by the Free Software Foundation, either version 3
\ of the License, or (at your option) any later version.
\ This program is distributed in the hope that it will be useful,
\ but WITHOUT ANY WARRANTY; without even the implied warranty of
\ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\ GNU General Public License for more details.
\ You should have received a copy of the GNU General Public License
\ along with this program. If not, see http://www.gnu.org/licenses/.
\ recognizer loop
Defer trace-recognizer ' drop is trace-recognizer
: recognize ( addr u rec-addr -- ... rectype ) \ gforth-experimental
\G apply a recognizer stack to a string, delivering a token
1 rec-level +! -rot >l >l
$@ bounds cell- swap cell- U-DO
@local0 @local1 I perform
dup IF
-1 rec-level +!
I @ trace-recognizer UNLOOP [ cell 8 = ] [IF] lp+2 [ELSE] lp+ [THEN] EXIT THEN drop
cell [ 2 cells ] Literal I cell- 2@ <> select \ skip double entries
\ note that we search first and then skip, because the first search
\ has a very likely hit. So doubles will be skipped, tripples not
-loop
-1 rec-level +!
0 [ cell 8 = ] [IF] lp+2 [ELSE] lp+ [THEN] ;
: recognizer-sequence: ( xt1 .. xtn n "name" -- ) \ gforth-experimental
\G concatenate a stack of recognizers to one recognizer with the
\G name @i{"name"}. @i{xtn} is tried first, @i{xt1} last, just
\G like on the recognizer stack
['] recognize do-stack: ;
\ : rec-sequence ( xt1 .. xtn n "name" -- ) \ gforth
\ n>r : nr> ]] 2>r [[ 0 ?DO
\ ]] 2r@ [[ compile,
\ ]] dup IF 2rdrop EXIT THEN drop [[
\ LOOP ]] 2rdrop ; [[ ;
' rec-num ' rec-nt 2 recognizer-sequence: default-recognize
' default-recognize is forth-recognize