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 | |
| 4 | // User-defined derives (ADR 0019): a trait marked `@derivable f` is |
| 5 | // derived as `f` applied to the value's constructor, each field replaced |
| 6 | // by the trait's method applied to it. |
| 7 | |
| 8 | pub type Json |
| 9 | { JNull, JInt(Int), JStr(String), JArr(List<Json>), JObj(List<(String, Json)>) } |
| 10 | |
| 11 | @derivable json_of_constructor |
| 12 | trait ToJson<a> |
| 13 | { |
| 14 | fn to_json :: x: a -> Json |
| 15 | } |
| 16 | |
| 17 | // Records become objects, other constructors arrays tagged by name. |
| 18 | fn json_of_constructor :: c: Constructor<Json> -> Json |
| 19 | { |
| 20 | match c.fields |
| 21 | { [] -> JStr(c.name) |
| 22 | , [f, ..] if f.label != "" -> |
| 23 | JObj(list.map c.fields fn f { (f.label, f.value) }) |
| 24 | , fields -> JArr([JStr(c.name), ..list.map fields fn f { f.value }]) |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | impl ToJson<Int> |
| 29 | { |
| 30 | fn to_json :: n |
| 31 | { |
| 32 | JInt(n) |
| 33 | } |
| 34 | } |
| 35 | |
| 36 | impl ToJson<String> |
| 37 | { |
| 38 | fn to_json :: s |
| 39 | { |
| 40 | JStr(s) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | impl ToJson<List<a>> where ToJson<a> |
| 45 | { |
| 46 | fn to_json :: xs |
| 47 | { |
| 48 | JArr(list.map xs to_json) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | fn render :: j: Json -> String |
| 53 | { |
| 54 | match j |
| 55 | { JNull -> "null" |
| 56 | , JInt(n) -> show n |
| 57 | , JStr(s) -> "\"" <> s <> "\"" |
| 58 | , JArr(items) -> "[" <> join (list.map items render) <> "]" |
| 59 | , JObj(entries) -> |
| 60 | "\{" |
| 61 | <> join (list.map entries fn e { "\"" <> e.0 <> "\": " <> render e.1 }) |
| 62 | <> "\}" |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | fn join :: xs: List<String> -> String |
| 67 | { |
| 68 | match xs { [] -> "", [x] -> x, [x, ..rest] -> x <> ", " <> join rest } |
| 69 | } |
| 70 | |
| 71 | // A second derivable trait, with an Int result: the number of nodes. |
| 72 | @derivable count_nodes |
| 73 | trait Size<a> |
| 74 | { |
| 75 | fn size :: x: a -> Int |
| 76 | } |
| 77 | |
| 78 | fn count_nodes :: c: Constructor<Int> -> Int |
| 79 | { |
| 80 | list.fold c.fields 1 fn n f { n + f.value } |
| 81 | } |
| 82 | |
| 83 | impl Size<Int> |
| 84 | { |
| 85 | fn size :: _ |
| 86 | { |
| 87 | 1 |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | @derive ToJson Show |
| 92 | pub type User { User(name: String, age: Int, tags: List<String>) } |
| 93 | |
| 94 | @derive ToJson Size |
| 95 | pub type Shape { Dot, Circle(Int), Rect(Int, Int) } |
| 96 | |
| 97 | // Generic: requires the trait of its parameter; recursive through it. |
| 98 | @derive ToJson Size Debug |
| 99 | pub type Tree<a> { Leaf, Node(Tree<a>, a, Tree<a>) } |
| 100 | |
| 101 | pub fn main :: -> () / <io> |
| 102 | { |
| 103 | let ann = User("ann", 41, ["admin", "ops"]) |
| 104 | io.println (render (to_json ann)) |
| 105 | io.println (show ann) |
| 106 | io.println (render (to_json [Dot, Circle(2), Rect(3, 4)])) |
| 107 | let tree = Node(Node(Leaf, 1, Leaf), 2, Leaf) |
| 108 | io.println (render (to_json tree)) |
| 109 | io.println (debug tree) |
| 110 | io.println |
| 111 | ( show (size tree) <> " " <> show (size Rect(3, 4)) <> " " |
| 112 | <> show (size Dot) |
| 113 | ) |
| 114 | } |
| 115 | //> {"name": "ann", "age": 41, "tags": ["admin", "ops"]} |
| 116 | //> User(ann, 41, [admin, ops]) |
| 117 | //> ["Dot", ["Circle", 2], ["Rect", 3, 4]] |
| 118 | //> ["Node", ["Node", "Leaf", 1, "Leaf"], 2, "Leaf"] |
| 119 | //> Node(Node(Leaf, 1, Leaf), 2, Leaf) |
| 120 | //> 7 3 1 |