yomi-core 0.1.0

Core of the yomi scripting language: parser, type checker, VM and resumable saves
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
469
470
471
472
473
474
475
476
477
//! Tests of the syntax that receives a command's return value in scripts.
//!
//! `let name = @command(...)` and `name = @command(...)`. Stop commands stop at a stop point, put the resume value
//! (`Input::Return`) into the variable, and proceed to the next statement. Effect commands put it into the variable right
//! away.

mod common;

use common::Counter;
use yomi_core::{
    DiagCode, Input, Program, Registry, ResolvedBy, Severity, Sugar, Value, Vm, VmConfig, Yield,
    compile, compile_with_warnings,
};

fn registry() -> Registry {
    let mut r = Registry::new();
    r.stop::<(String,), ()>("ui.say").unwrap();
    r.stop::<(Vec<String>, Vec<Option<String>>), i64>("ui.choose")
        .unwrap();
    r.stop::<(String,), String>("game.ask").unwrap();
    r.stop::<(i64,), i64>("game.roll").unwrap();
    r.stop::<(String,), bool>("game.yes").unwrap();
    r.stop::<(f64,), f64>("game.half").unwrap();
    r.stop::<(String,), Vec<String>>("game.names").unwrap();
    r.stop::<(), ()>("game.pause").unwrap();
    r.package::<Counter>().unwrap();
    r.bind(Sugar::Plain, "ui.say").unwrap();
    r.bind(Sugar::Choice, "ui.choose").unwrap();
    r
}

fn compile_ok(src: &str, reg: &Registry) -> Program {
    match compile(src, "main", reg) {
        Ok(p) => p,
        Err(d) => panic!("コンパイルできない: {d:?}\n{src}"),
    }
}

/// The list of codes of compile diagnostics (errors only).
fn errors(src: &str) -> Vec<DiagCode> {
    match compile_with_warnings(src, "main", &registry()) {
        Ok(_) => Vec::new(),
        Err(d) => d
            .iter()
            .filter(|d| d.severity == Severity::Error)
            .map(|d| d.code)
            .collect(),
    }
}

/// The return value decided by the number of the stop point (matching the command name and arguments).
fn answer(name: &str, args: &[Value], index: usize) -> Value {
    match name {
        "game.ask" => Value::Str(format!("答{index}").into()),
        "game.roll" => Value::Int(((index * 7) % 5 + 1) as i64),
        "game.yes" => Value::Bool(index.is_multiple_of(2)),
        "game.half" => match args.first() {
            Some(Value::Float(x)) => Value::Float(x / 2.0),
            _ => Value::Float(0.0),
        },
        "ui.choose" => Value::Int(0),
        _ => Value::Unit,
    }
}

/// Runs the script to the end and returns the sequence of stop points (`command(args) return value`) and the variables at
/// the end. At the `roundtrip_at`-th stop point, continues with a VM that was saved and loaded again.
fn play(src: &str, roundtrip_at: Option<usize>) -> (Vec<String>, Vec<String>) {
    let reg = registry();
    let prog = compile_ok(src, &reg);
    let mut vm = Vm::new(&prog, &reg, VmConfig::default()).unwrap();
    let mut log = Vec::new();
    let mut index = 0;
    loop {
        match vm.run().unwrap() {
            Yield::End => break,
            Yield::Stop { command, args, .. } => {
                if roundtrip_at == Some(index) {
                    let bytes = vm.save().unwrap();
                    let (loaded, report) =
                        Vm::load(&prog, &reg, VmConfig::default(), &bytes).unwrap();
                    assert_ne!(report.resolved_by, ResolvedBy::LabelStart);
                    assert_eq!(loaded.current(), vm.current());
                    vm = loaded;
                }
                let name = reg.command(command).name.clone();
                let ret = answer(&name, &args, index);
                let shown: Vec<String> = args.iter().map(Value::to_text).collect();
                log.push(format!("{name}({}) -> {}", shown.join(", "), ret.to_text()));
                vm.resume(Input::Return(ret)).unwrap();
                index += 1;
            }
            _ => unreachable!(),
        }
    }
    let vars = vm
        .variables()
        .iter()
        .filter(|v| !v.local)
        .map(|v| format!("{}={}", v.name, v.value.to_text()))
        .collect();
    (log, vars)
}

fn var(vm: &Vm<'_>, name: &str) -> Value {
    vm.variables()
        .into_iter()
        .find(|v| v.name == name)
        .unwrap_or_else(|| panic!("変数 {name} がない"))
        .value
}

fn stop_text(y: &Yield) -> String {
    match y {
        Yield::Stop { args, .. } => args.first().map(Value::to_text).unwrap_or_default(),
        _ => "<end>".to_string(),
    }
}

const BASIC: &str = "save var total: int = 0\nlabel start {\n  let name = @game.ask(\"名前は?\") #name\n  こんにちは ${name} #hello\n  let n: int = @game.roll(3)\n  total = @game.roll(n)\n  合計 ${total}、${n} #sum\n}\n";

// ---------------------------------------------------------------------------
// Execution
// ---------------------------------------------------------------------------

#[test]
fn stop_let_and_assignment_store_resume_value_and_go_to_next_statement() {
    let reg = registry();
    let prog = compile_ok(BASIC, &reg);
    let mut vm = Vm::new(&prog, &reg, VmConfig::default()).unwrap();
    let y = vm.run().unwrap();
    assert_eq!(stop_text(&y), "名前は?");
    // Before receiving, the default value
    assert_eq!(var(&vm, "name"), Value::Str("".into()));
    vm.resume(Input::Return(Value::Str("たろう".into())))
        .unwrap();
    let y = vm.run().unwrap();
    assert_eq!(stop_text(&y), "こんにちは たろう");
    assert_eq!(var(&vm, "name"), Value::Str("たろう".into()));
    vm.resume(Input::Return(Value::Unit)).unwrap();
    // let n = @game.roll(3)
    assert!(matches!(vm.run().unwrap(), Yield::Stop { .. }));
    vm.resume(Input::Return(Value::Int(2))).unwrap();
    // total = @game.roll(n) (the argument is the variable that received)
    match vm.run().unwrap() {
        Yield::Stop { args, .. } => assert_eq!(args, vec![Value::Int(2)]),
        other => panic!("{other:?}"),
    }
    vm.resume(Input::Return(Value::Int(9))).unwrap();
    assert_eq!(stop_text(&vm.run().unwrap()), "合計 9、2");
    assert_eq!(var(&vm, "total"), Value::Int(9));
}

#[test]
fn resume_with_mismatched_received_type_is_bad_input_and_variable_unchanged() {
    let reg = registry();
    let prog = compile_ok(BASIC, &reg);
    let mut vm = Vm::new(&prog, &reg, VmConfig::default()).unwrap();
    vm.run().unwrap();
    let e = vm.resume(Input::Return(Value::Int(1))).unwrap_err();
    assert_eq!(e.kind.code(), "bad_input");
    assert!(vm.is_stopped());
    vm.resume(Input::Return(Value::Str("ok".into()))).unwrap();
    vm.run().unwrap();
    assert_eq!(var(&vm, "name"), Value::Str("ok".into()));
}

#[test]
fn bool_and_float_can_be_received() {
    let src = "label start {\n  let y = @game.yes(\"はい?\")\n  let h = @game.half(5.0)\n  ${y} ${h}\n}\n";
    let (log, _) = play(src, None);
    assert_eq!(
        log,
        vec![
            "game.yes(はい?) -> true",
            "game.half(5.0) -> 2.5",
            "ui.say(true 2.5) -> "
        ]
    );
}

#[test]
fn effect_return_values_are_stored_immediately() {
    let reg = registry();
    let src = "import counter\nsave var seen_roll: int = 0\nlabel start {\n  let v = @counter.roll(6)\n  seen_roll = @counter.roll(6)\n  counter.step = @counter.roll(6)\n  値 ${v} ${seen_roll} ${counter.step} 状態 ${counter.get()}\n}\n";
    let prog = compile_ok(src, &reg);
    let mut vm = Vm::new(&prog, &reg, VmConfig::default()).unwrap();
    let y = vm.run().unwrap();
    let text = stop_text(&y);
    let nums: Vec<i64> = text
        .split(|c: char| !c.is_ascii_digit())
        .filter_map(|t| t.parse().ok())
        .collect();
    assert_eq!(nums.len(), 4, "{text}");
    // The last value drawn is in both the state and the host-declared variable
    assert_eq!(nums[2], nums[3]);
    for n in &nums {
        assert!((1..=6).contains(n));
    }
}

#[test]
fn can_receive_in_blocks_label_parameters_and_called_labels() {
    let src = "label start {\n  let i = 0\n  while i < 2 {\n    let w = @game.ask(\"言葉${i}\")\n    ${i}:${w}\n    i += 1\n  }\n  if i == 2 {\n    let k = @game.roll(i)\n    choice {\n      「はい」 => {\n        let who = @game.ask(\"誰?\")\n        ${who}と${k}\n      }\n      「いいえ」 => {\n        end\n      }\n    }\n  }\n  let t = call tail(\"甲\")\n  結果 ${t}\n}\nlabel tail(who: str) -> int {\n  let r = @game.roll(5)\n  ${who}:${r}\n  if r > 2 {\n    let extra = @game.roll(r)\n    return r + extra\n  }\n  return r\n}\n";
    let (base, vars) = play(src, None);
    assert!(base.len() >= 10, "{base:?}");
    // The same whichever stop point it is saved and loaded at
    for k in 0..base.len() {
        let (log, v) = play(src, Some(k));
        assert_eq!(log, base, "k={k}");
        assert_eq!(v, vars, "k={k}");
    }
    // The received value is used
    assert!(base.iter().any(|l| l.starts_with("ui.say(答")), "{base:?}");
}

// ---------------------------------------------------------------------------
// Save and load
// ---------------------------------------------------------------------------

#[test]
fn save_and_load_at_stop_point_store_resume_value_in_same_variable() {
    let reg = registry();
    let prog = compile_ok(BASIC, &reg);
    let mut vm = Vm::new(&prog, &reg, VmConfig::default()).unwrap();
    vm.run().unwrap();
    let bytes = vm.save().unwrap();
    let (mut loaded, report) = Vm::load(&prog, &reg, VmConfig::default(), &bytes).unwrap();
    assert_eq!(report.resolved_by, ResolvedBy::Id);
    assert!(loaded.is_stopped());
    loaded
        .resume(Input::Return(Value::Str("じろう".into())))
        .unwrap();
    assert_eq!(stop_text(&loaded.run().unwrap()), "こんにちは じろう");
    assert_eq!(var(&loaded, "name"), Value::Str("じろう".into()));
}

#[test]
fn can_receive_without_stable_id_found_by_hash() {
    let src = "label start {\n  あ\n  let a = @game.ask(\"一\")\n  let b = @game.ask(\"二\")\n  ${a}${b}\n}\n";
    let reg = registry();
    let prog = compile_ok(src, &reg);
    let mut vm = Vm::new(&prog, &reg, VmConfig::default()).unwrap();
    vm.run().unwrap();
    vm.resume(Input::Return(Value::Unit)).unwrap();
    vm.run().unwrap();
    vm.resume(Input::Return(Value::Str("A".into()))).unwrap();
    vm.run().unwrap();
    let bytes = vm.save().unwrap();
    let (mut loaded, report) = Vm::load(&prog, &reg, VmConfig::default(), &bytes).unwrap();
    assert_eq!(report.resolved_by, ResolvedBy::Hash);
    loaded
        .resume(Input::Return(Value::Str("B".into())))
        .unwrap();
    assert_eq!(stop_text(&loaded.run().unwrap()), "AB");
}

#[test]
fn load_without_position_reruns_that_let_from_label_start() {
    let old =
        "label start {\n  let a = @game.ask(\"一\")\n  let b = @game.ask(\"二\")\n  ${a}${b}\n}\n";
    // The local variables increased, so the position of the stop point cannot be trusted
    let new = "label start {\n  let z = 0\n  let a = @game.ask(\"一\")\n  let b = @game.ask(\"二\")\n  ${a}${b}${z}\n}\n";
    let reg = registry();
    let prog_old = compile_ok(old, &reg);
    let prog_new = compile_ok(new, &reg);
    let mut vm = Vm::new(&prog_old, &reg, VmConfig::default()).unwrap();
    vm.run().unwrap();
    vm.resume(Input::Return(Value::Str("A".into()))).unwrap();
    vm.run().unwrap();
    let bytes = vm.save().unwrap();
    let (mut loaded, report) = Vm::load(&prog_new, &reg, VmConfig::default(), &bytes).unwrap();
    assert_eq!(report.resolved_by, ResolvedBy::LabelStart);
    assert!(!loaded.is_stopped());
    // Start over from the first let
    match loaded.run().unwrap() {
        Yield::Stop { args, .. } => assert_eq!(args, vec![Value::Str("一".into())]),
        other => panic!("{other:?}"),
    }
    loaded
        .resume(Input::Return(Value::Str("X".into())))
        .unwrap();
    loaded.run().unwrap();
    assert_eq!(var(&loaded, "a"), Value::Str("X".into()));
    loaded
        .resume(Input::Return(Value::Str("Y".into())))
        .unwrap();
    assert_eq!(stop_text(&loaded.run().unwrap()), "XY0");
}

#[test]
fn save_version_is_12() {
    let reg = registry();
    let prog = compile_ok(BASIC, &reg);
    let mut vm = Vm::new(&prog, &reg, VmConfig::default()).unwrap();
    vm.run().unwrap();
    let mut bytes = vm.save().unwrap();
    assert_eq!(&bytes[4..8], &12u32.to_le_bytes());
    // Version 11 is not read
    bytes[4..8].copy_from_slice(&11u32.to_le_bytes());
    let e = Vm::load(&prog, &reg, VmConfig::default(), &bytes).unwrap_err();
    assert_eq!(e.kind.code(), "unsupported_version");
}

// ---------------------------------------------------------------------------
// Rollback and history
// ---------------------------------------------------------------------------

#[test]
fn rollback_restores_received_variable_to_value_before_stop_point() {
    let src = "save var g: int = 0\nlabel start {\n  let a = @game.roll(1)\n  あ ${a}\n  let b = @game.roll(1)\n  g = @game.roll(1)\n  い ${a} ${b} ${g}\n}\n";
    let reg = registry();
    let prog = compile_ok(src, &reg);
    let mut vm = Vm::new(&prog, &reg, VmConfig::default()).unwrap();
    vm.run().unwrap();
    vm.resume(Input::Return(Value::Int(1))).unwrap();
    vm.run().unwrap();
    vm.resume(Input::Return(Value::Unit)).unwrap();
    vm.run().unwrap(); // stop point of b
    let snap = vm.snapshot().unwrap();
    vm.resume(Input::Return(Value::Int(5))).unwrap();
    vm.run().unwrap(); // stop point of g
    vm.resume(Input::Return(Value::Int(7))).unwrap();
    assert_eq!(stop_text(&vm.run().unwrap()), "い 1 5 7");
    assert_eq!(var(&vm, "g"), Value::Int(7));
    vm.restore(&snap).unwrap();
    assert_eq!(var(&vm, "b"), Value::Int(0));
    assert_eq!(var(&vm, "g"), Value::Int(0));
    assert_eq!(var(&vm, "a"), Value::Int(1));
    vm.resume(Input::Return(Value::Int(2))).unwrap();
    vm.run().unwrap();
    vm.resume(Input::Return(Value::Int(3))).unwrap();
    assert_eq!(stop_text(&vm.run().unwrap()), "い 1 2 3");
}

#[test]
fn history_keeps_resume_values_in_saves_and_goes_back_with_rollback() {
    let reg = registry();
    let prog = compile_ok(BASIC, &reg);
    let mut vm = Vm::new(&prog, &reg, VmConfig::default()).unwrap();
    vm.run().unwrap();
    assert_eq!(vm.history()[0].ret, None);
    let first = vm.snapshot().unwrap();
    vm.resume(Input::Return(Value::Str("たろう".into())))
        .unwrap();
    assert_eq!(vm.history()[0].ret, Some(Value::Str("たろう".into())));
    vm.run().unwrap(); // hello (the return value is `()`)
    vm.resume(Input::Return(Value::Unit)).unwrap();
    assert_eq!(vm.history()[1].ret, None);
    vm.run().unwrap(); // n
    let bytes = vm.save().unwrap();
    let (loaded, _) = Vm::load(&prog, &reg, VmConfig::default(), &bytes).unwrap();
    assert_eq!(loaded.history(), vm.history());
    assert_eq!(loaded.history()[0].ret, Some(Value::Str("たろう".into())));
    // The return value of a choice is the number passed
    vm.restore(&first).unwrap();
    assert_eq!(vm.history().len(), 1);
    assert_eq!(vm.history()[0].ret, None);
    vm.resume(Input::Return(Value::Str("じろう".into())))
        .unwrap();
    assert_eq!(vm.history()[0].ret, Some(Value::Str("じろう".into())));
}

#[test]
fn choice_return_value_in_history_is_number_passed() {
    let src = "label start {\n  choice {\n    「はい」 => {\n      あ\n    }\n    「いいえ」 => {\n      い\n    }\n  }\n}\n";
    let reg = registry();
    let prog = compile_ok(src, &reg);
    let mut vm = Vm::new(&prog, &reg, VmConfig::default()).unwrap();
    vm.run().unwrap();
    vm.resume(Input::Return(Value::Int(1))).unwrap();
    assert_eq!(vm.history()[0].ret, Some(Value::Int(1)));
}

// ---------------------------------------------------------------------------
// Diagnostics
// ---------------------------------------------------------------------------

#[test]
fn at_mid_expression_is_invalid_command_use() {
    for src in [
        "label start {\n  let a = 1 + @game.roll(2)\n}\n",
        "label start {\n  if @game.yes(\"?\") {\n  }\n}\n",
        "label start {\n  @game.roll(@game.roll(1))\n}\n",
        "label start {\n  let a = (@game.roll(1))\n}\n",
    ] {
        let e = errors(src);
        assert!(!e.is_empty(), "{src}");
        assert!(
            e.iter().all(|c| *c == DiagCode::InvalidCommandUse),
            "{src}: {e:?}"
        );
    }
}

#[test]
fn unit_returning_commands_and_queries_cannot_be_received() {
    for src in [
        "label start {\n  let a = @game.pause()\n}\n",
        "label start {\n  let a = @ui.say(\"x\")\n}\n",
        "import counter\nlabel start {\n  let a = @counter.inc(1)\n}\n",
        "import counter\nlabel start {\n  let a = @counter.get()\n}\n",
        "import counter\nlabel start {\n  let a = 0\n  a = @counter.get()\n}\n",
    ] {
        assert_eq!(errors(src), vec![DiagCode::InvalidCommandUse], "{src}");
    }
}

#[test]
fn type_mismatch_when_receiving_variable_and_return_types_differ() {
    assert_eq!(
        errors("label start {\n  let a: int = @game.ask(\"x\")\n}\n"),
        vec![DiagCode::TypeMismatch]
    );
    assert_eq!(
        errors("label start {\n  let a = 0\n  a = @game.ask(\"x\")\n}\n"),
        vec![DiagCode::TypeMismatch]
    );
    assert_eq!(
        errors("save var n: bool = false\nlabel start {\n  n = @game.roll(1)\n}\n"),
        vec![DiagCode::TypeMismatch]
    );
}

#[test]
fn cannot_be_written_on_compound_assignment_rhs() {
    assert_eq!(
        errors("label start {\n  let a = 0\n  a += @game.roll(1)\n}\n"),
        vec![DiagCode::InvalidCommandUse]
    );
}

#[test]
fn list_return_values_can_be_received_in_variables() {
    assert_eq!(
        errors("label start {\n  let a = @game.names(\"x\")\n  let n: int = a.len()\n}\n"),
        vec![]
    );
    // The type is the command's return type (`list<str>`)
    assert_eq!(
        errors("label start {\n  let a: list<int> = @game.names(\"x\")\n}\n"),
        vec![DiagCode::TypeMismatch]
    );
}

#[test]
fn argument_errors_and_unregistered_commands_are_diagnosed() {
    assert_eq!(
        errors("label start {\n  let a = @game.roll(\"x\")\n}\n"),
        vec![DiagCode::TypeMismatch]
    );
    assert_eq!(
        errors("label start {\n  let a = @game.nothing(1)\n}\n"),
        vec![DiagCode::UnknownCommand]
    );
}

#[test]
fn received_variable_cannot_be_used_before_declaration() {
    assert_eq!(
        errors("label start {\n  let a = @game.roll(a)\n}\n"),
        vec![DiagCode::UnknownName]
    );
}

#[test]
fn deeply_nested_at_calls_are_rejected_at_depth_limit_without_exhausting_stack() {
    let n = 20_000;
    let src = format!(
        "label start {{\n  let a = @game.roll({}1{})\n}}\n",
        "@game.roll(".repeat(n),
        ")".repeat(n)
    );
    let r = compile_with_warnings(&src, "main", &registry());
    let d = r.expect_err("コンパイルできない");
    assert!(d.iter().any(|d| d.code == DiagCode::TooDeep), "{d:?}");
}