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 | // Case-of-case (ADR 0043) moves a block's switch into the match on its |
| 2 | // value and deletes the block's `let`: only when the match is the |
| 3 | // value's only use. The uses an earlier rewrite gives the value count |
| 4 | // too (issue #425): an alias's (`aliased`: `xs`'s block ends in a block |
| 5 | // inlined into it) and an inlined function's parameter's (`inlined`: |
| 6 | // `pick` uses `t` twice). Counting only the match left the other use |
| 7 | // unbound, and `louise build` panicked. |
| 8 | |
| 9 | import std/io |
| 10 | |
| 11 | type T { A, B } |
| 12 | |
| 13 | // Recursive, so never inlined: its use of `t` stays a use. |
| 14 | fn deep :: t: T n: Int -> Int |
| 15 | { |
| 16 | if n <= 0 |
| 17 | { |
| 18 | match t { A -> 10, B -> 20 } |
| 19 | } |
| 20 | else |
| 21 | { |
| 22 | deep t (n - 1) |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | fn pick :: t: T -> Int |
| 27 | { |
| 28 | let r = match t { A -> 1, B -> 2 } |
| 29 | r + deep t 3 |
| 30 | } |
| 31 | |
| 32 | fn aliased :: n: Int -> () / <io> |
| 33 | { |
| 34 | let xs = match Some(n) |
| 35 | { None -> [61], Some(m) -> if m > 2 { [34] } else { [32] } } |
| 36 | io.println (match xs { [] -> "empty", [x, ..rest] -> "first " <> show x }) |
| 37 | io.println (show xs) |
| 38 | } |
| 39 | |
| 40 | fn inlined :: n: Int -> () / <io> |
| 41 | { |
| 42 | let t = if n > 26 { A } else { B } |
| 43 | io.println (show (pick t)) |
| 44 | } |
| 45 | |
| 46 | pub fn main :: -> () / <io> |
| 47 | { |
| 48 | aliased 4 |
| 49 | aliased 1 |
| 50 | inlined 39 |
| 51 | inlined 2 |
| 52 | } |
| 53 | //> first 34 |
| 54 | //> [34] |
| 55 | //> first 32 |
| 56 | //> [32] |
| 57 | //> 11 |
| 58 | //> 22 |