-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.tp
More file actions
202 lines (145 loc) · 3.06 KB
/
Copy pathexample.tp
File metadata and controls
202 lines (145 loc) · 3.06 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
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
$MEM-GC
pub sct Student {
cstr name.
mui8 age.
mui8 grades.
}
pub enm Result {
Pass.
Fail.
}
err TestError {
mstr message.
}
operator +(Student a, Student b)!cui8 {
exit 8.
}
fc factorial(cui8 n)!cui8 {
if (n <= 1) {
exit 1.
}
exit n * factorial(n - 1).
}
fc greet(cstr name = "User")!cstr {
exit "Hello".
}
fc overloaded(cui8 x)!cui8 {
exit x + 1.
}
fc overloaded(cstr x)!cstr {
exit x.
}
fc calculate_average(mui8[] grades)!cf32 {
val mui8 total = 0.
for (grade : grades) {
total += grade.
}
exit total >> cf32 / 3.
}
fc get_result(cf32 average)!Result {
if (average >= 50) {
exit Result::Pass.
}
elif (average == 49) {
exit Result::Fail.
}
else {
exit Result::Fail.
}
}
fc main()!void {
// constants
val cstr school = "Teapot Academy".
// primitive types
val cbln passed = true.
val cchar letter = "A".
val csi32 signed_num = -5.
val cui32 unsigned_num = 5.
val cf32 float32 = 3.14.
val cf64 float64 = 3.14159265359.
val cdml money = 12.34.
val caint huge = 999999999999999999999999.
// null initialisation then modified
val mui8 counter.
counter += 1.
// structs
val Student student = Student(
"Alex",
15
).
student::grades = [80, 65, 90].
// references
val ref Student student_ref = student.
// tuple
val pair = (
10,
"hello"
).
// list
val list<ui8> numbers.
// map
val map[str]ui8 subjects = (
["Math",90]
["English",85]
).
// arrays
val mui8[] values = [1,2,3].
do {
val cf32 average = calculate_average(
student::grades
).
val Result result = get_result(
average
).
if (result == Result::Pass) {
}
if (
passed &&
average > 0 ||
~false
) {
}
// modified in loop
val mui8 attempts = 3.
while (attempts > 0) {
if (attempts == 2) {
continue.
}
if (attempts == 1) {
break.
}
attempts -= 1.
}
val cstr age_text =
student::age >> cstr.
val cstr first_letter =
student::name[0] >> cstr.
val cui8 fact = factorial(5).
val cstr msg1 = greet().
val cstr msg2 =
greet("Alex").
val cui8 a =
overloaded(5).
val cstr b =
overloaded("hello").
val Student merged =
student + student.
// modified later
val mui8 math =
(5 + 2) * 3.
math /= 2.
math *= 2.
math -= 1.
math += 1.
if (math == 10) {}
if (math ~= 10) {}
if (math > 10) {}
if (math < 10) {}
if (math >= 10) {}
if (math <= 10) {}
}
fail(TestError e) {
val cstr error_text =
e::message.
}
}