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.

96 lignes · 3230 octetsBrut · Historique
1import std/array.{type Array}
2import std/io
3import std/process.{type Pid, type Subject}
4import std/string
5import std/wire
6
7// A `Wire` dictionary is the type's descriptor (ADR 0072,
8// docs/wire-format.md): a literal for a monomorphic type, built from
9// literal pieces and the dictionaries a generic function is given.
10pub type Msg { Ping(reply: Subject<Int>), Stop }
11
12pub type Tree<a> { Leaf, Node(Tree<a>, a, Tree<a>) }
13
14// No field stores a value of `unit`: descriptors erase it.
15pub type Tagged<unit> { Tagged(Int) }
16
17pub type Keyed<k, v> { Keyed(key: k, values: List<v>) }
18
19fn of :: x: a -> String where Wire<a>
20{
21 wire.describe x
22}
23
24// Generic: `Option<` ... `>` around the given dictionary.
25fn some :: x: a -> String where Wire<a>
26{
27 wire.describe (Some(x))
28}
29
30// Two dictionaries, and one inside another type's instance.
31fn keyed :: k: k v: v -> String where Wire<k>, Wire<v>
32{
33 wire.describe (Keyed(key: k, values: [v]), [Some(v)])
34}
35
36// Generic over generic: `some`'s dictionary is built from this one's.
37fn nested :: x: a -> String where Wire<a>
38{
39 some [(x, x)]
40}
41
42pub fn main :: -> () / <io>
43{
44 io.println (of [1, 2])
45 let reply: Option<(String, Subject<Msg>)> = None
46 io.println (of reply)
47 io.println (of (Node(Leaf, 1.5, Leaf)))
48 let t: Tagged<String> = Tagged(1)
49 let u: Tagged<fn() -> ()> = Tagged(2)
50 io.println (of t)
51 io.println (of u)
52 let bytes: Option<Bytes> = None
53 io.println (of ((), True, bytes, (1, "two")))
54 io.println (some 1)
55 io.println (some "s")
56 io.println (some (Node(Leaf, [Stop], Leaf)))
57 io.println (keyed "k" 1)
58 let b: Tagged<Bool> = Tagged(3)
59 io.println (keyed (Some(1)) b)
60 io.println (nested 1)
61 io.println (nested [True])
62 let pids: Array<Pid> = array.empty ()
63 io.println (of pids)
64 // No value at hand: the type comes from `None`'s.
65 let msg: Option<Msg> = None
66 io.println (wire.describe_none msg)
67 // The table, which every descriptor above resolves against, and its
68 // hash: literals, only in a program that asks for them.
69 let table = wire.definitions ()
70 let has = fn entry { io.println (show (string.contains table entry)) }
71 has "Dwire_descriptors.Msg;0CPing;Freply;SiCStop;"
72 has "Dwire_descriptors.Tagged;0CTagged;F;i"
73 has "Dwire_descriptors.Keyed;2CKeyed;Fkey;$0Fvalues;Nstd/prelude.List;1$1"
74 has "Dstd/prelude.Bool;0CFalse;CTrue;"
75 io.println (show (string.length (wire.schema_hash ())))
76}
77//> Nstd/prelude.List;1i
78//> Nstd/prelude.Option;1T2sSNwire_descriptors.Msg;0
79//> Nwire_descriptors.Tree;1f
80//> Nwire_descriptors.Tagged;0
81//> Nwire_descriptors.Tagged;0
82//> T4uNstd/prelude.Bool;0Nstd/prelude.Option;1bT2is
83//> Nstd/prelude.Option;1i
84//> Nstd/prelude.Option;1s
85//> Nstd/prelude.Option;1Nwire_descriptors.Tree;1Nstd/prelude.List;1Nwire_descriptors.Msg;0
86//> T2Nwire_descriptors.Keyed;2siNstd/prelude.List;1Nstd/prelude.Option;1i
87//> T2Nwire_descriptors.Keyed;2Nstd/prelude.Option;1iNwire_descriptors.Tagged;0Nstd/prelude.List;1Nstd/prelude.Option;1Nwire_descriptors.Tagged;0
88//> Nstd/prelude.Option;1Nstd/prelude.List;1T2ii
89//> Nstd/prelude.Option;1Nstd/prelude.List;1T2Nstd/prelude.List;1Nstd/prelude.Bool;0Nstd/prelude.List;1Nstd/prelude.Bool;0
90//> Ap
91//> Nwire_descriptors.Msg;0
92//> True
93//> True
94//> True
95//> True
96//> 16