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.

100 lignes · 1697 octetsBrut · Historique
1import std/io
2import std/process.{type proc}
3import std/ref
4
5// `defer` cleanups (ADR 0016) may use effects whose operations can neither
6// unwind nor suspend: `io`, `ffi` and references.
7
8effect log
9{
10 fn log :: message: String -> ()
11}
12
13effect gen
14{
15 ctl yield :: x: Int -> ()
16}
17
18effect fail
19{
20 final ctl fail :: reason: String -> Never
21}
22
23pub fn prints :: -> Int / <io>
24{
25 defer io.println "bye"
26 1
27}
28
29pub fn restores :: -> Int
30{
31 region
32 {
33 let r = ref.new 0
34 defer ref.set r 0
35 ref.set r 1
36 ref.get r
37 }
38}
39
40// The block's value is the value of what follows the `defer`.
41pub fn value :: -> String / <io>
42{
43 defer io.println "bye"
44 "kept"
45}
46//~^^^ TYPE value: fn() -> String / <io>
47
48pub fn may_panic :: -> Int
49{
50 defer panic "allowed: panics unwind anyway"
51 1
52}
53
54pub fn logs :: -> () / <log>
55{
56 defer log "bye" //~ ERROR E0506 cannot perform `log`
57 ()
58}
59
60pub fn generates :: -> () / <gen>
61{
62 defer yield 1 //~ ERROR E0506 cannot perform `gen`
63 ()
64}
65
66pub fn fails :: -> () / <fail>
67{
68 defer fail "again" //~ ERROR E0506 cannot perform `fail`
69 ()
70}
71
72pub fn messages :: -> () / <proc>
73{
74 defer process.sleep 1 //~ ERROR E0506 cannot perform `proc`
75 ()
76}
77
78// A callback could do anything: its effects must be known (as in a
79// lambda, a declared row variable is not allowed here).
80pub fn calls :: action: fn() -> () / <e> -> () / <e>
81{
82 defer action () //~ ERROR E0506 cannot call code with unknown effects
83 ()
84}
85
86// An inferred callback becomes one without effects.
87fn twice :: cleanup
88{
89 defer cleanup ()
90 defer cleanup ()
91 ()
92}
93//~^^^^^ TYPE twice: fn(fn() -> ()) -> ()
94
95// The cleanup is unit.
96pub fn returns_unit :: -> ()
97{
98 defer 1 //~ ERROR E0202
99 ()
100}