revamped switch and match syntax

This commit is contained in:
mark
2023-05-26 20:02:46 +02:00
parent be9403d63d
commit 45f6f30de3
9 changed files with 370 additions and 423 deletions

View File

@@ -4,14 +4,14 @@ text := "𝔠𝔥 𝔨𝔞𝔫𝔫 𝔡𝔞𝔰 𝔳𝔢𝔯𝔡𝔞𝔪𝔪
fn random(min int max int) {
res := run_command("fish" ["-c" "random " + min.to_string() + " " + max.to_string() ...])
switch res {
[[]/int string string] res.1.trim().parse_int().assume1()
[[]/int string string] res res.1.trim().parse_int().assume1()
}
}
fn rnd() {
r := random(5 15)
switch r {
int r
[] 10
int r r
[] [] 10
}
}

View File

@@ -6,11 +6,11 @@ fn print_linked_list(start elem) {
println(start.0.to_string())
elem := start.1
switch! elem {
[] {
[] elem {
println("[END]")
true // break
}
elem &start = elem // continue
elem elem &start = elem // continue
}
}
[]

View File

@@ -7,11 +7,11 @@ fn map_string_to_int(list [string ...] func fn((string int))) {
elem := &list.remove(0)
switch! elem {
// if the element was present, run the map function to convert it from a string to an int and return the result
[string] {
[func.run(elem.0)]
[string] [elem] {
[func.run(elem)]
}
// if there are no more elements, return something that doesn't match.
[] []
[] [] []
}
}
}

View File

@@ -0,0 +1,19 @@
x := if true [10 "my text"] else 10
switch! x {
[int string] [num text] {
num.debug()
text.debug()
}
int num {
println("number:")
num.debug()
}
}
text := "12.5"
match {
parse_int(text) num println("int: " + num.to_string())
parse_float(text) num println("float: " + num.to_string())
true [] println("not a number: " + text)
}