2009-01-26 00:04:11 -05:00
|
|
|
! Copyright (C) 2007, 2009 Slava Pestov, Eduardo Cavazos.
|
2008-12-06 05:57:38 -05:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2015-07-20 20:33:33 -04:00
|
|
|
USING: accessors arrays assocs combinators continuations
|
2015-07-19 22:17:46 -04:00
|
|
|
effects.parser fry generic.parser kernel lexer locals.errors
|
2015-07-20 20:33:33 -04:00
|
|
|
locals.rewrite.closures locals.types make namespaces parser
|
|
|
|
quotations sequences splitting vocabs.parser words ;
|
2008-12-06 05:57:38 -05:00
|
|
|
IN: locals.parser
|
|
|
|
|
2009-03-06 20:48:04 -05:00
|
|
|
SYMBOL: in-lambda?
|
|
|
|
|
|
|
|
: ?rewrite-closures ( form -- form' )
|
|
|
|
in-lambda? get [ 1array ] [ rewrite-closures ] if ;
|
|
|
|
|
2013-03-20 12:48:55 -04:00
|
|
|
ERROR: invalid-local-name name ;
|
|
|
|
|
|
|
|
: check-local-name ( name -- name )
|
2015-08-13 19:13:05 -04:00
|
|
|
dup { "]" "]!" } member? [ invalid-local-name ] when ;
|
2013-03-20 12:48:55 -04:00
|
|
|
|
2008-12-06 05:57:38 -05:00
|
|
|
: make-local ( name -- word )
|
2013-03-20 12:48:55 -04:00
|
|
|
check-local-name "!" ?tail [
|
2008-12-06 05:57:38 -05:00
|
|
|
<local-reader>
|
2012-07-19 12:50:09 -04:00
|
|
|
dup <local-writer> dup name>> ,,
|
2008-12-06 05:57:38 -05:00
|
|
|
] [ <local> ] if
|
2012-07-19 12:50:09 -04:00
|
|
|
dup dup name>> ,, ;
|
2008-12-06 05:57:38 -05:00
|
|
|
|
|
|
|
: make-locals ( seq -- words assoc )
|
2012-07-19 12:50:09 -04:00
|
|
|
[ [ make-local ] map ] H{ } make ;
|
2008-12-06 05:57:38 -05:00
|
|
|
|
2010-03-01 01:06:47 -05:00
|
|
|
: parse-local-defs ( -- words assoc )
|
2015-06-08 07:53:59 -04:00
|
|
|
"|" parse-tokens make-locals ;
|
2010-03-01 01:06:47 -05:00
|
|
|
|
2009-03-06 20:48:04 -05:00
|
|
|
SINGLETON: lambda-parser
|
2008-12-06 05:57:38 -05:00
|
|
|
|
2015-06-22 04:53:03 -04:00
|
|
|
: with-lambda-scope ( assoc reader-quot: ( -- quot ) -- quot )
|
2009-03-06 20:48:04 -05:00
|
|
|
'[
|
2008-12-09 02:04:22 -05:00
|
|
|
in-lambda? on
|
2009-03-06 20:48:04 -05:00
|
|
|
lambda-parser quotation-parser set
|
2015-06-22 04:53:03 -04:00
|
|
|
use-words @
|
2015-07-20 20:33:33 -04:00
|
|
|
qualified-vocabs pop* ! can't use unuse-words here
|
|
|
|
] with-scope ; inline
|
2012-07-19 12:50:09 -04:00
|
|
|
|
2009-03-06 20:48:04 -05:00
|
|
|
: (parse-lambda) ( assoc -- quot )
|
2015-06-22 04:53:03 -04:00
|
|
|
[ \ ] parse-until >quotation ] with-lambda-scope ;
|
2008-12-06 05:57:38 -05:00
|
|
|
|
|
|
|
: parse-lambda ( -- lambda )
|
2010-03-01 01:06:47 -05:00
|
|
|
parse-local-defs
|
2009-03-06 20:48:04 -05:00
|
|
|
(parse-lambda) <lambda>
|
|
|
|
?rewrite-closures ;
|
|
|
|
|
2015-06-09 05:39:55 -04:00
|
|
|
: parse-multi-def ( -- multi-def assoc )
|
|
|
|
")" parse-tokens make-locals [ <multi-def> ] dip ;
|
|
|
|
|
|
|
|
: parse-single-def ( name -- def assoc )
|
|
|
|
[ make-local <def> ] H{ } make ;
|
2015-06-08 07:53:59 -04:00
|
|
|
|
2015-06-09 05:39:55 -04:00
|
|
|
: update-locals ( assoc -- )
|
2015-06-09 12:59:19 -04:00
|
|
|
qualified-vocabs last words>> swap assoc-union! drop ;
|
2009-10-28 16:40:07 -04:00
|
|
|
|
2015-06-09 05:39:55 -04:00
|
|
|
: parse-def ( name/paren -- def )
|
|
|
|
dup "(" = [ drop parse-multi-def ] [ parse-single-def ] if update-locals ;
|
2009-10-28 16:40:07 -04:00
|
|
|
|
2009-03-06 20:48:04 -05:00
|
|
|
M: lambda-parser parse-quotation ( -- quotation )
|
|
|
|
H{ } clone (parse-lambda) ;
|
2008-12-06 05:57:38 -05:00
|
|
|
|
|
|
|
: parse-binding ( end -- pair/f )
|
2010-07-06 16:20:08 -04:00
|
|
|
scan-token {
|
2008-12-06 05:57:38 -05:00
|
|
|
{ [ 2dup = ] [ 2drop f ] }
|
|
|
|
[ nip scan-object 2array ]
|
|
|
|
} cond ;
|
|
|
|
|
2009-03-06 20:48:04 -05:00
|
|
|
: parse-let ( -- form )
|
2009-10-27 15:19:05 -04:00
|
|
|
H{ } clone (parse-lambda) <let> ?rewrite-closures ;
|
2009-03-06 20:48:04 -05:00
|
|
|
|
2009-03-21 04:17:35 -04:00
|
|
|
: parse-locals ( -- effect vars assoc )
|
2011-10-17 01:50:30 -04:00
|
|
|
scan-effect
|
2009-03-21 04:17:35 -04:00
|
|
|
dup
|
2008-12-09 02:04:22 -05:00
|
|
|
in>> [ dup pair? [ first ] when ] map make-locals ;
|
2008-12-06 05:57:38 -05:00
|
|
|
|
2015-06-08 07:53:59 -04:00
|
|
|
: (parse-locals-definition) ( effect vars assoc reader-quot -- word quot effect )
|
2015-06-22 04:53:03 -04:00
|
|
|
with-lambda-scope <lambda>
|
2009-03-21 04:17:35 -04:00
|
|
|
[ nip "lambda" set-word-prop ]
|
2015-08-13 19:13:05 -04:00
|
|
|
[ nip rewrite-closures dup length 1 = [ first ] [ bad-rewrite ] if ]
|
2009-03-21 04:17:35 -04:00
|
|
|
[ drop nip ] 3tri ; inline
|
2008-12-06 05:57:38 -05:00
|
|
|
|
2015-06-08 07:53:59 -04:00
|
|
|
: parse-locals-definition ( word reader-quot -- word quot effect )
|
2011-10-13 19:40:52 -04:00
|
|
|
[ parse-locals ] dip (parse-locals-definition) ; inline
|
|
|
|
|
|
|
|
: parse-locals-method-definition ( word reader -- word quot effect )
|
|
|
|
[ parse-locals pick check-method-effect ] dip
|
|
|
|
(parse-locals-definition) ; inline
|
|
|
|
|
2009-03-21 04:17:35 -04:00
|
|
|
: (::) ( -- word def effect )
|
2012-08-24 18:53:00 -04:00
|
|
|
[
|
|
|
|
scan-new-word
|
|
|
|
[ parse-definition ]
|
|
|
|
parse-locals-definition
|
2012-08-24 19:07:31 -04:00
|
|
|
] with-definition ;
|
2008-12-06 05:57:38 -05:00
|
|
|
|
|
|
|
: (M::) ( -- word def )
|
2009-03-06 20:48:04 -05:00
|
|
|
[
|
2012-08-24 18:53:00 -04:00
|
|
|
scan-new-method
|
|
|
|
[
|
|
|
|
[ parse-definition ]
|
|
|
|
parse-locals-method-definition drop
|
|
|
|
] with-method-definition
|
2012-08-24 19:07:31 -04:00
|
|
|
] with-definition ;
|