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.

77 lignes · 1938 octetsBrut · Historique
1// All-static subtrees of HTML literals and templates are one immortal
2// block in the native backend, shared by every evaluation (the C
3// backend's `tl_lit_c` blocks; `LOUISE_NO_STATIC_CTORS` rebuilds them).
4// Behaviour is unchanged: holes evaluate left to right, effects
5// included, and escaping is as ever — the interpreter, which rebuilds
6// the tree per call, prints the same.
7
8import std/html.{type Html}
9import std/io
10import std/template.{pug}
11
12effect log
13{
14 fn log :: msg: String -> ()
15}
16
17fn traced :: mark: String value: String -> String / <log>
18{
19 log mark
20 value
21}
22
23fn traced_int :: mark: String value: Int -> Int / <log>
24{
25 log mark
26 value
27}
28
29fn with_log :: action: fn() -> a / <log, io | e> -> a / <io | e>
30{
31 handle
32 {
33 action ()
34 }
35 with
36 { log m -> io.println ("log: " <> m) }
37}
38
39// The attribute list and the <b> subtree are all-static (hoisted); the
40// holes are not. Attributes lower before children, so the log reads
41// "attr", "first", "second".
42fn row :: n: Int a: String b: String -> Html / <log>
43{
44 <p class="row" data-n={traced_int
45 "attr"
46 n}>{traced "first" a}<b>& static</b>{traced "second" b}</p>
47}
48
49pub fn main :: -> () / <io>
50{
51 use <- with_log
52 io.println (html.render (row 1 "x" "y"))
53 // The same literal evaluated again: the static parts are shared, the
54 // holes run again, in order.
55 io.println (html.render (row 2 "x" "y"))
56 // A pug template's static parts are the same statics (pug is compiled
57 // to the literal's constructors, ADR 0057); its holes run in order.
58 io.println
59 ( html.render
60 pug`
61 .card
62 h2 {traced "title" "T"}
63 p {traced "body" "B"}
64 `
65 )
66}
67//> log: attr
68//> log: first
69//> log: second
70//> <p class="row" data-n="1">x<b>&amp; static</b>y</p>
71//> log: attr
72//> log: first
73//> log: second
74//> <p class="row" data-n="2">x<b>&amp; static</b>y</p>
75//> log: title
76//> log: body
77//> <div class="card"><h2>T</h2><p>B</p></div>