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