]> gitweb.factorcode.org Git - factor.git/blob - core/syntax/syntax.factor
Merge branch 'master' of git://factorcode.org/git/factor into bags
[factor.git] / core / syntax / syntax.factor
1 ! Copyright (C) 2004, 2009 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: accessors alien arrays byte-arrays byte-vectors 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.hook 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 hash-sets ;
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-core-syntax ( name quot -- )
26     [ dup "syntax" lookup [ ] [ no-word-error ] ?if ] dip
27     define-syntax ;
28
29 [
30     { "]" "}" ";" ">>" } [ define-delimiter ] each
31
32     "PRIMITIVE:" [
33         "Primitive definition is not supported" throw
34     ] define-core-syntax
35
36     "CS{" [
37         "Call stack literals are not supported" throw
38     ] define-core-syntax
39
40     "!" [ lexer get next-line ] define-core-syntax
41
42     "#!" [ POSTPONE: ! ] define-core-syntax
43
44     "IN:" [ scan 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 use-vocab ] define-core-syntax
51
52     "UNUSE:" [ scan unuse-vocab ] define-core-syntax
53
54     "USING:" [ ";" [ use-vocab ] each-token ] define-core-syntax
55
56     "QUALIFIED:" [ scan dup add-qualified ] define-core-syntax
57
58     "QUALIFIED-WITH:" [ scan scan add-qualified ] define-core-syntax
59
60     "FROM:" [
61         scan "=>" expect ";" parse-tokens add-words-from
62     ] define-core-syntax
63
64     "EXCLUDE:" [
65         scan "=>" expect ";" parse-tokens add-words-excluding
66     ] define-core-syntax
67
68     "RENAME:" [
69         scan scan "=>" expect scan add-renamed-word
70     ] define-core-syntax
71
72     "HEX:" [ 16 parse-base ] define-core-syntax
73     "OCT:" [ 8 parse-base ] define-core-syntax
74     "BIN:" [ 2 parse-base ] define-core-syntax
75
76     "NAN:" [ 16 scan-base <fp-nan> suffix! ] define-core-syntax
77
78     "f" [ f suffix! ] define-core-syntax
79     "t" "syntax" lookup define-singleton-class
80
81     "CHAR:" [
82         scan {
83             { [ dup length 1 = ] [ first ] }
84             { [ "\\" ?head ] [ next-escape >string "" assert= ] }
85             [ name>char-hook get call( name -- char ) ]
86         } cond suffix!
87     ] define-core-syntax
88
89     "\"" [ parse-multiline-string suffix! ] define-core-syntax
90
91     "SBUF\"" [
92         lexer get skip-blank parse-string >sbuf suffix!
93     ] define-core-syntax
94
95     "P\"" [
96         lexer get skip-blank parse-string <pathname> suffix!
97     ] define-core-syntax
98
99     "[" [ parse-quotation suffix! ] define-core-syntax
100     "{" [ \ } [ >array ] parse-literal ] define-core-syntax
101     "V{" [ \ } [ >vector ] parse-literal ] define-core-syntax
102     "B{" [ \ } [ >byte-array ] parse-literal ] define-core-syntax
103     "BV{" [ \ } [ >byte-vector ] parse-literal ] define-core-syntax
104     "H{" [ \ } [ >hashtable ] parse-literal ] define-core-syntax
105     "T{" [ parse-tuple-literal suffix! ] define-core-syntax
106     "W{" [ \ } [ first <wrapper> ] parse-literal ] define-core-syntax
107     "HS{" [ \ } [ <hash-set> ] parse-literal ] define-core-syntax
108
109     "POSTPONE:" [ scan-word suffix! ] define-core-syntax
110     "\\" [ scan-word <wrapper> suffix! ] define-core-syntax
111     "M\\" [ scan-word scan-word method <wrapper> suffix! ] define-core-syntax
112     "inline" [ word make-inline ] define-core-syntax
113     "recursive" [ word make-recursive ] define-core-syntax
114     "foldable" [ word make-foldable ] define-core-syntax
115     "flushable" [ word make-flushable ] define-core-syntax
116     "delimiter" [ word t "delimiter" set-word-prop ] define-core-syntax
117     "deprecated" [ word make-deprecated ] define-core-syntax
118
119     "SYNTAX:" [
120         CREATE-WORD parse-definition define-syntax
121     ] define-core-syntax
122
123     "SYMBOL:" [
124         CREATE-WORD define-symbol
125     ] define-core-syntax
126
127     "SYMBOLS:" [
128         ";" [ create-in dup reset-generic define-symbol ] each-token
129     ] define-core-syntax
130
131     "SINGLETONS:" [
132         ";" [ create-class-in define-singleton-class ] each-token
133     ] define-core-syntax
134
135     "DEFER:" [
136         scan current-vocab create
137         [ fake-definition ] [ set-word ] [ undefined-def define ] tri
138     ] define-core-syntax
139     
140     "ALIAS:" [
141         CREATE-WORD scan-word define-alias
142     ] define-core-syntax
143
144     "CONSTANT:" [
145         CREATE-WORD scan-object define-constant
146     ] define-core-syntax
147
148     ":" [
149         (:) define-declared
150     ] define-core-syntax
151
152     "GENERIC:" [
153         [ simple-combination ] (GENERIC:)
154     ] define-core-syntax
155
156     "GENERIC#" [
157         [ scan-word <standard-combination> ] (GENERIC:)
158     ] define-core-syntax
159
160     "MATH:" [
161         [ math-combination ] (GENERIC:)
162     ] define-core-syntax
163
164     "HOOK:" [
165         [ scan-word <hook-combination> ] (GENERIC:)
166     ] define-core-syntax
167
168     "M:" [
169         (M:) define
170     ] define-core-syntax
171
172     "UNION:" [
173         CREATE-CLASS parse-definition define-union-class
174     ] define-core-syntax
175
176     "INTERSECTION:" [
177         CREATE-CLASS parse-definition define-intersection-class
178     ] define-core-syntax
179
180     "MIXIN:" [
181         CREATE-CLASS define-mixin-class
182     ] define-core-syntax
183
184     "INSTANCE:" [
185         location [
186             scan-word scan-word 2dup add-mixin-instance
187             <mixin-instance>
188         ] dip remember-definition
189     ] define-core-syntax
190
191     "PREDICATE:" [
192         CREATE-CLASS
193         scan "<" assert=
194         scan-word
195         parse-definition define-predicate-class
196     ] define-core-syntax
197
198     "SINGLETON:" [
199         CREATE-CLASS define-singleton-class
200     ] define-core-syntax
201
202     "TUPLE:" [
203         parse-tuple-definition define-tuple-class
204     ] define-core-syntax
205
206     "final" [
207         word make-final
208     ] define-core-syntax
209
210     "SLOT:" [
211         scan define-protocol-slot
212     ] define-core-syntax
213
214     "C:" [
215         CREATE-WORD scan-word define-boa-word
216     ] define-core-syntax
217
218     "ERROR:" [
219         parse-tuple-definition
220         pick save-location
221         define-error-class
222     ] define-core-syntax
223
224     "FORGET:" [
225         scan-object forget
226     ] define-core-syntax
227
228     "(" [
229         ")" parse-effect drop
230     ] define-core-syntax
231
232     "((" [
233         "))" parse-effect suffix!
234     ] define-core-syntax
235
236     "MAIN:" [ scan-word current-vocab (>>main) ] define-core-syntax
237
238     "<<" [
239         [
240             \ >> parse-until >quotation
241         ] with-nested-compilation-unit call( -- )
242     ] define-core-syntax
243
244     "call-next-method" [
245         current-method get [
246             literalize suffix!
247             \ (call-next-method) suffix!
248         ] [
249             not-in-a-method-error
250         ] if*
251     ] define-core-syntax
252     
253     "initial:" "syntax" lookup define-symbol
254
255     "read-only" "syntax" lookup define-symbol
256
257     "call(" [ \ call-effect parse-call( ] define-core-syntax
258
259     "execute(" [ \ execute-effect parse-call( ] define-core-syntax
260 ] with-compilation-unit