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#115 · main · 21 min 41 s
0Étoiles
0Forks
40Issues
| 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 | |
| 8 | import std/io |
| 9 | import std/list |
| 10 | |
| 11 | type Point { Point(x: Int, y: Int) } |
| 12 | |
| 13 | fn move_x :: p: Point by: Int -> Point |
| 14 | { |
| 15 | match p { Point(x, y) -> Point(x + by, y) } |
| 16 | } |
| 17 | |
| 18 | fn norm2 :: p: Point -> Int |
| 19 | { |
| 20 | match p { Point(x, y) -> x * x + y * y } |
| 21 | } |
| 22 | |
| 23 | fn 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. |
| 36 | fn 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. |
| 42 | fn bump :: p: Point -> Point |
| 43 | { |
| 44 | match p { Point(x, y) -> Point(x + 1, y + 1) } |
| 45 | } |
| 46 | |
| 47 | fn 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 | |
| 59 | pub 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 |