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/process.{type proc} |
| 3 | import std/ref |
| 4 | |
| 5 | // `defer` cleanups (ADR 0016) may use effects whose operations can neither |
| 6 | // unwind nor suspend: `io`, `ffi` and references. |
| 7 | |
| 8 | effect log |
| 9 | { |
| 10 | fn log :: message: String -> () |
| 11 | } |
| 12 | |
| 13 | effect gen |
| 14 | { |
| 15 | ctl yield :: x: Int -> () |
| 16 | } |
| 17 | |
| 18 | effect fail |
| 19 | { |
| 20 | final ctl fail :: reason: String -> Never |
| 21 | } |
| 22 | |
| 23 | pub fn prints :: -> Int / <io> |
| 24 | { |
| 25 | defer io.println "bye" |
| 26 | 1 |
| 27 | } |
| 28 | |
| 29 | pub 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`. |
| 41 | pub fn value :: -> String / <io> |
| 42 | { |
| 43 | defer io.println "bye" |
| 44 | "kept" |
| 45 | } |
| 46 | //~^^^ TYPE value: fn() -> String / <io> |
| 47 | |
| 48 | pub fn may_panic :: -> Int |
| 49 | { |
| 50 | defer panic "allowed: panics unwind anyway" |
| 51 | 1 |
| 52 | } |
| 53 | |
| 54 | pub fn logs :: -> () / <log> |
| 55 | { |
| 56 | defer log "bye" //~ ERROR E0506 cannot perform `log` |
| 57 | () |
| 58 | } |
| 59 | |
| 60 | pub fn generates :: -> () / <gen> |
| 61 | { |
| 62 | defer yield 1 //~ ERROR E0506 cannot perform `gen` |
| 63 | () |
| 64 | } |
| 65 | |
| 66 | pub fn fails :: -> () / <fail> |
| 67 | { |
| 68 | defer fail "again" //~ ERROR E0506 cannot perform `fail` |
| 69 | () |
| 70 | } |
| 71 | |
| 72 | pub 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). |
| 80 | pub 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. |
| 87 | fn twice :: cleanup |
| 88 | { |
| 89 | defer cleanup () |
| 90 | defer cleanup () |
| 91 | () |
| 92 | } |
| 93 | //~^^^^^ TYPE twice: fn(fn() -> ()) -> () |
| 94 | |
| 95 | // The cleanup is unit. |
| 96 | pub fn returns_unit :: -> () |
| 97 | { |
| 98 | defer 1 //~ ERROR E0202 |
| 99 | () |
| 100 | } |