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