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.

71 lignes · 1388 octetsBrut · Historique
1// Record workers (ADR 0069, issue #337): a single-constructor record's
2// fields become C parameters at a worker's boundary. Covered: a
3// worker's self-recursion (flat throughout), a worker calling a worker,
4// an escaping record (boxed at the escape), a borrowed record
5// parameter, and a function whose record cell is reused (FBIP), which
6// keeps the boxed path.
7
8import std/io
9import std/list
10
11type Point { Point(x: Int, y: Int) }
12
13fn move_x :: p: Point by: Int -> Point
14{
15 match p { Point(x, y) -> Point(x + by, y) }
16}
17
18fn norm2 :: p: Point -> Int
19{
20 match p { Point(x, y) -> x * x + y * y }
21}
22
23fn walk :: p: Point n: Int -> Point
24{
25 if n == 0
26 {
27 p
28 }
29 else
30 {
31 walk (move_x p 1) (n - 1)
32 }
33}
34
35// The record escapes into a list: boxed there, read back by a call.
36fn kept :: p: Point -> List<Point>
37{
38 [p, move_x p 10]
39}
40
41// The cell is reused (FBIP): no record worker, the boxed path stays.
42fn bump :: p: Point -> Point
43{
44 match p { Point(x, y) -> Point(x + 1, y + 1) }
45}
46
47fn bumps :: p: Point n: Int -> Point
48{
49 if n == 0
50 {
51 p
52 }
53 else
54 {
55 bumps (bump p) (n - 1)
56 }
57}
58
59pub fn main :: -> () / <io>
60{
61 let p = Point(3, 4)
62 io.println (show (norm2 p))
63 io.println (show (norm2 (walk p 1000)))
64 let ps = kept p
65 io.println (show (list.map ps fn q { norm2 q }))
66 io.println (show (norm2 (bumps p 100)))
67}
68//> 25
69//> 1006025
70//> [25, 185]
71//> 21425