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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
|
1. Function parameters:
sum :: func(a: int, b: int) int { return a + b; }
foo :: func() { x := sum(1, 2); }
2. Multideclarations:
a, b := 1, 2; /* Multiple assignment */
r, g, b, a: u8; /* Type cascading */
x, y, z: i8 = …; /* Multiple undefined assignment */
3. Strings and arrays:
hello_utf8 := "Hello, World";
hello_utf8: []u8 = "Hello, World";
hello_utf16: []u16 = "Hello, World";
hello_utf32: []u32 = "Hello, World";
X :: 3;
xs := […]int{1, 2, 3}; /* Implicit length */
ys := [X]int{1, 2, 3}; /* Explicit length */
/* Assign specific indicies */
zs := […]int{
[2] = 123,
[4] = 321,
};
4. Conditionals:
if x > y {
…
} else if y < x {
…
} else {
…
}
5. Switch statements
if x == {
case 0;
…
case 1;
…
fallthrough; /* Explicit fallthrough; implicit break */
case 2;
…
default;
…
}
6. Loops
loop { … } /* Infinite loop */
loop x > y { … } /* While loop */
loop x: xs { … } /* Range/for loop */
loop i: N…M { … } /* Loop i from N to M (integers) */
/* Break- and continue a loop iteration */
loop x > y {
if x == 42 {
break;
}
if x & 1 == 1 {
continue;
}
…
}
/* Labeled break/continue */
outer :: loop {
loop x > y {
if x == 42 {
break outer;
}
}
}
7. Multiple return values from functions
swap :: func(x, y: int) int, int { return y, x; }
x, y := 1, 2;
x, y = swap(x, y);
/* Note that this is a function that returns a function! The
returned function is one that takes an int and returns nothing */
foo :: func() (int) { … }
8. Default parameters
foo :: func(x, y: int, flags: Flags = 0) { … }
9. Variadic parameters
foo :: func(xs: …int) {
for x: xs { … }
foo(xs…);
}
10. Structure types
City :: struct {
name, country: []u8;
population: int;
european := false;
}
växjö := City{"Växjö", "Sweden", 40'000, true};
são_paulo := City{
name = "São Paulo",
country = "Brazil",
population = 12.33e6,
};
11. Enumerations
/* enum T ≣ enum(0, x + 1) T*/
Animal :: enum u8 {
BEAR; /* 0 */
CAT; /* 1 */
DOG = 42;
SNAKE; /* 43 */
}
FileFlag :: enum(0, 1 << x) u8 {
READ; /* 0x01 */
WRITE; /* 0x02 */
TRUNC; /* 0x04 */
EXEC = 0b0100'0000; /* 0x40 */
CLOEXEC; /* 0x80 */
RDWR = .READ | .WRITE; /* 0x3 */
}
ByteSize :: enum(1, x * 1000) int {
B; /* 1 */
KB; /* 1'000 */
MB; /* 1'000'000 */
GB; /* 1'000'000'000 */
TB; /* 1'000'000'000'000 */
}
12. Enum-indexed arrays
Direction :: enum int { L; R; U; D; }
Vector2 :: struct { x, y: int; }
OFFSETS :: [Direction]Vector2{
.L = {-1, 0},
.R = {+1, 0},
.U = {0, +1},
.D = {0, -1},
}
$add :: func (x, y: Vector2) Vector2 {
return {x.x + y.x, x.y + y.y};
}
x := Vector2{2, 4};
x += OFFSETS[.U]; /* x == Vector2{2, 5} */
x += OFFSETS[3]; /* Illegal */
x += OFFSETS[Direction{3}]; /* Legal */
13. Operator overloading
$add :: func(x, y: T1) T2 { … }
$add, $sub, $mul, $div, etc.
$dot, $cross, $star, etc. for Unicode operators (⋅, ×, ⋆, etc.)
|