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 | // The compile-time benchmark's std-heavy program (scripts/bench-compile.sh): |
| 2 | // it uses most of the standard library, so most of std's code is |
| 3 | // generated, compiled and linked. Its run time does not matter. |
| 4 | |
| 5 | import std/actor.{Continue, Stop} |
| 6 | import std/bytes |
| 7 | import std/dict.{type Dict} |
| 8 | import std/html.{type Element, type Html} |
| 9 | import std/io |
| 10 | import std/list |
| 11 | import std/option |
| 12 | import std/process.{type Subject, type proc, Down} |
| 13 | import std/ref |
| 14 | import std/result |
| 15 | import std/string |
| 16 | import std/supervisor.{Child, OneForOne} |
| 17 | |
| 18 | pub type Item { Item(name: String, price: Int, tags: List<String>) } |
| 19 | |
| 20 | type Shop { Put(Item), Total(Subject<Int>), Close(Subject<String>) } |
| 21 | |
| 22 | fn shop_step :: |
| 23 | items: Dict<String, Item> |
| 24 | message: Shop |
| 25 | -> actor.Next<Dict<String, Item>> / <proc, io> |
| 26 | { |
| 27 | match message |
| 28 | { Put(item) -> Continue(dict.insert items item.name item) |
| 29 | , Total(reply) -> |
| 30 | { |
| 31 | process.send reply (dict.fold items 0 fn acc _ item { acc + item.price }) |
| 32 | Continue(items) |
| 33 | } |
| 34 | , Close(reply) -> |
| 35 | { |
| 36 | process.send reply (string.join (dict.keys items) ",") |
| 37 | Stop |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | fn tag_view :: tag: String -> Element<msg> |
| 43 | { |
| 44 | <span class="tag">{tag}</span> |
| 45 | } |
| 46 | |
| 47 | fn item_view :: item: Item -> Html |
| 48 | { |
| 49 | <li data-price={item.price}>{item.name}{list.map item.tags tag_view}</li> |
| 50 | } |
| 51 | |
| 52 | fn page :: items: List<Item> -> Html |
| 53 | { |
| 54 | <html lang="en"><body><ul>{list.map items item_view}</ul></body></html> |
| 55 | } |
| 56 | |
| 57 | fn parse_prices :: text: String -> Result<List<Int>, String> |
| 58 | { |
| 59 | string.split text "," |
| 60 | |> list.map |
| 61 | fn part |
| 62 | { |
| 63 | string.to_int (string.trim part) |
| 64 | |> result.replace_error ("not a price: " <> part) |
| 65 | } |
| 66 | |> result.all |
| 67 | } |
| 68 | |
| 69 | fn checksum :: b: Bytes -> Int |
| 70 | { |
| 71 | list.fold (bytes.to_list b) 0 fn acc x { (acc * 31 + x) % 1000003 } |
| 72 | } |
| 73 | |
| 74 | fn counted :: xs: List<Int> -> Int |
| 75 | { |
| 76 | region |
| 77 | { |
| 78 | let total = ref.new 0 |
| 79 | list.each xs fn x { ref.set total (ref.get total + x) } |
| 80 | ref.get total |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | pub fn main :: -> () / <io, proc> |
| 85 | { |
| 86 | let items = |
| 87 | [ Item("apple", 3, ["fruit", "red"]) |
| 88 | , Item("bread", 5, ["bakery"]) |
| 89 | , Item("cheese", 12, ["dairy", "aged"]) |
| 90 | ] |
| 91 | let shop = |
| 92 | actor.start (dict.empty ()) fn stock message { shop_step stock message } |
| 93 | list.each items fn item { actor.cast shop Put(item) } |
| 94 | io.println ("total " <> show (actor.call shop Total 1000)) |
| 95 | io.println ("closed " <> actor.call shop Close 1000) |
| 96 | let markup = html.render (page items) |
| 97 | io.println ("page " <> show (string.length markup)) |
| 98 | let prices = parse_prices "3, 5, 12" |
| 99 | io.println |
| 100 | match prices { Ok(xs) -> "prices " <> show (counted xs), Error(e) -> e } |
| 101 | io.println |
| 102 | ( string.to_int "x" |
| 103 | |> result.to_option |
| 104 | |> option.map show |
| 105 | |> option.unwrap_or "no int" |
| 106 | ) |
| 107 | let data = |
| 108 | bytes.build |
| 109 | (bytes.append_string (bytes.append_byte (bytes.builder ()) 65) markup) |
| 110 | io.println |
| 111 | ("bytes " <> show (bytes.length data) <> " " <> show (checksum data)) |
| 112 | let sup = |
| 113 | { |
| 114 | use <- process.spawn |
| 115 | supervisor.supervise |
| 116 | OneForOne |
| 117 | [Child("once", fn { io.println "child ran" })] |
| 118 | 1 |
| 119 | } |
| 120 | let Down(_, _) = process.receive_forever (process.monitor sup) |
| 121 | io.println "done" |
| 122 | } |
| 123 | //> total 20 |
| 124 | //> closed apple,bread,cheese |
| 125 | //> page 282 |
| 126 | //> prices 20 |
| 127 | //> no int |
| 128 | //> bytes 283 494095 |
| 129 | //> child ran |
| 130 | //> done |