tobogganSe connecter

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.

109 lignes · 1846 octetsBrut · Historique
1import std/io
2import std/list
3
4// `_` placeholders: pipes and captures (ADR 0023).
5
6type Point { Point(x: Int, y: Int) }
7
8fn add :: x: Int y: Int -> Int
9{
10 x + y
11}
12
13fn 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.
19pub fn increment :: -> fn(Int) -> Int
20{
21 add 1 _
22}
23
24fn captured ::
25{
26 add 1 _
27}
28//~^ TYPE captured: fn() -> fn(Int) -> Int / <e>
29
30fn 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.
37pub fn print_all :: xs: List<String> -> () / <io>
38{
39 list.each xs (io.println _)
40}
41
42pub fn print_none :: xs: List<String> -> ()
43{
44 list.each xs (io.println _) //~ ERROR E0502
45}
46
47pub fn placed :: xs: List<Int> -> List<Int>
48{
49 xs |> list.map (add 10 _)
50}
51
52pub 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.
58pub fn misplaced :: s: String -> Int
59{
60 s |> add 1 _ //~ ERROR E0202
61}
62
63fn two_holes ::
64{
65 add3 1 _ _ //~ ERROR E0116 only one `_`
66}
67
68pub fn piped_two_holes :: x: Int -> Int
69{
70 x |> add3 _ 1 _ //~ ERROR E0116 only one `_`
71}
72
73pub fn with_spread :: p: Point -> Point
74{
75 1 |> Point(x: _, ..p) //~ ERROR E0116 spread
76}
77
78pub fn not_an_argument :: -> Int
79{
80 let x = _ //~ ERROR E0116 call's arguments
81 x
82}
83
84pub fn in_a_list :: -> List<Int>
85{
86 [1, _] //~ ERROR E0116 call's arguments
87}
88
89pub fn in_an_operator :: -> Int
90{
91 1 + _ //~ ERROR E0116 call's arguments
92}
93
94pub fn piped_into_hole :: x: Int -> Int
95{
96 x |> _ //~ ERROR E0116 call's arguments
97}
98
99pub 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.
106fn wrong_arity ::
107{
108 add 1 2 _ //~ ERROR E0205
109}