-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.my
345 lines (270 loc) · 5.25 KB
/
example.my
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
# line comment
# The syntax is still in flux be here is what I have so far
# Hello World example
print('Hello World')
Int moogles # Initialize an Integer, must be assigned a value before it can be used
number = 23 # Type Inference
# Decimal will be used by default (as soon as a decimal standard is implimented)
decimal_number=1.2+2.4 # Spaces are generally insignificant, but encouraged for readability
# If a floating point number is desired then one could include the Float type identifier
question = 'what\'s going on' # Escaping
Str something # Initialize a String
things = [1, 2, 3] # Array, homogeneous
other_things = (1, 'Hello') # List, non-homogeneous
stuff = {first_name: 'Samus', 'last_name': 'Aran'} # Dictionary, key strings don't have to be in quotes
Int[] other_stuff = [] # Empty Array of Ints
if number > 23
print('greater than 23')
else if number == 23
print('equals 23')
else
print('less than 23')
if something is not number \ # Continuing statement onto next line
and true # Indentaion is with tabs only
print('They are not the same')
for x in 0..40 # For loop using a range
print(x * 2)
for item in things # Iterate over objects
print(item)
while number > 1
number -= 1
print(number)
if 2 in things
print('yes')
if 2 not in things
print('no')
moogles = 1
switch moogles # Switch soon to be replaced with Pattern Matching
case 1
print('One Moogle')
case 2
print('Two Moogles')
break
default
print(moogles)
print("Lots of Moogles")
case 3
print('Three Moogles')
# Pattern matching
s = 'hello world'
match s.contains
| 'hello'
print('Greating')
| 'goodby'
print('Farewell')
| _
print('Nothing')
# Function Return notation
def fib(Int n) -> Int
a = 0
b = 1
for _ in 0..n
prev_a = a
a = b
b = prev_a + b
return a
def fib_rec(Int n) -> Int
if n == 0
return 0
if n == 1
return 1
return fib_rec(n - 1) + fib_rec(n - 2)
def factorial(Int n) -> Int
if n <= 1
return 1
return n * factorial(n - 1)
# Assign anonymous function to a variable
myfunc = def (Int x, Int y) -> Int
if x > y
return x + y
else
return x * y
print(myfunc(2, 3))
def bar(Int x=4, Int y=5) -> Int
return x * y
baz = bar
print(baz())
def foo(Int x) -> Int
return baz(x) + x
print(foo(2))
print('🍌')
print('夜のコンサートは最高でした。') # I have no idea what this says
# Type Aliasing
type FInt = Func[Int] -> Int
# All types (including structs and classes) should start with a capital
def do_stuff(Int x, FInt callback) -> Int
x **= 2
x = callback(x)
return x
num = do_stuff(3,
def (Int y) -> Int
y += 7
return y
)
print(
num
)
# Closure
def start_at(Int x) -> FInt
def increment_by(Int y) -> Int
return x + y
return increment_by
start_at_5 = start_at(5)
start_at_27 = start_at(27)
print(start_at_5(4))
print(start_at_27(15))
Int num = 1.0::Int # Casting a Decimal to Int
print(num)
# User input
Int age = input('How old are you?')
# String Interpolation
print('Wow! You are {age} years old?!')
Enum Colors(
GREEN,
RED,
BLUE,
YELLOW
)
Struct Circle
Int radius
Int x
Int y
Circle cir = {radius=5, x=2, y=4}
print(cir.radius)
abstract class Vehicle
# Constructor
new(Str make, Int year, Str color)
this.year = year
this.make # Automtically assigns make from argument if they have the same name
this._color = color
Str this._fav_color
# Inheritance
class Car(Vehicle)
new(Str make, Int year, color='green', hatchback=false)
# Design by Contract
require year < 10
this.hatchback
super.Vehicle(make, year, color)
void print_year()
print('This car was made in {this.year}')
class Person
new(Str name, Int age)
this.name
this.age
frank = Person('Frank', 30)
print(frank.age)
garry = true
larry = false
num = 8
if not num == 7
num += 2
else if garry
num -= 1
else
num *= 4
print(num)
print(garry)
print(larry)
def is_less_than_5(Int x) -> Bool
if x >= 5
return false
else
return true
print(is_less_than_5(3))
print(is_less_than_5(5))
print(is_less_than_5(7))
# Testing scope
def do_stuff(Int y) -> void
print(y)
def square(Int x) -> Int
do_stuff(x)
return x ** 2
print(square(5))
def greet_name(Str name) -> void
print('Hello ' + name)
greet_name('Anthony')
x = 0
while x < 10
if x == 1
break
x += 1
switch x
case 2
print('x is Two')
break
case 1
print('x is One')
default
print("Sorry, wasn't paying attention")
y = 10
for x in 0..y + 1
print(x::Str + ' ' + fib(x)::Str)
things = [3, 2, 1]
print(things[1 + 1])
Dec a
b = 3
def four() -> Int
return 2 + 2
Bool yes = true
f = four()
Float g = 7.5
h = 5
no = false
if no
g = 2.6
if yes and no
h += h
else if yes
g = 3.6
else
g = 7.6
if yes
g += 7
first_name = 'Cid'
last_name = 'Fabool'
full_name = first_name + ' ' + last_name
a = 2 * g
d = 0
c = a::Int ** b
def kupo(Int first, Int second) -> Int
# Nested function
def mog() -> Int
return 54
if first < second
return first + second
else
return mog()
e = kupo(c, b)
while b < 30
b += c + 1
if b >= 20
b += 2
else
b += 1
d += c + b
print('a')
print(a)
print('b')
print(b)
print('c')
print(c)
print('d')
print(d)
print('e')
print(e)
print('f')
print(f)
print('g')
print(g)
print('h')
print(h)
print('yes')
print(yes)
print('no')
print(no)
print('first_name')
print(first_name)
print('last_name')
print(last_name)
print('full_name')
print(full_name)