]> gitweb.factorcode.org Git - factor.git/blob - core/syntax/syntax.factor
7d710717aaa93b4939c9af1d0a773b900a0ece18
[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 ;
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-in ] define-core-syntax
45
46     "PRIVATE>" [ in get ".private" ?tail drop set-in ] define-core-syntax
47
48     "<PRIVATE" [
49         POSTPONE: PRIVATE> in get ".private" append set-in
50     ] define-core-syntax
51
52     "USE:" [ scan use+ ] define-core-syntax
53
54     "USING:" [ ";" parse-tokens add-use ] 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 swap add-words-from
62     ] define-core-syntax
63
64     "EXCLUDE:" [
65         scan "=>" expect ";" parse-tokens swap 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     "f" [ f parsed ] define-core-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( name -- char ) ]
84         } cond parsed
85     ] define-core-syntax
86
87     "\"" [ parse-string parsed ] define-core-syntax
88
89     "SBUF\"" [
90         lexer get skip-blank parse-string >sbuf parsed
91     ] define-core-syntax
92
93     "P\"" [
94         lexer get skip-blank parse-string <pathname> parsed
95     ] define-core-syntax
96
97     "[" [ parse-quotation parsed ] define-core-syntax
98     "{" [ \ } [ >array ] parse-literal ] define-core-syntax
99     "V{" [ \ } [ >vector ] parse-literal ] define-core-syntax
100     "B{" [ \ } [ >byte-array ] parse-literal ] define-core-syntax
101     "BV{" [ \ } [ >byte-vector ] parse-literal ] define-core-syntax
102     "H{" [ \ } [ >hashtable ] parse-literal ] define-core-syntax
103     "T{" [ parse-tuple-literal parsed ] define-core-syntax
104     "W{" [ \ } [ first <wrapper> ] parse-literal ] define-core-syntax
105
106     "POSTPONE:" [ scan-word parsed ] define-core-syntax
107     "\\" [ scan-word <wrapper> parsed ] define-core-syntax
108     "M\\" [ scan-word scan-word method <wrapper> parsed ] define-core-syntax
109     "inline" [ word make-inline ] define-core-syntax
110     "recursive" [ word make-recursive ] define-core-syntax
111     "foldable" [ word make-foldable ] define-core-syntax
112     "flushable" [ word make-flushable ] define-core-syntax
113     "delimiter" [ word t "delimiter" set-word-prop ] define-core-syntax
114
115     "SYNTAX:" [
116         CREATE-WORD parse-definition define-syntax
117     ] define-core-syntax
118
119     "SYMBOL:" [
120         CREATE-WORD define-symbol
121     ] define-core-syntax
122
123     "SYMBOLS:" [
124         ";" parse-tokens
125         [ create-in dup reset-generic define-symbol ] each
126     ] define-core-syntax
127
128     "SINGLETONS:" [
129         ";" parse-tokens
130         [ create-class-in define-singleton-class ] each
131     ] define-core-syntax
132
133     "DEFER:" [
134         scan current-vocab create
135         [ fake-definition ] [ set-word ] [ [ undefined ] define ] tri
136     ] define-core-syntax
137     
138     "ALIAS:" [
139         CREATE-WORD scan-word define-alias
140     ] define-core-syntax
141
142     "CONSTANT:" [
143         CREATE-WORD scan-object define-constant
144     ] define-core-syntax
145
146     ":" [
147         (:) define-declared
148     ] define-core-syntax
149
150     "GENERIC:" [
151         [ simple-combination ] (GENERIC:)
152     ] define-core-syntax
153
154     "GENERIC#" [
155         [ scan-word <standard-combination> ] (GENERIC:)
156     ] define-core-syntax
157
158     "MATH:" [
159         [ math-combination ] (GENERIC:)
160     ] define-core-syntax
161
162     "HOOK:" [
163         [ scan-word <hook-combination> ] (GENERIC:)
164     ] define-core-syntax
165
166     "M:" [
167         (M:) define
168     ] define-core-syntax
169
170     "UNION:" [
171         CREATE-CLASS parse-definition define-union-class
172     ] define-core-syntax
173
174     "INTERSECTION:" [
175         CREATE-CLASS parse-definition define-intersection-class
176     ] define-core-syntax
177
178     "MIXIN:" [
179         CREATE-CLASS define-mixin-class
180     ] define-core-syntax
181
182     "INSTANCE:" [
183         location [
184             scan-word scan-word 2dup add-mixin-instance
185             <mixin-instance>
186         ] dip remember-definition
187     ] define-core-syntax
188
189     "PREDICATE:" [
190         CREATE-CLASS
191         scan "<" assert=
192         scan-word
193         parse-definition define-predicate-class
194     ] define-core-syntax
195
196     "SINGLETON:" [
197         CREATE-CLASS define-singleton-class
198     ] define-core-syntax
199
200     "TUPLE:" [
201         parse-tuple-definition define-tuple-class
202     ] define-core-syntax
203
204     "SLOT:" [
205         scan define-protocol-slot
206     ] define-core-syntax
207
208     "C:" [
209         CREATE-WORD scan-word define-boa-word
210     ] define-core-syntax
211
212     "ERROR:" [
213         parse-tuple-definition
214         pick save-location
215         define-error-class
216     ] define-core-syntax
217
218     "FORGET:" [
219         scan-object forget
220     ] define-core-syntax
221
222     "(" [
223         ")" parse-effect drop
224     ] define-core-syntax
225
226     "((" [
227         "))" parse-effect parsed
228     ] define-core-syntax
229
230     "MAIN:" [ scan-word in get vocab (>>main) ] define-core-syntax
231
232     "<<" [
233         [
234             \ >> parse-until >quotation
235         ] with-nested-compilation-unit call( -- )
236     ] define-core-syntax
237
238     "call-next-method" [
239         current-method get [
240             literalize parsed
241             \ (call-next-method) parsed
242         ] [
243             not-in-a-method-error
244         ] if*
245     ] define-core-syntax
246     
247     "initial:" "syntax" lookup define-symbol
248     
249     "read-only" "syntax" lookup define-symbol
250
251     "call(" [ \ call-effect parse-call( ] define-core-syntax
252
253     "execute(" [ \ execute-effect parse-call( ] define-core-syntax
254 ] with-compilation-unit