Fix up peg memoization of compiled parsers
parent
e6da3bc43a
commit
7c0535884e
|
@ -14,9 +14,14 @@ SYMBOL: ignore
|
||||||
: <parse-result> ( remaining ast -- parse-result )
|
: <parse-result> ( remaining ast -- parse-result )
|
||||||
parse-result construct-boa ;
|
parse-result construct-boa ;
|
||||||
|
|
||||||
SYMBOL: compiled-parsers
|
|
||||||
SYMBOL: packrat
|
SYMBOL: packrat
|
||||||
|
|
||||||
|
: compiled-parsers ( -- cache )
|
||||||
|
\ compiled-parsers get-global [ H{ } clone dup \ compiled-parsers set-global ] unless* ;
|
||||||
|
|
||||||
|
: reset-compiled-parsers ( -- )
|
||||||
|
H{ } clone \ compiled-parsers set-global ;
|
||||||
|
|
||||||
GENERIC: (compile) ( parser -- quot )
|
GENERIC: (compile) ( parser -- quot )
|
||||||
|
|
||||||
:: run-packrat-parser ( input quot c -- result )
|
:: run-packrat-parser ( input quot c -- result )
|
||||||
|
@ -35,14 +40,12 @@ GENERIC: (compile) ( parser -- quot )
|
||||||
#! Look to see if the given parser has been compiled.
|
#! Look to see if the given parser has been compiled.
|
||||||
#! If not, compile it to a temporary word, cache it,
|
#! If not, compile it to a temporary word, cache it,
|
||||||
#! and return it. Otherwise return the existing one.
|
#! and return it. Otherwise return the existing one.
|
||||||
compiled-parsers get [
|
compiled-parsers [
|
||||||
(compile) [ run-parser ] curry define-temp
|
(compile) [ run-parser ] curry define-temp
|
||||||
] cache ;
|
] cache ;
|
||||||
|
|
||||||
: compile ( parser -- word )
|
: compile ( parser -- word )
|
||||||
H{ } clone compiled-parsers [
|
[ compiled-parser ] with-compilation-unit ;
|
||||||
[ compiled-parser ] with-compilation-unit
|
|
||||||
] with-variable ;
|
|
||||||
|
|
||||||
: parse ( state parser -- result )
|
: parse ( state parser -- result )
|
||||||
compile execute ;
|
compile execute ;
|
||||||
|
|
Loading…
Reference in New Issue