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 | import std/io |
| 2 | import std/list |
| 3 | |
| 4 | // `_` placeholders: pipes and captures (ADR 0023). |
| 5 | |
| 6 | type Point { Point(x: Int, y: Int) } |
| 7 | |
| 8 | fn add :: x: Int y: Int -> Int |
| 9 | { |
| 10 | x + y |
| 11 | } |
| 12 | |
| 13 | fn add3 :: x: Int y: Int z: Int -> Int |
| 14 | { |
| 15 | x + y + z |
| 16 | } |
| 17 | |
| 18 | // A capture is a one-parameter function, typed like the `fn` it stands for. |
| 19 | pub fn increment :: -> fn(Int) -> Int |
| 20 | { |
| 21 | add 1 _ |
| 22 | } |
| 23 | |
| 24 | fn captured :: |
| 25 | { |
| 26 | add 1 _ |
| 27 | } |
| 28 | //~^ TYPE captured: fn() -> fn(Int) -> Int / <e> |
| 29 | |
| 30 | fn written_out :: |
| 31 | { |
| 32 | fn v { add 1 v } |
| 33 | } |
| 34 | //~^ TYPE written_out: fn() -> fn(Int) -> Int / <e> |
| 35 | |
| 36 | // Its effects are the call's, performed when it is called. |
| 37 | pub fn print_all :: xs: List<String> -> () / <io> |
| 38 | { |
| 39 | list.each xs (io.println _) |
| 40 | } |
| 41 | |
| 42 | pub fn print_none :: xs: List<String> -> () |
| 43 | { |
| 44 | list.each xs (io.println _) //~ ERROR E0502 |
| 45 | } |
| 46 | |
| 47 | pub fn placed :: xs: List<Int> -> List<Int> |
| 48 | { |
| 49 | xs |> list.map (add 10 _) |
| 50 | } |
| 51 | |
| 52 | pub fn labelled :: y: Int -> Point |
| 53 | { |
| 54 | y |> Point(x: 0, y: _) |
| 55 | } |
| 56 | |
| 57 | // A wrong placement is a type error, as for any argument. |
| 58 | pub fn misplaced :: s: String -> Int |
| 59 | { |
| 60 | s |> add 1 _ //~ ERROR E0202 |
| 61 | } |
| 62 | |
| 63 | fn two_holes :: |
| 64 | { |
| 65 | add3 1 _ _ //~ ERROR E0116 only one `_` |
| 66 | } |
| 67 | |
| 68 | pub fn piped_two_holes :: x: Int -> Int |
| 69 | { |
| 70 | x |> add3 _ 1 _ //~ ERROR E0116 only one `_` |
| 71 | } |
| 72 | |
| 73 | pub fn with_spread :: p: Point -> Point |
| 74 | { |
| 75 | 1 |> Point(x: _, ..p) //~ ERROR E0116 spread |
| 76 | } |
| 77 | |
| 78 | pub fn not_an_argument :: -> Int |
| 79 | { |
| 80 | let x = _ //~ ERROR E0116 call's arguments |
| 81 | x |
| 82 | } |
| 83 | |
| 84 | pub fn in_a_list :: -> List<Int> |
| 85 | { |
| 86 | [1, _] //~ ERROR E0116 call's arguments |
| 87 | } |
| 88 | |
| 89 | pub fn in_an_operator :: -> Int |
| 90 | { |
| 91 | 1 + _ //~ ERROR E0116 call's arguments |
| 92 | } |
| 93 | |
| 94 | pub fn piped_into_hole :: x: Int -> Int |
| 95 | { |
| 96 | x |> _ //~ ERROR E0116 call's arguments |
| 97 | } |
| 98 | |
| 99 | pub fn in_use :: xs: List<String> -> () / <io> |
| 100 | { |
| 101 | use x <- list.each _ //~ ERROR E0116 `use` |
| 102 | io.println x |
| 103 | } |
| 104 | |
| 105 | // A capture's arity is its callee's: here one argument too many. |
| 106 | fn wrong_arity :: |
| 107 | { |
| 108 | add 1 2 _ //~ ERROR E0205 |
| 109 | } |