diff --git a/examples/02_Calc_Sum.mers b/examples/02_Calc_Sum.mers index d30fce2..482a83a 100755 --- a/examples/02_Calc_Sum.mers +++ b/examples/02_Calc_Sum.mers @@ -1,10 +1,10 @@ total := 0.0 -().loop(() -> { +loop { ("Total: ", total, ". Type a number to change.").concat.println ().read_line.trim.parse_float.try(( - n -> &total = (total, n).sum, + (n) -> &total = (total, n).sum // not a number, so return a 1-tuple to break from the loop () -> (()) )) -}) +} "Goodbye.".println diff --git a/examples/03_Basic_Calculator.mers b/examples/03_Basic_Calculator.mers index d0cabc8..4018ded 100755 --- a/examples/03_Basic_Calculator.mers +++ b/examples/03_Basic_Calculator.mers @@ -5,12 +5,12 @@ current := 0.0 -().loop(() -> { +loop { ("[ ", current, " ]").concat.println input := ().read_line.trim num := (input, 1).substring.trim.parse_float.try(( - () -> 0.0, - val -> val + (val) -> val + () -> 0.0 )) mode := input.substring(0, 1) if mode.eq("+") { @@ -26,4 +26,4 @@ current := 0.0 } else if (input.eq("exit"), input.eq("")).any { (()) } -}) +} diff --git a/examples/04_Greatest_Common_Divisor.mers b/examples/04_Greatest_Common_Divisor.mers index 2a5cccf..d2845ac 100755 --- a/examples/04_Greatest_Common_Divisor.mers +++ b/examples/04_Greatest_Common_Divisor.mers @@ -1,5 +1,5 @@ gcd := vals -> { - ().loop(() -> { + loop { (a, b) := vals if a.eq(b) (a) @@ -7,21 +7,17 @@ gcd := vals -> { &vals = (a, b.subtract(a)) else &vals = (a.subtract(b), b) - }) + } } get_num := () -> { - line := ().read_line.trim - ( - line.parse_float, - ( - () -> { - ("error: '", line, "' not a number!").concat.println - 1.panic - } - n -> n, - ) - ).try + loop { + line := ().read_line.trim + line.parse_float.try(( + (n) -> (n) + () -> ("Error: '", line, "' not a number!").concat.println + )) + } } ("gcd of 899 and 2900 is ", (899, 2900).gcd).concat.println // 29 diff --git a/examples/05_Matrix_Multiplicator.mers b/examples/05_Matrix_Multiplicator.mers index 6bf475c..e0ecefa 100755 --- a/examples/05_Matrix_Multiplicator.mers +++ b/examples/05_Matrix_Multiplicator.mers @@ -1,53 +1,9 @@ -// if string is a + delim + b, returns (a, b), -// otherwise returns (). b can contain delim. -split_once := (s, delim) -> { - ( - (s, delim).index_of, - ( - index -> ( - (s, 0, index).substring, - (s, (index, delim.len).sum).substring - ) - () -> () - ) - ).try -} - -// repeats split_once until no delim is left and returns a list of strings -split_ := (s, delim, require_nonempty) -> { - out := (s).as_list - &out.pop - ().loop( - () -> s.split_once(delim).try(( - (s1, s2) -> { - &s = s2 - if s1.len.signum.eq(1) - &out.push(s1) - } - () -> { - if s.len.signum.eq(1) - &out.push(s) - (()) - } - )) - ) - out -} - -split := (s, delim) -> s.split_(delim, true) - parse_matrix_line := input -> { - vals := input.split(" ") + vals := input.str_split(" ") vals_len := vals.len // reverse vals vals_rev := {() -> &vals.pop}.as_list - nums := {() -> &vals_rev.pop.try(( - () -> () - (v) -> v.parse_float.try(( - () -> () - v -> (v) - )) - ))}.as_list + nums := vals_rev.filter_map(v -> v.parse_float).as_list if nums.len.eq(vals_len) if nums.len.signum.eq(1) (nums) @@ -58,15 +14,16 @@ parse_matrix_line := input -> { read_matrix_line := width -> { ().read_line.trim.parse_matrix_line.try(( (line) -> { + line := [List] line w := width.deref if w.eq(0) { width = line.len } else { - ().loop(() -> + loop { if line.len.subtract(w).signum.eq(-1) { &line.push(0.0) } else (()) - ) + } } (line) } @@ -89,12 +46,12 @@ matrix_get := (matrix, (line, col)) -> { leftpad := (str, l) -> { str := (str).concat d := l.subtract(str.len) - ().loop(() -> + loop { if d.signum.eq(1) { &str = (" ", str).concat &d = d.subtract(1) } else (()) - ) + } str } diff --git a/examples/06_TicTacToe.mers b/examples/06_TicTacToe.mers index 5137365..c280a87 100644 --- a/examples/06_TicTacToe.mers +++ b/examples/06_TicTacToe.mers @@ -55,14 +55,14 @@ check_board := () -> { else if (f3.eq(0).eq(false), (f3, f5, f7).eq).all f3 } -().loop(() -> { +loop { (if player c_blue else c_green, "Turn: ", if player "x" else "o", c_reset).concat.println ().print_board - ().loop(() -> { + loop { (if player c_blue else c_green, "> ", c_gray).concat.print input := ().read_line.trim input.parse_int.try(( - field -> { + (field) -> { if (field.gt(0), field.ltoe(9)).all { (l, m, r) := if field.gt(6) (&f1, &f2, &f3) else if field.gt(3) (&f4, &f5, &f6) else (&f7, &f8, &f9) col := field.subtract(1).modulo(3) @@ -77,7 +77,7 @@ check_board := () -> { } () -> "not a number!".println )) - }) + } &player = player.eq(false) ().check_board.try(( () -> (), @@ -94,4 +94,4 @@ check_board := () -> { (()) } )) -}) +} diff --git a/examples/07_Threads.mers b/examples/07_Threads.mers index 26da3e2..ced4558 100644 --- a/examples/07_Threads.mers +++ b/examples/07_Threads.mers @@ -1,20 +1,20 @@ is_prime := n -> { 0.1.sleep // wow, what an inefficient algorithm, oh no d := 1 - ().loop(() -> { + loop { &d = (d, 1).sum if d.lt(n) { if n.modulo(d).eq(0) (false) } else (true) - }) + } } find_primes := start_at -> { - () -> (().loop(() -> { + () -> (loop { out := if start_at.is_prime (start_at) &start_at = (start_at, 1).sum out - })) + }) } primes_count := 0 @@ -24,10 +24,10 @@ background_thread := {() -> }.thread // Show status to the user while the background thread works -().loop(() -> { +loop { 0.2.sleep (" Found ", primes_count, " primes...\r").concat.print if background_thread.thread_finished (()) -}) +} ("\nPrimes: ", background_thread.thread_await).concat.println diff --git a/examples/08_Type_Annotations.mers b/examples/08_Type_Annotations.mers index de5b233..c2e2c2e 100644 --- a/examples/08_Type_Annotations.mers +++ b/examples/08_Type_Annotations.mers @@ -77,4 +77,4 @@ make_list_maybe_none := (val_1, val_2) -> { maybe_none_list := ("one", 2).make_list_maybe_none maybe_none_list.println -maybe_none_list // use `--check only` to see the type: `List` +maybe_none_list // use `check` to see the type: `List` diff --git a/examples/fib.mers b/examples/fib.mers index 2dc7f61..ea54fe3 100755 --- a/examples/fib.mers +++ b/examples/fib.mers @@ -1,7 +1,7 @@ fib := n -> { // we start with these two values v := (0, 1) - {() -> { + loop { // subtract 1 from n &n = (n, -1).sum // extract the latest values @@ -17,7 +17,7 @@ fib := n -> { // so we break with the latest value (r) } - }}.loop + } } ((1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 25, 50, 100), i -> { diff --git a/mers_lib/src/parsing/types.rs b/mers_lib/src/parsing/types.rs index 97880ae..5dfa643 100755 --- a/mers_lib/src/parsing/types.rs +++ b/mers_lib/src/parsing/types.rs @@ -60,14 +60,14 @@ pub fn parse_single_type(src: &mut Source, srca: &Arc) -> Result { - src.next_char(); - break; - } - Some(',') => { + Some(',' | ')') => { + let last = src.peek_char().is_some_and(|c| c == ')'); if inner_f.is_empty() { inner_t.push(t); src.next_char(); + if last { + break; + } } else { let pos1 = src.get_pos(); src.next_char();