-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayground.rz
206 lines (162 loc) · 2.91 KB
/
playground.rz
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
200
201
202
203
204
205
206
var breakfast = "beignets"
var beverage = "cafe au lait"
breakfast = "a " + "beignets with " + beverage
print breakfast
// Scopes
var a = 1
{
print a // 1
var a = 3
print a // 3
{
a = 5
}
print a // 5
}
print a // 1
var aa = 0
{
aa = 1
var b = 6
aa = b
}
print aa // 6
// Conditionals
if true { print "oui" }
if true
{
print "oiu"}
if false { print "oui" } else { print "non" }
var if_tests = 3
if true { if_tests = 5 } else { if_tests = -8 }
print if_tests
if false { if_tests = 65 } else { if_tests = -42 }
print if_tests
var aaa = 0
if true {
aaa = 1
var b = 6
aaa = b
aaa = 8
}
print aaa
if true or true { print "oui" }
if true or false { print "oui" }
if false or true { print "oui" }
if false or false { print "non" } else { print "oui" }
if false and false { print "non" } else { print "oui" }
if true and false { print "non" } else { print "oui" }
if false and true { print "non" } else { print "oui" }
if true and true { print "oui" } else { print "non" }
if true and true or false { print "oui" } else { print "non" }
if !!false or true and false or !!true { print "oui" }
// While
var incr = 0
while incr < 5 { incr = incr + 1 }
print incr
// For
for i in 5 { print i }
// Functions
fn are_we_fn_yet() {
print "yes we are"
}
print are_we_fn_yet
are_we_fn_yet()
// Native fnction
print clock
var start = clock()
print start
print clock() - start
// Fib
fn fib(n) {
if n < 2 { return n }
return fib(n - 2) + fib(n - 1)
}
var start_fib = clock()
print fib(5)
print clock() - start_fib
// Closure
{
var a = 123
fn print_a() { print a }
print_a()
}
var x = "global"
fn outer() {
var x = "outer"
fn inner() {
print x
}
inner()
}
outer() // "outer"
fn outer() {
var x = "outside"
fn inner() {
print x
}
inner()
}
outer() // "outside"
// Closed up upvalue
fn outer() {
var x = "outside"
fn inner() {
print x
}
return inner
}
var closure = outer()
closure() // "outside"
var globalSet
var globalGet
fn main() {
var a = "initial"
fn set() { a = "updated" }
fn get() { print a }
globalSet = set
globalGet = get
}
main()
globalSet()
globalGet()
/// Struct
struct Brioche {}
print Brioche
print Brioche()
struct Pair {}
var pair = Pair()
pair.first = 1
pair.second = 2
print pair.first + pair.second
// Bound method
struct Scone {
fn topping(first, second) {
print "scone with " + first + " and " + second
}
}
var scone = Scone()
scone.topping("berries", "cream")
// Initializer
struct CoffeeMaker {
fn init(coffee) {
self.coffee = coffee
}
fn brew() {
print "Enjoy your cup of " + self.coffee
self.coffee = null
}
}
var maker = CoffeeMaker("coffee and chicory")
maker.brew()
// Invoke method in field
struct Oops {
fn init() {
fn f() {
print "not a method"
}
self.field = f
}
}
var oops = Oops()
oops.field()