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.tuple classes.tuple.parser combinators effects
5 effects.parser fry generic generic.parser generic.standard
6 interpolate io.streams.string kernel lexer locals.parser
7 locals.rewrite.closures locals.types make macros namespaces
8 parser quotations sequences vocabs.parser words words.symbol ;
15 : scan-param ( -- obj ) scan-object literalize ;
17 : define* ( word def -- ) over set-word define ;
19 : define-declared* ( word def effect -- ) pick set-word define-declared ;
21 : define-simple-generic* ( word effect -- ) over set-word define-simple-generic ;
23 TUPLE: fake-call-next-method ;
25 TUPLE: fake-quotation seq ;
27 GENERIC: >fake-quotations ( quot -- fake )
29 M: callable >fake-quotations
30 >array >fake-quotations fake-quotation boa ;
32 M: array >fake-quotations [ >fake-quotations ] { } map-as ;
34 M: object >fake-quotations ;
36 GENERIC: (fake-quotations>) ( fake -- )
38 : fake-quotations> ( fake -- quot )
39 [ (fake-quotations>) ] [ ] make ;
41 M: fake-quotation (fake-quotations>)
42 [ seq>> [ (fake-quotations>) ] each ] [ ] make , ;
44 M: array (fake-quotations>)
45 [ [ (fake-quotations>) ] each ] { } make , ;
47 M: fake-call-next-method (fake-quotations>)
48 drop method-body get literalize , \ (call-next-method) , ;
50 M: object (fake-quotations>) , ;
52 : parse-definition* ( accum -- accum )
53 parse-definition >fake-quotations parsed
54 [ fake-quotations> first ] over push-all ;
56 : parse-declared* ( accum -- accum )
58 [ parse-definition* ] dip
64 { ";" [ tuple parsed f parsed ] }
65 { "<" [ scan-param parsed [ parse-tuple-slots ] { } make parsed ] }
68 [ parse-slot-name [ parse-tuple-slots ] when ] { }
72 \ define-tuple-class parsed ;
77 [ create-method-in dup method-body set ] over push-all
85 [ [ [ boa ] curry ] over push-all ] dip parsed
86 \ define-declared* parsed ;
91 \ define-declared* parsed ;
95 \ define-symbol parsed ;
100 \ define-syntax parsed ;
105 \ add-mixin-instance parsed ;
109 complete-effect parsed
110 \ define-simple-generic* parsed ;
115 \ define-macro parsed ;
117 SYNTAX: `inline [ word make-inline ] over push-all ;
119 SYNTAX: `call-next-method T{ fake-call-next-method } parsed ;
121 : (INTERPOLATE) ( accum quot -- accum )
122 [ scan interpolate-locals ] dip
123 '[ _ with-string-writer @ ] parsed ;
127 SYNTAX: IS [ dup search [ ] [ no-word ] ?if ] (INTERPOLATE) ;
129 SYNTAX: DEFINES [ create-in ] (INTERPOLATE) ;
131 SYNTAX: DEFINES-CLASS [ create-class-in ] (INTERPOLATE) ;
133 DEFER: ;FUNCTOR delimiter
137 : functor-words ( -- assoc )
139 { "TUPLE:" POSTPONE: `TUPLE: }
140 { "M:" POSTPONE: `M: }
141 { "C:" POSTPONE: `C: }
143 { "GENERIC:" POSTPONE: `GENERIC: }
144 { "INSTANCE:" POSTPONE: `INSTANCE: }
145 { "SYNTAX:" POSTPONE: `SYNTAX: }
146 { "SYMBOL:" POSTPONE: `SYMBOL: }
147 { "inline" POSTPONE: `inline }
148 { "MACRO:" POSTPONE: `MACRO: }
149 { "call-next-method" POSTPONE: `call-next-method }
152 : push-functor-words ( -- )
153 functor-words use-words ;
155 : pop-functor-words ( -- )
156 functor-words unuse-words ;
158 : parse-functor-body ( -- form )
160 "WHERE" parse-bindings*
161 [ \ ;FUNCTOR parse-until >quotation ] ((parse-lambda)) <let*> 1quotation
164 : (FUNCTOR:) ( -- word def effect )
165 CREATE-WORD [ parse-functor-body ] parse-locals-definition ;
169 SYNTAX: FUNCTOR: (FUNCTOR:) define-declared ;