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