-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-1.cl
132 lines (114 loc) · 3.26 KB
/
test-1.cl
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
(*
* Here is a file with a dumb math class
*)
class Main inherits IO {
main(): Object
{
{
out_string("Let's do some math!\n");
-- here is a math with 3
let math3: Math_with_X <- new Math_with_X in
{
math3 <- math3.init(3); -- set x to 3
let y: Int in
{
y <- math3.add_y(4); -- y = 3"4
let exp: Int in
{
exp <- math3@Math_with_X.exp_y(y); -- exp = 3^7
out_string("3+4 is: ");
out_int(y);
out_string("\n3^7 is: ");
out_int(exp);
out_string("\n");
};
y <- 3;
-- imbed another y in these lets, dispatch on void, div by 0
let math2: Math_with_X <- new Math_with_X in {
math2 <- math2.init(2);
let y: Int, n: General_Math, z: Int <-1, s: String <-"hey" in
{
n.add(2, y);
n <- new General_Math;
y <- math2.add_y(4); -- y = 2+4
z <- z/0;
s <- s.substr(4,10);
let exp: Int in
{
exp <- math2.exp_y(y); -- exp = 2^6
out_string("2+4 is: ");
out_int(y);
out_string("\n2^6 is: ");
out_int(exp);
out_string("\n");
};
};
};
};
};
}
};
};
-- there is a lot of curly braces there - jeez
class General_Math{
add(x: Int, y: Int): Int{
x + y
};
multiply(x: Int, y: Int): Int {
x * y
};
exp(x: Int, y: Int): Int {
let exp_result : Int <- 1 in {
while 0 < y loop
{
exp_result <- exp_result*x;
y <- y -1;
} pool;
exp_result;
}
};
};
-- do math with a number x
class Math_with_X inherits General_Math
{
this_x: Int;
init(x: Int): Math_with_X
{
{
if 0 < x then
this_x <- x
else
this_x <- 0
fi;
self;
}
};
add_y(y: Int): Int { this_x + y};
multiply_by_y(y: Int): Int
{
if y = 0 then
0
else
let result: Int in {
if 0 < y then
result <- add(this_x, multiply_by_y(y - 1))
else result fi;
result;
}
fi
};
exp_y(y: Int): Int
{
if y = 0 then
1
else
-- ooh recursion!
let result: Int in {
if 0 < y then
result <- multiply(this_x, exp_y(y - 1))
else result fi;
result;
}
fi
};
};