1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays classes.mixin classes.parser
4 classes.singleton classes.tuple classes.tuple.parser
5 combinators effects.parser fry functors.backend generic
6 generic.parser interpolate io.streams.string kernel lexer
7 locals.parser locals.types macros make namespaces parser
8 quotations sequences vocabs.parser words words.symbol ;
15 TUPLE: fake-call-next-method ;
17 TUPLE: fake-quotation seq ;
19 GENERIC: >fake-quotations ( quot -- fake )
21 M: callable >fake-quotations
22 >array >fake-quotations fake-quotation boa ;
24 M: array >fake-quotations [ >fake-quotations ] { } map-as ;
26 M: object >fake-quotations ;
28 GENERIC: (fake-quotations>) ( fake -- )
30 : fake-quotations> ( fake -- quot )
31 [ (fake-quotations>) ] [ ] make ;
33 M: fake-quotation (fake-quotations>)
34 [ seq>> [ (fake-quotations>) ] each ] [ ] make , ;
36 M: array (fake-quotations>)
37 [ [ (fake-quotations>) ] each ] { } make , ;
39 M: fake-call-next-method (fake-quotations>)
40 drop method-body get literalize , \ (call-next-method) , ;
42 M: object (fake-quotations>) , ;
44 : parse-definition* ( accum -- accum )
45 parse-definition >fake-quotations suffix!
46 [ fake-quotations> first ] append! ;
48 : parse-declared* ( accum -- accum )
50 [ parse-definition* ] dip
53 FUNCTOR-SYNTAX: TUPLE:
56 { ";" [ tuple suffix! f suffix! ] }
57 { "<" [ scan-param suffix! [ parse-tuple-slots ] { } make suffix! ] }
60 [ parse-slot-name [ parse-tuple-slots ] when ] { }
64 \ define-tuple-class suffix! ;
66 FUNCTOR-SYNTAX: SINGLETON:
68 \ define-singleton-class suffix! ;
70 FUNCTOR-SYNTAX: MIXIN:
72 \ define-mixin-class suffix! ;
77 [ create-method-in dup method-body set ] append!
85 [ [ [ boa ] curry ] append! ] dip suffix!
86 \ define-declared* suffix! ;
91 \ define-declared* suffix! ;
93 FUNCTOR-SYNTAX: SYMBOL:
95 \ define-symbol suffix! ;
97 FUNCTOR-SYNTAX: SYNTAX:
100 \ define-syntax suffix! ;
102 FUNCTOR-SYNTAX: INSTANCE:
105 \ add-mixin-instance suffix! ;
107 FUNCTOR-SYNTAX: GENERIC:
109 complete-effect suffix!
110 \ define-simple-generic* suffix! ;
112 FUNCTOR-SYNTAX: MACRO:
115 \ define-macro suffix! ;
117 FUNCTOR-SYNTAX: inline [ word make-inline ] append! ;
119 FUNCTOR-SYNTAX: call-next-method T{ fake-call-next-method } suffix! ;
121 : (INTERPOLATE) ( accum quot -- accum )
122 [ scan interpolate-locals ] dip
123 '[ _ with-string-writer @ ] suffix! ;
127 SYNTAX: IS [ dup search [ ] [ no-word ] ?if ] (INTERPOLATE) ;
129 SYNTAX: DEFERS [ current-vocab create ] (INTERPOLATE) ;
131 SYNTAX: DEFINES [ create-in ] (INTERPOLATE) ;
133 SYNTAX: DEFINES-PRIVATE [ begin-private create-in end-private ] (INTERPOLATE) ;
135 SYNTAX: DEFINES-CLASS [ create-class-in ] (INTERPOLATE) ;
137 DEFER: ;FUNCTOR delimiter
141 : push-functor-words ( -- )
142 functor-words use-words ;
144 : pop-functor-words ( -- )
145 functor-words unuse-words ;
147 : parse-functor-body ( -- form )
149 "WHERE" parse-bindings*
150 [ \ ;FUNCTOR parse-until >quotation ] ((parse-lambda)) <let*> 1quotation
153 : (FUNCTOR:) ( -- word def effect )
154 CREATE-WORD [ parse-functor-body ] parse-locals-definition ;
158 SYNTAX: FUNCTOR: (FUNCTOR:) define-declared ;