]> gitweb.factorcode.org Git - factor.git/blob - core/syntax/syntax.factor
eaf5ffea051bd2fe3953cc91b4a0ae0ddb8a6d1f
[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 bit-vectors byte-arrays
4 byte-vectors definitions generic hashtables kernel math
5 namespaces parser sequences strings sbufs vectors words
6 quotations io assocs splitting classes.tuple generic.standard
7 generic.math classes io.files vocabs float-arrays float-vectors
8 classes.union classes.mixin classes.predicate classes.singleton
9 compiler.units combinators debugger ;
10 IN: bootstrap.syntax
11
12 ! These words are defined as a top-level form, instead of with
13 ! defining parsing words, because during stage1 bootstrap, the
14 ! "syntax" vocabulary is copied from the host. When stage1
15 ! bootstrap completes, the host's syntax vocabulary is deleted
16 ! from the target, then this top-level form creates the
17 ! target's "syntax" vocabulary as one of the first things done
18 ! in stage2.
19
20 : define-delimiter ( name -- )
21     "syntax" lookup t "delimiter" set-word-prop ;
22
23 : define-syntax ( name quot -- )
24     >r "syntax" lookup dup r> define t "parsing" set-word-prop ;
25
26 [
27     { "]" "}" ";" ">>" } [ define-delimiter ] each
28
29     "PRIMITIVE:" [
30         "Primitive definition is not supported" throw
31     ] define-syntax
32
33     "CS{" [
34         "Call stack literals are not supported" throw
35     ] define-syntax
36
37     "!" [ lexer get next-line ] define-syntax
38
39     "#!" [ POSTPONE: ! ] define-syntax
40
41     "IN:" [ scan set-in ] define-syntax
42
43     "PRIVATE>" [ in get ".private" ?tail drop set-in ] define-syntax
44
45     "<PRIVATE" [
46         POSTPONE: PRIVATE> in get ".private" append set-in
47     ] define-syntax
48
49     "USE:" [ scan use+ ] define-syntax
50
51     "USING:" [ ";" parse-tokens add-use ] define-syntax
52
53     "HEX:" [ 16 parse-base ] define-syntax
54     "OCT:" [ 8 parse-base ] define-syntax
55     "BIN:" [ 2 parse-base ] define-syntax
56
57     "f" [ f parsed ] define-syntax
58     "t" "syntax" lookup define-singleton-class
59
60     "CHAR:" [
61         scan {
62             { [ dup length 1 = ] [ first ] }
63             { [ "\\" ?head ] [ next-escape drop ] }
64             [ name>char-hook get call ]
65         } cond parsed
66     ] define-syntax
67
68     "\"" [ parse-string parsed ] define-syntax
69
70     "SBUF\"" [
71         lexer get skip-blank parse-string >sbuf parsed
72     ] define-syntax
73
74     "P\"" [
75         lexer get skip-blank parse-string <pathname> parsed
76     ] define-syntax
77
78     "[" [ \ ] [ >quotation ] parse-literal ] define-syntax
79     "{" [ \ } [ >array ] parse-literal ] define-syntax
80     "V{" [ \ } [ >vector ] parse-literal ] define-syntax
81     "B{" [ \ } [ >byte-array ] parse-literal ] define-syntax
82     "BV{" [ \ } [ >byte-vector ] parse-literal ] define-syntax
83     "?{" [ \ } [ >bit-array ] parse-literal ] define-syntax
84     "?V{" [ \ } [ >bit-vector ] parse-literal ] define-syntax
85     "F{" [ \ } [ >float-array ] parse-literal ] define-syntax
86     "FV{" [ \ } [ >float-vector ] 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 in get create
105         dup old-definitions get first delete-at
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     "MIXIN:" [
141         CREATE-CLASS define-mixin-class
142     ] define-syntax
143
144     "INSTANCE:" [
145         location >r
146         scan-word scan-word 2dup add-mixin-instance
147         <mixin-instance> r> remember-definition
148     ] define-syntax
149
150     "PREDICATE:" [
151         CREATE-CLASS
152         scan "<" assert=
153         scan-word
154         parse-definition define-predicate-class
155     ] define-syntax
156
157     "SINGLETON:" [
158         scan create-class-in
159         dup save-location define-singleton-class
160     ] define-syntax
161
162     "TUPLE:" [
163         parse-tuple-definition define-tuple-class
164     ] define-syntax
165
166     "C:" [
167         CREATE-WORD
168         scan-word dup check-tuple
169         [ boa ] curry define-inline
170     ] define-syntax
171
172     "ERROR:" [
173         parse-tuple-definition
174         pick save-location
175         define-error-class
176     ] define-syntax
177
178     "FORGET:" [
179         scan-object forget
180     ] define-syntax
181
182     "(" [
183         parse-effect word
184         [ swap "declared-effect" set-word-prop ] [ drop ] if*
185     ] define-syntax
186
187     "MAIN:" [ scan-word in get vocab set-vocab-main ] define-syntax
188
189     "<<" [
190         [ \ >> parse-until >quotation ] with-compilation-unit
191         call
192     ] define-syntax
193
194     "call-next-method" [
195         current-class get literalize parsed
196         current-generic get literalize parsed
197         \ (call-next-method) parsed
198     ] define-syntax
199 ] with-compilation-unit