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