Fix up peg memoization of compiled parsers

db4
Chris Double 2008-03-27 15:21:38 +13:00
parent e6da3bc43a
commit 7c0535884e
1 changed files with 8 additions and 5 deletions

View File

@ -14,9 +14,14 @@ SYMBOL: ignore
: <parse-result> ( remaining ast -- parse-result )
parse-result construct-boa ;
SYMBOL: compiled-parsers
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 )
:: 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.
#! If not, compile it to a temporary word, cache it,
#! and return it. Otherwise return the existing one.
compiled-parsers get [
compiled-parsers [
(compile) [ run-parser ] curry define-temp
] cache ;
: compile ( parser -- word )
H{ } clone compiled-parsers [
[ compiled-parser ] with-compilation-unit
] with-variable ;
[ compiled-parser ] with-compilation-unit ;
: parse ( state parser -- result )
compile execute ;