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