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 | import std/process.{type proc, Crashed} |
| 4 | import std/ref |
| 5 | |
| 6 | // `defer` (ADR 0016): a cleanup runs when the rest of its block is left, |
| 7 | // however it is left: normally, by `final ctl` unwinding, by cancellation |
| 8 | // of a `ctl` body that is never resumed, or because its actor is killed. |
| 9 | |
| 10 | effect fail |
| 11 | { |
| 12 | final ctl fail :: reason: String -> Never |
| 13 | } |
| 14 | |
| 15 | effect gen |
| 16 | { |
| 17 | ctl yield :: x: Int -> () |
| 18 | } |
| 19 | |
| 20 | effect log |
| 21 | { |
| 22 | fn log :: message: String -> () |
| 23 | } |
| 24 | |
| 25 | fn attempt :: action: fn() -> a / <fail | e> -> Result<a, String> / <e> |
| 26 | { |
| 27 | handle |
| 28 | { |
| 29 | Ok(action ()) |
| 30 | } |
| 31 | with |
| 32 | { fail reason -> Error(reason) } |
| 33 | } |
| 34 | |
| 35 | fn render :: r |
| 36 | { |
| 37 | match r { Ok(v) -> "ok " <> show v, Error(e) -> "failed: " <> e } |
| 38 | } |
| 39 | |
| 40 | // Cleanups run in reverse order; the block's value is the rest's value. |
| 41 | fn ordered :: -> Int / <io> |
| 42 | { |
| 43 | defer io.println "first deferred, runs last" |
| 44 | let xs = list.range 1 10 |
| 45 | defer |
| 46 | { |
| 47 | io.println |
| 48 | ("second deferred, runs first (" <> show (list.length xs) <> " held)") |
| 49 | } |
| 50 | list.length xs * 2 |
| 51 | } |
| 52 | |
| 53 | // Every frame on the way holds a cleanup and a list when `fail` unwinds. |
| 54 | fn deep :: n -> Int / <fail, io> |
| 55 | { |
| 56 | if n == 0 |
| 57 | { |
| 58 | fail "bottom" |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | let held = list.range 1 n |
| 63 | defer io.println |
| 64 | ("leaving " <> show n <> " holding " <> show (list.length held)) |
| 65 | deep (n - 1) + 1 |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | // The first value; the generator is cancelled, so its cleanup runs then. |
| 70 | fn first :: action: fn() -> () / <gen | e> -> Int / <e> |
| 71 | { |
| 72 | handle |
| 73 | { |
| 74 | action () |
| 75 | } |
| 76 | with |
| 77 | { yield x -> x, return _ -> -1 } |
| 78 | } |
| 79 | |
| 80 | pub fn main :: -> () / <io, proc> |
| 81 | { |
| 82 | io.println ("value " <> show (ordered ())) |
| 83 | io.println (render (attempt fn { deep 3 })) |
| 84 | let one = |
| 85 | { |
| 86 | use <- first |
| 87 | // Long enough to be preempted while the generator is cancelled. |
| 88 | defer io.println |
| 89 | ("generator cleaned up after " <> show (list.length (list.range 1 5000))) |
| 90 | yield 1 |
| 91 | io.println "never printed" |
| 92 | } |
| 93 | io.println ("first " <> show one) |
| 94 | // Cleanups may use references of enclosing regions. |
| 95 | let restored = region |
| 96 | { |
| 97 | let depth = ref.new 0 |
| 98 | let r: Result<Int, String> = |
| 99 | attempt |
| 100 | fn |
| 101 | { |
| 102 | ref.set depth 1 |
| 103 | defer ref.set depth 0 |
| 104 | fail "inside" |
| 105 | } |
| 106 | show (ref.get depth) <> " after " <> render r |
| 107 | } |
| 108 | io.println ("depth " <> restored) |
| 109 | // A killed actor unwinds: its cleanup runs before it is gone. |
| 110 | let worker = |
| 111 | { |
| 112 | use <- process.spawn |
| 113 | defer io.println "worker cleaned up" |
| 114 | let never: process.Subject<Int> = process.subject () |
| 115 | process.receive_forever never |
| 116 | () |
| 117 | } |
| 118 | process.sleep 1 |
| 119 | process.kill worker |
| 120 | process.sleep 1 |
| 121 | // A panic in a `fn` clause unwinds the body that performed it. |
| 122 | let (_, down) = |
| 123 | { |
| 124 | use <- process.spawn_monitor |
| 125 | handle |
| 126 | { |
| 127 | defer io.println "crasher cleaned up" |
| 128 | log "boom" |
| 129 | } |
| 130 | with |
| 131 | { log m -> panic m } |
| 132 | } |
| 133 | match process.receive_forever down |
| 134 | { process.Down(_, Crashed(m)) -> io.println ("crasher crashed: " <> m) |
| 135 | , _ -> io.println "crasher stopped" |
| 136 | } |
| 137 | io.println "done" |
| 138 | } |
| 139 | //> second deferred, runs first (10 held) |
| 140 | //> first deferred, runs last |
| 141 | //> value 20 |
| 142 | //> leaving 1 holding 1 |
| 143 | //> leaving 2 holding 2 |
| 144 | //> leaving 3 holding 3 |
| 145 | //> failed: bottom |
| 146 | //> generator cleaned up after 5000 |
| 147 | //> first 1 |
| 148 | //> depth 0 after failed: inside |
| 149 | //> worker cleaned up |
| 150 | //> crasher cleaned up |
| 151 | //> crasher crashed: boom |
| 152 | //> done |