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