first impl of builtin member methods (thus far, string.size implemented. more to...
[aqualang.git] / examples / basics.jtc
blob6cf6a070dd347a6f83910aff81cd45ecd387f39f
1 // examples of the basic syntax
3 pi = 3.14159 // numbers are always doubles
5 greeting = "Hello World!" // string
7 square = func(x) { // functions are first class citizens
8     return x*x
11 arr = [pi, greeting, nil] // array types
12 arr[2] = square //replace the nil element (indices start at 0)
13 arr += [23] // append
14 length = size(arr) // use the size operator to find an arrays length
16 // tables are associative containers that can use any object as key
17 table = {
18     "pi" = 3.14159
19     23 = "twenty three"
20     "bar" = square
22 table.e = 2.71 // equivalent to table["e"] = 2.71
23 table.bar = func() { print(this.e, "\n") }
24 table.bar()
26 // flow control uses C style
27 if(pi > 2) {
28     print("pi is greater than 2\n")
29 } else {
30     print("pi is less or equal 2\n")
33 x = 2
34 while(x < 1024) {
35     print(x = square(x) ", ")
37 print("\n")
39 //no ++ or -- operators
40 for(i = 0;i<size(arr);i+=1) {
41     print(arr[i] ", ")
43 print("\n")
45 // for each syntax
46 for(value : arr) {
47     print(value ", ")
49 print("\n")
51 // for each on tables has no specific order
52 for(value : table) {
53     print(value ", ")
55 print("\n")
57 // iterate over key value pairs
58 for(key, value : table) {
59     print(key " = " value  ", ")
61 print("\n")