| 📦 Turbo Golo d710c1b k33g 17h ago | 1 | #!/usr/bin/env golo |
| 2 | module demo.Tour |
| 3 | |
| 4 | # A tour of what Turbo Golo colours, one construct per line or two. Every line |
| 5 | # here runs: `golo demos/syntax-tour/tour.golo` prints its way through. What the |
| 6 | # lexer reads but the interpreter's parser then refuses is in lexer-only.golo |
| 7 | # beside this file, which is coloured but does not run. |
| 8 | |
| 9 | import gololang.Errors |
| 10 | |
| 11 | ---- |
| 12 | A block comment runs from four dashes to the next four, over as many lines as |
| 13 | it likes, with --- near misses and "quotes" inside it left alone. |
| 14 | ---- |
| 15 | |
| 16 | # Structs, unions and augmentations are capitalised by convention, and the |
| 17 | # convention is what the colour follows. |
| 18 | struct Point = { x, y } |
| 19 | |
| 20 | union Shape = { |
| 21 | Circle = { radius } |
| 22 | Rect = { width, height } |
| 23 | } |
| 24 | |
| 25 | augment Shape$Circle { |
| 26 | function diameter = |this| -> this: radius() * 2 |
| 27 | } |
| 28 | |
| 29 | # A declared name is a function even though no parenthesis follows it. |
| 30 | function twice = |x| -> x * 2 |
| 31 | |
| 32 | function hidden = |x| { |
| 33 | return x + 1 |
| 34 | } |
| 35 | |
| 36 | function main = |args| { |
| 37 | # Numbers: integers, doubles, exponents. |
| 38 | let count = 42 |
| 39 | let ratio = 3.14 |
| 40 | let small = 1.5e-3 |
| 41 | let big = 2E10 |
| 42 | |
| 43 | # Strings, escapes, a multi-line string, and a hash that is not a comment. |
| 44 | let greeting = "Hello, \"Golo\"\n" |
| 45 | let hash = "# not a comment" |
| 46 | let dashes = "---- not a comment ----" |
| 47 | let multi = """ |
| 48 | a multi-line "string" |
| 49 | with "quotes" left alone |
| 50 | """ |
| 51 | |
| 52 | # Builtins are the interpreter's own functions; DynamicObject is one too. |
| 53 | println(greeting + hash + dashes) |
| 54 | println(multi) |
| 55 | println(str(count) + " " + str(ratio) + " " + str(small) + " " + str(big)) |
| 56 | let bag = DynamicObject() |
| 57 | bag: name("Golo") |
| 58 | println(bag: name()) |
| 59 | |
| 60 | # Collections and comprehensions. |
| 61 | let xs = list[1, 2, 3] |
| 62 | let squares = list[x * x foreach x in range(1, 6) when x > 2] |
| 63 | let pairs = map[["one", 1], ["two", 2]] |
| 64 | println(xs + " " + squares + " " + pairs) |
| 65 | |
| 66 | # Control flow: if, while, for, foreach, match. |
| 67 | var i = 0 |
| 68 | while i < 2 { |
| 69 | i = i + 1 |
| 70 | } |
| 71 | for (var j = 0, j < 2, j = j + 1) { |
| 72 | println("for " + j) |
| 73 | } |
| 74 | foreach x in xs { |
| 75 | println("foreach " + x) |
| 76 | } |
| 77 | let label = match { |
| 78 | when count < 0 then "negative" |
| 79 | when count == 0 then "zero" |
| 80 | otherwise "positive" |
| 81 | } |
| 82 | println(label) |
| 83 | if count > 40 and not (count is null) { |
| 84 | println("big enough") |
| 85 | } else { |
| 86 | println("small") |
| 87 | } |
| 88 | |
| 89 | # Structs, unions, augmentations, closures. |
| 90 | let p = Point(1, 2) |
| 91 | let c = Shape_Circle(2.0) |
| 92 | println(p: x() + p: y()) |
| 93 | println(c: isCircle() + " " + c: diameter()) |
| 94 | let f = |a, b| -> a + b |
| 95 | println(f(twice(20), hidden(1))) |
| 96 | |
| 97 | # Errors, and the Option union from gololang.Errors — Some is a variant, |
| 98 | # not a builtin, so it takes the colour every capitalised name does. |
| 99 | try { |
| 100 | throw "boom" |
| 101 | } catch (e) { |
| 102 | println("caught: " + e) |
| 103 | } finally { |
| 104 | println("done") |
| 105 | } |
| 106 | let maybe = Some(1) |
| 107 | println(maybe: isSome()) |
| 108 | |
| 109 | # Names may be any Unicode letter, or an emoji. |
| 110 | let été = "summer" |
| 111 | let 😀 = "smile" |
| 112 | println(été + " " + 😀) |
| 113 | |
| 114 | # Safe navigation on a null. |
| 115 | let nothing = null |
| 116 | println(nothing?: x()) |
| 117 | } |
| 118 | |