1 ! Copyright (C) 2008, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: kernel quotations classes.tuple make combinators generic
4 words interpolate namespaces sequences io.streams.string fry
5 classes.mixin effects lexer parser classes.tuple.parser
6 effects.parser locals.types locals.parser
7 locals.rewrite.closures vocabs.parser arrays accessors ;
14 : scan-param ( -- obj ) scan-object literalize ;
16 : define* ( word def effect -- ) pick set-word define-declared ;
18 TUPLE: fake-quotation seq ;
20 GENERIC: >fake-quotations ( quot -- fake )
22 M: callable >fake-quotations
23 >array >fake-quotations fake-quotation boa ;
25 M: array >fake-quotations [ >fake-quotations ] { } map-as ;
27 M: object >fake-quotations ;
29 GENERIC: fake-quotations> ( fake -- quot )
31 M: fake-quotation fake-quotations>
32 seq>> [ fake-quotations> ] map >quotation ;
34 M: array fake-quotations> [ fake-quotations> ] map ;
36 M: object fake-quotations> ;
38 : parse-definition* ( -- )
39 parse-definition >fake-quotations parsed \ fake-quotations> parsed ;
41 : DEFINE* ( accum -- accum ) effect get parsed \ define* parsed ;
46 { ";" [ tuple parsed f parsed ] }
47 { "<" [ scan-param parsed [ parse-tuple-slots ] { } make parsed ] }
50 [ parse-slot-name [ parse-tuple-slots ] when ] { }
54 \ define-tuple-class parsed ; parsing
60 \ create-method parsed
68 [ [ boa ] curry ] over push-all
80 \ add-mixin-instance parsed ; parsing
82 : `inline \ inline parsed ; parsing
84 : `parsing \ parsing parsed ; parsing
87 ")" parse-effect effect set ; parsing
89 : (INTERPOLATE) ( accum quot -- accum )
90 [ scan interpolate-locals ] dip
91 '[ _ with-string-writer @ ] parsed ;
95 : IS [ dup search [ ] [ no-word ] ?if ] (INTERPOLATE) ; parsing
97 : DEFINES [ create-in ] (INTERPOLATE) ; parsing
99 DEFER: ;FUNCTOR delimiter
103 : functor-words ( -- assoc )
105 { "TUPLE:" POSTPONE: `TUPLE: }
106 { "M:" POSTPONE: `M: }
107 { "C:" POSTPONE: `C: }
109 { "INSTANCE:" POSTPONE: `INSTANCE: }
110 { "inline" POSTPONE: `inline }
111 { "parsing" POSTPONE: `parsing }
115 : push-functor-words ( -- )
116 functor-words use get push ;
118 : pop-functor-words ( -- )
119 functor-words use get delq ;
121 : parse-functor-body ( -- form )
125 "WHERE" parse-bindings* \ ;FUNCTOR (parse-lambda)
131 : (FUNCTOR:) ( -- word def )
133 parse-locals dup push-locals
134 parse-functor-body swap pop-locals <lambda>
135 rewrite-closures first ;
139 : FUNCTOR: (FUNCTOR:) define ; parsing