]> gitweb.factorcode.org Git - factor.git/blob - core/syntax/syntax.factor
4d4b81d00ea360688cea2d22ef3904658e53c0ef
[factor.git] / core / syntax / syntax.factor
1 ! Copyright (C) 2004, 2008 Slava Pestov.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien arrays bit-arrays byte-arrays byte-vectors
4 definitions generic hashtables kernel math namespaces parser
5 lexer sequences strings strings.parser sbufs vectors
6 words quotations io assocs splitting classes.tuple
7 generic.standard generic.math generic.parser classes io.files
8 vocabs float-arrays classes.parser classes.union
9 classes.intersection classes.mixin classes.predicate
10 classes.singleton classes.tuple.parser compiler.units
11 combinators debugger effects.parser ;
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-syntax ( name quot -- )
26     >r "syntax" lookup dup r> define t "parsing" set-word-prop ;
27
28 [
29     { "]" "}" ";" ">>" } [ define-delimiter ] each
30
31     "PRIMITIVE:" [
32         "Primitive definition is not supported" throw
33     ] define-syntax
34
35     "CS{" [
36         "Call stack literals are not supported" throw
37     ] define-syntax
38
39     "!" [ lexer get next-line ] define-syntax
40
41     "#!" [ POSTPONE: ! ] define-syntax
42
43     "IN:" [ scan set-in ] define-syntax
44
45     "PRIVATE>" [ in get ".private" ?tail drop set-in ] define-syntax
46
47     "<PRIVATE" [
48         POSTPONE: PRIVATE> in get ".private" append set-in
49     ] define-syntax
50
51     "USE:" [ scan use+ ] define-syntax
52
53     "USING:" [ ";" parse-tokens add-use ] define-syntax
54
55     "HEX:" [ 16 parse-base ] define-syntax
56     "OCT:" [ 8 parse-base ] define-syntax
57     "BIN:" [ 2 parse-base ] define-syntax
58
59     "f" [ f parsed ] define-syntax
60     "t" "syntax" lookup define-singleton-class
61
62     "CHAR:" [
63         scan {
64             { [ dup length 1 = ] [ first ] }
65             { [ "\\" ?head ] [ next-escape drop ] }
66             [ name>char-hook get call ]
67         } cond parsed
68     ] define-syntax
69
70     "\"" [ parse-string parsed ] define-syntax
71
72     "SBUF\"" [
73         lexer get skip-blank parse-string >sbuf parsed
74     ] define-syntax
75
76     "P\"" [
77         lexer get skip-blank parse-string <pathname> parsed
78     ] define-syntax
79
80     "[" [ \ ] [ >quotation ] parse-literal ] define-syntax
81     "{" [ \ } [ >array ] parse-literal ] define-syntax
82     "V{" [ \ } [ >vector ] parse-literal ] define-syntax
83     "B{" [ \ } [ >byte-array ] parse-literal ] define-syntax
84     "BV{" [ \ } [ >byte-vector ] parse-literal ] define-syntax
85     "?{" [ \ } [ >bit-array ] parse-literal ] define-syntax
86     "F{" [ \ } [ >float-array ] parse-literal ] define-syntax
87     "H{" [ \ } [ >hashtable ] parse-literal ] define-syntax
88     "T{" [ \ } [ >tuple ] parse-literal ] define-syntax
89     "W{" [ \ } [ first <wrapper> ] parse-literal ] define-syntax
90
91     "POSTPONE:" [ scan-word parsed ] define-syntax
92     "\\" [ scan-word literalize parsed ] define-syntax
93     "inline" [ word make-inline ] define-syntax
94     "foldable" [ word make-foldable ] define-syntax
95     "flushable" [ word make-flushable ] define-syntax
96     "delimiter" [ word t "delimiter" set-word-prop ] define-syntax
97     "parsing" [ word t "parsing" set-word-prop ] define-syntax
98
99     "SYMBOL:" [
100         CREATE-WORD define-symbol
101     ] define-syntax
102
103     "DEFER:" [
104         scan current-vocab create
105         dup old-definitions get [ delete-at ] with each
106         set-word
107     ] define-syntax
108
109     ":" [
110         (:) define
111     ] define-syntax
112
113     "GENERIC:" [
114         CREATE-GENERIC define-simple-generic
115     ] define-syntax
116
117     "GENERIC#" [
118         CREATE-GENERIC
119         scan-word <standard-combination> define-generic
120     ] define-syntax
121
122     "MATH:" [
123         CREATE-GENERIC
124         T{ math-combination } define-generic
125     ] define-syntax
126
127     "HOOK:" [
128         CREATE-GENERIC scan-word
129         <hook-combination> define-generic
130     ] define-syntax
131
132     "M:" [
133         (M:) define
134     ] define-syntax
135
136     "UNION:" [
137         CREATE-CLASS parse-definition define-union-class
138     ] define-syntax
139
140     "INTERSECTION:" [
141         CREATE-CLASS parse-definition define-intersection-class
142     ] define-syntax
143
144     "MIXIN:" [
145         CREATE-CLASS define-mixin-class
146     ] define-syntax
147
148     "INSTANCE:" [
149         location >r
150         scan-word scan-word 2dup add-mixin-instance
151         <mixin-instance> r> remember-definition
152     ] define-syntax
153
154     "PREDICATE:" [
155         CREATE-CLASS
156         scan "<" assert=
157         scan-word
158         parse-definition define-predicate-class
159     ] define-syntax
160
161     "SINGLETON:" [
162         CREATE-CLASS define-singleton-class
163     ] define-syntax
164
165     "TUPLE:" [
166         parse-tuple-definition define-tuple-class
167     ] define-syntax
168
169     "C:" [
170         CREATE-WORD
171         scan-word check-tuple-class
172         [ boa ] curry define-inline
173     ] define-syntax
174
175     "ERROR:" [
176         parse-tuple-definition
177         pick save-location
178         define-error-class
179     ] define-syntax
180
181     "FORGET:" [
182         scan-object forget
183     ] define-syntax
184
185     "(" [
186         ")" parse-effect
187         word dup [ set-stack-effect ] [ 2drop ] if
188     ] define-syntax
189
190     "((" [
191         "))" parse-effect parsed
192     ] define-syntax
193
194     "MAIN:" [ scan-word in get vocab set-vocab-main ] define-syntax
195
196     "<<" [
197         [
198             \ >> parse-until >quotation
199         ] with-nested-compilation-unit call
200     ] define-syntax
201
202     "call-next-method" [
203         current-class get current-generic get
204         2dup [ word? ] both? [
205             [ literalize parsed ] bi@
206             \ (call-next-method) parsed
207         ] [
208             not-in-a-method-error
209         ] if
210     ] define-syntax
211 ] with-compilation-unit