]> gitweb.factorcode.org Git - factor.git/blob - misc/syntax-test.factor
Fixes #2966
[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 { ALIEN: 1234 } [ ALIEN: 1234 [ { alien } declare void* <ref> ] compile-call void* deref ] unit-test
162 { ALIEN: 1234 } [ ALIEN: 1234 [ { c-ptr } declare void* <ref> ] compile-call void* deref ] unit-test
163 { f } [ f [ { POSTPONE: f } declare void* <ref> ] compile-call void* deref ] unit-test
164
165 ! Symbols and literals
166
167     \ foo
168     $ foo
169     M\ foo bar
170
171     MAIN: word
172     CONSTANT: word value
173     SYMBOL: word
174     SYMBOLS: words ... ;
175
176     COLOR: red
177     COLOR: #336699
178
179 ! Math
180
181     1 2 +
182     3 4 -
183     5 6 *
184     7 8 /
185     32 2^
186     10 10^
187
188 ! Examples
189
190     [ 1 ] unless*
191     >boolean
192     <wrapper>
193     +@
194     [ [ { } ] ?{ } ]
195     H{ ?{ { } } } assoc-empty?
196     ?{ t t f } nth
197     5 >bignum
198     1 2 pick set-nth
199     5 f <array>
200     (clone)
201
202     [| a b | ]
203     [let [let { } ] ]
204
205 ! Strings
206
207     ""
208     "test"
209     SBUF" foo"
210     SBUF" hello world "
211     "\s"
212     "\\foo"
213     "\"hello\""
214     "\a\b\e\f\n\r\t\s\v\s\0\\\""
215     "\x01\xaF\uffffff"
216     "\0123\148"
217
218     URL" http://google.com"
219     R" asdf"
220
221     """>json"""
222
223     "{ 1 2 3 }"
224
225     [[{ 1 2 3 }]]
226
227 ! Triple quote strings (old Factor)
228
229     """hello, world"""
230     """ hello, world """
231     """this is a
232     multiline string"""
233
234 ! Multiline strings
235
236     [[this is a weird new string]]
237     [=[this is a weird new string]=]
238     [==[this is a weird new string]==]
239     [===[this is a weird new string]===]
240     [====[this is a weird new string]====]
241     [=====[this is a weird new string]=====]
242     [======[this is a weird new string]======]
243
244     HEREDOC: END
245     foo
246 END
247
248     HEREDOC: foo bar baz
249     foo
250 foo bar baz
251
252     STRING: foo
253 asdf\f
254 ;
255
256     drop
257 ! Containers
258
259     H{ { 1 2 } }
260     HS{ 1 2 3 }
261     { 4 5 6 }
262     V{ "asdf" 5 }
263     ${ 1 foo 3 }
264     ?{ t t f f t }
265
266 ! Quotations
267
268     [ 2^ * ]
269     '[ _ sqrt ]
270     '[ _ @ ]
271     $[ 1 2 + ]
272     [let ]
273     [| | ]
274
275 ! Tuples
276
277     T{ foo f 1 2 3 }
278     T{ foo { a 5 } }
279
280 ! Symbols are colored wrong:
281
282     : rock ( -- ) \ rock computer play. ;
283
284 ! SBUF is colored wrong:
285
286     SBUF" " clone swap [ " " append ] [ number>string append ] interleave
287
288 ! Update to new library words:
289
290     key? and assoc-empty? are not colored
291     tail* is not highlighted
292
293 ! IN poker, unicode characters:
294
295     t
296
297     f
298
299     CHAR: -
300     CHAR: a
301     CHAR: symbol-for-end-of-transmission
302     CHAR: snowman
303     CHAR: ☃
304
305     { CHAR: a CHAR: S }
306     { CHAR: b CHAR: D }
307     { CHAR: c CHAR: H }
308     { CHAR: d CHAR: C }
309
310 ! Bin
311
312     0b10101
313     0B10101
314
315 ! Oct
316
317     0o432
318     0O1234567
319     0o1234567
320     0o7
321
322 ! Hex
323
324     0xCAFEBABE
325     0XCAFEBABE
326     0x1AB4p30
327
328 ! Dec
329
330     1,000
331     10,000
332
333 ! Float
334
335     1e10
336     -1.5e-5
337
338
339 ! Weird numbers
340
341     1,234+56/78
342     +1/3
343     1+1/3
344     -1/3
345     -1-1/3
346     -1,234-1/34
347     1.
348     +1.5
349     -1.5e30
350     1.5e-30
351     1,000.1,2
352     NAN: CAFE1234 0,. ! third token wrong
353     0,. ! wrong, next line also wrong
354     0,.
355     NAN: ! ff 0xff comment
356         xCAFE1234 ! wrong
357         ff ! shouldn't match as a hex number
358     NAN: 0
359     drop
360     NAN: !
361         ! a 1 comment 1
362         f
363
364     NAN:
365 f,
366     NAN: ALKSJDflKJ ! XXX: should error
367
368 ! Not numbers
369
370     ,0.1
371     .
372     -.
373     1foo
374     1.5bar
375     +foo
376     -bar
377     *baz
378     qux*
379     /i
380     (1string)
381     ?1+
382
383 ! Comments in STRUCT: definitions
384 ! STRUCT: features like bitfields, etc.
385
386     STRUCT: foo
387     { a int initial: 0 } ! a comment
388     { b c-string }
389     { c char[4] }
390     { d void* }
391     { e int bits: 8 }
392     ;
393
394 ! Stack effects
395
396     ( -- )
397     ( x -- )
398     ( x -- x )
399     ( x x -- x )
400     ( x x -- x x )
401
402     ( quot: ( a -- b ) -- )
403     ( x quot: ( a -- b ) -- y )
404     ( ..a quot: ( ..a x -- ..b ) -- ..b )
405
406     ( x n -- (x)n )
407
408     ( p: ! inline comment
409 boolean -- q: boolean )
410     ( m: integer -- n: float )
411     ( :integer -- :float )
412
413     ( x -- y )
414
415 ! Weird stuff:
416
417     key?
418     key?thing
419     flushablething
420     flushable
421     <PRIVATEfoo
422     [[asdf]]foo
423     "asdf"foo
424     foo"asdf"foo
425     foo"asdf"
426
427 << 5 1 + . >> 1
428
429 : foo ( x -- y ) foo>> + ; inline
430
431 +@
432 +byte+
433
434 pair?
435 tail?
436
437 0.1
438 10,0.1
439 1.23
440 .1
441 -.1
442 -0.1
443 -0,1.1
444 1.
445
446 ! Numeral comma separator parsing (!: wrong, ~: other):
447   ! int
448   0 00 0,0 +0,0 -0,,0
449   /* ! */ ,0 ,00 0,, 00,, +,0 -,,0 +0, -0,, /* ~ */ , +, -,,
450
451   ! float
452   0,0e0 0e0,0 0,0e0,0 +0,0e+0,0 -0,0e-0,0
453   /* ~ */ e e, ,e ,e, +e -e +e, -e,
454   /* ~ */ +e -e +,e -,e +e+, -e-, +,e-,, -,,e+,
455   /* ~ */ +e -e +,e -,e +e+, -e-, +,e-,, -,,e+,
456   /* ! */ e0, -e,0 ,0e, 0,e, +,e0 -,e-0 0,e0 +0,0e+ -0,0e-,, 0e+ -0e-
457   /* ! */ +0e+0, -0e-,,0
458
459   ! float
460   0,0. .0,0 /* ! */ 0,. .,0 ,.0 0., ,0. .0,
461   +0,0.0 -0,0.0,0
462   0,0.0,0e0 0,0.0,0e0,0
463   0,0.0,0e+0,0 0,0.0,0e-0,0
464
465   ! ratio
466   /* ~ */ / /. +/ -/ ,/ /, 0/ /0
467   0/1 1/1 1/1. 1/0. 0/0. /* ! */ 0/0 1/0 1./1
468   1/2 +1/2 -1/2 +2/2 /* ! */ 1/+2 1/-2 1/+0 +2/+2
469   +1+1/2 -0-1/2 /* ! */ +1+2/2 +1+1/2. +0-1/2 -0+1/2
470
471 ! Regexp is colored wrong (on Github):
472
473 R/ foo/
474 R/ foo/s
475
476 : using-line ( source -- vocabs )
477     R/ USING: [^;]* ;/s all-matching-subseqs ?first
478     [ { } ] [ " \n" split rest but-last ] if-empty ;