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