-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtour.ppga
199 lines (166 loc) · 4.75 KB
/
tour.ppga
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
Multi-line comments look like this.
*/
// The single-line comment is also C-style.
// Variables and functions may be declared with `let` and `global`:
let a = -1;
let b = 2 + 2;
let c = 3 * 3;
let d = 4 / 4;
let e = 4 \ 4; // integer division
let f = 5 ** 5;
let g = 6 % 7;
let h = "abc" .. "d"; // Concatenation is .. (using + would require a type system)
let i = true != false and 3 < 4 or 5 >= 6 or 3 <= 7 and 10 > 2;
// Create a global function
global fn h() { }
// A block.
{
// Arrays use python-style syntax and are initialized from 0.
let arr = [1, 2, 3];
// Dicts are similar to lua but don't require the `[]`
let dict = {1 = 2, 3 = 4};
let empty = {};
// You can print table using the dump() function
print(dump(dict));
}
fn f(x) {
print("x", "is", x);
// string interpolation, transpiles to "x is " .. tostring(x)
print(f"x is {x}");
// Create a new local variable
let y = x;
// Create a new global variable
global z = 5;
fn packed() {
return y, z;
}
if not x {
// returns the result of packed as a
return packed(); // => return (packed())
}
// unpacks the result of packed() as two values
return ...packed(); // => return table.unpack({packed()})
}
let y = f(false);
print(f"f returned: {y}");
let y, x = f(35);
print(f"f returned: {y}, {z}");
// if statements may be chained like this:
if x < 20 {
print(f"x is < 20: {x}");
} else if x < 30 {
print(f"x is < 30: {x}");
} else {
print(f"x is >= 30: {x}");
}
// A match statement is a handy replacement for a bunch of chained if statements
match x + 29 as y {
// Checks that involve the bound variable (y in this case)
// transpile to "value" conditions: if y < 60 then ... end
y < 60 {
print(f"y is < 60: {y}");
}
// Checks that do not mention the bound variable
// transpile to "comparison" conditions: if y == 60 then ... end
60 {
print(f"y is {y}");
}
// A pattern maybe any expression ...
y ** 0.5 == 8 {
print(f"sqrt(y) is 8: {y}");
}
// ... including attribute accesses and function calls
(fn (z) => z > 60)(y) {
print(f"y is > 60: {y}");
}
// The wildcard `_` branch transpiles to `else ... end`
_ {
print("Everything else goes here.");
}
}
// Create and call a closure
let g = fn (y, f) {
// The ?? operator returns the default value on the right side
// if the value on the left side is `nil`.
f = f ?? (fn(x) => x);
print(f(y));
};
g(12); // 12
g(12, fn(x) => x * x); // 144
for i in range(1, 10) {
let fib = [0, 1];
// Code inside `lua` blocks is de-indented once
// and directly pasted into the resulting lua file.
lua {
for j = 2, i do
fib[j] = fib[j - 2] + fib[j - 1]
end
}
print(fib[len(fib) - 1]);
}
let container = [1, 2, 3];
container["string"] = "hello";
// Table iteration, uses pairs
for key, value in container {
print(key, value);
}
// Array iteration, uses ipairs
fori idx, value in container {
print(idx, value);
}
// Error handling works Go-style: all error-handling facilities expect a tuple of (ok, err),
// where either of the elements, but not both, is nil.
let fail = false;
fn may_fail() {
if fail {
return nil, "error";
}
return "success", nil;
}
fn another_fail() {
return nil, "error 2";
}
fn recovery() {
return "recovered", nil;
}
fn main() {
// The ? operator simplifies Go-style error handling.
// By default, this will make the whole program crash if an error is encountered.
let ok = may_fail()?;
// The line above will expand into a piece of code similar to the code below.
// The default error callback is what makes the script crash.
//
// let ok = nil
// {
// let _ok, _err = __default_error_handler(__default_error_callback, may_fail());
// if _err != nil {
// return nil, _err;
// }
// ok = _ok
// }
print(f"First result: {ok}");
// Sometimes it is desirable to log or try to recover from the error.
// An err block may be used for this purpose:
let ok = another_fail() err {
print(f"An error has occurred: {err}");
return ...recovery();
}?;
// The statement below looks almost exactly as the previous `?` example,
// except that the error handler is custom:
//
// let ok = nil
// {
// let _ok, _err = __default_error_handler(fn (err) {
// print(f"An error has occurred: {err}");
// return ...recovery();
// }, another_fail());
// if _err != nil {
// return nil, _err;
// }
// ok = _ok
// }
return ok;
}
let res = main();
print(f"Main returned: {res}");