louise
The Louise language: compiler, runtime, std, tooling
Clone avec HTTPS, un jeton personnel en mot de passe, ou avec SSH, une clé ajoutée dans vos paramètres.
Dernier runRéussi#125 · main · 16 min 13 s
0Étoiles
0Forks
40Issues
| 1 | // Early `return` across a `use` callback, `defer`, `handle` and `region` |
| 2 | // bodies (ADR 0037): the function is left through the code in between, |
| 3 | // whose cleanups run once, and nothing leaks. |
| 4 | |
| 5 | import std/io |
| 6 | import std/list |
| 7 | import std/ref |
| 8 | import std/result |
| 9 | |
| 10 | /// The first number over 10, found inside a `use` callback. |
| 11 | fn first_big :: xs: List<Int> -> Option<Int> |
| 12 | { |
| 13 | { |
| 14 | use x <- list.each xs |
| 15 | if x > 10 |
| 16 | { |
| 17 | return Some(x) |
| 18 | } |
| 19 | } |
| 20 | None |
| 21 | } |
| 22 | |
| 23 | fn parse :: s: String -> Result<Int, String> |
| 24 | { |
| 25 | match s { "one" -> Ok(1), "two" -> Ok(2), _ -> Error("not a number: {s}") } |
| 26 | } |
| 27 | |
| 28 | /// Nested `use`s: a `return` in the inner callback leaves both. |
| 29 | fn sum :: a: String b: String -> Result<Int, String> |
| 30 | { |
| 31 | use x <- result.try (parse a) |
| 32 | use y <- result.try (parse b) |
| 33 | if x == y |
| 34 | { |
| 35 | return Error("the same number twice") |
| 36 | } |
| 37 | Ok(x + y) |
| 38 | } |
| 39 | |
| 40 | /// The cleanup runs once, whichever way the body is left. |
| 41 | fn guarded :: n: Int -> String / <io> |
| 42 | { |
| 43 | defer |
| 44 | { |
| 45 | io.println "cleanup for {n}" |
| 46 | } |
| 47 | if n < 0 |
| 48 | { |
| 49 | return "negative" |
| 50 | } |
| 51 | "fine" |
| 52 | } |
| 53 | |
| 54 | effect ask |
| 55 | { |
| 56 | ctl ask :: -> Int |
| 57 | } |
| 58 | |
| 59 | /// Out of a `handle` body: the handler's `return` clause doesn't run. |
| 60 | fn answered :: limit: Int -> String |
| 61 | { |
| 62 | let n = handle |
| 63 | { |
| 64 | let answer = ask () |
| 65 | if answer > limit |
| 66 | { |
| 67 | return "too big: {answer}" |
| 68 | } |
| 69 | answer |
| 70 | } |
| 71 | with |
| 72 | { ask -> resume 42, return v -> v * 10 } |
| 73 | "answered {n}" |
| 74 | } |
| 75 | |
| 76 | /// Out of a `region`, with a reference created inside. |
| 77 | fn counted :: xs: List<Int> stop: Int -> Int |
| 78 | { |
| 79 | region |
| 80 | { |
| 81 | let total = ref.new 0 |
| 82 | list.each xs fn x { ref.set total (ref.get total + x) } |
| 83 | if ref.get total > stop |
| 84 | { |
| 85 | return -1 |
| 86 | } |
| 87 | ref.get total |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | effect amb |
| 92 | { |
| 93 | multi ctl choose :: -> Bool |
| 94 | } |
| 95 | |
| 96 | fn all :: action: fn() -> a / <amb | e> -> List<a> / <e> |
| 97 | { |
| 98 | handle |
| 99 | { |
| 100 | [action ()] |
| 101 | } |
| 102 | with |
| 103 | { choose -> list.append (resume True) (resume False) } |
| 104 | } |
| 105 | |
| 106 | /// A multi-shot handler inside the function: a `return` from one branch |
| 107 | /// leaves the function, and the other branch never runs. |
| 108 | fn first_choice :: -> List<String> |
| 109 | { |
| 110 | let results = |
| 111 | all |
| 112 | fn |
| 113 | { |
| 114 | if choose () |
| 115 | { |
| 116 | "left" |
| 117 | } |
| 118 | else |
| 119 | { |
| 120 | "right" |
| 121 | } |
| 122 | } |
| 123 | { |
| 124 | use r <- list.each results |
| 125 | return [r, "first"] |
| 126 | } |
| 127 | [] |
| 128 | } |
| 129 | |
| 130 | pub fn main :: -> () / <io> |
| 131 | { |
| 132 | io.println "{first_big [3, 12, 40]} {first_big [1, 2]}" |
| 133 | io.println "{sum "one" "two"} {sum "two" "two"} {sum "one" "six"}" |
| 134 | io.println (guarded (-1)) |
| 135 | io.println (guarded 1) |
| 136 | io.println "{answered 100} / {answered 10}" |
| 137 | io.println "{counted [1, 2, 3] 10} {counted [5, 6, 7] 10}" |
| 138 | io.println "{first_choice ()}" |
| 139 | } |
| 140 | //> Some(12) None |
| 141 | //> Ok(3) Error(the same number twice) Error(not a number: six) |
| 142 | //> cleanup for -1 |
| 143 | //> negative |
| 144 | //> cleanup for 1 |
| 145 | //> fine |
| 146 | //> answered 420 / too big: 42 |
| 147 | //> 6 -1 |
| 148 | //> [left, first] |