#!/usr/bin/env golo module demo.Tour # A tour of what Turbo Golo colours, one construct per line or two. Every line # here runs: `golo demos/syntax-tour/tour.golo` prints its way through. What the # lexer reads but the interpreter's parser then refuses is in lexer-only.golo # beside this file, which is coloured but does not run. import gololang.Errors ---- A block comment runs from four dashes to the next four, over as many lines as it likes, with --- near misses and "quotes" inside it left alone. ---- # Structs, unions and augmentations are capitalised by convention, and the # convention is what the colour follows. struct Point = { x, y } union Shape = { Circle = { radius } Rect = { width, height } } augment Shape$Circle { function diameter = |this| -> this: radius() * 2 } # A declared name is a function even though no parenthesis follows it. function twice = |x| -> x * 2 function hidden = |x| { return x + 1 } function main = |args| { # Numbers: integers, doubles, exponents. let count = 42 let ratio = 3.14 let small = 1.5e-3 let big = 2E10 # Strings, escapes, a multi-line string, and a hash that is not a comment. let greeting = "Hello, \"Golo\"\n" let hash = "# not a comment" let dashes = "---- not a comment ----" let multi = """ a multi-line "string" with "quotes" left alone """ # Builtins are the interpreter's own functions; DynamicObject is one too. println(greeting + hash + dashes) println(multi) println(str(count) + " " + str(ratio) + " " + str(small) + " " + str(big)) let bag = DynamicObject() bag: name("Golo") println(bag: name()) # Collections and comprehensions. let xs = list[1, 2, 3] let squares = list[x * x foreach x in range(1, 6) when x > 2] let pairs = map[["one", 1], ["two", 2]] println(xs + " " + squares + " " + pairs) # Control flow: if, while, for, foreach, match. var i = 0 while i < 2 { i = i + 1 } for (var j = 0, j < 2, j = j + 1) { println("for " + j) } foreach x in xs { println("foreach " + x) } let label = match { when count < 0 then "negative" when count == 0 then "zero" otherwise "positive" } println(label) if count > 40 and not (count is null) { println("big enough") } else { println("small") } # Structs, unions, augmentations, closures. let p = Point(1, 2) let c = Shape_Circle(2.0) println(p: x() + p: y()) println(c: isCircle() + " " + c: diameter()) let f = |a, b| -> a + b println(f(twice(20), hidden(1))) # Errors, and the Option union from gololang.Errors — Some is a variant, # not a builtin, so it takes the colour every capitalised name does. try { throw "boom" } catch (e) { println("caught: " + e) } finally { println("done") } let maybe = Some(1) println(maybe: isSome()) # Names may be any Unicode letter, or an emoji. let été = "summer" let 😀 = "smile" println(été + " " + 😀) # Safe navigation on a null. let nothing = null println(nothing?: x()) }