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
|
1. Function parameters:
sum :: (a: int, b: int) int { return a + b; }
foo :: () { 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_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 {
…
}
/* Shortform */
if x > y
foo();
/* Bad style! */
if x > y foo();
5. Switch statements
if x == {
case 0;
…
case 1;
…
fallthrough; /* Explicit fallthrough; implicit break */
case 2;
…
default;
…
}
6. Loops
loop { … } /* Infinite loop */
/* Shorthand syntax */
loop
foo();
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 :: (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 :: () (int) { … }
8. Default parameters
foo :: (x, y: int, flags: Flags = 0) { … }
9. Variadic parameters
foo :: (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
Animal :: enum u8 {
BEAR; /* 0 */
CAT; /* 1 */
DOG = 42;
SNAKE; /* 43 */
}
FileFlag :: flag_enum u8 {
READ; /* 0x01 */
WRITE; /* 0x02 */
TRUNC; /* 0x04 */
EXEC = 0b0100'0000; /* 0x40 */
CLOEXEC; /* 0x80 */
RDWR = .READ | .WRITE; /* 0x3 */
}
|