-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathideas.mal
More file actions
109 lines (95 loc) · 2.08 KB
/
ideas.mal
File metadata and controls
109 lines (95 loc) · 2.08 KB
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
max :: fn(a: _, b: _) -> _ {
return if a > b { a } else { b }
}
x := 42
print(x)
class Vec3 {
x : double
y : double
z : double
b_op + :: fn(Vec3 other) -> Vec3 {
// impl...
}
b_op - :: fn(Vec3 other) -> Vec3 {
// impl...
}
b_op * :: fn(double scalar) -> Vec3 {
// impl...
}
}
p1 := new Vec3
p2 := new Vec3
p3 := p1 + p2
class Greeter {
m_greeting : string
Greeter(greeting: string) {
m_greeting = greeting
}
greet :: fn(name: string) {
print("#{m_greeting} #{name}")
}
}
greeter := new Greeter("Hello, ")
greeter.greet("Bob") // Hello, Bob
/////////////////////////////////////////////////////////////////////////////////
// Matching
// matching requires the type being matched against to implement a "matches"
// method which is called with the thing being matched
//
// match a {
// typeof string => print("it's a string"),
// 1 => print("one"),
// 2 | 3 | 5 | 7 | 11 => print("prime"),
// 13...19 => print("teen"),
// _ => print("ain't special")
// }
//
// gets converted into:
//
// if (typeof string).matches(a) {
// print("it's a string")
// }
// else if 1.matches(a) {
// print("one")
// }
// else if 2.matches(a) || 3.matches(a) || 5.matches(a)... {
// print("prime")
// }
// else if range(13,19).matches(a) {
// print("teen")
// }
// else {
// print("ain't special")
// }
//
//
// n: int
// match {
// n is not int: print("not an int!"),
// n % 15 == 0: print("fizzbuzz"),
// n % 5 == 0: print("fizz")<
// n % 3 == 0: print("buzz"),
// _: print(n)
// }
//
// this is syntactic sugar for `match true { ... }'
//
// if (n is not int).matches(true) {
// print("fizzbuzz")
// }
// else if (n % 15 == 0).matches(true) {
// print("fizzbuzz")
// }
// else if (n % 5 == 0).matches(true) {
// print("fizz")
// }
// else if (n % 3 == 0).matches(true) {
// print("buzz")
// }
// else {
// print(n)
// }
//
// ideally, `bool.matches(true)' will be optimized away
//
/////////////////////////////////////////////////////////////////////////////////