]> gitweb.factorcode.org Git - factor.git/blob - core/syntax/syntax.factor
7a181425b2b2a0797c333afb38048fc436d1b501
[factor.git] / core / syntax / syntax.factor
1 ! Copyright (C) 2004, 2010 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors arrays byte-arrays byte-vectors classes
4 classes.algebra.private classes.builtin classes.error
5 classes.intersection classes.maybe classes.mixin classes.parser
6 classes.predicate classes.singleton classes.tuple
7 classes.tuple.parser classes.union combinators compiler.units
8 definitions effects effects.parser fry generic generic.hook
9 generic.math generic.parser generic.standard hash-sets
10 hashtables hashtables.identity io.pathnames kernel lexer
11 locals.errors locals.parser macros math memoize namespaces
12 parser quotations sbufs sequences slots source-files splitting
13 strings strings.parser vectors vocabs.parser words words.alias
14 words.constant words.symbol ;
15 IN: bootstrap.syntax
16
17 ! These words are defined as a top-level form, instead of with
18 ! defining parsing words, because during stage1 bootstrap, the
19 ! "syntax" vocabulary is copied from the host. When stage1
20 ! bootstrap completes, the host's syntax vocabulary is deleted
21 ! from the target, then this top-level form creates the
22 ! target's "syntax" vocabulary as one of the first things done
23 ! in stage2.
24
25 : define-delimiter ( name -- )
26     "syntax" lookup-word t "delimiter" set-word-prop ;
27
28 : define-core-syntax ( name quot -- )
29     [ dup "syntax" lookup-word [ ] [ no-word-error ] ?if ] dip
30     define-syntax ;
31
32 [
33     { "]" "}" ";" ">>" } [ define-delimiter ] each
34
35     "PRIMITIVE:" [
36         current-vocab name>>
37         scan-word scan-effect ensure-primitive
38     ] define-core-syntax
39
40     "CS{" [
41         "Call stack literals are not supported" throw
42     ] define-core-syntax
43
44     "IN:" [ scan-token set-current-vocab ] define-core-syntax
45
46     "<PRIVATE" [ begin-private ] define-core-syntax
47
48     "PRIVATE>" [ end-private ] define-core-syntax
49
50     "USE:" [ scan-token use-vocab ] define-core-syntax
51
52     "UNUSE:" [ scan-token unuse-vocab ] define-core-syntax
53
54     "USING:" [ ";" [ use-vocab ] each-token ] define-core-syntax
55
56     "QUALIFIED:" [ scan-token dup add-qualified ] define-core-syntax
57
58     "QUALIFIED-WITH:" [ scan-token scan-token add-qualified ] define-core-syntax
59
60     "FROM:" [
61         scan-token "=>" expect ";" parse-tokens add-words-from
62     ] define-core-syntax
63
64     "EXCLUDE:" [
65         scan-token "=>" expect ";" parse-tokens add-words-excluding
66     ] define-core-syntax
67
68     "RENAME:" [
69         scan-token scan-token "=>" expect scan-token add-renamed-word
70     ] define-core-syntax
71
72     "NAN:" [ 16 scan-base <fp-nan> suffix! ] define-core-syntax
73
74     "f" [ f suffix! ] define-core-syntax
75
76     "CHAR:" [
77         lexer get parse-raw [ "token" throw-unexpected-eof ] unless* {
78             { [ dup length 1 = ] [ first ] }
79             { [ "\\" ?head ] [ next-escape >string "" assert= ] }
80             [ name>char-hook get call( name -- char ) ]
81         } cond suffix!
82     ] define-core-syntax
83
84     "\"" [ parse-string suffix! ] define-core-syntax
85
86     "SBUF\"" [
87         lexer get skip-blank parse-string >sbuf suffix!
88     ] define-core-syntax
89
90     "P\"" [
91         lexer get skip-blank parse-string <pathname> suffix!
92     ] define-core-syntax
93
94     "[" [ parse-quotation suffix! ] define-core-syntax
95     "{" [ \ } [ >array ] parse-literal ] define-core-syntax
96     "V{" [ \ } [ >vector ] parse-literal ] define-core-syntax
97     "B{" [ \ } [ >byte-array ] parse-literal ] define-core-syntax
98     "BV{" [ \ } [ >byte-vector ] parse-literal ] define-core-syntax
99     "H{" [ \ } [ parse-hashtable ] parse-literal ] define-core-syntax
100     "IH{" [ \ } [ >identity-hashtable ] parse-literal ] define-core-syntax
101     "T{" [ parse-tuple-literal suffix! ] define-core-syntax
102     "W{" [ \ } [ first <wrapper> ] parse-literal ] define-core-syntax
103     "HS{" [ \ } [ >hash-set ] parse-literal ] define-core-syntax
104
105     "POSTPONE:" [ scan-word suffix! ] define-core-syntax
106     "\\" [ scan-word <wrapper> suffix! ] define-core-syntax
107     "M\\" [ scan-word scan-word lookup-method <wrapper> suffix! ] define-core-syntax
108     "auto-use" [ auto-use? on ] define-core-syntax
109     "delimiter" [ last-word t "delimiter" set-word-prop ] define-core-syntax
110     "deprecated" [ last-word make-deprecated ] define-core-syntax
111     "flushable" [ last-word make-flushable ] define-core-syntax
112     "foldable" [ last-word make-foldable ] define-core-syntax
113     "inline" [ last-word make-inline ] define-core-syntax
114     "recursive" [ last-word make-recursive ] define-core-syntax
115
116     "SYNTAX:" [
117         scan-new-word parse-definition define-syntax
118     ] define-core-syntax
119
120     "BUILTIN:" [
121         scan-word-name
122         current-vocab lookup-word
123         (parse-tuple-definition)
124         2drop builtin-class check-instance drop
125     ] define-core-syntax
126
127     "SYMBOL:" [
128         scan-new-word define-symbol
129     ] define-core-syntax
130
131     "SYMBOLS:" [
132         ";" [ create-word-in [ reset-generic ] [ define-symbol ] bi ] each-token
133     ] define-core-syntax
134
135     "SINGLETONS:" [
136         ";" [ create-class-in define-singleton-class ] each-token
137     ] define-core-syntax
138
139     "DEFER:" [
140         scan-token current-vocab create-word
141         [ fake-definition ] [ set-last-word ] [ undefined-def define ] tri
142     ] define-core-syntax
143
144     "ALIAS:" [
145         scan-new-word scan-word define-alias
146     ] define-core-syntax
147
148     "CONSTANT:" [
149         scan-new-word scan-object define-constant
150     ] define-core-syntax
151
152     ":" [
153         (:) define-declared
154     ] define-core-syntax
155
156     "GENERIC:" [
157         [ simple-combination ] (GENERIC:)
158     ] define-core-syntax
159
160     "GENERIC#:" [
161         [ scan-number <standard-combination> ] (GENERIC:)
162     ] define-core-syntax
163
164     "MATH:" [
165         [ math-combination ] (GENERIC:)
166     ] define-core-syntax
167
168     "HOOK:" [
169         [ scan-word <hook-combination> ] (GENERIC:)
170     ] define-core-syntax
171
172     "M:" [
173         (M:) define
174     ] define-core-syntax
175
176     "UNION:" [
177         scan-new-class parse-array-def define-union-class
178     ] define-core-syntax
179
180     "INTERSECTION:" [
181         scan-new-class parse-array-def define-intersection-class
182     ] define-core-syntax
183
184     "MIXIN:" [
185         scan-new-class define-mixin-class
186     ] define-core-syntax
187
188     "INSTANCE:" [
189         location [
190             scan-word scan-word 2dup add-mixin-instance
191             <mixin-instance>
192         ] dip remember-definition
193     ] define-core-syntax
194
195     "PREDICATE:" [
196         scan-new-class
197         "<" expect
198         scan-class
199         parse-definition define-predicate-class
200     ] define-core-syntax
201
202     "SINGLETON:" [
203         scan-new-class define-singleton-class
204     ] define-core-syntax
205
206     "TUPLE:" [
207         parse-tuple-definition define-tuple-class
208     ] define-core-syntax
209
210     "final" [
211         last-word make-final
212     ] define-core-syntax
213
214     "SLOT:" [
215         scan-token define-protocol-slot
216     ] define-core-syntax
217
218     "C:" [
219         scan-new-word scan-word define-boa-word
220     ] define-core-syntax
221
222     "ERROR:" [
223         parse-tuple-definition
224         pick save-location
225         define-error-class
226     ] define-core-syntax
227
228     "FORGET:" [
229         scan-object forget
230     ] define-core-syntax
231
232     "(" [
233         ")" parse-effect suffix!
234     ] define-core-syntax
235
236     "MAIN:" [
237         scan-word dup \ [ = [
238             drop "( main )" <uninterned-word> dup
239             parse-quotation ( -- ) define-declared
240         ] when dup ( -- ) check-stack-effect
241         [ current-vocab main<< ]
242         [ current-source-file get [ main<< ] [ drop ] if* ] bi
243     ] define-core-syntax
244
245     "<<" [
246         [
247             \ >> parse-until >quotation
248         ] with-nested-compilation-unit call( -- )
249     ] define-core-syntax
250
251     "call-next-method" [
252         current-method get [
253             literalize suffix!
254             \ (call-next-method) suffix!
255         ] [
256             not-in-a-method-error
257         ] if*
258     ] define-core-syntax
259
260     "maybe{" [
261         \ } [ <anonymous-union> <maybe> ] parse-literal
262     ] define-core-syntax
263
264     "not{" [
265         \ } [ <anonymous-union> <anonymous-complement> ] parse-literal
266     ] define-core-syntax
267
268     "intersection{" [
269          \ } [ <anonymous-intersection> ] parse-literal
270     ] define-core-syntax
271
272     "union{" [
273         \ } [ <anonymous-union> ] parse-literal
274     ] define-core-syntax
275
276     "initial:" "syntax" lookup-word define-symbol
277
278     "read-only" "syntax" lookup-word define-symbol
279
280     "call(" [ \ call-effect parse-call-paren ] define-core-syntax
281
282     "execute(" [ \ execute-effect parse-call-paren ] define-core-syntax
283
284     "<<<<<<<" [ version-control-merge-conflict ] define-core-syntax
285     "=======" [ version-control-merge-conflict ] define-core-syntax
286     ">>>>>>>" [ version-control-merge-conflict ] define-core-syntax
287
288     "<<<<<<" [ version-control-merge-conflict ] define-core-syntax
289     "======" [ version-control-merge-conflict ] define-core-syntax
290     ">>>>>>" [ version-control-merge-conflict ] define-core-syntax
291
292     "'[" [
293          t in-fry? [ parse-quotation ] with-variable fry append!
294     ] define-core-syntax
295
296     "'{" [
297          t in-fry? [ \ } parse-until >array ] with-variable fry append!
298     ] define-core-syntax
299
300     "'HS{" [
301          t in-fry? [ \ } parse-until >array ] with-variable fry
302          [ >hash-set ] compose append!
303     ] define-core-syntax
304
305     "'H{" [
306          t in-fry? [ \ } parse-until >array ] with-variable fry
307          [ parse-hashtable ] compose append!
308     ] define-core-syntax
309
310     "_" [
311         in-fry? get [ \ _ suffix! ] [ not-in-a-fry ] if
312     ] define-core-syntax
313
314     "@" [
315         in-fry? get [ \ @ suffix! ] [ not-in-a-fry ] if
316     ] define-core-syntax
317
318     "MACRO:" [ (:) define-macro ] define-core-syntax
319
320     "MEMO:" [ (:) define-memoized ] define-core-syntax
321     "IDENTITY-MEMO:" [ (:) define-identity-memoized ] define-core-syntax
322
323     ":>" [
324         in-lambda? get [ :>-outside-lambda-error ] unless
325         scan-token parse-def suffix!
326     ] define-core-syntax
327     "[|" [ parse-lambda append! ] define-core-syntax
328     "[let" [ parse-let append! ] define-core-syntax
329
330     "::" [ (::) define-declared ] define-core-syntax
331     "M::" [ (M::) define ] define-core-syntax
332     "MACRO::" [ (::) define-macro ] define-core-syntax
333     "MEMO::" [ (::) define-memoized ] define-core-syntax
334     "IDENTITY-MEMO::" [ (::) define-identity-memoized ] define-core-syntax
335 ] with-compilation-unit