]> gitweb.factorcode.org Git - factor.git/blob - misc/syntax-test.factor
misc: more syntax tests.
[factor.git] / misc / syntax-test.factor
1 #!/usr/bin/env foo
2
3 ! Comments
4
5     ! Normal comments
6     ! More comments
7
8     /* C
9     style 
10     comments */
11
12     /* comment */
13     /* multline ( x -- y )
14       2  comment */
15      6 /* something else */ 2 +
16
17 ! Imports
18
19     USING: vocabularies ... ;
20     USE: vocabulary
21     UNUSE: vocabulary
22     IN: vocabulary
23     FROM: vocab => words ... ;
24     EXCLUDE: vocab => words ... ;
25     QUALIFIED: vocab
26     QUALIFIED-WITH: vocab word-prefix
27     RENAME: word vocab => new-name
28     ALIAS: new-word existing-word
29     DEFER: word
30     FORGET: word
31     POSTPONE: word
32     SLOT: name
33
34 ! Classes
35
36     MIXIN: class
37     TUPLE: class slots ... ;
38     TUPLE: class < superclass slots ... ;
39     BUILTIN: class slots ... ;
40     INSTANCE: instance mixin
41     SINGLETON: class
42     SINGLETONS: words ... ;
43     PREDICATE: class < superclass predicate... ;
44
45 ! Examples
46
47     TUPLE: interval-map { array array read-only } ;
48     BUILTIN: string { length array-capacity read-only initial: 0 } aux ;
49
50 ! Definitions
51
52     : word ( x -- ) drop ;
53     : word error drop ;
54     :: word ( x -- ) x drop ;
55     TYPED: word ( a b: class ... -- x: class y ... ) body ;
56     TYPED:: word ( a b: class ... -- x: class y ... ) body ;
57     MACRO: word ( inputs... -- ) definition... ) ;
58     MACRO:: word ( vars... -- outputs... ) definition... ) ;
59     M: object explain drop "an object" print ;
60     M: class generic definition... ;
61     M:: class generic ( vars... -- outputs... ) body... ;
62     M:: class generic error body... ;
63     GENERIC: word ( stack -- effect )
64     GENERIC: word
65         ( stack -- effect )
66     GENERIC: word
67 ( stack -- effect )
68     GENERIC: word ! comment
69         ( stack -- effect )
70     GENERIC: word drop ! 3rd token wrong
71     GENERIC: word ! next line wrong
72         drop
73     GENERIC: word
74 drop ! wrong
75     HOOK: word variable ( stack -- effect )
76     GENERIC#: word 1 ( stack -- effect )
77     GENERIC#: ! comment
78         word 1 ( stack -- effect )
79     GENERIC#: word 1 ( stack -- effect ) drop ! last token other
80     GENERIC#: word ! 2 should GENERIC# stack effect error
81         1 2 ( stack -- effect )
82     GENERIC#: word ! 2nd eff. should be independent of GENERIC#,
83         1 ! and 2 & 3 shouldn't GENERIC# highlight
84         ( stack -- effect ) ( independent -- effect ) 2 3
85     GENERIC#: word 1 ! comment
86         drop ! wrong
87     MATH: + ( x y -- z ) foldable flushable
88     C: <foo> foo
89     CONSTRUCTOR: <circle> circle ( radius -- obj ) ;
90
91 ! Private definitions
92
93 <PRIVATE
94
95     : word ( x -- ) drop ;
96     :: word ( x -- ) x drop ;
97     TYPED: word ( a b: class ... -- x: class y ... ) body ;
98     TYPED:: word ( a b: class ... -- x: class y ... ) body ;
99     MACRO: word ( inputs... -- ) definition... ) ;
100     MACRO:: word ( vars... -- outputs... ) definition... ) ;
101     M: class generic definition... ;
102     M:: class generic ( vars... -- outputs... ) body... ;
103     GENERIC: word ( stack -- effect )
104     HOOK: word variable ( stack -- effect )
105     GENERIC#: word 1 ( stack -- effect )
106     MATH: + ( x y -- z ) foldable flushable
107     C: <foo> foo
108
109 PRIVATE>
110
111 ! Alien
112
113     LIBRARY: name
114     TYPEDEF: old new
115     ENUM: type words... ;
116     ENUM: type < base-type words...
117     FUNCTION: return name ( parameters ) ;
118     FUNCTION-ALIAS: factor-name return name ( parameters ) ;
119
120 ! Symbols and literals
121
122     \ foo
123     $ foo
124     M\ foo bar
125
126     MAIN: word
127     CONSTANT: word value
128     SYMBOL: word
129     SYMBOLS: words ... ;
130
131 ! Math
132
133     1 2 +
134     3 4 -
135     5 6 *
136     7 8 /
137     32 2^
138     10 10^
139
140 ! Examples
141
142     [ 1 ] unless*
143     >boolean
144     <wrapper>
145     +@
146     H{ } assoc-empty?
147     5 >bignum
148     1 2 pick set-nth
149     5 f <array>
150     (clone)
151
152 ! Strings
153
154     ""
155     "test"
156     SBUF" foo"
157     SBUF" hello world "
158     "\s"
159     "\\foo"
160     "\"hello\""
161     "\a\b\e\f\n\r\t\s\v\s\0\\\""
162     "\x01\xaF\uffffff"
163
164     URL" http://google.com"
165     R" asdf"
166
167     """">json""""
168
169 ! Triple quote strings (old Factor)
170
171     """hello, world"""
172     """ hello, world """
173     """this is a
174     multiline string"""
175
176 ! Multiline strings
177
178     [=[this is a weird new string]=]
179
180 ! Containers
181
182     H{ { 1 2 } }
183     HS{ 1 2 3 }
184     { 4 5 6 }
185     V{ "asdf" 5 }
186     ${ 1 foo 3 }
187
188 ! Quotations
189
190     [ 2^ * ]
191     '[ _ sqrt ]
192     $[ 1 2 + ]
193
194 ! Tuples
195
196     T{ foo f 1 2 3 }
197     T{ foo { a 5 } }
198
199 ! Symbols are colored wrong:
200
201     : rock ( -- ) \ rock computer play. ;
202
203 ! SBUF is colored wrong:
204
205     SBUF" " clone swap [ " " append ] [ number>string append ] interleave
206
207 ! Update to new library words:
208
209     key? and assoc-empty? are not colored
210     tail* is not highlighted
211
212 ! IN poker, unicode characters:
213
214     t
215
216     f
217
218     CHAR: -
219     CHAR: a
220     CHAR: symbol-for-end-of-transmission
221     CHAR: snowman
222
223     { CHAR: a CHAR: S }
224     { CHAR: b CHAR: D }
225     { CHAR: c CHAR: H }
226     { CHAR: d CHAR: C }
227
228 ! Bin
229
230     0b10101
231     0B10101
232
233 ! Oct
234
235     0o432
236     0O1234567
237     0o1234567
238     0o7
239
240 ! Hex
241
242     0xCAFEBABE
243     0XCAFEBABE
244     0x1AB4p30
245
246 ! Dec
247
248     1,000
249     10,000
250
251 ! Float
252
253     1e10
254     -1.5e-5
255
256
257 ! Weird numbers
258
259     1,234+56/78
260     +1/3
261     1+1/3
262     -1/3
263     -1-1/3
264     -1,234-1/34
265     1.
266     +1.5
267     -1.5e30
268     1.5e-30
269     1,000.1,2
270     NAN: CAFE1234 0,. ! third token wrong
271     0,. ! wrong, next line also wrong
272     0,.
273     NAN: ! ff 0xff comment
274         xCAFE1234 ! wrong
275         ff ! shouldn't match as a hex number
276     NAN: 0
277     drop
278     NAN: !
279         ! a 1 comment 1
280         f
281
282     NAN:
283 f,
284     NAN: ALKSJDflKJ ! XXX: should error
285
286 ! Not numbers
287
288     ,0.1
289     .
290     -.
291     1foo
292     1.5bar
293     +foo
294     -bar
295     *baz
296     qux*
297     /i
298     (1string)
299     ?1+
300
301 ! Comments in STRUCT: definitions
302 ! STRUCT: features like bitfields, etc.
303
304     STRUCT: foo
305     { a int initial: 0 } ! a comment
306     { b c-string }
307     { c char[4] }
308     { d void* }
309     { e int bits: 8 }
310     ;
311
312 ! Stack effects
313
314     ( -- )
315     ( x -- )
316     ( x -- x )
317     ( x x -- x )
318     ( x x -- x x )
319
320     ( quot: ( a -- b ) -- )
321     ( x quot: ( a -- b ) -- y )
322     ( ..a quot: ( ..a x -- ..b ) -- ..b )
323
324     ( x n -- (x)n )
325
326     ( p:
327 boolean -- q: boolean )
328     ( m: integer -- n: float )
329     ( :integer -- :float )
330
331     ( x -- y )
332
333 ! Weird stuff:
334
335     key?
336     key?thing
337     flushablething
338     flushable
339     <PRIVATEfoo
340     "asdf"foo
341
342 << 5 1 + . >> 1
343
344 : foo ( x -- y ) foo>> + ; inline
345
346 +@
347 +byte+
348
349 pair?
350 tail?
351
352 0.1
353 10,0.1
354 1.23
355 .1
356 -.1
357 -0.1
358 -0,1.1
359 1.
360
361 ! Numeral comma separator parsing (!: wrong, ~: other):
362   ! int
363   0 00 0,0 +0,0 -0,,0
364   /* ! */ ,0 ,00 0,, 00,, +,0 -,,0 +0, -0,, /* ~ */ , +, -,,
365
366   ! float
367   0,0e0 0e0,0 0,0e0,0 +0,0e+0,0 -0,0e-0,0
368   /* ~ */ e e, ,e ,e, +e -e +e, -e,
369   /* ~ */ +e -e +,e -,e +e+, -e-, +,e-,, -,,e+,
370   /* ~ */ +e -e +,e -,e +e+, -e-, +,e-,, -,,e+,
371   /* ! */ e0, -e,0 ,0e, 0,e, +,e0 -,e-0 0,e0 +0,0e+ -0,0e-,, 0e+ -0e-
372   /* ! */ +0e+0, -0e-,,0
373
374   ! float
375   0,0. .0,0 /* ! */ 0,. .,0 ,.0 0., ,0. .0,
376   +0,0.0 -0,0.0,0
377   0,0.0,0e0 0,0.0,0e0,0
378   0,0.0,0e+0,0 0,0.0,0e-0,0
379
380   ! ratio
381   /* ~ */ / /. +/ -/ ,/ /, 0/ /0
382   0/1 1/1 1/1. 1/0. 0/0. /* ! */ 0/0 1/0 1./1
383   1/2 +1/2 -1/2 +2/2 /* ! */ 1/+2 1/-2 1/+0 +2/+2
384   +1+1/2 -0-1/2 /* ! */ +1+2/2 +1+1/2. +0-1/2 -0+1/2
385
386 ! Regexp is colored wrong (on Github):
387
388 : using-line ( source -- vocabs )
389     R/ USING: [^;]* ;/s all-matching-subseqs ?first
390     [ { } ] [ " \n" split rest but-last ] if-empty ;