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.
|
2012-07-19 12:50:09 -04:00
|
|
|
USING: accessors arrays assocs combinators effects.parser
|
2009-01-26 00:04:11 -05:00
|
|
|
generic.parser kernel lexer locals.errors fry
|
2008-12-06 05:57:38 -05:00
|
|
|
locals.rewrite.closures locals.types make namespaces parser
|
2008-12-17 19:10:01 -05:00
|
|
|
quotations sequences splitting words vocabs.parser ;
|
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 ;
|
|
|
|
|
2008-12-06 05:57:38 -05:00
|
|
|
: make-local ( name -- word )
|
|
|
|
"!" ?tail [
|
|
|
|
<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 )
|
2012-07-19 12:50:09 -04:00
|
|
|
[ "|" [ make-local ] map-tokens ] H{ } make ;
|
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
|
|
|
|
2009-03-06 20:48:04 -05:00
|
|
|
SYMBOL: locals
|
|
|
|
|
|
|
|
: ((parse-lambda)) ( assoc quot -- quot' )
|
|
|
|
'[
|
2008-12-09 02:04:22 -05:00
|
|
|
in-lambda? on
|
2009-03-06 20:48:04 -05:00
|
|
|
lambda-parser quotation-parser set
|
2009-05-14 23:31:29 -04:00
|
|
|
[ locals set ]
|
|
|
|
[ use-words @ ]
|
|
|
|
[ unuse-words ] tri
|
2009-03-06 20:48:04 -05:00
|
|
|
] with-scope ; inline
|
2012-07-19 12:50:09 -04:00
|
|
|
|
2009-03-06 20:48:04 -05:00
|
|
|
: (parse-lambda) ( assoc -- quot )
|
|
|
|
[ \ ] parse-until >quotation ] ((parse-lambda)) ;
|
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 ;
|
|
|
|
|
2009-10-28 16:40:07 -04:00
|
|
|
: parse-multi-def ( locals -- multi-def )
|
2012-07-19 12:50:09 -04:00
|
|
|
[ [ ")" [ make-local ] map-tokens ] H{ } make ] dip
|
|
|
|
swap assoc-union! drop <multi-def> ;
|
2009-10-28 16:40:07 -04:00
|
|
|
|
|
|
|
: parse-def ( name/paren locals -- def )
|
2012-07-19 12:50:09 -04:00
|
|
|
over "(" =
|
|
|
|
[ nip parse-multi-def ]
|
|
|
|
[ [ [ make-local ] H{ } make ] dip swap assoc-union! drop <def> ]
|
|
|
|
if ;
|
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
|
|
|
|
2011-10-13 19:40:52 -04:00
|
|
|
: (parse-locals-definition) ( effect vars assoc reader -- word quot effect )
|
2009-03-06 20:48:04 -05:00
|
|
|
((parse-lambda)) <lambda>
|
2009-03-21 04:17:35 -04:00
|
|
|
[ nip "lambda" set-word-prop ]
|
|
|
|
[ nip rewrite-closures dup length 1 = [ first ] [ bad-rewrite ] if ]
|
|
|
|
[ drop nip ] 3tri ; inline
|
2008-12-06 05:57:38 -05:00
|
|
|
|
2011-10-13 19:40:52 -04:00
|
|
|
: parse-locals-definition ( word reader -- word quot effect )
|
|
|
|
[ 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:04:49 -04:00
|
|
|
] in-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:04:49 -04:00
|
|
|
] in-definition ;
|