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
61 : DEFINE* ( accum -- accum ) \ define-declared* parsed ;
66 { ";" [ tuple parsed f parsed ] }
67 { "<" [ scan-param parsed [ parse-tuple-slots ] { } make parsed ] }
70 [ parse-slot-name [ parse-tuple-slots ] when ] { }
74 \ define-tuple-class parsed ;
79 [ create-method-in dup method-body set ] over push-all
87 [ [ [ boa ] curry ] over push-all ] dip parsed
88 \ define-declared* parsed ;
93 \ define-declared* parsed ;
97 \ define-symbol parsed ;
102 \ define-syntax parsed ;
107 \ add-mixin-instance parsed ;
111 complete-effect parsed
112 \ define-simple-generic* parsed ;
117 \ define-macro parsed ;
119 SYNTAX: `inline [ word make-inline ] over push-all ;
121 SYNTAX: `call-next-method T{ fake-call-next-method } parsed ;
123 : (INTERPOLATE) ( accum quot -- accum )
124 [ scan interpolate-locals ] dip
125 '[ _ with-string-writer @ ] parsed ;
129 SYNTAX: IS [ dup search [ ] [ no-word ] ?if ] (INTERPOLATE) ;
131 SYNTAX: DEFINES [ create-in ] (INTERPOLATE) ;
133 SYNTAX: DEFINES-CLASS [ create-class-in ] (INTERPOLATE) ;
135 DEFER: ;FUNCTOR delimiter
139 : functor-words ( -- assoc )
141 { "TUPLE:" POSTPONE: `TUPLE: }
142 { "M:" POSTPONE: `M: }
143 { "C:" POSTPONE: `C: }
145 { "GENERIC:" POSTPONE: `GENERIC: }
146 { "INSTANCE:" POSTPONE: `INSTANCE: }
147 { "SYNTAX:" POSTPONE: `SYNTAX: }
148 { "SYMBOL:" POSTPONE: `SYMBOL: }
149 { "inline" POSTPONE: `inline }
150 { "MACRO:" POSTPONE: `MACRO: }
151 { "call-next-method" POSTPONE: `call-next-method }
154 : push-functor-words ( -- )
155 functor-words use get push ;
157 : pop-functor-words ( -- )
158 functor-words use get delq ;
160 : parse-functor-body ( -- form )
162 "WHERE" parse-bindings*
163 [ \ ;FUNCTOR parse-until >quotation ] ((parse-lambda)) <let*> 1quotation
166 : (FUNCTOR:) ( -- word def effect )
167 CREATE-WORD [ parse-functor-body ] parse-locals-definition ;
171 SYNTAX: FUNCTOR: (FUNCTOR:) define-declared ;