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.

151 lignes · 3187 octetsBrut · Historique
1import std/io
2import std/list
3import std/process.{type proc, Crashed, Down}
4
5// Reuse (issue #34) while the stack unwinds or is suspended: a cell kept
6// for reuse, or fields moved out of a released cell, must be dropped
7// exactly once when a `final ctl`, a cancelled `ctl` body or a panic
8// leaves the frame holding them.
9
10effect fail
11{
12 final ctl fail :: reason: String -> Never
13}
14
15effect gen
16{
17 ctl yield :: x: Int -> ()
18}
19
20fn attempt :: action: fn() -> a / <fail | e> -> Result<a, String> / <e>
21{
22 handle
23 {
24 Ok(action ())
25 }
26 with
27 { fail reason -> Error(reason) }
28}
29
30fn render :: r: Result<List<Int>, String> -> String
31{
32 match r { Ok(xs) -> "ok " <> show xs, Error(e) -> "failed: " <> e }
33}
34
35// `map` keeps each matched cell for the cell it builds after calling `f`:
36// when `f` unwinds, every frame drops the cell it kept.
37fn checked :: x
38{
39 if x == 7
40 {
41 fail "seven"
42 }
43 else
44 {
45 x * 3
46 }
47}
48
49// A tuple parameter only read is borrowed; the failure unwinds through
50// the caller that still owns it.
51fn scaled :: p xs
52{
53 let (factor, limit) = p
54 list.map
55 xs
56 fn x
57 {
58 if x > limit
59 {
60 fail ("over " <> show limit)
61 }
62 else
63 {
64 x * factor
65 }
66 }
67}
68
69// The sum of everything the action yields.
70fn total :: action: fn() -> () / <gen | e> -> Int / <e>
71{
72 handle
73 {
74 action ()
75 }
76 with
77 { yield x -> x + resume (), return _ -> 0 }
78}
79
80// The first value yielded; the body is cancelled, never resumed.
81fn first :: action: fn() -> () / <gen | e> -> Int / <e>
82{
83 handle
84 {
85 action ()
86 }
87 with
88 { yield x -> x, return _ -> -1 }
89}
90
91fn yielding :: xs
92{
93 let ys =
94 list.map
95 xs
96 fn x
97 {
98 yield x
99 x + 1
100 }
101 yield (list.fold ys 0 fn a b { a + b })
102}
103
104pub fn main :: -> () / <io, proc>
105{
106 io.println (render (attempt fn { list.map (list.range 1 5) checked }))
107 io.println (render (attempt fn { list.map (list.range 1 20) checked }))
108 let shared = list.range 1 10
109 io.println
110 ( render (attempt fn { list.map shared checked }) <> " "
111 <> show (list.length shared)
112 )
113 io.println (render (attempt fn { scaled (2, 100) (list.range 1 4) }))
114 io.println (render (attempt fn { scaled (2, 3) (list.range 1 4) }))
115 // Suspended while holding kept cells, then resumed or cancelled.
116 io.println (show (total fn { yielding (list.range 1 4) }))
117 io.println (show (first fn { yielding (list.range 1 4) }))
118 // An actor panics in the middle of a map over its own list.
119 let worker =
120 {
121 use <- process.spawn
122 let xs =
123 list.map
124 (list.range 1 1000)
125 fn x
126 {
127 if x == 500
128 {
129 panic ("worker failed at " <> show x)
130 }
131 else
132 {
133 x + 1
134 }
135 }
136 io.println ("unreachable " <> show (list.length xs))
137 }
138 let Down(_, why) = process.receive_forever (process.monitor worker)
139 match why
140 { Crashed(m) -> io.println ("worker down: " <> m)
141 , _ -> io.println "worker stopped"
142 }
143}
144//> ok [3, 6, 9, 12, 15]
145//> failed: seven
146//> failed: seven 10
147//> ok [2, 4, 6, 8]
148//> failed: over 3
149//> 24
150//> 1
151//> worker down: worker failed at 500