Repository navigation
Expand file tree
/
Copy pathall.sol
More file actions
240 lines (212 loc) · 7.52 KB
/
Copy pathall.sol
File metadata and controls
240 lines (212 loc) · 7.52 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
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
// ============================================================
// SOL - Simple demonstration..
// ============================================================
fun main(): nothing
std.outln("=== SOL Data Types Demo ===")
std.outln("")
// --- Integer Types ---
std.outln("--- Integer Types ---")
imm a: integer -> 42
std.outln("integer: " + type.toString(a))
imm b: int16 -> 100
std.outln("int16: " + type.toString(b))
imm c: int32 -> 1000
std.outln("int32: " + type.toString(c))
imm d: int64 -> 10000
std.outln("int64: " + type.toString(d))
std.outln("")
// --- Float Types ---
std.outln("--- Float Types ---")
imm pi: float -> 3.14159
std.outln("float: " + type.toString(pi))
imm f32: float32 -> 2.5
std.outln("float32: " + type.toString(f32))
imm f64: float64 -> 9.8
std.outln("float64: " + type.toString(f64))
std.outln("")
// --- Boolean ---
std.outln("--- Boolean ---")
imm t: boolean -> true
imm f: boolean -> false
std.outln("true: " + type.toString(t))
std.outln("false: " + type.toString(f))
std.outln("")
// --- String ---
std.outln("--- String ---")
imm greeting: string -> "Hello, SOL!"
std.outln("string: " + greeting)
std.outln("upper: " + string.upper(greeting))
std.outln("lower: " + string.lower(greeting))
std.outln("length: " + type.toString(string.length(greeting)))
std.outln("trim: \"" + string.trim(" spaces ") + "\"")
std.outln("")
// --- Any ---
imm anyType: any -> "any"
// --- Char ---
std.outln("--- Char ---")
imm ch: char -> 'X'
std.outln("char: " + ch)
std.outln("")
// --- Number Literals ---
std.outln("--- Number Literals ---")
imm hexVal: integer -> 0xFF
std.outln("hex 0xFF: " + type.toString(hexVal))
imm octVal: integer -> 0o755
std.outln("oct 0o755: " + type.toString(octVal))
imm binVal: integer -> 0b1010
std.outln("bin 0b1010: " + type.toString(binVal))
std.outln("")
// --- Type Conversions ---
std.outln("--- Type Conversions ---")
imm strFromInt: string -> type.toString(42)
std.outln("toString(42): " + strFromInt)
imm intFromStr: integer -> type.strToInteger("123")
std.outln("strToInteger(\"123\"): " + type.toString(intFromStr))
imm floatFromInt: float -> type.iToFloat(5)
std.outln("iToFloat(5): " + type.toString(floatFromInt))
imm intFromFloat: integer -> type.flToInt(3.14)
std.outln("flToInt(3.14): " + type.toString(intFromFloat))
imm hexStr: string -> type.iToHex(255)
std.outln("iToHex(255): " + hexStr)
imm octStr: string -> type.iToOct(8)
std.outln("iToOct(8): " + octStr)
imm boolFromInt: boolean -> type.intToBool(1)
std.outln("intToBool(1): " + type.toString(boolFromInt))
imm intFromBool: integer -> type.boolToInt(true)
std.outln("boolToInt(true): " + type.toString(intFromBool))
std.outln("")
// --- List ---
std.outln("--- List ---")
imm nums: list -> [1, 2, 3, 4, 5]
std.outln("list: " + type.toString(nums))
std.outln("len: " + type.toString(list.len(nums)))
std.outln("first: " + type.toString(list.first(nums)))
std.outln("last: " + type.toString(list.last(nums)))
imm pushed: list -> list.push(nums, 6)
std.outln("push(6): " + type.toString(pushed))
imm sorted: list -> list.sort([3, 1, 4, 1, 5])
std.outln("sort: " + type.toString(sorted))
imm reversed: list -> list.reverse(nums)
std.outln("reverse: " + type.toString(reversed))
std.outln("")
// --- Map ---
std.outln("--- Map ---")
imm user: map -> {}
user -> map.set(user, "name", "Alice")
user -> map.set(user, "age", 30)
std.outln("map: " + type.toString(user))
std.outln("get name: " + map.get(user, "name"))
std.outln("get age: " + type.toString(map.get(user, "age")))
std.outln("keys: " + type.toString(map.keys(user)))
std.outln("len: " + type.toString(map.len(user)))
std.outln("")
// --- Result ---
std.outln("--- Result ---")
imm okVal: Result<integer, string> -> Ok(42)
imm errVal: Result<integer, string> -> Err("something went wrong")
std.outln("Ok(42): " + type.toString(result.isOk(okVal)))
std.outln("unwrap Ok: " + type.toString(result.unwrap(okVal)))
std.outln("Err: " + type.toString(result.isErr(errVal)))
std.outln("unwrapOr Err: " + type.toString(result.unwrapOr(errVal, 0)))
std.outln("")
// --- Template ---
std.outln("--- Template ---")
templ Person
name: string
age: integer
end
imm alice: Person -> Person("Alice", 30)
std.outln("Person.name: " + alice.name)
std.outln("Person.age: " + type.toString(alice.age))
alice.age -> 31
std.outln("After birthday: " + type.toString(alice.age))
std.outln("")
// --- Data (immutable) ---
std.outln("--- Data ---")
data Point
x: float
y: float
end
imm p: Point -> Point(3.0, 4.0)
std.outln("Point.x: " + type.toString(p.x))
std.outln("Point.y: " + type.toString(p.y))
std.outln("")
// --- Nothing ---
std.outln("--- Nothing ---")
imm empty: nothing -> nothing
imm empty2: undefined -> undefined
imm empty3: void -> void
// All output None
std.outln("nothing: " + type.toString(empty))
std.outln("")
// --- Arithmetic & Logic ---
std.outln("--- Arithmetic & Logic ---")
std.outln("5 + 3 = " + type.toString(5 + 3))
std.outln("10 - 4 = " + type.toString(10 - 4))
std.outln("6 * 7 = " + type.toString(6 * 7))
std.outln("20 / 4 = " + type.toString(20 / 4))
std.outln("17 % 5 = " + type.toString(17 % 5))
std.outln("2 ** 8 = " + type.toString(2 ** 8))
std.outln("5 == 5: " + type.toString(5 == 5))
std.outln("5 != 3: " + type.toString(5 != 3))
std.outln("10 > 5: " + type.toString(10 > 5))
std.outln("true && false: " + type.toString(true && false))
std.outln("true || false: " + type.toString(true || false))
std.outln("!true: " + type.toString(!true))
std.outln("")
// --- Control Flow ---
std.outln("--- Control Flow ---")
imm score: integer -> 85
if (score >= 90)
std.outln("Grade: A")
elseif (score >= 80)
std.outln("Grade: B")
elseif (score >= 70)
std.outln("Grade: C")
else
std.outln("Grade: F")
end
nonimm counter: integer -> 3
while (counter > 0)
std.outln("countdown: " + type.toString(counter))
counter -> counter - 1
end
for (imm i: integer -> 0; i < 3; i -> i + 1)
std.outln("for loop i=" + type.toString(i))
end
imm fruits: list -> ["apple", "banana", "cherry"]
for (fruit in fruits)
std.outln("fruit: " + fruit)
end
std.outln("")
// --- Match ---
std.outln("--- Match ---")
imm dayNum: integer -> 3
match (dayNum)
1 -> std.outln("Monday")
2 -> std.outln("Tuesday")
3 -> std.outln("Wednesday")
_ -> std.outln("Other day")
end
std.outln("")
// --- Functions ---
std.outln("--- Functions ---")
std.outln("add(10, 20) = " + type.toString(add(10, 20)))
std.outln("greet(\"Bob\") = " + greet("Bob"))
std.outln("factorial(6) = " + type.toString(factorial(6)))
std.outln("")
std.outln("=== Demo Complete ===")
end
fun add(a: integer, b: integer): integer
ret a + b
end
fun greet(name: string): string
ret "Hello, " + name + "!"
end
fun factorial(n: integer): integer
if (n <= 1)
ret 1
end
ret n * factorial(n - 1)
end
main()