plum

#treesitter#compiler#wasm

git clone https://git.pyrossh.dev/plum

A statically typed, imperative programming language inspired by rust, python


30cf07e — Peter John 2026-11-10T19:24:51+05:30
Tests: a partial revert takes dependent callers along; games build byte-identically
compiler/driver/tests/driver_tests.rs CHANGED
@@ -457,6 +457,96 @@ fn aFailingMethodAndGenericCopyRevertAlone() {
457
457
  assert_eq!(dropped[0].reverted, vec!["Cell.bad".to_string(), "same".to_string()], "{dropped:?}");
458
458
  }
459
459
 
460
+ const PARTIAL_CALLERS: &str = "\
461
+ import std/Int
462
+
463
+ fun good(n: Int) -> Int = n + 1
464
+
465
+ fun bad(n: Int) -> Int = n * 2
466
+
467
+ fun twice(n: Int) -> Int = good(n) + good(n)
468
+
469
+ fun keep(n: Int) -> Int = n - 1
470
+
471
+ fun main() -> Int = good(1) + bad(2) + twice(3) + keep(4)
472
+ ";
473
+
474
+ /// Renames every call of `from` in `f` to `to`, and gives each a last argument `extra` if set.
475
+ fn redirect(f: &mut plum_core::ast::Fn, from: &str, to: &str, extra: Option<i64>) {
476
+ use plum_core::ast::{Arg, Expr, ExprKind, FnBody};
477
+ let mut go = |e: &mut Expr| {
478
+ if let ExprKind::FnCall(c) = &mut e.kind {
479
+ if c.name == from {
480
+ c.name = to.to_string();
481
+ if let Some(x) = extra {
482
+ c.args.push(Arg::Positional(Expr::Int(x)));
483
+ }
484
+ }
485
+ }
486
+ };
487
+ match &mut f.body {
488
+ FnBody::Expr(e) => plum_core::visit::visitExpr(e, &mut go),
489
+ FnBody::Block(b) => plum_core::visit::visitBlock(b, &mut go),
490
+ FnBody::Extern => {}
491
+ }
492
+ }
493
+
494
+ /// A rewrite whose callers depend on the fns it changed: it adds a broken copy `bad2` of
495
+ /// `bad` and makes `main` call it, gives `good` a second parameter (and a broken result)
496
+ /// with `twice` passing it, and rewrites `keep` on its own.
497
+ fn breakWithCallers(copy: &mut Source, _: &Env) -> bool {
498
+ use plum_core::ast::{Param, ParamType, Type};
499
+ let int = Type { name: "Int".to_string(), generics: vec![] };
500
+ let mut added = None;
501
+ for item in &mut copy.items {
502
+ let Item::Fn(f) = item else { continue };
503
+ match f.name.as_str() {
504
+ "bad" => {
505
+ let mut g = f.clone();
506
+ g.name = "bad2".to_string();
507
+ g.returns = Some(Type { name: "Bool".to_string(), generics: vec![] });
508
+ plum_core::ast::refreshFnIds(&mut g);
509
+ added = Some(g);
510
+ }
511
+ "good" => {
512
+ f.params.push(Param { name: "m".to_string(), ty: ParamType::Type(int.clone()), default: None });
513
+ f.returns = Some(Type { name: "Bool".to_string(), generics: vec![] });
514
+ }
515
+ "twice" => redirect(f, "good", "good", Some(0)),
516
+ "main" => redirect(f, "bad", "bad2", None),
517
+ "keep" => f.body = plum_core::ast::FnBody::Expr(plum_core::ast::Expr::Int(3)),
518
+ _ => {}
519
+ }
520
+ }
521
+ copy.items.push(Item::Fn(added.expect("bad")));
522
+ true
523
+ }
524
+
525
+ /// A partial revert never leaves a caller rewritten against a callee that went back: an
526
+ /// added fn that fails takes its rewritten callers along (`main`), and a callee whose
527
+ /// signature the pass changed takes the callers that relied on it (`twice`, found by the
528
+ /// next round's re-check). Only `keep`, rewritten on its own, is kept.
529
+ #[test]
530
+ fn aRevertTakesTheCallersThatDependOnIt() {
531
+ let src = load("drv_partial_callers", PARTIAL_CALLERS);
532
+ let pass = Pass { name: "break callers", target: Target::Pre, needsHints: false, perFn: true, enabled: |_| true, rewrite: breakWithCallers };
533
+ let (r, dropped) = plum_driver::checkReport(&src, &PassOptions::default(), &[pass]);
534
+ let info = r.expect("still checks");
535
+ assert_eq!(dropped.len(), 1, "{dropped:?}");
536
+ assert_eq!(dropped[0].reverted, ["bad2", "good", "main", "twice"].map(String::from).to_vec(), "{dropped:?}");
537
+ let lowered = info.lowered.as_ref().unwrap();
538
+ let fnOf = |n: &str| {
539
+ lowered.items.iter().find_map(|i| match i {
540
+ Item::Fn(f) if f.name == n => Some(f.clone()),
541
+ _ => None,
542
+ })
543
+ };
544
+ assert!(fnOf("bad2").is_none(), "the failing added fn is removed");
545
+ assert_eq!(fnOf("good").expect("good").params.len(), 1, "good is back to one parameter");
546
+ let keep = fnOf("keep").expect("keep");
547
+ assert!(matches!(&keep.body, plum_core::ast::FnBody::Expr(e) if matches!(e.kind, plum_core::ast::ExprKind::Int(3))), "keep's rewrite is kept: {:?}", keep.body);
548
+ }
549
+
460
550
  /// Every game (and the scene-tree benchmark) builds with every pass kept: a rewrite that
461
551
  /// fails its re-check, even in one fn, is reported here.
462
552
  #[test]
compiler/game/tests/host_test.rs CHANGED
@@ -676,3 +676,16 @@ fn inputIsPolledOncePerFrameWhateverTheTicks() {
676
676
  assert!(perFrame.windows(2).all(|w| w[0] == w[1]), "{game}: input reads per frame vary with the ticks: {perFrame:?}");
677
677
  }
678
678
  }
679
+
680
+ /// Builds are reproducible: compiling a game again gives the very same bytes. Each `HashMap`
681
+ /// hashes with keys of its own, so a pass or codegen whose output followed a map's order
682
+ /// would differ between two compiles in one process too.
683
+ #[test]
684
+ fn exampleGamesCompileToTheSameBytesEveryTime() {
685
+ for game in ["examples/games/orb_run.plum", "examples/games/kids/balloon_pop.plum"] {
686
+ let first = compile(&root().join(game));
687
+ for _ in 0..2 {
688
+ assert!(compile(&root().join(game)) == first, "{game}: two compiles gave different bytes");
689
+ }
690
+ }
691
+ }