2010-02-23 05:57:13 -05:00
|
|
|
! Copyright (C) 2008, 2010 Slava Pestov.
|
2008-06-25 04:25:08 -04:00
|
|
|
! See http://factorcode.org/license.txt for BSD license.
|
2016-03-30 20:21:58 -04:00
|
|
|
USING: accessors arrays combinators continuations effects kernel
|
|
|
|
lexer make namespaces parser sequences sets splitting
|
|
|
|
vocabs.parser words ;
|
2008-06-25 04:25:08 -04:00
|
|
|
IN: effects.parser
|
|
|
|
|
2008-07-18 20:22:59 -04:00
|
|
|
DEFER: parse-effect
|
|
|
|
|
|
|
|
ERROR: bad-effect ;
|
2010-03-11 04:25:13 -05:00
|
|
|
ERROR: invalid-row-variable ;
|
|
|
|
ERROR: row-variable-can't-have-type ;
|
2010-03-05 16:30:10 -05:00
|
|
|
ERROR: stack-effect-omits-dashes ;
|
|
|
|
|
|
|
|
SYMBOL: effect-var
|
|
|
|
|
2010-03-11 04:03:40 -05:00
|
|
|
<PRIVATE
|
|
|
|
: end-token? ( end token -- token ? ) [ nip ] [ = ] 2bi ; inline
|
2016-03-25 04:50:00 -04:00
|
|
|
: effect-opener? ( token -- token ? ) dup { f "(" "--" } member? ; inline
|
|
|
|
: effect-closer? ( token -- token ? ) dup ")" sequence= ; inline
|
2010-03-11 04:25:13 -05:00
|
|
|
: row-variable? ( token -- token' ? ) ".." ?head ; inline
|
2010-03-11 04:03:40 -05:00
|
|
|
|
|
|
|
: parse-effect-var ( first? var name -- var )
|
2010-03-05 16:30:10 -05:00
|
|
|
nip
|
2015-08-13 19:13:05 -04:00
|
|
|
[ ":" ?tail [ row-variable-can't-have-type ] when ] curry
|
|
|
|
[ invalid-row-variable ] if ;
|
2010-03-05 16:30:10 -05:00
|
|
|
|
2010-03-11 04:03:40 -05:00
|
|
|
: parse-effect-value ( token -- value )
|
2011-11-22 02:00:52 -05:00
|
|
|
":" ?tail [ scan-object 2array ] when ;
|
2010-03-11 04:03:40 -05:00
|
|
|
PRIVATE>
|
|
|
|
|
2010-03-05 16:30:10 -05:00
|
|
|
: parse-effect-token ( first? var end -- var more? )
|
2011-10-01 19:42:37 -04:00
|
|
|
scan-token {
|
2010-03-11 04:03:40 -05:00
|
|
|
{ [ end-token? ] [ drop nip f ] }
|
2015-08-13 19:13:05 -04:00
|
|
|
{ [ effect-opener? ] [ bad-effect ] }
|
|
|
|
{ [ effect-closer? ] [ stack-effect-omits-dashes ] }
|
2010-03-11 04:25:13 -05:00
|
|
|
{ [ row-variable? ] [ parse-effect-var t ] }
|
2010-03-11 04:03:40 -05:00
|
|
|
[ [ drop ] 2dip parse-effect-value , t ]
|
|
|
|
} cond ;
|
2008-07-18 20:22:59 -04:00
|
|
|
|
2010-03-05 16:30:10 -05:00
|
|
|
: parse-effect-tokens ( end -- var tokens )
|
|
|
|
[
|
|
|
|
[ t f ] dip [ parse-effect-token [ f ] 2dip ] curry [ ] while nip
|
|
|
|
] { } make ;
|
2009-08-12 00:09:02 -04:00
|
|
|
|
2008-07-18 20:22:59 -04:00
|
|
|
: parse-effect ( end -- effect )
|
2010-03-05 16:30:10 -05:00
|
|
|
[ "--" parse-effect-tokens ] dip parse-effect-tokens
|
|
|
|
<variable-effect> ;
|
2009-03-16 21:11:36 -04:00
|
|
|
|
2011-10-17 01:50:30 -04:00
|
|
|
: scan-effect ( -- effect )
|
2009-03-22 18:59:40 -04:00
|
|
|
"(" expect ")" parse-effect ;
|
|
|
|
|
2015-07-27 12:53:10 -04:00
|
|
|
: parse-call-paren ( accum word -- accum )
|
2009-10-28 16:29:01 -04:00
|
|
|
[ ")" parse-effect ] dip 2array append! ;
|
2010-02-23 05:57:13 -05:00
|
|
|
|
2016-03-30 16:16:15 -04:00
|
|
|
CONSTANT: in-definition HS{ }
|
2012-08-24 19:07:31 -04:00
|
|
|
|
2012-08-24 18:53:00 -04:00
|
|
|
ERROR: can't-nest-definitions word ;
|
|
|
|
|
2016-03-30 16:16:15 -04:00
|
|
|
: set-in-definition ( -- )
|
2016-03-30 20:21:58 -04:00
|
|
|
manifest get current-vocab>> t or in-definition ?adjoin
|
2016-03-30 16:16:15 -04:00
|
|
|
[ last-word can't-nest-definitions ] unless ;
|
|
|
|
|
|
|
|
: unset-in-definition ( -- )
|
2016-03-30 20:21:58 -04:00
|
|
|
manifest get current-vocab>> t or in-definition delete ;
|
2012-08-24 18:53:00 -04:00
|
|
|
|
2012-08-24 19:07:31 -04:00
|
|
|
: with-definition ( quot -- )
|
2016-03-30 16:16:15 -04:00
|
|
|
[ set-in-definition ] prepose [ unset-in-definition ] [ ] cleanup ; inline
|
2012-08-24 18:53:00 -04:00
|
|
|
|
2010-02-23 05:57:13 -05:00
|
|
|
: (:) ( -- word def effect )
|
2012-08-24 18:53:00 -04:00
|
|
|
[
|
|
|
|
scan-new-word
|
|
|
|
scan-effect
|
|
|
|
parse-definition swap
|
2012-08-24 19:07:31 -04:00
|
|
|
] with-definition ;
|