1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
#!/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())
}
|