factor/basis/functors/functors.factor

183 lines
4.4 KiB
Factor
Raw Normal View History

! Copyright (C) 2008, 2009 Slava Pestov.
2008-11-14 21:18:16 -05:00
! See http://factorcode.org/license.txt for BSD license.
2009-10-28 00:50:48 -04:00
USING: accessors arrays assocs classes.mixin classes.parser
classes.singleton classes.tuple classes.tuple.parser
combinators effects.parser fry functors.backend generic
generic.parser interpolate io.streams.string kernel lexer
locals.parser locals.types macros make namespaces parser
quotations sequences vocabs.parser words words.symbol ;
2008-11-14 21:18:16 -05:00
IN: functors
! This is a hack
2009-01-28 18:07:31 -05:00
<PRIVATE
TUPLE: fake-call-next-method ;
TUPLE: fake-quotation seq ;
GENERIC: >fake-quotations ( quot -- fake )
M: callable >fake-quotations
>array >fake-quotations fake-quotation boa ;
M: array >fake-quotations [ >fake-quotations ] { } map-as ;
M: object >fake-quotations ;
GENERIC: (fake-quotations>) ( fake -- )
: fake-quotations> ( fake -- quot )
[ (fake-quotations>) ] [ ] make ;
M: fake-quotation (fake-quotations>)
[ seq>> [ (fake-quotations>) ] each ] [ ] make , ;
M: array (fake-quotations>)
[ [ (fake-quotations>) ] each ] { } make , ;
M: fake-call-next-method (fake-quotations>)
drop \ method get literalize , \ (call-next-method) , ;
M: object (fake-quotations>) , ;
2009-03-17 03:19:50 -04:00
: parse-definition* ( accum -- accum )
2009-10-28 14:38:27 -04:00
parse-definition >fake-quotations suffix!
[ fake-quotations> first ] append! ;
: parse-declared* ( accum -- accum )
2009-03-22 18:59:40 -04:00
complete-effect
[ parse-definition* ] dip
2009-10-28 14:38:27 -04:00
suffix! ;
FUNCTOR-SYNTAX: TUPLE:
2009-10-28 14:38:27 -04:00
scan-param suffix!
2008-11-14 21:18:16 -05:00
scan {
2009-10-28 14:38:27 -04:00
{ ";" [ tuple suffix! f suffix! ] }
{ "<" [ scan-param suffix! [ parse-tuple-slots ] { } make suffix! ] }
2008-11-14 21:18:16 -05:00
[
2009-10-28 14:38:27 -04:00
[ tuple suffix! ] dip
2008-11-14 21:18:16 -05:00
[ parse-slot-name [ parse-tuple-slots ] when ] { }
2009-10-28 14:38:27 -04:00
make suffix!
2008-11-14 21:18:16 -05:00
]
} case
\ define-tuple-class* suffix! ;
2008-11-14 21:18:16 -05:00
FUNCTOR-SYNTAX: final
[ word make-final ] append! ;
FUNCTOR-SYNTAX: SINGLETON:
2009-10-28 14:38:27 -04:00
scan-param suffix!
\ define-singleton-class suffix! ;
2009-07-22 03:06:24 -04:00
FUNCTOR-SYNTAX: MIXIN:
2009-10-28 14:38:27 -04:00
scan-param suffix!
\ define-mixin-class suffix! ;
2009-07-22 03:06:24 -04:00
FUNCTOR-SYNTAX: M:
2009-10-28 14:38:27 -04:00
scan-param suffix!
scan-param suffix!
[ create-method-in dup \ method set ] append!
parse-definition*
2009-10-28 14:38:27 -04:00
\ define* suffix! ;
2008-11-14 21:18:16 -05:00
FUNCTOR-SYNTAX: C:
2009-10-28 14:38:27 -04:00
scan-param suffix!
scan-param suffix!
2009-03-22 18:59:40 -04:00
complete-effect
[ [ [ boa ] curry ] append! ] dip suffix!
2009-10-28 14:38:27 -04:00
\ define-declared* suffix! ;
2008-11-14 21:18:16 -05:00
FUNCTOR-SYNTAX: :
2009-10-28 14:38:27 -04:00
scan-param suffix!
parse-declared*
2009-10-28 14:38:27 -04:00
\ define-declared* suffix! ;
2008-11-14 21:18:16 -05:00
FUNCTOR-SYNTAX: SYMBOL:
2009-10-28 14:38:27 -04:00
scan-param suffix!
\ define-symbol suffix! ;
2009-04-27 15:02:14 -04:00
FUNCTOR-SYNTAX: SYNTAX:
2009-10-28 14:38:27 -04:00
scan-param suffix!
parse-definition*
2009-10-28 14:38:27 -04:00
\ define-syntax suffix! ;
2008-11-14 21:18:16 -05:00
FUNCTOR-SYNTAX: INSTANCE:
2009-10-28 14:38:27 -04:00
scan-param suffix!
scan-param suffix!
\ add-mixin-instance suffix! ;
2008-11-14 21:18:16 -05:00
FUNCTOR-SYNTAX: GENERIC:
2009-10-28 14:38:27 -04:00
scan-param suffix!
complete-effect suffix!
\ define-simple-generic* suffix! ;
2009-05-01 17:16:40 -04:00
FUNCTOR-SYNTAX: MACRO:
2009-10-28 14:38:27 -04:00
scan-param suffix!
2009-05-24 10:36:24 -04:00
parse-declared*
2009-10-28 14:38:27 -04:00
\ define-macro suffix! ;
2009-05-24 10:36:24 -04:00
FUNCTOR-SYNTAX: inline [ word make-inline ] append! ;
2008-11-14 21:18:16 -05:00
2009-10-28 14:38:27 -04:00
FUNCTOR-SYNTAX: call-next-method T{ fake-call-next-method } suffix! ;
2008-11-14 21:18:16 -05:00
: (INTERPOLATE) ( accum quot -- accum )
[ scan interpolate-locals ] dip
2009-10-28 14:38:27 -04:00
'[ _ with-string-writer @ ] suffix! ;
2008-11-14 21:18:16 -05:00
2009-01-28 18:07:31 -05:00
PRIVATE>
SYNTAX: IS [ dup search [ ] [ no-word ] ?if ] (INTERPOLATE) ;
2008-11-14 21:18:16 -05:00
SYNTAX: DEFERS [ current-vocab create ] (INTERPOLATE) ;
SYNTAX: DEFINES [ create-in ] (INTERPOLATE) ;
2008-11-14 21:18:16 -05:00
SYNTAX: DEFINES-PRIVATE [ begin-private create-in end-private ] (INTERPOLATE) ;
SYNTAX: DEFINES-CLASS [ create-class-in ] (INTERPOLATE) ;
2008-11-14 21:18:16 -05:00
DEFER: ;FUNCTOR delimiter
2009-01-28 18:07:31 -05:00
<PRIVATE
2008-11-14 21:18:16 -05:00
: push-functor-words ( -- )
functor-words use-words ;
2008-11-14 21:18:16 -05:00
: pop-functor-words ( -- )
functor-words unuse-words ;
2008-11-14 21:18:16 -05:00
2009-10-28 00:50:48 -04:00
: (parse-bindings) ( end -- )
dup parse-binding dup [
first2 [ make-local ] dip 2array ,
(parse-bindings)
] [ 2drop ] if ;
: with-bindings ( quot -- words assoc )
'[
in-lambda? on
_ H{ } make-assoc
] { } make swap ; inline
: parse-bindings ( end -- words assoc )
[
namespace use-words
(parse-bindings)
namespace unuse-words
] with-bindings ;
2008-11-14 21:18:16 -05:00
: parse-functor-body ( -- form )
push-functor-words
2009-10-28 00:50:48 -04:00
"WHERE" parse-bindings
[ [ swap <def> suffix ] { } assoc>map concat ]
[ [ \ ;FUNCTOR parse-until >quotation ] ((parse-lambda)) ] bi*
[ ] append-as
pop-functor-words ;
2008-11-14 21:18:16 -05:00
: (FUNCTOR:) ( -- word def effect )
CREATE-WORD [ parse-functor-body ] parse-locals-definition ;
2008-11-14 21:18:16 -05:00
2009-01-28 18:07:31 -05:00
PRIVATE>
SYNTAX: FUNCTOR: (FUNCTOR:) define-declared ;