]> gitweb.factorcode.org Git - factor.git/blob - core/syntax/syntax.factor
de3be98ceb28b201dd729e67daa1fc357561dcbc
[factor.git] / core / syntax / syntax.factor
1 ! Copyright (C) 2004, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien arrays byte-arrays definitions generic
4 hashtables kernel math namespaces parser lexer sequences strings
5 strings.parser sbufs vectors words words.symbol words.constant
6 words.alias quotations io assocs splitting classes.tuple
7 generic.standard generic.math generic.parser classes
8 io.pathnames vocabs vocabs.parser classes.parser classes.union
9 classes.intersection classes.mixin classes.predicate
10 classes.singleton classes.tuple.parser compiler.units
11 combinators effects.parser slots ;
12 IN: bootstrap.syntax
13
14 ! These words are defined as a top-level form, instead of with
15 ! defining parsing words, because during stage1 bootstrap, the
16 ! "syntax" vocabulary is copied from the host. When stage1
17 ! bootstrap completes, the host's syntax vocabulary is deleted
18 ! from the target, then this top-level form creates the
19 ! target's "syntax" vocabulary as one of the first things done
20 ! in stage2.
21
22 : define-delimiter ( name -- )
23     "syntax" lookup t "delimiter" set-word-prop ;
24
25 : define-syntax ( name quot -- )
26     [ dup "syntax" lookup [ dup ] [ no-word-error ] ?if ] dip
27     define make-parsing ;
28
29 [
30     { "]" "}" ";" ">>" } [ define-delimiter ] each
31
32     "PRIMITIVE:" [
33         "Primitive definition is not supported" throw
34     ] define-syntax
35
36     "CS{" [
37         "Call stack literals are not supported" throw
38     ] define-syntax
39
40     "!" [ lexer get next-line ] define-syntax
41
42     "#!" [ POSTPONE: ! ] define-syntax
43
44     "IN:" [ scan set-in ] define-syntax
45
46     "PRIVATE>" [ in get ".private" ?tail drop set-in ] define-syntax
47
48     "<PRIVATE" [
49         POSTPONE: PRIVATE> in get ".private" append set-in
50     ] define-syntax
51
52     "USE:" [ scan use+ ] define-syntax
53
54     "USING:" [ ";" parse-tokens add-use ] define-syntax
55
56     "QUALIFIED:" [ scan dup add-qualified ] define-syntax
57
58     "QUALIFIED-WITH:" [ scan scan add-qualified ] define-syntax
59
60     "FROM:" [
61         scan "=>" expect ";" parse-tokens swap add-words-from
62     ] define-syntax
63
64     "EXCLUDE:" [
65         scan "=>" expect ";" parse-tokens swap add-words-excluding
66     ] define-syntax
67
68     "RENAME:" [
69         scan scan "=>" expect scan add-renamed-word
70     ] define-syntax
71
72     "HEX:" [ 16 parse-base ] define-syntax
73     "OCT:" [ 8 parse-base ] define-syntax
74     "BIN:" [ 2 parse-base ] define-syntax
75
76     "f" [ f parsed ] define-syntax
77     "t" "syntax" lookup define-singleton-class
78
79     "CHAR:" [
80         scan {
81             { [ dup length 1 = ] [ first ] }
82             { [ "\\" ?head ] [ next-escape >string "" assert= ] }
83             [ name>char-hook get call ]
84         } cond parsed
85     ] define-syntax
86
87     "\"" [ parse-string parsed ] define-syntax
88
89     "SBUF\"" [
90         lexer get skip-blank parse-string >sbuf parsed
91     ] define-syntax
92
93     "P\"" [
94         lexer get skip-blank parse-string <pathname> parsed
95     ] define-syntax
96
97     "[" [ parse-quotation parsed ] define-syntax
98     "{" [ \ } [ >array ] parse-literal ] define-syntax
99     "V{" [ \ } [ >vector ] parse-literal ] define-syntax
100     "B{" [ \ } [ >byte-array ] parse-literal ] define-syntax
101     "H{" [ \ } [ >hashtable ] parse-literal ] define-syntax
102     "T{" [ parse-tuple-literal parsed ] define-syntax
103     "W{" [ \ } [ first <wrapper> ] parse-literal ] define-syntax
104
105     "POSTPONE:" [ scan-word parsed ] define-syntax
106     "\\" [ scan-word <wrapper> parsed ] define-syntax
107     "inline" [ word make-inline ] define-syntax
108     "recursive" [ word make-recursive ] define-syntax
109     "foldable" [ word make-foldable ] define-syntax
110     "flushable" [ word make-flushable ] define-syntax
111     "delimiter" [ word t "delimiter" set-word-prop ] define-syntax
112     "parsing" [ word make-parsing ] define-syntax
113
114     "SYMBOL:" [
115         CREATE-WORD define-symbol
116     ] define-syntax
117
118     "SYMBOLS:" [
119         ";" parse-tokens
120         [ create-in dup reset-generic define-symbol ] each
121     ] define-syntax
122
123     "SINGLETONS:" [
124         ";" parse-tokens
125         [ create-class-in define-singleton-class ] each
126     ] define-syntax
127     
128     "ALIAS:" [
129         CREATE-WORD scan-word define-alias
130     ] define-syntax
131
132     "CONSTANT:" [
133         CREATE scan-object define-constant
134     ] define-syntax
135
136     "DEFER:" [
137         scan current-vocab create
138         [ fake-definition ] [ set-word ] [ [ undefined ] define ] tri
139     ] define-syntax
140
141     ":" [
142         (:) define
143     ] define-syntax
144
145     "GENERIC:" [
146         CREATE-GENERIC define-simple-generic
147     ] define-syntax
148
149     "GENERIC#" [
150         CREATE-GENERIC
151         scan-word <standard-combination> define-generic
152     ] define-syntax
153
154     "MATH:" [
155         CREATE-GENERIC
156         T{ math-combination } define-generic
157     ] define-syntax
158
159     "HOOK:" [
160         CREATE-GENERIC scan-word
161         <hook-combination> define-generic
162     ] define-syntax
163
164     "M:" [
165         (M:) define
166     ] define-syntax
167
168     "UNION:" [
169         CREATE-CLASS parse-definition define-union-class
170     ] define-syntax
171
172     "INTERSECTION:" [
173         CREATE-CLASS parse-definition define-intersection-class
174     ] define-syntax
175
176     "MIXIN:" [
177         CREATE-CLASS define-mixin-class
178     ] define-syntax
179
180     "INSTANCE:" [
181         location [
182             scan-word scan-word 2dup add-mixin-instance
183             <mixin-instance>
184         ] dip remember-definition
185     ] define-syntax
186
187     "PREDICATE:" [
188         CREATE-CLASS
189         scan "<" assert=
190         scan-word
191         parse-definition define-predicate-class
192     ] define-syntax
193
194     "SINGLETON:" [
195         CREATE-CLASS define-singleton-class
196     ] define-syntax
197
198     "TUPLE:" [
199         parse-tuple-definition define-tuple-class
200     ] define-syntax
201
202     "SLOT:" [
203         scan define-protocol-slot
204     ] define-syntax
205
206     "C:" [
207         CREATE-WORD scan-word define-boa-word
208     ] define-syntax
209
210     "ERROR:" [
211         parse-tuple-definition
212         pick save-location
213         define-error-class
214     ] define-syntax
215
216     "FORGET:" [
217         scan-object forget
218     ] define-syntax
219
220     "(" [
221         ")" parse-effect
222         word dup [ set-stack-effect ] [ 2drop ] if
223     ] define-syntax
224
225     "((" [
226         "))" parse-effect parsed
227     ] define-syntax
228
229     "MAIN:" [ scan-word in get vocab (>>main) ] define-syntax
230
231     "<<" [
232         [
233             \ >> parse-until >quotation
234         ] with-nested-compilation-unit call
235     ] define-syntax
236
237     "call-next-method" [
238         current-method get [
239             literalize parsed
240             \ (call-next-method) parsed
241         ] [
242             not-in-a-method-error
243         ] if*
244     ] define-syntax
245     
246     "initial:" "syntax" lookup define-symbol
247     
248     "read-only" "syntax" lookup define-symbol
249 ] with-compilation-unit