]> gitweb.factorcode.org Git - factor.git/blob - core/syntax/syntax.factor
3512b92e4c21bfb922ad826f820852f8ec105945
[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 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     "H{" [ \ } [ >hashtable ] parse-literal ] define-core-syntax
102     "T{" [ parse-tuple-literal parsed ] define-core-syntax
103     "W{" [ \ } [ first <wrapper> ] parse-literal ] define-core-syntax
104
105     "POSTPONE:" [ scan-word parsed ] define-core-syntax
106     "\\" [ scan-word <wrapper> parsed ] define-core-syntax
107     "M\\" [ scan-word scan-word method <wrapper> parsed ] define-core-syntax
108     "inline" [ word make-inline ] define-core-syntax
109     "recursive" [ word make-recursive ] define-core-syntax
110     "foldable" [ word make-foldable ] define-core-syntax
111     "flushable" [ word make-flushable ] define-core-syntax
112     "delimiter" [ word t "delimiter" set-word-prop ] define-core-syntax
113
114     "SYNTAX:" [
115         CREATE-WORD parse-definition define-syntax
116     ] define-core-syntax
117
118     "SYMBOL:" [
119         CREATE-WORD define-symbol
120     ] define-core-syntax
121
122     "SYMBOLS:" [
123         ";" parse-tokens
124         [ create-in dup reset-generic define-symbol ] each
125     ] define-core-syntax
126
127     "SINGLETONS:" [
128         ";" parse-tokens
129         [ create-class-in define-singleton-class ] each
130     ] define-core-syntax
131
132     "DEFER:" [
133         scan current-vocab create
134         [ fake-definition ] [ set-word ] [ [ undefined ] define ] tri
135     ] define-core-syntax
136     
137     "ALIAS:" [
138         CREATE-WORD scan-word define-alias
139     ] define-core-syntax
140
141     "CONSTANT:" [
142         CREATE-WORD scan-object define-constant
143     ] define-core-syntax
144
145     ":" [
146         (:) define-declared
147     ] define-core-syntax
148
149     "GENERIC:" [
150         [ simple-combination ] (GENERIC:)
151     ] define-core-syntax
152
153     "GENERIC#" [
154         [ scan-word <standard-combination> ] (GENERIC:)
155     ] define-core-syntax
156
157     "MATH:" [
158         [ math-combination ] (GENERIC:)
159     ] define-core-syntax
160
161     "HOOK:" [
162         [ scan-word <hook-combination> ] (GENERIC:)
163     ] define-core-syntax
164
165     "M:" [
166         (M:) define
167     ] define-core-syntax
168
169     "UNION:" [
170         CREATE-CLASS parse-definition define-union-class
171     ] define-core-syntax
172
173     "INTERSECTION:" [
174         CREATE-CLASS parse-definition define-intersection-class
175     ] define-core-syntax
176
177     "MIXIN:" [
178         CREATE-CLASS define-mixin-class
179     ] define-core-syntax
180
181     "INSTANCE:" [
182         location [
183             scan-word scan-word 2dup add-mixin-instance
184             <mixin-instance>
185         ] dip remember-definition
186     ] define-core-syntax
187
188     "PREDICATE:" [
189         CREATE-CLASS
190         scan "<" assert=
191         scan-word
192         parse-definition define-predicate-class
193     ] define-core-syntax
194
195     "SINGLETON:" [
196         CREATE-CLASS define-singleton-class
197     ] define-core-syntax
198
199     "TUPLE:" [
200         parse-tuple-definition define-tuple-class
201     ] define-core-syntax
202
203     "SLOT:" [
204         scan define-protocol-slot
205     ] define-core-syntax
206
207     "C:" [
208         CREATE-WORD scan-word define-boa-word
209     ] define-core-syntax
210
211     "ERROR:" [
212         parse-tuple-definition
213         pick save-location
214         define-error-class
215     ] define-core-syntax
216
217     "FORGET:" [
218         scan-object forget
219     ] define-core-syntax
220
221     "(" [
222         ")" parse-effect drop
223     ] define-core-syntax
224
225     "((" [
226         "))" parse-effect parsed
227     ] define-core-syntax
228
229     "MAIN:" [ scan-word in get vocab (>>main) ] define-core-syntax
230
231     "<<" [
232         [
233             \ >> parse-until >quotation
234         ] with-nested-compilation-unit call( -- )
235     ] define-core-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-core-syntax
245     
246     "initial:" "syntax" lookup define-symbol
247     
248     "read-only" "syntax" lookup define-symbol
249
250     "call(" [ \ call-effect parse-call( ] define-core-syntax
251
252     "execute(" [ \ execute-effect parse-call( ] define-core-syntax
253 ] with-compilation-unit