Debian Codegraph @

tests/inputs/Arturo.art

raw
1 ; this is a comment
2 ; this is another comment
3
4 ;---------------------------------
5 ; VARIABLES & VALUES
6 ;---------------------------------
7
8 ; numbers
9 a1: 2
10 a2: 3.14
11 a3: to :complex [1 2.0] ; 1.0+2.0i
12
13 ; strings
14 c1: "this is a string"
15 c2: {
16 this is a multiline string
17 that is indentation-agnostic
18 }
19 c3: {:
20 this is
21 a verbatim
22 multiline string
23 which will remain exactly
24 as the original
25 :}
26
27 ; characters
28 ch: `c`
29
30 ; blocks/arrays
31 d: [1 2 3]
32
33 ; dictionaries
34 e: #[
35 name: "John"
36 surname: "Doe"
37 age: 34
38 likes: [pizza spaghetti]
39 ]
40
41 ; yes, functions are values too
42 f: function [x][
43 2 * x
44 ]
45
46 ; colors - right, you can directly define them as well!
47 g1: #red
48 g2: #0077BF
49
50 ; dates
51 h: now ; 2021-05-03T17:10:48+02:00
52
53 ; logical values
54 i1: true
55 i2: false
56 i3: maybe
57
58 ;---------------------------------
59 ; BASIC OPERATORS
60 ;---------------------------------
61
62 ; simple arithmetic
63 1 + 1 ; => 2
64 8 - 1 ; => 7
65 4.2 - 1.1 ; => 3.1
66 10 * 2 ; => 20
67 35 / 4 ; => 8
68 35 // 4 ; => 8.75
69 2 ^ 5 ; => 32
70 5 % 3 ; => 2
71
72 ; bitwise operators
73 and 3 5 ; => 1
74 or 3 5 ; => 7
75 xor 3 5 ; => 6
76
77 ; pre-defined constants
78 pi ; => 3.141592653589793
79 epsilon ; => 2.718281828459045
80 null ; => null
81 true ; => true
82 false ; => false
83
84 ;---------------------------------
85 ; COMPARISON OPERATORS
86 ;---------------------------------
87
88 ; equality
89 1 = 1 ; => true
90 2 = 1 ; => false
91
92 ; inequality
93 1 <> 1 ; => false
94 2 <> 1 ; => true
95
96 ; more comparisons
97 1 < 10 ; => true
98 1 =< 10 ; => true
99 10 =< 10 ; => true
100 1 > 10 ; => false
101 1 >= 10 ; => false
102 11 >= 10 ; => true
103
104 ;---------------------------------
105 ; CONDITIONALS
106 ;---------------------------------
107
108 ; logical operators
109 and? true true ; => true
110 and? true false ; => false
111 or? true false ; => true
112 or? false false ; => false
113
114 and? [1=2][2<3] ; => false
115 ; (the second block will not be evaluated)
116
117 ; simple if statements
118 if 2 > 1 [ print "yes!"] ; yes!
119 if 3 <> 2 -> print "true!" ; true!
120
121 ; if/else statements
122 ; note it's 'if?' not 'if'
123 if? 2 > 3 -> print "2 is greater than 3"
124 else -> print "2 is not greater than 3" ; 2 is not greater than 3
125
126 ; switch statements
127 switch 2 > 3 -> print "2 is greater than 3"
128 -> print "2 is not greater than 3" ; 2 is not greater than 3
129
130 a: (2 > 3)?["yes"]["no"] ; a: "no"
131 a: (2 > 3)? -> "yes" -> "no" ; a: "no" (exactly the same as above)
132
133 ; case/when statements
134 case [1]
135 when? [>2] -> print "1 is greater than 2. what?!"
136 when? [<0] -> print "1 is less than 0. nope..."
137 else -> print "here we are!" ; here we are!
138
139 ;---------------------------------
140 ; LOOPS
141 ;---------------------------------
142
143 ; with `loop`
144 arr: [1 4 5 3]
145 loop arr 'x [
146 print ["x =" x]
147 ]
148 ; x = 1
149 ; x = 4
150 ; x = 5
151 ; x = 3
152
153 ; with loop and custom index
154 loop.with:'i arr 'x [
155 print ["item at position" i "=>" x]
156 ]
157 ; item at position 0 => 1
158 ; item at position 1 => 4
159 ; item at position 2 => 5
160 ; item at position 3 => 3
161
162 ; using ranges
163 loop 1..3 'x -> ; since it's a single statement
164 print x ; there's no need for [block] notation
165 ; we can wrap it up using the `->` syntactic sugar
166
167 loop `a`..`c` 'ch ->
168 print ch
169 ; a
170 ; b
171 ; c
172
173 ; picking multiple items
174 loop 1..10 [x y] ->
175 print ["x =" x ", y =" y]
176 ; x = 1 , y = 2
177 ; x = 3 , y = 4
178 ; x = 5 , y = 6
179 ; x = 7 , y = 8
180 ; x = 9 , y = 10
181
182 ; looping through a dictionary
183 dict: #[name: "John", surname: "Doe", age: 34]
184 loop dict [key value][
185 print [key "->" value]
186 ]
187 ; name -> John
188 ; surname -> Doe
189 ; age -> 34
190
191 ; while loops
192 i: new 0
193 while [i<3][
194 print ["i =" i]
195 inc 'i
196 ]
197 ; i = 0
198 ; i = 1
199 ; i = 2
200
201 ;---------------------------------
202 ; STRINGS
203 ;---------------------------------
204
205 ; case
206 a: "tHis Is a stRinG"
207 print upper a ; THIS IS A STRING
208 print lower a ; this is a string
209 print capitalize a ; THis Is a stRinG
210
211 ; concatenation
212 a: "Hello " ++ "World!" ; a: "Hello World!"
213
214 ; strings as an array
215 split "hello" ; => [h e l l o]
216 split.words "hello world" ; => [hello world]
217
218 print first "hello" ; h
219 print last "hello" ; o
220
221 ; conversion
222 to :string 123 ; => "123"
223 to :integer "123" ; => 123
224
225 ; joining strings together
226 join ["hello" "world"] ; => "helloworld"
227 join.with:"-" ["hello" "world"] ; => "hello-world"
228
229 ; string interpolation
230 x: 2
231 print ~"x = |x|" ; x = 2
232
233 ; interpolation with `print`
234 print ["x =" x] ; x = 2
235 ; (`print` works by calculating the given block
236 ; and joining the different values as strings
237 ; with a single space between them)
238
239 ; templates
240 print render.template {
241 <||= switch x=2 [ ||>
242 Yes, x = 2
243 <||][||>
244 No, x is not 2
245 <||]||>
246 } ; Yes, x = 2
247
248 ; matching
249 prefix? "hello" "he" ; => true
250 suffix? "hello" "he" ; => false
251
252 contains? "hello" "ll" ; => true
253 contains? "hello" "he" ; => true
254 contains? "hello" "x" ; => false
255
256 in? "ll" "hello" ; => true
257 in? "x" "hello" ; => false
258
259 ;---------------------------------
260 ; BLOCKS
261 ;---------------------------------
262
263 ; calculate a block
264 arr: [1 1+1 1+1+1]
265 @arr ; => [1 2 3]
266
267 ; execute a block
268 sth: [print "Hello world"] ; this is perfectly valid,
269 ; could contain *anything*
270 ; and will not be executed...
271
272 do sth ; Hello world
273 ; (...until we tell it to)
274
275 ; array indexing
276 arr: ["zero" "one" "two" "three"]
277 print first arr ; zero
278 print arr\0 ; zero
279 print last arr ; three
280 print arr\3 ; three
281
282 x: 2
283 print get arr x ; two
284 print arr\[x] ; two
285
286 ; setting an array element
287 arr\0: "nada"
288 set arr 2 "dos"
289 print arr ; nada one dos three
290
291 ; adding elements to an array
292 arr: new []
293 'arr ++ "one"
294 'arr ++ "two"
295 print arr ; one two
296
297 ; remove elements from an array
298 arr: new ["one" "two" "three" "four"]
299 'arr -- "two" ; arr: ["one" "three" "four"]
300 remove 'arr .index 0 ; arr: ["three" "four"]
301
302 ; getting the size of an array
303 arr: ["one" 2 "three" 4]
304 print size arr ; 4
305
306 ; getting a slice of an array
307 print slice ["one" "two" "three" "four"] 0 1 ; one two
308
309 ; check if array contains a specific element
310 print contains? arr "one" ; true
311 print contains? arr "five" ; false
312
313 ; sorting array
314 arr: [1 5 3 2 4]
315 sort arr ; => [1 2 3 4 5]
316 sort.descending arr ; => [5 4 3 2 1]
317
318 ; mapping values
319 map 1..10 [x][2*x] ; => [2 4 6 8 10 12 14 16 18 20]
320 map 1..10 'x -> 2*x ; same as above
321 map 1..10 => [2*&] ; same as above
322 map 1..10 => [2*] ; same as above
323
324 ; selecting/filtering array values
325 select 1..10 [x][odd? x] ; => [1 3 5 7 9]
326 select 1..10 => odd? ; same as above
327
328 filter 1..10 => odd? ; => [2 4 6 8 10]
329 ; (now, we leave out all odd numbers -
330 ; while select keeps them)
331
332 ; misc operations
333 arr: ["one" 2 "three" 4]
334 reverse arr ; => [4 "three" 2 "one"]
335 shuffle arr ; => [2 4 "three" "one"]
336 unique [1 2 3 2 3 1] ; => [1 2 3]
337 permutate [1 2 3] ; => [[1 2 3] [1 3 2] [3 1 2] [2 1 3] [2 3 1] [3 2 1]]
338 take 1..10 3 ; => [1 2 3]
339 repeat [1 2] 3 ; => [1 2 1 2 1 2]
340
341 ;---------------------------------
342 ; FUNCTIONS
343 ;---------------------------------
344
345 ; declaring a function
346 f: function [x][ 2*x ]
347 f: function [x]-> 2*x ; same as above
348 f: $[x]->2*x ; same as above (only using the `$` alias
349 ; for the `function`... function)
350
351 ; calling a function
352 f 10 ; => 20
353
354 ; returning a value
355 g: function [x][
356 if x < 2 -> return 0
357
358 res: 0
359 loop 0..x 'z [
360 res: res + z
361 ]
362 return res
363 ]
364
365 ;---------------------------------
366 ; CUSTOM TYPES
367 ;---------------------------------
368
369 ; defining a custom type
370 define :person [
371 ; with custom post-construction initializer
372 init: method [name, surname, age][
373 this\name: capitalize name
374 this\surname: surname
375 this\age: age
376 ]
377
378 ; custom print function
379 string: method [][
380 render "NAME: |this\name|, SURNAME: |this\surname|, AGE: |this\age|"
381 ]
382
383 ; custom comparison operator
384 compare: sortable 'age
385
386 ; create a method for our custom type
387 sayHello: method [][
388 print ["Hello" this\name]
389 ]
390 ]
391
392 ; create new objects of our custom type
393 a: to :person ["John" "Doe" 34]! ; let's create 2 "Person"s
394 b: to :person ["jane" "Doe" 33]! ; and another one
395
396 ; call inner method
397 a\sayHello ; Hello John
398 b\sayHello ; Hello Jane
399
400 ; access object fields
401 print ["The first person's name is:" a\name] ; The first person's name is: John
402 print ["The second person's name is:" b\name] ; The second person's name is: Jane
403
404 ; changing object fields
405 a\name: "Bob"
406 a\sayHello ; Hello Bob
407
408 ; verifying object type
409 print type a ; :person
410 print is? :person a ; true
411
412 ; printing objects
413 print a ; NAME: John, SURNAME: Doe, AGE: 34
414
415 ; sorting user objects (using custom comparator)
416 sort @[a b] ; Jane..., John...
417 sort.descending @[a b] ; John..., Jane...