rustyfi-lang 0.1.4

Abstract syntax tree, elaboration, evaluator, and primitives for SATySFi
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
//! Prim surface `annot.satyh` needs: a typecheck half (real source through
//! the real pipeline) and an eval half (direct `Ast` apply chains). The
//! register-* prims are FAITHFUL as of Group A: they error outside
//! `fire_hooks`' `current_page` window and record a real `Annot`/
//! `NamedDest` inside it.
//!
//! `annot.satyh` itself reaches the PDF end-to-end via the capstone suite
//! (`crates/rustyfi/tests/fixtures/href.saty`); this file only proves the
//! primitive surface type-checks and evaluates correctly in isolation.

use rustyfi_backend::{FontKey, FontMetrics, HorzBox, Length, PureHorzBox};
use rustyfi_lang::ast::Ast;
use rustyfi_lang::eval;
use rustyfi_lang::prim_types;
use rustyfi_lang::primitives;
use rustyfi_lang::value::Value;
use rustyfi_lang::{elaborate, typecheck, CompileError};
use rustyfi_syntax::Span;

struct Mono;

impl FontMetrics for Mono {
    fn advance(&self, _f: FontKey, _c: char, size: Length) -> Option<Length> {
        Some(size * 0.5)
    }
    fn ascender(&self, _f: FontKey, size: Length) -> Length {
        size * 0.75
    }
    fn descender(&self, _f: FontKey, size: Length) -> Length {
        size * 0.25
    }
}

const NEW_NAMES: &[&str] = &[
    "get-leftmost-script",
    "get-rightmost-script",
    "inline-frame-breakable",
    "register-destination",
    "register-link-to-uri",
    "register-link-to-location",
];

#[test]
fn every_new_primitive_resolves_in_base_env_and_has_a_registered_type() {
    let env = primitives::base_env();
    for name in NEW_NAMES {
        assert!(
            env.lookup(name).is_some(),
            "primitive `{name}` is not bound in base_env()"
        );
        assert!(
            prim_types::primitive_type(name).is_some(),
            "primitive `{name}` has no registered type"
        );
    }
}

fn typecheck_str(src: &str) -> Result<(), CompileError> {
    let file = rustyfi_syntax::parse_file(src)?;
    let env = primitives::base_env();
    let store = rustyfi_lang::symbol::SymbolStore::new();
    let scope = elaborate::Scope::new(&store, env.names());
    let program = elaborate::elaborate_program(&file, &scope)?;
    typecheck::typecheck(&program)?;
    Ok(())
}

fn assert_well_typed(src: &str) {
    if let Err(e) = typecheck_str(src) {
        panic!("expected {src:?} to type-check, got error: {e}");
    }
}

fn assert_type_error(src: &str) {
    match typecheck_str(src) {
        Ok(()) => panic!("expected {src:?} to be rejected by the typechecker, but it passed"),
        Err(CompileError::Type(_)) => {}
        Err(other) => panic!("expected {src:?} to fail with a type error, got: {other}"),
    }
}

#[test]
fn get_leftmost_and_rightmost_script_typecheck() {
    assert_well_typed("get-leftmost-script inline-nil");
    assert_well_typed("get-rightmost-script inline-nil");
}

#[test]
fn get_leftmost_script_rejects_a_non_inline_boxes_argument() {
    assert_type_error("get-leftmost-script 3");
}

#[test]
fn inline_frame_breakable_typechecks() {
    assert_well_typed(
        "let mydeco pt l1 l2 l3 = []
         in
         inline-frame-breakable (0pt, 0pt, 0pt, 0pt) (mydeco, mydeco, mydeco, mydeco) inline-nil",
    );
}

#[test]
fn inline_frame_breakable_rejects_a_three_element_paddings_tuple() {
    assert_type_error(
        "let mydeco pt l1 l2 l3 = []
         in
         inline-frame-breakable (0pt, 0pt, 0pt) (mydeco, mydeco, mydeco, mydeco) inline-nil",
    );
}

#[test]
fn register_destination_typechecks() {
    assert_well_typed("register-destination `chapter1` (10pt, 20pt)");
}

#[test]
fn register_destination_rejects_a_non_string_name() {
    assert_type_error("register-destination 3 (10pt, 20pt)");
}

#[test]
fn register_link_to_uri_typechecks_with_and_without_a_border() {
    assert_well_typed("register-link-to-uri `https://example.com` (0pt, 0pt) 10pt 10pt 10pt None");
    assert_well_typed(
        "register-link-to-uri `https://example.com` (0pt, 0pt) 10pt 10pt 10pt \
         (Some (1pt, Gray 0.5))",
    );
}

#[test]
fn register_link_to_uri_rejects_a_malformed_border_argument() {
    assert_type_error("register-link-to-uri `u` (0pt, 0pt) 10pt 10pt 10pt 5");
}

#[test]
fn register_link_to_location_typechecks() {
    assert_well_typed("register-link-to-location `chapter1` (0pt, 0pt) 10pt 10pt 10pt None");
}

/// The actual shape `annot.satyh:29-40`'s `\href` body reduces to (minus the
/// `direct \href` command sugar and `read-inline`, out of scope here): guard
/// both edges with `get-leftmost-script`/`get-rightmost-script` +
/// `script-guard`, wrap the body in `inline-frame-breakable`, concatenate
/// with `++`. Proves the whole prim surface composes, not just each prim in
/// isolation.
#[test]
fn the_href_shaped_composition_typechecks() {
    assert_well_typed(
        "let guard ib =
           match get-leftmost-script ib with
           | Some s -> script-guard s inline-nil
           | None -> inline-nil
         in
         let mydeco pt l1 l2 l3 = []
         in
         let framed =
           inline-frame-breakable (0pt, 0pt, 0pt, 0pt) (mydeco, mydeco, mydeco, mydeco) inline-nil
         in
         (guard inline-nil) ++ framed ++ (guard inline-nil)",
    );
}

// Eval half — `Ast` apply chains through `eval::Interp`; no parser involved.

fn var(name: &str) -> Ast {
    Ast::Var(name.to_string(), Span::default())
}

fn apply_all(name: &str, args: Vec<Ast>) -> Ast {
    args.into_iter()
        .fold(var(name), |f, a| Ast::Apply(Box::new(f), Box::new(a)))
}

fn str_lit(s: &str) -> Ast {
    Ast::Str(s.to_string())
}

fn len(pt: f64) -> Ast {
    Ast::Length(Length::pt(pt))
}

fn point(x_pt: f64, y_pt: f64) -> Ast {
    Ast::Tuple(vec![len(x_pt), len(y_pt)])
}

fn border_none() -> Ast {
    Ast::Ctor("None".to_string(), None)
}

fn border_some(width_pt: f64, gray: f64) -> Ast {
    Ast::Ctor(
        "Some".to_string(),
        Some(Box::new(Ast::Tuple(vec![
            len(width_pt),
            Ast::Ctor("Gray".to_string(), Some(Box::new(Ast::Float(gray)))),
        ]))),
    )
}

fn run(ast: &Ast) -> Value {
    try_run(ast).expect("evaluation should succeed")
}

fn try_run(ast: &Ast) -> Result<Value, eval::EvalError> {
    let env = primitives::base_env();
    let mono = Mono;
    let mut interp = eval::Interp::new(&mono);
    interp.eval(&env, ast)
}

#[test]
fn get_leftmost_and_rightmost_script_return_none_stand_in() {
    for name in ["get-leftmost-script", "get-rightmost-script"] {
        match run(&apply_all(name, vec![var("inline-nil")])) {
            Value::Ctor(ctor, None) => assert_eq!(ctor, "None"),
            other => panic!("expected `{name} inline-nil` to evaluate to None, got {other:?}"),
        }
    }
}

/// The four-closure `deco-set` argument is popped and interned WHOLE (all
/// four closures — a breakable frame that splits fires `decoH`/`decoM`/
/// `decoT` per fragment, see the prim's doc comment); a dummy
/// `(fun _ _ _ _ -> [])`-shaped tuple stands in here since the closures
/// themselves are never invoked at construction time (only later, by
/// `fire_hooks`/`apply_deco`).
fn dummy_decoset() -> Ast {
    Ast::Tuple(vec![Ast::Unit, Ast::Unit, Ast::Unit, Ast::Unit])
}

#[test]
fn inline_frame_breakable_splices_its_contents_between_markers() {
    let ast = apply_all(
        "inline-frame-breakable",
        vec![
            Ast::Tuple(vec![len(2.0), len(3.0), len(4.0), len(5.0)]),
            dummy_decoset(),
            var("inline-nil"),
        ],
    );
    let env = primitives::base_env();
    let mono = Mono;
    let mut interp = eval::Interp::new(&mono);
    let v = interp.eval(&env, &ast).expect("evaluation should succeed");
    let Value::InlineBoxes(boxes) = v else {
        panic!("expected inline-boxes")
    };
    let marker = |end| {
        HorzBox::Pure(PureHorzBox::InlineFrameMarker {
            id: rustyfi_backend::DecoId(0),
            end,
            height: Length::pt(4.0), // inner(0) + paddingT
            depth: Length::pt(5.0),  // inner(0) + paddingB
        })
    };
    assert_eq!(
        boxes,
        vec![
            marker(false),
            // paddingL / paddingR as ordinary fixed boxes INSIDE the bracket,
            // upstream's `append_horz_padding` (lineBreak.ml:79).
            HorzBox::Pure(PureHorzBox::FixedEmpty {
                width: Length::pt(2.0)
            }),
            HorzBox::Pure(PureHorzBox::FixedEmpty {
                width: Length::pt(3.0)
            }),
            marker(true),
        ],
        "the frame's contents are SPLICED into the enclosing box stream \
         (so the paragraph breaker sees their break opportunities) between \
         a zero-width marker pair carrying the padded vertical extent"
    );
    assert_eq!(
        interp.decos.len(),
        1,
        "the whole deco-set must be interned into interp.decos"
    );
    assert!(
        matches!(interp.decos[0], eval::DecoEntry::InlineBreakable { .. }),
        "a breakable frame interns all four closures, not a single `Inline` deco"
    );
}

/// `register-destination`/`register-link-to-*` are gated on
/// `interp.current_page`: they only succeed while `fire_hooks` is walking a
/// page — i.e. from a hook or a fired decoration.
fn eval_during_page_break(ast: &Ast) -> Result<(Value, eval::Interp<'static>), eval::EvalError> {
    let metrics: &'static Mono = Box::leak(Box::new(Mono));
    let env = primitives::base_env();
    let mut interp = eval::Interp::new(metrics);
    interp.current_page = Some(0);
    let v = interp.eval(&env, ast)?;
    Ok((v, interp))
}

#[test]
fn register_destination_outside_a_page_break_errors() {
    let ast = apply_all(
        "register-destination",
        vec![str_lit("chapter1"), point(10.0, 20.0)],
    );
    let err = try_run(&ast).expect_err("must error outside fire_hooks' current_page window");
    assert!(
        err.msg.contains("page breaking"),
        "error should name the during-page-break gate: {}",
        err.msg
    );
}

#[test]
fn register_destination_during_a_page_break_records_a_named_destination() {
    let ast = apply_all(
        "register-destination",
        vec![str_lit("chapter1"), point(10.0, 20.0)],
    );
    let (v, interp) = eval_during_page_break(&ast).expect("must succeed inside the window");
    assert!(matches!(v, Value::Unit));
    assert_eq!(interp.destinations.len(), 1);
    let d = &interp.destinations[0];
    assert_eq!(d.page, 0);
    assert_eq!(d.name, "nameddest0");
    assert_eq!(d.x, Length::pt(10.0));
    assert_eq!(d.y, Length::pt(20.0));
}

#[test]
fn register_link_to_uri_outside_a_page_break_errors() {
    let ast = apply_all(
        "register-link-to-uri",
        vec![
            str_lit("https://example.com"),
            point(0.0, 0.0),
            len(10.0),
            len(10.0),
            len(10.0),
            border_none(),
        ],
    );
    let err = try_run(&ast).expect_err("must error outside fire_hooks' current_page window");
    assert!(
        err.msg.contains("page breaking"),
        "error should name the during-page-break gate: {}",
        err.msg
    );
}

#[test]
fn register_link_to_uri_during_a_page_break_records_an_annot_with_no_border() {
    let ast = apply_all(
        "register-link-to-uri",
        vec![
            str_lit("https://example.com"),
            point(0.0, 0.0),
            len(10.0),
            len(10.0),
            len(10.0),
            border_none(),
        ],
    );
    let (v, interp) = eval_during_page_break(&ast).expect("must succeed inside the window");
    assert!(matches!(v, Value::Unit));
    assert_eq!(interp.annotations.len(), 1);
    let a = &interp.annotations[0];
    assert_eq!(a.page, 0);
    assert_eq!(a.border, None);
    assert_eq!(
        a.rect,
        (
            Length::pt(0.0),
            Length::pt(-10.0),
            Length::pt(10.0),
            Length::pt(10.0)
        ),
        "rect = (x, y - depth, x + width, y + height)"
    );
    assert_eq!(
        a.action,
        rustyfi_backend::AnnotAction::Uri("https://example.com".to_string())
    );
}

#[test]
fn register_link_to_uri_during_a_page_break_records_an_annot_with_a_border() {
    let ast = apply_all(
        "register-link-to-uri",
        vec![
            str_lit("https://example.com"),
            point(0.0, 0.0),
            len(10.0),
            len(10.0),
            len(10.0),
            border_some(1.0, 0.5),
        ],
    );
    let (_v, interp) = eval_during_page_break(&ast).expect("must succeed inside the window");
    assert_eq!(interp.annotations.len(), 1);
    assert_eq!(
        interp.annotations[0].border,
        Some((Length::pt(1.0), rustyfi_backend::Color::Gray(0.5)))
    );
}

#[test]
fn register_link_to_uri_rejects_a_malformed_border_argument_without_panicking() {
    let ast = apply_all(
        "register-link-to-uri",
        vec![
            str_lit("https://example.com"),
            point(0.0, 0.0),
            len(1.0),
            len(1.0),
            len(1.0),
            Ast::Int(5),
        ],
    );
    let err = try_run(&ast).expect_err("a malformed border must be a clean EvalError, not a panic");
    assert!(
        err.msg.contains("option"),
        "error should name the expected shape: {}",
        err.msg
    );
}

#[test]
fn register_link_to_location_outside_a_page_break_errors() {
    let ast = apply_all(
        "register-link-to-location",
        vec![
            str_lit("chapter1"),
            point(0.0, 0.0),
            len(10.0),
            len(10.0),
            len(10.0),
            border_none(),
        ],
    );
    let err = try_run(&ast).expect_err("must error outside fire_hooks' current_page window");
    assert!(
        err.msg.contains("page breaking"),
        "error should name the during-page-break gate: {}",
        err.msg
    );
}

#[test]
fn register_link_to_location_during_a_page_break_resolves_through_the_shared_name_table() {
    let ast = apply_all(
        "register-link-to-location",
        vec![
            str_lit("chapter1"),
            point(0.0, 0.0),
            len(10.0),
            len(10.0),
            len(10.0),
            border_none(),
        ],
    );
    let (v, interp) = eval_during_page_break(&ast).expect("must succeed inside the window");
    assert!(matches!(v, Value::Unit));
    assert_eq!(interp.annotations.len(), 1);
    assert_eq!(
        interp.annotations[0].action,
        rustyfi_backend::AnnotAction::GotoName("nameddest0".to_string()),
        "the key resolves through the SAME dest_name table register-destination uses, \
         minting a stable name even though nothing has registered that destination yet"
    );
}