plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
d5c0a4d
— Peter John
2026-11-06T22:16:45+05:30
Simpler language: examples and tests off return, while and local reassignment
- compiler/checker/src/callsites.rs +4 -2
- compiler/checker/tests/checker/checker_tests.rs +10 -10
- compiler/checker/tests/checker/closure_hint_test.rs +1 -1
- compiler/checker/tests/checker/features/closure_types.rs +1 -1
- compiler/checker/tests/checker/features/primitive_generics.rs +1 -1
- compiler/checker/tests/checker/fip_test.rs +1 -2
- compiler/checker/tests/checker/fusion_pipeline_test.rs +2 -3
- compiler/checker/tests/checker/modules_tests.rs +1 -1
- compiler/checker/tests/checker/question_tests.rs +5 -5
- compiler/cli/src/main.rs +11 -2
- compiler/cli/tests/cli/assert_once_test.rs +1 -1
- compiler/cli/tests/cli/fusion_agree_test.rs +55 -0
- compiler/cli/tests/cli/main_result_test.rs +2 -2
- compiler/cli/tests/cli/no_fbip_test.rs +1 -1
- compiler/cli/tests/cli/test_scope_test.rs +1 -1
- compiler/core/src/drive.rs +1 -1
- compiler/core/tests/core/closure_spec_test.rs +24 -24
- compiler/core/tests/core/drive_test.rs +5 -3
- compiler/core/tests/core/formatter_test.rs +11 -13
- compiler/core/tests/core/ids_spans_test.rs +1 -1
- compiler/core/tests/core/parser_test.rs +4 -4
- compiler/core/tests/core/purity_test.rs +1 -1
- compiler/core/tests/core/rename_test.rs +3 -3
- compiler/core/tests/core/simplify_test.rs +1 -1
- compiler/core/tests/core/tail_test.rs +1 -1
- compiler/core/tests/core/trmc_test.rs +24 -47
- compiler/core/tests/core/uniqueness_test.rs +8 -8
- compiler/core/tests/core/visit_test.rs +2 -2
- compiler/game/web-test/fixtures/stress_state_view.plum +14 -19
- compiler/game/web-test/fixtures/stress_state_view_deep.plum +5 -9
- compiler/wasm-codegen/tests/wasm_codegen/codegen_tests.rs +56 -46
- compiler/wasm-codegen/tests/wasm_codegen/e2e_types/type_param_static.rs +2 -2
- compiler/wasm-codegen/tests/wasm_codegen/examples_test.rs +1 -1
- compiler/wasm-codegen/tests/wasm_codegen/fusion_alloc_test.rs +4 -4
- docs/plans/2026-11-06-simpler-language.md +1 -1
- docs/plans/2026-11-06-simpler-language.t9-notes.md +47 -0
- examples/arrays.plum +18 -36
- examples/basics.plum +14 -11
- examples/control_flow.plum +124 -91
- examples/errors.plum +71 -58
- examples/functional.plum +19 -17
- examples/games/game_loop.plum +21 -22
- examples/games/kids/balloon_pop.plum +43 -39
- examples/games/orb_run.plum +63 -66
- examples/generics.plum +9 -14
- examples/json.plum +1 -1
- examples/maps.plum +1 -3
- examples/match.plum +25 -19
- examples/recursion.plum +13 -10
- examples/sequences.plum +68 -56
- examples/structs_records.plum +22 -24
- examples/unions.plum +8 -11
- std/game/input.plum +15 -3
- tests/core/for_next.plum +11 -9
- tests/fbip/aliasing.plum +69 -93
- tests/fbip/array_in_place.plum +43 -0
- tests/fbip/array_names.plum +12 -7
- tests/fbip/array_with.plum +37 -40
- tests/fbip/fip.plum +11 -16
- tests/fbip/slice_shapes.plum +28 -63
- tests/fbip/spread_ro_method.plum +9 -12
- tests/fbip_probes/array_set.plum +1 -4
- tests/fbip_probes/map_set_get.plum +1 -4
- tests/fbip_probes/struct_spread.plum +1 -3
- tests/param_defaults.plum +6 -7
- tests/syntax/block_values_passes.plum +15 -17
- tests/syntax/question.plum +7 -5
- tests/wasm_codegen/e2e_tail/question_in_tail_not_tail_call.plum +4 -3
- tests/wasm_codegen/e2e_tail/result_mutual_million.plum +6 -4
- tests/wasm_codegen/e2e_tail/result_self_loop_million.plum +5 -4
- tests/wasm_codegen/e2e_tail/result_self_loop_returns_err.plum +3 -2
- tests/wasm_codegen/e2e_types/chained_err_value_is_first.plum +3 -2
- tests/wasm_codegen/e2e_types/chained_question_returns_first_err.plum +3 -2
- tests/wasm_codegen/e2e_types/nested_generic.plum +2 -2
- tests/wasm_codegen/e2e_types/ok_unit_and_expression_position.plum +3 -2
- tests/wasm_codegen/e2e_types/seq_with_optional_next.plum +7 -6
- tests/wasm_codegen/e2e_types/static_on_builtin.plum +1 -1
- tests/wasm_codegen/e2e_types/static_on_builtin_method_and_json_cycle.plum +1 -1
- tests/wasm_codegen/e2e_types/static_on_type_param.plum +1 -1
- tests/wasm_codegen/e2e_types/user_type_shadows_std_pair_imported_by_std.plum +1 -3
compiler/checker/src/callsites.rs
CHANGED
|
@@ -141,7 +141,9 @@ pub(crate) fn registerCalls(
|
|
|
141
141
|
// A static method of a generic type whose signature never mentions an
|
|
142
142
|
// owner generic (`Box[T].ints(n) -> Box[Int]` called as `Box.ints(3)`):
|
|
143
143
|
// nothing at the call pins it, and nothing in the signature depends on
|
|
144
|
-
// it, so any one copy will do — pin it to `Unit`.
|
|
144
|
+
// it, so any one copy will do — pin it to `Unit`. Likewise a method called on a bare
|
|
145
|
+
// nullary variant (`Nil.fold(0, f)`): the checker accepted the call with nothing
|
|
146
|
+
// fixing `T`, and no element of that type exists at run time.
|
|
145
147
|
let owner_gens: Vec<String> = match &f.type_param {
|
|
146
148
|
Some(r) => enumGenericsOf(r, ctx).to_vec(),
|
|
147
149
|
None => Vec::new(),
|
|
@@ -161,7 +163,7 @@ pub(crate) fn registerCalls(
|
|
|
161
163
|
for g in &gens {
|
|
162
164
|
match cs.args.get(g).map(|t| substVars(t, &binds)) {
|
|
163
165
|
Some(t) if !hasFreeVars(&t) => args.push(t),
|
|
164
|
-
_ if
|
|
166
|
+
_ if owner_gens.contains(g) && !sig_free.contains(g) => {
|
|
165
167
|
args.push(PlumType::TUnit)
|
|
166
168
|
}
|
|
167
169
|
_ => break,
|
compiler/checker/tests/checker/checker_tests.rs
CHANGED
|
@@ -438,7 +438,7 @@ fn unknown_method_on_heap_const_is_checker_error() {
|
|
|
438
438
|
|
|
439
439
|
#[test]
|
|
440
440
|
fn recovered_call_failure_does_not_reposition_genuine_error() {
|
|
441
|
-
let src = "enum Box[T] =\n | Box(v: T)\n\nenum Cfg =\n | Cfg(a: Int)\n\n Cell = Box(1)\n\nfun f() -> Int =\n
|
|
441
|
+
let src = "enum Box[T] =\n | Box(v: T)\n\nenum Cfg =\n | Cfg(a: Int)\n\n Cell = Box(1)\n\nfun f() -> Int =\n \"{Cfg.Cell.size()}\"\n";
|
|
442
442
|
let errs = checkSrc(src).unwrap_err();
|
|
443
443
|
// The interpolated call is a genuine error now (it used to be dropped, leaving only
|
|
444
444
|
// the return mismatch): reported at the call inside the string.
|
|
@@ -607,12 +607,12 @@ fn rejects_basic_expressions_and_calls() {
|
|
|
607
607
|
r("fun f(c: Bool) -> Int =\n if c then 1 else \"no\"\n", &["inline if branches"]),
|
|
608
608
|
r("fun f() -> Bool =\n True\n", &["found True"]),
|
|
609
609
|
r("fun f(b: Bool) -> Int =\n match b\n True => 1\n false => 0\n", &["unknown tag True"]),
|
|
610
|
-
r("fun main() -> Int =\n
|
|
610
|
+
r("fun main() -> Int =\n for x := range 5.5f\n x\n 0\n", &["'5.5f' is not iterable"]),
|
|
611
611
|
r("fun five() -> Float =\n 5.0f\n\nfun main() -> Int =\n for x := range five()\n x\n 0\n", &["'five()' is not iterable"]),
|
|
612
612
|
r("fun a(p: Int) -> Int =\n p\n\nfun main() =\n x := a(\"a\" => 1)\n", &["import std/Map"]),
|
|
613
613
|
r("fun f() -> Bool =\n a := |x| x\n b := |x| x\n a == b\n", &["can't compare functions"]),
|
|
614
614
|
r("fun f(a: fn(Int) -> Int, b: fn(Int) -> Int) -> Bool =\n a == b\n", &["can't compare functions"]),
|
|
615
|
-
r("fun u() =\n
|
|
615
|
+
r("fun u() =\n Unit\n\nfun f() -> Bool =\n u() == u()\n", &["can't compare Unit values"]),
|
|
616
616
|
// enum/class fields and constructors
|
|
617
617
|
r(&format!("{cat}fun getAge() -> Int =\n self.age\n"), &["`self` is only available in a method"]),
|
|
618
618
|
// a `fun` without `self` is a static: called on the type, never on a value
|
|
@@ -626,7 +626,7 @@ fn rejects_basic_expressions_and_calls() {
|
|
|
626
626
|
r(&format!("{cat} fun breakCat(self) =\n self.nope = 1\n"), &["no field 'nope' on type 'Cat'"]),
|
|
627
627
|
r("enum Shape =\n | Square(x: Int, y: Int)\n\nfun bad() -> Shape =\n Square(x: 1)\n", &["variant 'Square' missing field 'y'"]),
|
|
628
628
|
r("enum Shape =\n | Square(x: Int, y: Int)\n\nfun bad() -> Shape =\n Square(x: 1, y: 2, z: 3)\n", &["unknown field 'z' on variant 'Square'"]),
|
|
629
|
-
r("enum Shape =\n | Circle(radius: Int)\n | Square(radius: Int)\n\nfun bad() -> Int =\n r := Circle(radius: 5)\n
|
|
629
|
+
r("enum Shape =\n | Circle(radius: Int)\n | Square(radius: Int)\n\nfun bad() -> Int =\n r := Circle(radius: 5)\n r.radius\n", &["no field 'radius' on type 'Shape'"]),
|
|
630
630
|
r("enum Maybe =\n | Just(Int)\n | Nothing\n\nfun bad() -> Maybe =\n Just(\"x\")\n", &["variant 'Just' arg 0: type mismatch"]),
|
|
631
631
|
r("enum Shape =\n | Rect(Float, Float)\n | Circle(Float)\n\nfun bad() -> Shape =\n Rect(1.0)\n", &["variant 'Rect': expected 2 arg(s), got 1"]),
|
|
632
632
|
// variant as a parameter type rejects a different variant
|
|
@@ -649,12 +649,12 @@ fn rejects_bad_patterns_and_non_exhaustive_matches() {
|
|
|
649
649
|
r("enum Shape =\n | Rect(Float, Float)\n | Circle(Float)\n\nfun bad(s: Shape) -> Float =\n match s\n Rect(w) =>\n w\n _ =>\n 0.0\n", &["expects 2 field(s), got 1"]),
|
|
650
650
|
r("enum Point =\n | Point(x: Int, y: Int)\n\nfun sumPoint(p: Point) -> Int =\n match p\n Point(x) => x\n", &["expects 2 field(s), got 1"]),
|
|
651
651
|
r("fun f(n: Int) -> Int =\n match n\n MAX => 1\n _ => 0\n", &["unknown tag MAX"]),
|
|
652
|
-
r("fun f(n: Int) -> Int =\n if MAX := n\n
|
|
652
|
+
r("fun f(n: Int) -> Int =\n if MAX := n\n 1\n else\n 0\n", &["unknown tag MAX"]),
|
|
653
653
|
// pattern-if
|
|
654
|
-
r(&format!("{shape}fun f(s: Shape) -> Int =\n if Circle(r) := s\n
|
|
654
|
+
r(&format!("{shape}fun f(s: Shape) -> Int =\n if Circle(r) := s\n r\n r\n"), &["undefined name"]),
|
|
655
|
-
r(&format!("{shape}fun f(s: Shape) -> Int =\n if Circle(r) := s\n
|
|
655
|
+
r(&format!("{shape}fun f(s: Shape) -> Int =\n if Circle(r) := s\n r\n else\n r\n"), &["undefined name"]),
|
|
656
|
-
r(&format!("{shape}fun f(s: Shape) -> Int =\n if Rect(w, h) := s, w\n
|
|
656
|
+
r(&format!("{shape}fun f(s: Shape) -> Int =\n if Rect(w, h) := s, w\n w\n else\n 0\n"), &["condition after ',' must be Bool"]),
|
|
657
|
-
r(&format!("{shape}fun f(s: Shape) -> Int =\n if Rect(w) := s\n
|
|
657
|
+
r(&format!("{shape}fun f(s: Shape) -> Int =\n if Rect(w) := s\n w\n else\n 0\n"), &["expects 2 field(s), got 1"]),
|
|
658
658
|
]);
|
|
659
659
|
}
|
|
660
660
|
|
|
@@ -791,7 +791,7 @@ fn underscoreDiscardIsAccepted() {
|
|
|
791
791
|
#[test]
|
|
792
792
|
fn discardTargetTwiceInOneScopeIsFine() {
|
|
793
793
|
assert!(
|
|
794
|
-
checkDiscard("fun f() =\n _ := five()\n _ := five()\n _
|
|
794
|
+
checkDiscard("fun f() =\n _ := five()\n _ := five()\n _ := five()\n")
|
|
795
795
|
.is_ok()
|
|
796
796
|
);
|
|
797
797
|
}
|
compiler/checker/tests/checker/closure_hint_test.rs
CHANGED
|
@@ -49,7 +49,7 @@ fn ambiguous_method_name_specialised_by_receiver_type() {
|
|
|
49
49
|
#[test]
|
|
50
50
|
fn ambiguous_method_name_specialised_in_lowered_program() {
|
|
51
51
|
// a `for` loop makes the checker check a lowered copy, as for every std program
|
|
52
|
-
let src = format!("{TYPES}\nfun count(n: Int) -> Int =\n
|
|
52
|
+
let src = format!("{TYPES}\nfun count(n: Int) -> Int =\n for i := range n\n _ := i\n n\n");
|
|
53
53
|
let calls = calledByUse(&src);
|
|
54
54
|
assert!(calls.iter().any(|n| n.starts_with("apply$c")), "Box.apply not specialised: {calls:?}");
|
|
55
55
|
}
|
compiler/checker/tests/checker/features/closure_types.rs
CHANGED
|
@@ -37,7 +37,7 @@ fn local_closure_never_called_error() {
|
|
|
37
37
|
|
|
38
38
|
#[test]
|
|
39
39
|
fn local_closure_called_in_loop_body_not_an_error() {
|
|
40
|
-
let r = check("fun main() -> Int =\n g := |x| x + 1\n
|
|
40
|
+
let r = check("fun main() -> Int =\n g := |x| x + 1\n for i := range 3\n _ := g(i)\n 0\n");
|
|
41
41
|
assert!(r.is_ok(), "{r:?}");
|
|
42
42
|
}
|
|
43
43
|
|
compiler/checker/tests/checker/features/primitive_generics.rs
CHANGED
|
@@ -23,7 +23,7 @@ fn primitiveGenericsAreNotPseudoVariants() {
|
|
|
23
23
|
fn arrayElementTypeIsEnforced() {
|
|
24
24
|
let source = load(
|
|
25
25
|
"c",
|
|
26
|
-
"import std/Array\nimport std/Int\nimport std/Str\n\nfun main() -> Str =\n a := Array[Int]().push(3)\n
|
|
26
|
+
"import std/Array\nimport std/Int\nimport std/Str\n\nfun main() -> Str =\n a := Array[Int]().push(3)\n a.get(0)\n",
|
|
27
27
|
);
|
|
28
28
|
assert!(checkSource(&source).is_err());
|
|
29
29
|
}
|
compiler/checker/tests/checker/fip_test.rs
CHANGED
|
@@ -148,8 +148,7 @@ struct Bx(items: Array[Int], n: Int) =
|
|
|
148
148
|
fip fun put(self, x: Int) -> Bx = Bx(..self, items: self.items.with(0, x))
|
|
149
149
|
fun once(n: Int) -> Bx =
|
|
150
150
|
b := Bx(items: Array.filled(n, 0), n: n)
|
|
151
|
-
b
|
|
151
|
+
b.put(1)
|
|
152
|
-
b
|
|
153
152
|
";
|
|
154
153
|
run("fip_method_ok", body, &PassOptions::default()).expect("compiles");
|
|
155
154
|
}
|
compiler/checker/tests/checker/fusion_pipeline_test.rs
CHANGED
|
@@ -23,10 +23,9 @@ fun total(xs: Cells) -> Int = sumL(mapCells(xs))
|
|
|
23
23
|
|
|
24
24
|
# a loop makes the checker lower the program, as std programs always are
|
|
25
25
|
fun count(n: Int) -> Int =
|
|
26
|
-
s := 0
|
|
27
26
|
for i := range n
|
|
28
|
-
|
|
27
|
+
_ := i
|
|
29
|
-
|
|
28
|
+
n
|
|
30
29
|
";
|
|
31
30
|
|
|
32
31
|
/// The checker plus the default pass list.
|
compiler/checker/tests/checker/modules_tests.rs
CHANGED
|
@@ -121,7 +121,7 @@ fn a_local_in_one_branch_does_not_capture_the_module_in_another() {
|
|
|
121
121
|
run(
|
|
122
122
|
"branch",
|
|
123
123
|
&format!(
|
|
124
|
-
"{BOTH}fun f(c: Bool) -> Int =\n before := mathx.clamp(500)\n if c\n mathx := 2\n
|
|
124
|
+
"{BOTH}fun f(c: Bool) -> Int =\n before := mathx.clamp(500)\n if c\n mathx := 2\n mathx + before\n else\n mathx.clamp(500) + mathx.Limit\n"
|
|
125
125
|
),
|
|
126
126
|
)
|
|
127
127
|
.unwrap();
|
compiler/checker/tests/checker/question_tests.rs
CHANGED
|
@@ -108,7 +108,7 @@ fn fn_typed_param_shadows_top_level_fn_arity() {
|
|
|
108
108
|
fn bare_value_into_option_return_errors() {
|
|
109
109
|
rejected("fun f(x: Int) -> Option[Int] = x", "wrap it with Some(..)");
|
|
110
110
|
rejected("fun f(o: Option[Int]) -> Option[Int] =\n match o\n Some(v) => v\n None => None", "Some(");
|
|
111
|
-
rejected("fun f(o: Option[Int]) -> Option[Int] =\n if Some(v) := o\n
|
|
111
|
+
rejected("fun f(o: Option[Int]) -> Option[Int] =\n if Some(v) := o\n v\n else\n None", "Some(");
|
|
112
112
|
}
|
|
113
113
|
|
|
114
114
|
#[test]
|
|
@@ -155,7 +155,7 @@ fn wrap_hint_into_generic_option_slot_is_one_error() {
|
|
|
155
155
|
#[test]
|
|
156
156
|
fn non_final_branch_values_are_not_the_return_value() {
|
|
157
157
|
ok("fun f(x: Int) -> Option[Int] =\n if x > 1\n x\n else\n 2\n match x\n 1 => 3\n _ => 4\n Some(x)");
|
|
158
|
-
ok("fun f(x: Int) -> Option[Int] =\n
|
|
158
|
+
ok("fun f(x: Int) -> Option[Int] =\n if x > 1\n y := x\n y\n else\n 0\n None");
|
|
159
159
|
}
|
|
160
160
|
|
|
161
161
|
#[test]
|
|
@@ -171,10 +171,10 @@ fn branch_tail_mismatch_reported_once() {
|
|
|
171
171
|
#[test]
|
|
172
172
|
fn plain_name_bind_on_option_errors() {
|
|
173
173
|
rejected(
|
|
174
|
-
"fun g() -> Option[Int] = Some(1)\nfun f() -> Int =\n if x := g()\n
|
|
174
|
+
"fun g() -> Option[Int] = Some(1)\nfun f() -> Int =\n if x := g()\n x\n else\n 0",
|
|
175
175
|
"Some(x) := v",
|
|
176
176
|
);
|
|
177
|
-
ok("fun g() -> Option[Int] = Some(1)\nfun f() -> Int =\n if Some(x) := g()\n
|
|
177
|
+
ok("fun g() -> Option[Int] = Some(1)\nfun f() -> Int =\n if Some(x) := g()\n x\n else\n 0");
|
|
178
178
|
}
|
|
179
179
|
|
|
180
180
|
const NUM_BOX: &str = "\
|
|
@@ -230,7 +230,7 @@ fn question_rejected_forms() {
|
|
|
230
230
|
("fun f() -> Option[Int] =\n b := true && g()? > 0\n Some(1)", "conditionally"),
|
|
231
231
|
("fun f() -> Option[Int] =\n b := if true then g()? else 1\n Some(b)", "conditionally"),
|
|
232
232
|
("fun f() -> Option[Int] =\n h := || g()?\n Some(1)", "closure"),
|
|
233
|
-
("fun f() -> Option[Int] =\n while g()? > 0\n
|
|
233
|
+
("fun f() -> Option[Int] =\n while g()? > 0\n _ := 1\n Some(1)", "conditionally"),
|
|
234
234
|
] {
|
|
235
235
|
rejected(&format!("{g}{body}"), needle);
|
|
236
236
|
}
|
compiler/cli/src/main.rs
CHANGED
|
@@ -600,8 +600,8 @@ fn readStrResult(store: &mut wasmtime::Store<()>, val: &wasmtime::Val) -> Result
|
|
|
600
600
|
/// working directory, so `plum build` works no matter where it's invoked from.
|
|
601
601
|
fn runtimeCrateDir() -> std::path::PathBuf {
|
|
602
602
|
std::path::Path::new(env!("CARGO_MANIFEST_DIR"))
|
|
603
|
-
.
|
|
603
|
+
.parent()
|
|
604
|
-
.expect("plum-cli should
|
|
604
|
+
.expect("plum-cli should sit next to plum-runtime under compiler/")
|
|
605
605
|
.join("runtime")
|
|
606
606
|
}
|
|
607
607
|
|
|
@@ -661,3 +661,12 @@ fn cmdBuild(
|
|
|
661
661
|
eprintln!("built {} → {}", file.display(), out_path.display());
|
|
662
662
|
Ok(())
|
|
663
663
|
}
|
|
664
|
+
|
|
665
|
+
#[cfg(test)]
|
|
666
|
+
mod runtime_path_tests {
|
|
667
|
+
/// `plum build` hands this manifest to cargo, so a moved crate must fail here.
|
|
668
|
+
#[test]
|
|
669
|
+
fn runtimeCrateIsWhereBuildLooks() {
|
|
670
|
+
assert!(super::runtimeCrateDir().join("Cargo.toml").is_file(), "{:?}", super::runtimeCrateDir());
|
|
671
|
+
}
|
|
672
|
+
}
|
compiler/cli/tests/cli/assert_once_test.rs
CHANGED
|
@@ -28,7 +28,7 @@ struct Counter(mut n: Int) =
|
|
|
28
28
|
|
|
29
29
|
fun bump(self) -> Int =
|
|
30
30
|
self.n = self.n + 1
|
|
31
|
-
|
|
31
|
+
self.n
|
|
32
32
|
|
|
33
33
|
test "operands once"
|
|
34
34
|
a := Counter(n: 0)
|
compiler/cli/tests/cli/fusion_agree_test.rs
CHANGED
|
@@ -47,3 +47,58 @@ fn shadowedEffectsAgree() {
|
|
|
47
47
|
fn paramDefaultsAgree() {
|
|
48
48
|
passesWithAndWithoutFusion("param_defaults.plum");
|
|
49
49
|
}
|
|
50
|
+
|
|
51
|
+
/// A list literal fused down to its end leaves a call on bare `Nil` to a closure's copy
|
|
52
|
+
/// (`Nil.fold$c0(..)`), which nothing but the receiver could type. Each program is run with
|
|
53
|
+
/// fusion on and off: both must compile and print the expected output. `xs.fold` at the end of
|
|
54
|
+
/// `main`, an expression body, and a callee handed `[]` were the shapes that failed.
|
|
55
|
+
#[test]
|
|
56
|
+
fn closureCopiesInTailPositionAgree() {
|
|
57
|
+
const HEAD: &str = "import std/Int\nimport std/Str\nimport std/Io\nimport std/List\nimport std/Option\n\n";
|
|
58
|
+
let cases: &[(&str, &str, &str)] = &[
|
|
59
|
+
("list_fold_tail", "fun main() -> Int =\n xs := [1, 2, 3]\n xs.fold(0, |a, x| a + x)\n", "6"),
|
|
60
|
+
("list_fold_body", "fun sum(xs: List[Int]) -> Int = xs.fold(0, |a, x| a + x)\n\nfun main() -> Int = sum([1, 2, 3])\n", "6"),
|
|
61
|
+
("list_fold_empty", "fun sum(xs: List[Int]) -> Int = xs.fold(0, |a, x| a + x)\n\nfun main() -> Int = sum([])\n", "0"),
|
|
62
|
+
("list_fold_literal", "fun main() -> Int = [1, 2, 3].fold(0, |a, x| a + x)\n", "6"),
|
|
63
|
+
("list_fold_empty_literal", "fun main() -> Int = Nil.fold(5, |a, x| a + x)\n", "5"),
|
|
64
|
+
("list_fold_bound", "fun main() -> Int =\n xs := [1, 2, 3]\n r := xs.fold(0, |a, x| a + x)\n r\n", "6"),
|
|
65
|
+
("list_each_tail", "fun main(io: Io) =\n xs := [1, 2]\n xs.each(|x| io.printLn(\"{x}\"))\n", "1\n2"),
|
|
66
|
+
("list_each_body", "fun show(io: Io, xs: List[Int]) = xs.each(|x| io.printLn(\"{x}\"))\n\nfun main(io: Io) = show(io, [3, 4])\n", "3\n4"),
|
|
67
|
+
("list_each_empty", "fun show(io: Io, xs: List[Int]) = xs.each(|x| io.printLn(\"{x}\"))\n\nfun main(io: Io) = show(io, [])\n", ""),
|
|
68
|
+
("list_find_tail", "fun main() -> Int =\n xs := [1, 2, 3]\n xs.find(|x| x > 1).unwrapOr(-1)\n", "2"),
|
|
69
|
+
("list_find_body", "fun big(xs: List[Int]) -> Option[Int] = xs.find(|x| x > 1)\n\nfun main() -> Int = big([1, 2, 3]).unwrapOr(-1)\n", "2"),
|
|
70
|
+
("list_find_empty", "fun big(xs: List[Int]) -> Option[Int] = xs.find(|x| x > 1)\n\nfun main() -> Int = big([]).unwrapOr(-1)\n", "-1"),
|
|
71
|
+
("list_find_miss", "fun main() -> Int =\n xs := [1, 2, 3]\n xs.find(|x| x > 9).unwrapOr(-1)\n", "-1"),
|
|
72
|
+
("range_fold_tail", "fun main() -> Int = 1.to(3).fold(0, |a, x| a + x)\n", "6"),
|
|
73
|
+
("range_fold_body", "fun sum(n: Int) -> Int = 0.until(n).fold(0, |a, x| a + x)\n\nfun main() -> Int = sum(4)\n", "6"),
|
|
74
|
+
("range_fold_empty", "fun main() -> Int = 5.until(5).fold(7, |a, x| a + x)\n", "7"),
|
|
75
|
+
("range_each_tail", "fun main(io: Io) = 1.to(2).each(|x| io.printLn(\"{x}\"))\n", "1\n2"),
|
|
76
|
+
("range_each_empty", "fun main(io: Io) = 5.until(5).each(|x| io.printLn(\"{x}\"))\n", ""),
|
|
77
|
+
("range_find_tail", "fun main() -> Int = 1.to(3).find(|x| x > 1).unwrapOr(-1)\n", "2"),
|
|
78
|
+
("range_find_body", "fun big(n: Int) -> Option[Int] = 0.until(n).find(|x| x > 1)\n\nfun main() -> Int = big(4).unwrapOr(-1)\n", "2"),
|
|
79
|
+
("range_find_empty", "fun big(n: Int) -> Option[Int] = 0.until(n).find(|x| x > 1)\n\nfun main() -> Int = big(0).unwrapOr(-1)\n", "-1"),
|
|
80
|
+
];
|
|
81
|
+
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).ancestors().nth(2).unwrap();
|
|
82
|
+
let dir = std::env::temp_dir().join(format!("plum_tail_copies_{}", std::process::id()));
|
|
83
|
+
std::fs::create_dir_all(&dir).unwrap();
|
|
84
|
+
let mut failures = Vec::new();
|
|
85
|
+
for (name, program, want) in cases {
|
|
86
|
+
let src = dir.join(format!("{name}.plum"));
|
|
87
|
+
std::fs::write(&src, format!("{HEAD}{program}")).unwrap();
|
|
88
|
+
for flags in [&[][..], &["--no-fusion"][..]] {
|
|
89
|
+
let out = Command::new(plumBin())
|
|
90
|
+
.arg("run")
|
|
91
|
+
.args(flags)
|
|
92
|
+
.arg(&src)
|
|
93
|
+
.arg("--lib-path")
|
|
94
|
+
.arg(root)
|
|
95
|
+
.output()
|
|
96
|
+
.expect("failed to run plum");
|
|
97
|
+
let got = String::from_utf8_lossy(&out.stdout);
|
|
98
|
+
if !out.status.success() || got.trim() != *want {
|
|
99
|
+
failures.push(format!("{name} {flags:?}: wanted {want:?}, got {:?}, stderr: {}", got.trim(), String::from_utf8_lossy(&out.stderr)));
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
assert!(failures.is_empty(), "{}", failures.join("\n"));
|
|
104
|
+
}
|
compiler/cli/tests/cli/main_result_test.rs
CHANGED
|
@@ -65,7 +65,7 @@ fn unitOkExitsZero() {
|
|
|
65
65
|
("unitok", "fun main() -> Result[Unit, AppError] =\n Ok(Unit)\n"),
|
|
66
66
|
(
|
|
67
67
|
"unitokif",
|
|
68
|
-
"fun step(n: Int) -> Result[Unit, AppError] =\n if n > 0\n
|
|
68
|
+
"fun step(n: Int) -> Result[Unit, AppError] =\n if n > 0\n Err(Bad)\n else\n Ok(Unit)\n\nfun main() -> Result[Unit, AppError] =\n step(0)?\n step(-1)\n",
|
|
69
69
|
),
|
|
70
70
|
] {
|
|
71
71
|
let out = runSource(name, body);
|
|
@@ -81,7 +81,7 @@ fn unitErrExitsOneAndNamesTheVariant() {
|
|
|
81
81
|
("uniterr", "fun main() -> Result[Unit, AppError] =\n Err(Bad)\n", "crash: Bad"),
|
|
82
82
|
(
|
|
83
83
|
"uniterrq",
|
|
84
|
-
"fun step(n: Int) -> Result[Unit, AppError] =\n if n > 0\n
|
|
84
|
+
"fun step(n: Int) -> Result[Unit, AppError] =\n if n > 0\n Err(Bad)\n else\n Ok(Unit)\n\nfun main() -> Result[Unit, AppError] =\n step(0)?\n step(1)?\n Ok(Unit)\n",
|
|
85
85
|
"crash: Bad",
|
|
86
86
|
),
|
|
87
87
|
] {
|
compiler/cli/tests/cli/no_fbip_test.rs
CHANGED
|
@@ -11,7 +11,7 @@ fn plumBin() -> std::path::PathBuf {
|
|
|
11
11
|
path
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
const SRC: &str = "import std/Int\nimport std/Str\nimport std/Io\n\nstruct Cell(label: Str, n: Int)\n\nfun
|
|
14
|
+
const SRC: &str = "import std/Int\nimport std/Str\nimport std/Io\n\nstruct Cell(label: Str, n: Int)\n\nfun bump(c: Cell) -> Cell = Cell(..c, n: c.n + 1)\n\nfun main(io: Io) =\n c := bump(bump(bump(bump(bump(bump(bump(bump(bump(bump(Cell(label: \"c\", n: 0)))))))))))\n io.printLn(\"{c.n}\")\n";
|
|
15
15
|
|
|
16
16
|
fn run(src: &std::path::Path, flags: &[&str]) -> (String, String) {
|
|
17
17
|
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).ancestors().nth(2).unwrap();
|
compiler/cli/tests/cli/test_scope_test.rs
CHANGED
|
@@ -60,7 +60,7 @@ fn failingImportedTestDoesNotFailImporter() {
|
|
|
60
60
|
.join(format!("scope_imp_{}", std::process::id()));
|
|
61
61
|
std::fs::create_dir_all(&dir).unwrap();
|
|
62
62
|
let rel = format!("target/scope_imp_{}/lib", std::process::id());
|
|
63
|
-
std::fs::write(dir.join("lib.plum"), "module lib\n\nimport std/Int\n\nfun one() -> Int =\n
|
|
63
|
+
std::fs::write(dir.join("lib.plum"), "module lib\n\nimport std/Int\n\nfun one() -> Int =\n 1\n\ntest \"bad\"\n assert 1 == 2\n").unwrap();
|
|
64
64
|
let f = dir.join("main.plum");
|
|
65
65
|
std::fs::write(
|
|
66
66
|
&f,
|
compiler/core/src/drive.rs
CHANGED
|
@@ -2108,7 +2108,7 @@ impl Driver {
|
|
|
2108
2108
|
return own;
|
|
2109
2109
|
}
|
|
2110
2110
|
match self.ctorOwner.get(t) {
|
|
2111
|
-
Some(o) if o != t &&
|
|
2111
|
+
Some(o) if o != t && self.fns.get(&format!("{o}.{method}")).is_some_and(|f| f.has_self) => format!("{o}.{method}"),
|
|
2112
2112
|
_ => own,
|
|
2113
2113
|
}
|
|
2114
2114
|
}
|
compiler/core/tests/core/closure_spec_test.rs
CHANGED
|
@@ -170,7 +170,7 @@ fn withHead(body: &str) -> String {
|
|
|
170
170
|
#[test]
|
|
171
171
|
fn map_literal_no_call_ref() {
|
|
172
172
|
let src = withHead(
|
|
173
|
-
"fun main() -> Int =\n xs := [1, 2, 3, 4]\n
|
|
173
|
+
"fun main() -> Int =\n xs := [1, 2, 3, 4]\n xs.map(|x| x * 2).fold(0, |a, x| a + x)\n",
|
|
174
174
|
);
|
|
175
175
|
let names = copies("map_lit", &src);
|
|
176
176
|
assert!(names.contains(&"List.map$c0".to_string()), "{names:?}");
|
|
@@ -183,7 +183,7 @@ fn map_literal_no_call_ref() {
|
|
|
183
183
|
#[test]
|
|
184
184
|
fn capture_becomes_param() {
|
|
185
185
|
let src = withHead(
|
|
186
|
-
"fun main() -> Int =\n k := 10\n xs := [1, 2, 3]\n
|
|
186
|
+
"fun main() -> Int =\n k := 10\n xs := [1, 2, 3]\n xs.map(|x| x + k).fold(0, |a, x| a + x)\n",
|
|
187
187
|
);
|
|
188
188
|
let names = copies("capture", &src);
|
|
189
189
|
assert!(names.contains(&"List.map$c0".to_string()), "{names:?}");
|
|
@@ -195,7 +195,7 @@ fn capture_becomes_param() {
|
|
|
195
195
|
#[test]
|
|
196
196
|
fn recursive_copy_calls_itself() {
|
|
197
197
|
let src = withHead(
|
|
198
|
-
"fun main() -> Int =\n xs := [1, 2, 3, 4, 5]\n
|
|
198
|
+
"fun main() -> Int =\n xs := [1, 2, 3, 4, 5]\n xs.fold(0, |a, x| a + x)\n",
|
|
199
199
|
);
|
|
200
200
|
let l = lowered("recursive", &src);
|
|
201
201
|
let f = l
|
|
@@ -214,7 +214,7 @@ fn recursive_copy_calls_itself() {
|
|
|
214
214
|
#[test]
|
|
215
215
|
fn fn_name_argument() {
|
|
216
216
|
let src = withHead(
|
|
217
|
-
"fun double(x: Int) -> Int = x * 2\n\nfun main() -> Int =\n xs := [1, 2, 3]\n
|
|
217
|
+
"fun double(x: Int) -> Int = x * 2\n\nfun main() -> Int =\n xs := [1, 2, 3]\n xs.map(double).fold(0, |a, x| a + x)\n",
|
|
218
218
|
);
|
|
219
219
|
let names = copies("fnname", &src);
|
|
220
220
|
assert!(names.contains(&"List.map$c0".to_string()), "{names:?}");
|
|
@@ -240,7 +240,7 @@ fn copies_of_same_named_methods_have_different_names() {
|
|
|
240
240
|
#[test]
|
|
241
241
|
fn escaping_closure_untouched() {
|
|
242
242
|
let src = withHead(
|
|
243
|
-
"struct Box(cb: fn(Int) -> Int)\n\nfun keep(f: fn(Int) -> Int) -> Box = Box(cb: f)\n\nfun main() -> Int =\n b := keep(|x| x + 1)\n g := b.cb\n
|
|
243
|
+
"struct Box(cb: fn(Int) -> Int)\n\nfun keep(f: fn(Int) -> Int) -> Box = Box(cb: f)\n\nfun main() -> Int =\n b := keep(|x| x + 1)\n g := b.cb\n g(41)\n",
|
|
244
244
|
);
|
|
245
245
|
assert!(userCopies("escaping", &src).is_empty());
|
|
246
246
|
assert_eq!(runInt("escaping", &src), 42);
|
|
@@ -249,7 +249,7 @@ fn escaping_closure_untouched() {
|
|
|
249
249
|
#[test]
|
|
250
250
|
fn dedup_same_closure_twice() {
|
|
251
251
|
let src = withHead(
|
|
252
|
-
"fun main() -> Int =\n xs := [1, 2, 3]\n a := xs.map(|x| x * 2).fold(0, |a, x| a + x)\n b := xs.map(|x| x * 2).fold(0, |a, x| a + x)\n
|
|
252
|
+
"fun main() -> Int =\n xs := [1, 2, 3]\n a := xs.map(|x| x * 2).fold(0, |a, x| a + x)\n b := xs.map(|x| x * 2).fold(0, |a, x| a + x)\n a + b\n",
|
|
253
253
|
);
|
|
254
254
|
let names = copies("dedup", &src);
|
|
255
255
|
assert_eq!(names.iter().filter(|n| n.starts_with("List.map$")).count(), 1, "{names:?}");
|
|
@@ -258,11 +258,11 @@ fn dedup_same_closure_twice() {
|
|
|
258
258
|
|
|
259
259
|
#[test]
|
|
260
260
|
fn cap_eight_copies() {
|
|
261
|
-
let mut body = String::from("fun main() -> Int =\n xs := [1, 2, 3]\n
|
|
261
|
+
let mut body = String::from("fun main() -> Int =\n xs := [1, 2, 3]\n");
|
|
262
262
|
for i in 0..10 {
|
|
263
|
-
body.push_str(&format!("
|
|
263
|
+
body.push_str(&format!(" r{0} := xs.map(|x| x + {0}).fold(0, |a, x| a + x)\n", i));
|
|
264
264
|
}
|
|
265
|
-
body.push_str("
|
|
265
|
+
body.push_str(" r0 + r1 + r2 + r3 + r4 + r5 + r6 + r7 + r8 + r9\n");
|
|
266
266
|
let src = withHead(&body);
|
|
267
267
|
let names = copies("cap8", &src);
|
|
268
268
|
assert_eq!(names.iter().filter(|n| n.starts_with("List.map$")).count(), 8, "{names:?}");
|
|
@@ -273,7 +273,7 @@ fn cap_eight_copies() {
|
|
|
273
273
|
#[test]
|
|
274
274
|
fn block_closure_is_inlined() {
|
|
275
275
|
let src = withHead(
|
|
276
|
-
"fun main() -> Int =\n xs := [1, 2, 3, 4]\n k := 10\n y := 1000\n r := xs.fold(0, |a, x|\n y := x * k\n a + y\n )\n
|
|
276
|
+
"fun main() -> Int =\n xs := [1, 2, 3, 4]\n k := 10\n y := 1000\n r := xs.fold(0, |a, x|\n y := x * k\n a + y\n )\n r + y\n",
|
|
277
277
|
);
|
|
278
278
|
let names = copies("block_closure", &src);
|
|
279
279
|
assert!(names.contains(&"List.fold$c0".to_string()), "{names:?}");
|
|
@@ -283,11 +283,11 @@ fn block_closure_is_inlined() {
|
|
|
283
283
|
#[test]
|
|
284
284
|
fn results_equal_original() {
|
|
285
285
|
let src = withHead(
|
|
286
|
-
"fun main() -> Int =\n xs := [1, 2, 3, 4, 5, 6, 7, 8]\n k := 3\n a := xs.map(|x| x * k).filter(|x| x % 2 == 0).fold(0, |a, x| a + x)\n b := xs.filter(|x| x > k).map(|x| x - 1).fold(100, |a, x| a - x)\n c := xs.fold(1, |a, x| if x > 4 then a + x else a)\n
|
|
286
|
+
"fun main() -> Int =\n xs := [1, 2, 3, 4, 5, 6, 7, 8]\n k := 3\n a := xs.map(|x| x * k).filter(|x| x % 2 == 0).fold(0, |a, x| a + x)\n b := xs.filter(|x| x > k).map(|x| x - 1).fold(100, |a, x| a - x)\n c := xs.fold(1, |a, x| if x > 4 then a + x else a)\n a * 10000 + b * 100 + c\n",
|
|
287
287
|
);
|
|
288
288
|
// Same program through closures held in locals, which are not specialised.
|
|
289
289
|
let plain = withHead(
|
|
290
|
-
"fun main() -> Int =\n xs := [1, 2, 3, 4, 5, 6, 7, 8]\n k := 3\n m1 := |x| x * k\n f1 := |x| x % 2 == 0\n s1 := |a, x| a + x\n a := xs.map(m1).filter(f1).fold(0, s1)\n f2 := |x| x > k\n m2 := |x| x - 1\n s2 := |a, x| a - x\n b := xs.filter(f2).map(m2).fold(100, s2)\n s3 := |a, x| if x > 4 then a + x else a\n c := xs.fold(1, s3)\n
|
|
290
|
+
"fun main() -> Int =\n xs := [1, 2, 3, 4, 5, 6, 7, 8]\n k := 3\n m1 := |x| x * k\n f1 := |x| x % 2 == 0\n s1 := |a, x| a + x\n a := xs.map(m1).filter(f1).fold(0, s1)\n f2 := |x| x > k\n m2 := |x| x - 1\n s2 := |a, x| a - x\n b := xs.filter(f2).map(m2).fold(100, s2)\n s3 := |a, x| if x > 4 then a + x else a\n c := xs.fold(1, s3)\n a * 10000 + b * 100 + c\n",
|
|
291
291
|
);
|
|
292
292
|
assert!(!copies("equal", &src).is_empty());
|
|
293
293
|
assert_eq!(runInt("equal", &src), runInt("equal_plain", &plain));
|
|
@@ -296,7 +296,7 @@ fn results_equal_original() {
|
|
|
296
296
|
#[test]
|
|
297
297
|
fn list_pipeline_million_timed() {
|
|
298
298
|
let src = withHead(
|
|
299
|
-
"fun
|
|
299
|
+
"fun buildDown(i: Int, out: List[Int]) -> List[Int] =\n if i > 0 then buildDown(i - 1, Cons(head: i, tail: out)) else out\n\nfun build(n: Int) -> List[Int] = buildDown(n, Nil)\n\nfun main() -> Int =\n xs := build(1000000)\n xs.map(|x| x * 2).filter(|x| x % 4 == 0).fold(0, |a, x| a + x)\n",
|
|
300
300
|
);
|
|
301
301
|
let bytes = compile("million", &src);
|
|
302
302
|
let t = std::time::Instant::now();
|
|
@@ -309,10 +309,10 @@ fn list_pipeline_million_timed() {
|
|
|
309
309
|
assert_eq!(r, 500001000000);
|
|
310
310
|
// Reference: the same pipeline through closures held in locals (not specialised).
|
|
311
311
|
let plain = withHead(
|
|
312
|
-
"fun
|
|
312
|
+
"fun buildDown(i: Int, out: List[Int]) -> List[Int] =\n if i > 0 then buildDown(i - 1, Cons(head: i, tail: out)) else out\n\nfun build(n: Int) -> List[Int] = buildDown(n, Nil)\n\nfun main() -> Int =\n xs := build(1000000)\n m := |x| x * 2\n f := |x| x % 4 == 0\n s := |a, x| a + x\n xs.map(m).filter(f).fold(0, s)\n",
|
|
313
313
|
);
|
|
314
314
|
let buildOnly = withHead(
|
|
315
|
-
"fun
|
|
315
|
+
"fun buildDown(i: Int, out: List[Int]) -> List[Int] =\n if i > 0 then buildDown(i - 1, Cons(head: i, tail: out)) else out\n\nfun build(n: Int) -> List[Int] = buildDown(n, Nil)\n\nfun main() -> Int =\n xs := build(1000000)\n xs.length()\n",
|
|
316
316
|
);
|
|
317
317
|
let bb = compile("million_build", &buildOnly);
|
|
318
318
|
let t = std::time::Instant::now();
|
|
@@ -324,7 +324,7 @@ fn list_pipeline_million_timed() {
|
|
|
324
324
|
("fold only", "xs.fold(0, |a, x| a + x)"),
|
|
325
325
|
] {
|
|
326
326
|
let one = withHead(&format!(
|
|
327
|
-
"fun
|
|
327
|
+
"fun buildDown(i: Int, out: List[Int]) -> List[Int] =\n if i > 0 then buildDown(i - 1, Cons(head: i, tail: out)) else out\n\nfun build(n: Int) -> List[Int] = buildDown(n, Nil)\n\nfun main() -> Int =\n xs := build(1000000)\n {expr}\n"
|
|
328
328
|
));
|
|
329
329
|
let b = compile("million_stage", &one);
|
|
330
330
|
let t = std::time::Instant::now();
|
|
@@ -340,7 +340,7 @@ fn list_pipeline_million_timed() {
|
|
|
340
340
|
#[test]
|
|
341
341
|
fn copies_get_trmc_and_tail_marks() {
|
|
342
342
|
let src = withHead(
|
|
343
|
-
"fun main() -> Int =\n xs := [1, 2, 3]\n
|
|
343
|
+
"fun main() -> Int =\n xs := [1, 2, 3]\n xs.map(|x| x * 2).fold(0, |a, x| a + x)\n",
|
|
344
344
|
);
|
|
345
345
|
let l = lowered("trmc_copy", &src);
|
|
346
346
|
let text = |n: &str| {
|
|
@@ -367,7 +367,7 @@ fn deep_recursive_closure_callee() {
|
|
|
367
367
|
// levels of non-escaping params (`outer` -> `mid` -> `loop`) and is also used by
|
|
368
368
|
// `mid` itself.
|
|
369
369
|
let src = withHead(
|
|
370
|
-
"fun loop(n: Int, acc: Int, f: fn(Int) -> Int) -> Int =\n if n == 0 then acc else loop(n - 1, f(acc), f)\n\nfun mid(n: Int, f: fn(Int) -> Int) -> Int = loop(n, f(0), f)\n\nfun outer(n: Int, g: fn(Int) -> Int) -> Int = mid(n, g)\n\nfun main() -> Int =\n k := 2\n
|
|
370
|
+
"fun loop(n: Int, acc: Int, f: fn(Int) -> Int) -> Int =\n if n == 0 then acc else loop(n - 1, f(acc), f)\n\nfun mid(n: Int, f: fn(Int) -> Int) -> Int = loop(n, f(0), f)\n\nfun outer(n: Int, g: fn(Int) -> Int) -> Int = mid(n, g)\n\nfun main() -> Int =\n k := 2\n outer(100000, |x| x + 1) + outer(50000, |x| x + k)\n",
|
|
371
371
|
);
|
|
372
372
|
let names = copies("deep", &src);
|
|
373
373
|
for callee in ["loop", "mid", "outer"] {
|
|
@@ -377,7 +377,7 @@ fn deep_recursive_closure_callee() {
|
|
|
377
377
|
let bytes = compile("deep", &src);
|
|
378
378
|
// Same program through closures held in locals (not specialised).
|
|
379
379
|
let plain = withHead(
|
|
380
|
-
"fun loop(n: Int, acc: Int, f: fn(Int) -> Int) -> Int =\n if n == 0 then acc else loop(n - 1, f(acc), f)\n\nfun mid(n: Int, f: fn(Int) -> Int) -> Int = loop(n, f(0), f)\n\nfun outer(n: Int, g: fn(Int) -> Int) -> Int = mid(n, g)\n\nfun main() -> Int =\n k := 2\n a := |x| x + 1\n b := |x| x + k\n
|
|
380
|
+
"fun loop(n: Int, acc: Int, f: fn(Int) -> Int) -> Int =\n if n == 0 then acc else loop(n - 1, f(acc), f)\n\nfun mid(n: Int, f: fn(Int) -> Int) -> Int = loop(n, f(0), f)\n\nfun outer(n: Int, g: fn(Int) -> Int) -> Int = mid(n, g)\n\nfun main() -> Int =\n k := 2\n a := |x| x + 1\n b := |x| x + k\n outer(100000, a) + outer(50000, b)\n",
|
|
381
381
|
);
|
|
382
382
|
assert!(userCopies("deep_plain", &plain).is_empty());
|
|
383
383
|
let r = runIntBytes(&bytes);
|
|
@@ -389,11 +389,11 @@ fn deep_recursive_closure_callee() {
|
|
|
389
389
|
fn many_call_sites_capped() {
|
|
390
390
|
// 20 distinct closures to one callee: exactly MAX_COPIES (8) copies, the rest left as
|
|
391
391
|
// ordinary closure calls, every result right.
|
|
392
|
-
let mut body = String::from("fun apply(x: Int, f: fn(Int) -> Int) -> Int = f(x)\n\nfun main() -> Int =\n
|
|
392
|
+
let mut body = String::from("fun apply(x: Int, f: fn(Int) -> Int) -> Int = f(x)\n\nfun main() -> Int =\n");
|
|
393
393
|
for i in 0..20 {
|
|
394
|
-
body.push_str(&format!("
|
|
394
|
+
body.push_str(&format!(" r{1} := apply(5, |x| x * {0} + {1})\n", i + 1, i));
|
|
395
395
|
}
|
|
396
|
-
body.push_str("
|
|
396
|
+
body.push_str(" r0 + r1 + r2 + r3 + r4 + r5 + r6 + r7 + r8 + r9 + r10 + r11 + r12 + r13 + r14 + r15 + r16 + r17 + r18 + r19\n");
|
|
397
397
|
let src = withHead(&body);
|
|
398
398
|
let names = copies("many20", &src);
|
|
399
399
|
assert_eq!(copiesOf(&names, "apply"), 8, "{names:?}");
|
|
@@ -444,7 +444,7 @@ fn pattern_decl() {
|
|
|
444
444
|
// (here it shadows the outer `n`). Block-bodied closures are not inlined today, so this
|
|
445
445
|
// pins the program's result through whichever path the pass takes.
|
|
446
446
|
let src = withHead(
|
|
447
|
-
"fun main() -> Int =\n k := 10\n n := 1000\n xs := [0, 1, 2]\n
|
|
447
|
+
"fun main() -> Int =\n k := 10\n n := 1000\n xs := [0, 1, 2]\n xs.fold(0, |a, x|\n r := a\n match x\n 0 => r + 100\n n => r + n + k\n )\n",
|
|
448
448
|
);
|
|
449
449
|
assert_eq!(runInt("patdecl", &src), 100 + 11 + 12);
|
|
450
450
|
}
|
|
@@ -453,7 +453,7 @@ fn pattern_decl() {
|
|
|
453
453
|
fn identical_closures_share_a_copy() {
|
|
454
454
|
// Same closure text at two positions: one copy (the dedup key ignores positions).
|
|
455
455
|
let src = withHead(
|
|
456
|
-
"fun main() -> Int =\n xs := [1, 2, 3]\n a := xs.map(|x| x + 1).fold(0, |a, x| a + x)\n b := xs.map(|x| x + 1).fold(0, |a, x| a + x)\n
|
|
456
|
+
"fun main() -> Int =\n xs := [1, 2, 3]\n a := xs.map(|x| x + 1).fold(0, |a, x| a + x)\n b := xs.map(|x| x + 1).fold(0, |a, x| a + x)\n a + b\n",
|
|
457
457
|
);
|
|
458
458
|
let names = copies("dedup", &src);
|
|
459
459
|
assert!(names.contains(&"List.map$c0".to_string()), "{names:?}");
|
compiler/core/tests/core/drive_test.rs
CHANGED
|
@@ -138,7 +138,7 @@ fun n(xs: Bag) -> Int = mapB(xs).length()
|
|
|
138
138
|
#[test]
|
|
139
139
|
fn singleUseBindingFuses() {
|
|
140
140
|
let (s, _) = run(&format!(
|
|
141
|
-
"{MAP_SUM}fun total(xs: Lst) -> Int =\n p := mapL(xs)\n
|
|
141
|
+
"{MAP_SUM}fun total(xs: Lst) -> Int =\n p := mapL(xs)\n sumL(p)\n"
|
|
142
142
|
));
|
|
143
143
|
let t = dbg(getFn(&s, "total").unwrap());
|
|
144
144
|
assert!(t.contains("sumL$mapL$0") && !t.contains("name: \"mapL\""), "{t}");
|
|
@@ -146,7 +146,7 @@ fn singleUseBindingFuses() {
|
|
|
146
146
|
|
|
147
147
|
#[test]
|
|
148
148
|
fn sharedResultUnchanged() {
|
|
149
|
-
let src = format!("{MAP_SUM}fun total(xs: Lst) -> Int =\n p := mapL(xs)\n a := sumL(p)\n b := sumL(p)\n
|
|
149
|
+
let src = format!("{MAP_SUM}fun total(xs: Lst) -> Int =\n p := mapL(xs)\n a := sumL(p)\n b := sumL(p)\n a + b\n");
|
|
150
150
|
let before = parse(&format!("{PRE}{src}"));
|
|
151
151
|
let (s, notes) = run(&src);
|
|
152
152
|
assert!(notes.iter().any(|n| n.contains("shared result")), "{notes:?}");
|
|
@@ -410,7 +410,9 @@ fun frame(io: Io, x: Int) =
|
|
|
410
410
|
fn block_bodied_producer_not_fused_note() {
|
|
411
411
|
let (s, notes) = run(
|
|
412
412
|
"fun mapB(xs: Lst) -> Lst =
|
|
413
|
+
for i := range 1
|
|
414
|
+
_ := i
|
|
413
|
-
|
|
415
|
+
match xs
|
|
414
416
|
Nil => Nil
|
|
415
417
|
Cons(h, t) => Cons(head: h * 2, tail: mapB(t))
|
|
416
418
|
fun sumL(xs: Lst) -> Int = match xs
|
compiler/core/tests/core/formatter_test.rs
CHANGED
|
@@ -26,21 +26,19 @@ enum Cat =
|
|
|
26
26
|
|
|
27
27
|
fun classify(x: Int) -> Str =
|
|
28
28
|
if x < 0
|
|
29
|
-
|
|
29
|
+
\"neg\"
|
|
30
30
|
else if x == 0
|
|
31
|
-
|
|
31
|
+
\"zero\"
|
|
32
32
|
else
|
|
33
|
-
|
|
33
|
+
\"pos\"
|
|
34
34
|
|
|
35
|
-
fun loop() -> Int =
|
|
35
|
+
fun loop(c: Counter) -> Int =
|
|
36
|
-
i := 0
|
|
37
|
-
while i < 5
|
|
36
|
+
while c.i < 5
|
|
38
|
-
i = i + 1
|
|
37
|
+
c.i = c.i + 1
|
|
39
|
-
|
|
38
|
+
c.i
|
|
40
39
|
|
|
41
40
|
fun main() =
|
|
42
41
|
assert 1 == 1
|
|
43
|
-
return
|
|
44
42
|
";
|
|
45
43
|
let once = formatSource(input).expect("first pass");
|
|
46
44
|
assert!(once.contains("module test"), "got: {once}");
|
|
@@ -196,7 +194,7 @@ fn formatsBindCrashAndQuestion() {
|
|
|
196
194
|
|
|
197
195
|
#[test]
|
|
198
196
|
fn formatsBangAndQuestionPostfix() {
|
|
199
|
-
let input = "fun f(s: Str) -> Int =\n a := g(s)!+1\n d := g(s)?.x\n
|
|
197
|
+
let input = "fun f(s: Str) -> Int =\n a := g(s)!+1\n d := g(s)?.x\n a\n";
|
|
200
198
|
let out = formatSource(input).expect("format");
|
|
201
199
|
assert!(out.contains("g(s)! + 1"), "{out}");
|
|
202
200
|
assert!(out.contains("d := g(s)?.x"), "{out}");
|
|
@@ -206,7 +204,7 @@ fn formatsBangAndQuestionPostfix() {
|
|
|
206
204
|
#[test]
|
|
207
205
|
fn alreadyFormattedSourceIsUnchanged() {
|
|
208
206
|
for input in [
|
|
209
|
-
"fun main() =\n x = 1 + 2\n",
|
|
207
|
+
"fun main() =\n x := 1 + 2\n",
|
|
210
208
|
"struct Vec2(x: Float, y: Float) =\n Zero = Vec2(x: 0.0, y: 0.0)\n\n fun len(self) -> Float =\n self.x\n",
|
|
211
209
|
"struct Vec2(x: Float, y: Float)\n\nfun f() -> Int =\n 1\n",
|
|
212
210
|
"struct Pair[K, V](key: K, val: V)\n",
|
|
@@ -223,7 +221,7 @@ fn alreadyFormattedSourceIsUnchanged() {
|
|
|
223
221
|
fn spacesOperatorsAndSimpleFunctions() {
|
|
224
222
|
let r = formatSource("fun add(a: Int, b: Int) -> Int =\n a+b\n").expect("format");
|
|
225
223
|
assert!(r.contains("a + b"), "{r}");
|
|
226
|
-
let r = formatSource("fun main() =\n x = 1\n").expect("format");
|
|
224
|
+
let r = formatSource("fun main() =\n p.x = 1\n").expect("format");
|
|
227
225
|
assert!(r.contains("main()") && r.contains("x ="), "{r}");
|
|
228
226
|
}
|
|
229
227
|
|
|
@@ -348,7 +346,7 @@ fn assertFormatsUnchanged(src: &str) {
|
|
|
348
346
|
#[test]
|
|
349
347
|
fn format_blank_line_before_comment_in_enum_body() {
|
|
350
348
|
assertFormatsUnchanged(
|
|
351
|
-
"enum Lnk[T] =\n | Nl\n | Cn(head: T, tail: Lnk[T])\n\n fun size(self) -> Int = match self\n Nl => 0\n Cn(_, t) => 1 + t.size()\n\n # walks the list\n fun isNl(self) -> Bool =\n if Cn(_, _) := self\n
|
|
349
|
+
"enum Lnk[T] =\n | Nl\n | Cn(head: T, tail: Lnk[T])\n\n fun size(self) -> Int = match self\n Nl => 0\n Cn(_, t) => 1 + t.size()\n\n # walks the list\n fun isNl(self) -> Bool =\n if Cn(_, _) := self\n false\n else\n true\n",
|
|
352
350
|
);
|
|
353
351
|
}
|
|
354
352
|
|
compiler/core/tests/core/ids_spans_test.rs
CHANGED
|
@@ -36,7 +36,7 @@ fn allIds(src: &Source) -> Vec<NodeId> {
|
|
|
36
36
|
out
|
|
37
37
|
}
|
|
38
38
|
|
|
39
|
-
const PROGRAM: &str = "fun f(a: Int) -> Int =\n x := a + 1\n if x > 2\n
|
|
39
|
+
const PROGRAM: &str = "fun f(a: Int) -> Int =\n x := a + 1\n if x > 2\n x * 2\n else\n g(x, a - 1)\n\nfun g(a: Int, b: Int) -> Int = a + b\n";
|
|
40
40
|
|
|
41
41
|
#[test]
|
|
42
42
|
fn ids_unique_and_kept_by_clone() {
|
compiler/core/tests/core/parser_test.rs
CHANGED
|
@@ -80,7 +80,7 @@ enum Cat =
|
|
|
80
80
|
fn closureLiteralParsesWithParamsAndBody() {
|
|
81
81
|
let src = "\
|
|
82
82
|
fun useClosure() -> Bool =
|
|
83
|
-
cb = |v|
|
|
83
|
+
cb := |v|
|
|
84
84
|
true
|
|
85
85
|
cb(5)
|
|
86
86
|
";
|
|
@@ -200,7 +200,7 @@ fn test_double_question_expression_is_a_syntax_error() {
|
|
|
200
200
|
|
|
201
201
|
#[test]
|
|
202
202
|
fn if_and_while_bindings_parse() {
|
|
203
|
-
let src = parse("fun f(a: Option[Int]) -> Int =\n if x := a\n
|
|
203
|
+
let src = parse("fun f(a: Option[Int]) -> Int =\n if x := a\n x\n while y := a\n y\n if z := a then z else 0\n");
|
|
204
204
|
let f = onlyFn(&src);
|
|
205
205
|
let FnBody::Block(b) = &f.body else { panic!() };
|
|
206
206
|
match &b.stmts[0] {
|
|
@@ -283,7 +283,7 @@ fn firstExprDebug(src: &str) -> String {
|
|
|
283
283
|
|
|
284
284
|
#[test]
|
|
285
285
|
fn closureUnderscoreParam() {
|
|
286
|
-
let d = firstExprDebug("fun f() =\n g = |_| 1\n");
|
|
286
|
+
let d = firstExprDebug("fun f() =\n g := |_| 1\n");
|
|
287
287
|
assert!(d.contains("\"_0\""), "{d}");
|
|
288
288
|
}
|
|
289
289
|
|
|
@@ -390,7 +390,7 @@ fn bang_vs_neq_lexing() {
|
|
|
390
390
|
|
|
391
391
|
#[test]
|
|
392
392
|
fn pattern_if_conditions_parse() {
|
|
393
|
-
let src = parse("enum Sh =\n | Circle(r: Int)\n | Rect(w: Int, h: Int)\n\nfun f(s: Sh) -> Int =\n if Circle(r) := s\n
|
|
393
|
+
let src = parse("enum Sh =\n | Circle(r: Int)\n | Rect(w: Int, h: Int)\n\nfun f(s: Sh) -> Int =\n if Circle(r) := s\n r\n else if Rect(w, _) := s, w > 1, w < 9\n w\n else\n 0\n");
|
|
394
394
|
let f = src
|
|
395
395
|
.items
|
|
396
396
|
.iter()
|
compiler/core/tests/core/purity_test.rs
CHANGED
|
@@ -31,7 +31,7 @@ fn param_field_write_impure_fresh_local_pure() {
|
|
|
31
31
|
|
|
32
32
|
#[test]
|
|
33
33
|
fn aliased_local_write_impure() {
|
|
34
|
-
let s = format!("{P}fun g(p: Pt) -> Pt =\n q :=
|
|
34
|
+
let s = format!("{P}fun g(p: Pt) -> Pt =\n q := p\n q.x = 1\n q\n");
|
|
35
35
|
assert!(pure(&s).is_empty());
|
|
36
36
|
}
|
|
37
37
|
|
compiler/core/tests/core/rename_test.rs
CHANGED
|
@@ -38,7 +38,7 @@ fn events(src: &str) -> Vec<(String, &'static str)> {
|
|
|
38
38
|
|
|
39
39
|
#[test]
|
|
40
40
|
fn renamesDeclThenRef() {
|
|
41
|
-
let src = "fun t() -> Int =\n x := 1\n y := x\n
|
|
41
|
+
let src = "fun t() -> Int =\n x := 1\n y := x\n y\n";
|
|
42
42
|
let mut b = body(src);
|
|
43
43
|
renameStmts(&mut b.stmts, &mut |n, _| {
|
|
44
44
|
if n == "x" {
|
|
@@ -65,7 +65,7 @@ fn patternNames() {
|
|
|
65
65
|
|
|
66
66
|
#[test]
|
|
67
67
|
fn rename_shadowing() {
|
|
68
|
-
let ev = events("fun t(x: Int) -> Int =\n f := |x| x + 1\n
|
|
68
|
+
let ev = events("fun t(x: Int) -> Int =\n f := |x| x + 1\n f(x)\n");
|
|
69
69
|
let decl = ev.iter().position(|e| *e == ("x".into(), "decl")).unwrap();
|
|
70
70
|
let inner = ev.iter().position(|e| *e == ("x".into(), "ref")).unwrap();
|
|
71
71
|
assert!(decl < inner, "{ev:?}");
|
|
@@ -73,7 +73,7 @@ fn rename_shadowing() {
|
|
|
73
73
|
|
|
74
74
|
#[test]
|
|
75
75
|
fn field_assign_target() {
|
|
76
|
-
let ev = events("fun t(p: Int) -> Int =\n self.x = 5\n
|
|
76
|
+
let ev = events("fun t(p: Int) -> Int =\n self.x = 5\n 0\n");
|
|
77
77
|
assert!(!ev.iter().any(|(n, _)| n == "x"), "{ev:?}");
|
|
78
78
|
}
|
|
79
79
|
|
compiler/core/tests/core/simplify_test.rs
CHANGED
|
@@ -201,7 +201,7 @@ fn closureParamShadowingParamNotSubstituted() {
|
|
|
201
201
|
#[test]
|
|
202
202
|
fn outer_read_then_nested_shadow_kept() {
|
|
203
203
|
let b = simp(
|
|
204
|
-
"fun f(x: Int) -> Int = match Cons(head: 1, tail: Nil)\n Cons(h, t) =>\n y := x + h\n if y > 0\n x := 5\n
|
|
204
|
+
"fun f(x: Int) -> Int = match Cons(head: 1, tail: Nil)\n Cons(h, t) =>\n y := x + h\n if y > 0\n x := 5\n x\n else\n y\n Nil => 0\n",
|
|
205
205
|
"f",
|
|
206
206
|
&[],
|
|
207
207
|
);
|
compiler/core/tests/core/tail_test.rs
CHANGED
|
@@ -39,7 +39,7 @@ fn self_tail_in_match_arm() {
|
|
|
39
39
|
|
|
40
40
|
#[test]
|
|
41
41
|
fn self_tail_in_if_else() {
|
|
42
|
-
let src = "fun f(n: Int) -> Int =\n if n == 0\n
|
|
42
|
+
let src = "fun f(n: Int) -> Int =\n if n == 0\n 0\n else\n f(n - 1)\n";
|
|
43
43
|
assert_eq!(sites(src, "f"), one("f", TailKind::SelfCall));
|
|
44
44
|
}
|
|
45
45
|
|
compiler/core/tests/core/trmc_test.rs
CHANGED
|
@@ -108,41 +108,23 @@ fun filter[T](xs: Link[T], keep: fn(T) -> Bool) -> Link[T] = match xs
|
|
|
108
108
|
rest := filter(t, keep)
|
|
109
109
|
if keep(h) then Cons(head: h, tail: rest) else rest
|
|
110
110
|
|
|
111
|
+
fun sumFrom(cur: Link[Int], total: Int) -> Int = match cur
|
|
112
|
+
Nil => total
|
|
113
|
+
Cons(h, t) => sumFrom(t, total + h)
|
|
114
|
+
|
|
111
|
-
fun sum(xs: Link[Int]) -> Int =
|
|
115
|
+
fun sum(xs: Link[Int]) -> Int = sumFrom(xs, 0)
|
|
112
|
-
|
|
116
|
+
|
|
113
|
-
total := 0
|
|
114
|
-
while true
|
|
115
|
-
|
|
117
|
+
fun countFrom[T](cur: Link[T], n: Int) -> Int = match cur
|
|
116
|
-
|
|
118
|
+
Nil => n
|
|
117
|
-
break
|
|
118
|
-
|
|
119
|
+
Cons(_, t) => countFrom(t, n + 1)
|
|
119
|
-
|
|
120
|
+
|
|
120
|
-
cur = t
|
|
121
|
-
return total
|
|
122
|
-
|
|
123
|
-
fun count[T](xs: Link[T]) -> Int =
|
|
121
|
+
fun count[T](xs: Link[T]) -> Int = countFrom(xs, 0)
|
|
124
|
-
|
|
122
|
+
|
|
125
|
-
n := 0
|
|
126
|
-
while true
|
|
127
|
-
|
|
123
|
+
fun toIntFrom(cur: Link[Int], r: Int) -> Int = match cur
|
|
128
|
-
|
|
124
|
+
Nil => r
|
|
129
|
-
break
|
|
130
|
-
|
|
125
|
+
Cons(h, t) => toIntFrom(t, r * 10 + h)
|
|
131
|
-
|
|
126
|
+
|
|
132
|
-
cur = t
|
|
133
|
-
return n
|
|
134
|
-
|
|
135
|
-
fun toInt(xs: Link[Int]) -> Int =
|
|
127
|
+
fun toInt(xs: Link[Int]) -> Int = toIntFrom(xs, 0)
|
|
136
|
-
cur := xs
|
|
137
|
-
r := 0
|
|
138
|
-
while true
|
|
139
|
-
match cur
|
|
140
|
-
Nil =>
|
|
141
|
-
break
|
|
142
|
-
Cons(h, t) =>
|
|
143
|
-
r = r * 10 + h
|
|
144
|
-
cur = t
|
|
145
|
-
return r
|
|
146
128
|
|
|
147
129
|
";
|
|
148
130
|
|
|
@@ -252,7 +234,7 @@ struct Log(mut n: Int)
|
|
|
252
234
|
|
|
253
235
|
fun note(l: Log, x: Int) -> Int =
|
|
254
236
|
l.n = l.n * 10 + x
|
|
255
|
-
|
|
237
|
+
x
|
|
256
238
|
|
|
257
239
|
fun walk(xs: Link[Int], log: Log, tag: Int) -> Link[Int] = match xs
|
|
258
240
|
Nil => Nil
|
|
@@ -312,7 +294,7 @@ fn structNewCount(wasm: &[u8], name: &str) -> usize {
|
|
|
312
294
|
|
|
313
295
|
#[test]
|
|
314
296
|
fn map_single_pass_one_alloc_per_cell() {
|
|
315
|
-
let p = prog("fun main() -> Int =
|
|
297
|
+
let p = prog("fun main() -> Int =\n ys := map(fromTo(0, 3), |x| x + 1)\n count(ys) + count(ys)\n");
|
|
316
298
|
let bytes = compileSource(&load("single", &p)).unwrap();
|
|
317
299
|
// One struct.new per cell (the member is the enum subtype), in one loop.
|
|
318
300
|
assert_eq!(structNewCount(&bytes, "map"), 1);
|
|
@@ -420,12 +402,11 @@ fun main() -> Int =
|
|
|
420
402
|
import std/List
|
|
421
403
|
import std/Int
|
|
422
404
|
|
|
405
|
+
fun buildDown(i: Int, out: List[Int]) -> List[Int] =
|
|
406
|
+
if i > 0 then buildDown(i - 1, Cons(head: i, tail: out)) else out
|
|
407
|
+
|
|
423
408
|
fun main() -> Int =
|
|
424
|
-
xs: List[Int] := Nil
|
|
425
|
-
i := 1000000
|
|
426
|
-
while i > 0
|
|
427
|
-
|
|
409
|
+
xs := buildDown(1000000, Nil)
|
|
428
|
-
i = i - 1
|
|
429
410
|
evens := xs.filter(|x| x % 2 == 0)
|
|
430
411
|
small := [1, 2, 3, 4, 5, 6].filter(|x| x > 3)
|
|
431
412
|
none: List[Int] := Nil
|
|
@@ -622,11 +603,7 @@ fn mentions_field_not_var() {
|
|
|
622
603
|
"\
|
|
623
604
|
struct Wrap(build: Int)
|
|
624
605
|
|
|
625
|
-
fun pick(w: Wrap) -> Int =
|
|
606
|
+
fun pick(w: Wrap) -> Int = w.build
|
|
626
|
-
r := 0
|
|
627
|
-
while r < w.build
|
|
628
|
-
r = r + 1
|
|
629
|
-
r
|
|
630
607
|
|
|
631
608
|
fun build(a: Int, b: Int) -> Link[Int] =
|
|
632
609
|
if a >= b then Nil else Cons(head: pick(Wrap(build: a)), tail: build(a + 1, b))
|
compiler/core/tests/core/uniqueness_test.rs
CHANGED
|
@@ -72,7 +72,7 @@ fn step_called_only_with_unique_args_is_unique() {
|
|
|
72
72
|
let s = format!(
|
|
73
73
|
"{PRE}fun step(s: Pt) -> Pt = Pt(..s, x: s.x + 1)\n\
|
|
74
74
|
fun tick(w: World) -> World =\n w.s = step(w.s)\n w\n\
|
|
75
|
-
fun go() -> Pt =
|
|
75
|
+
fun go() -> Pt = step(step(Pt(x: 0, y: 0)))\n\
|
|
76
76
|
fun mk() -> World = World(s: Pt(x: 0, y: 0), n: 0)\n"
|
|
77
77
|
);
|
|
78
78
|
let (src, r) = run(&s);
|
|
@@ -104,7 +104,7 @@ fn stored_in_slice_not_unique() {
|
|
|
104
104
|
fn captured_by_closure_not_unique() {
|
|
105
105
|
let s = format!(
|
|
106
106
|
"{PRE}fun step(s: Pt) -> Pt = Pt(..s, x: 1)\n\
|
|
107
|
-
fun go() -> Pt =\n t := Pt(x: 0, y: 0)\n f := || t.x\n
|
|
107
|
+
fun go() -> Pt =\n t := Pt(x: 0, y: 0)\n f := || t.x\n step(t)\n"
|
|
108
108
|
);
|
|
109
109
|
let (_, r) = run(&s);
|
|
110
110
|
assert!(!proved(&r, "step:3"), "{r:?}");
|
|
@@ -159,7 +159,7 @@ fn self_spread_in_method_unique() {
|
|
|
159
159
|
let s = "struct Pt(x: Int, y: Int) =\n fun bump(self) -> Pt = Pt(..self, x: self.x + 1)\n\
|
|
160
160
|
struct World(p: Pt, n: Int)\n\
|
|
161
161
|
fun tick(w: World) -> World =\n w.p = w.p.bump()\n w\n\
|
|
162
|
-
fun go() -> Pt =
|
|
162
|
+
fun go() -> Pt = Pt(x: 0, y: 0).bump().bump()\n\
|
|
163
163
|
fun mk() -> World = World(p: Pt(x: 0, y: 0), n: 0)\n";
|
|
164
164
|
let (src, r) = run(s);
|
|
165
165
|
assert!(proved(&r, "Pt.bump:2"), "{r:?}");
|
|
@@ -172,7 +172,7 @@ fn trait_dispatch_param_unique() {
|
|
|
172
172
|
let s = "struct Cat(x: Int) =\n fun shift(self, d: Int) -> Cat = Cat(..self, x: self.x + d)\n\
|
|
173
173
|
struct Dog(x: Int) =\n fun shift(self, d: Int) -> Dog = Dog(..self, x: self.x + d)\n\
|
|
174
174
|
fun go() -> Cat =\n a := Cat(x: 0)\n a.shift(1)\n\
|
|
175
|
-
fun go2() -> Dog =\n b := Dog(x: 0)\n b
|
|
175
|
+
fun go2() -> Dog =\n b := Dog(x: 0)\n b.shift(2)\n";
|
|
176
176
|
let (_, r) = run(s);
|
|
177
177
|
assert!(proved(&r, "Cat.shift:2"), "{r:?}");
|
|
178
178
|
assert!(proved(&r, "Dog.shift:4"), "{r:?}");
|
|
@@ -352,7 +352,7 @@ fn a_method_storing_self_is_not_proved_in_place() {
|
|
|
352
352
|
fn stored_in_two_fields_not_unique() {
|
|
353
353
|
let s = format!(
|
|
354
354
|
"{CELL}struct Two(a: Cell, b: Cell)\n\
|
|
355
|
-
fun go() -> Two =\n v := Cell(label: \"a\", n: 0)\n t := Two(a: v, b: v)\n
|
|
355
|
+
fun go() -> Two =\n v := Cell(label: \"a\", n: 0)\n t := Two(a: v, b: v)\n w := bump(v)\n t\n"
|
|
356
356
|
);
|
|
357
357
|
let (_, r) = run(&s);
|
|
358
358
|
assert!(!provedFn(&r, "bump"), "{r:?}");
|
|
@@ -370,7 +370,7 @@ fn returned_with_another_reference_not_unique() {
|
|
|
370
370
|
#[test]
|
|
371
371
|
fn update_in_one_branch_with_old_value_read_after_not_unique() {
|
|
372
372
|
let s = format!(
|
|
373
|
-
"{CELL}fun go(flag: Bool) -> Int =\n c := Cell(label: \"a\", n: 0)\n d := c\n if flag\n
|
|
373
|
+
"{CELL}fun go(flag: Bool) -> Int =\n c := Cell(label: \"a\", n: 0)\n d := c\n e := if flag\n bump(d)\n else\n d\n c.n + e.n\n"
|
|
374
374
|
);
|
|
375
375
|
let (_, r) = run(&s);
|
|
376
376
|
assert!(!provedFn(&r, "bump"), "{r:?}");
|
|
@@ -449,7 +449,7 @@ fn a_closure_parameter_named_like_a_fresh_fn_is_not_that_fn() {
|
|
|
449
449
|
// `mk` in `go` is the parameter, not the free fn `mk`: its result may be shared.
|
|
450
450
|
let s = format!(
|
|
451
451
|
"{CELL}fun mk() -> Cell = Cell(label: \"m\", n: 0)\n\
|
|
452
|
-
fun go(mk: fn() -> Cell) -> Cell =\n y := mk()\n
|
|
452
|
+
fun go(mk: fn() -> Cell) -> Cell =\n y := mk()\n bump(y)\n"
|
|
453
453
|
);
|
|
454
454
|
let (_, r) = run(&s);
|
|
455
455
|
assert!(!provedFn(&r, "bump"), "{r:?}");
|
|
@@ -461,7 +461,7 @@ fn a_closure_field_named_like_a_fresh_method_is_not_that_method() {
|
|
|
461
461
|
let s = format!(
|
|
462
462
|
"{CELL}struct Maker(k: Int) =\n fun made(self) -> Cell = Cell(label: \"m\", n: 0)\n\
|
|
463
463
|
struct Hook(made: fn() -> Cell)\n\
|
|
464
|
-
fun go(h: Hook) -> Cell =\n y := h.made()\n
|
|
464
|
+
fun go(h: Hook) -> Cell =\n y := h.made()\n bump(y)\n"
|
|
465
465
|
);
|
|
466
466
|
let (_, r) = run(&s);
|
|
467
467
|
assert!(!provedFn(&r, "bump"), "{r:?}");
|
compiler/core/tests/core/visit_test.rs
CHANGED
|
@@ -65,7 +65,7 @@ fn patternBindersIgnoresUnderscore() {
|
|
|
65
65
|
|
|
66
66
|
#[test]
|
|
67
67
|
fn statementWalkers() {
|
|
68
|
-
let src = "fun t(x: Int) -> Int =\n if x > 1\n
|
|
68
|
+
let src = "fun t(x: Int) -> Int =\n if x > 1\n 1\n else\n 2\n";
|
|
69
69
|
let FnBody::Block(mut b) = fnBody(src) else { panic!() };
|
|
70
70
|
let s = &mut b.stmts[0];
|
|
71
71
|
assert_eq!(stmtExprs(s).len(), 1);
|
|
@@ -77,6 +77,6 @@ fn statementWalkers() {
|
|
|
77
77
|
|
|
78
78
|
#[test]
|
|
79
79
|
fn countVarSeesClosures() {
|
|
80
|
-
let src = "fun t(x: Int) -> Int =\n f := |y| y + x\n
|
|
80
|
+
let src = "fun t(x: Int) -> Int =\n f := |y| y + x\n x + f(x)\n";
|
|
81
81
|
assert_eq!(countVar(&fnBody(src), "x"), 3);
|
|
82
82
|
}
|
compiler/game/web-test/fixtures/stress_state_view.plum
CHANGED
|
@@ -115,12 +115,10 @@ fun newState(sheet: Texture, white: Texture) -> State =
|
|
|
115
115
|
# --- step: pure ---
|
|
116
116
|
|
|
117
117
|
fun stepDrifter(d: Drifter) -> Drifter =
|
|
118
|
-
|
|
118
|
+
movedX := d.x + d.dx
|
|
119
|
-
|
|
119
|
+
movedY := d.y + d.dy
|
|
120
|
-
if
|
|
120
|
+
nx := if movedX > Float(WorldW) then movedX - Float(WorldW) else movedX
|
|
121
|
-
nx = nx - Float(WorldW)
|
|
122
|
-
if
|
|
121
|
+
ny := if movedY > Float(WorldH) then movedY - Float(WorldH) else movedY
|
|
123
|
-
ny = ny - Float(WorldH)
|
|
124
122
|
Drifter(id: d.id, x: nx, y: ny, w: d.w, h: d.h, dx: d.dx, dy: d.dy, color: d.color)
|
|
125
123
|
|
|
126
124
|
fun stepDancer(d: Dancer, dt: Float) -> Dancer =
|
|
@@ -146,13 +144,14 @@ fun countClicks(es: List[UiEvent]) -> Int = match es
|
|
|
146
144
|
fun stepUi(s: State, input: InputEvent) -> State =
|
|
147
145
|
po := input.position()
|
|
148
146
|
if po.isNone()
|
|
147
|
+
s
|
|
149
|
-
|
|
148
|
+
else
|
|
150
|
-
|
|
149
|
+
pos := po.unwrap()
|
|
151
|
-
|
|
150
|
+
hit := buttonsView(s.ui).hitId(pos.x, pos.y)
|
|
152
|
-
|
|
151
|
+
r := ui.step(s.ui, input, hit)
|
|
153
|
-
|
|
152
|
+
State(
|
|
154
|
-
|
|
153
|
+
sheet: s.sheet, white: s.white, spinners: s.spinners, drifters: s.drifters, counters: s.counters,
|
|
155
|
-
|
|
154
|
+
pulsers: s.pulsers, anim: s.anim, clock: s.clock, ui: r.ui, clicks: s.clicks + countClicks(r.events))
|
|
156
155
|
|
|
157
156
|
# --- view: pure, the whole tree every frame ---
|
|
158
157
|
|
|
@@ -208,9 +207,7 @@ struct World(state: State, input: Input) =
|
|
|
208
207
|
fun update(self, io: Io, dt: Float) -> World =
|
|
209
208
|
input := self.input.poll(io)
|
|
210
209
|
inputs := input.events
|
|
211
|
-
s := step(self.state, dt)
|
|
212
|
-
for i := range inputs.length()
|
|
213
|
-
|
|
210
|
+
s := 0.until(inputs.length()).fold(step(self.state, dt), |current, i| stepUi(current, inputs.at(i)))
|
|
214
211
|
World(state: s, input: input)
|
|
215
212
|
|
|
216
213
|
fun draw(self, io: Io) =
|
|
@@ -221,9 +218,7 @@ fun main(io: Io) -> World =
|
|
|
221
218
|
World(state: newState(gfx.loadTexture(io, "sprites.png"), whiteTexture(io)), input: Input.create())
|
|
222
219
|
|
|
223
220
|
test "ten frames of step run"
|
|
224
|
-
s := newState(Texture(id: 0), Texture(id: 0))
|
|
221
|
+
s := 0.until(10).fold(newState(Texture(id: 0), Texture(id: 0)), |current, _| step(current, 0.016))
|
|
225
|
-
for i := range 10
|
|
226
|
-
s = step(s, 0.016)
|
|
227
222
|
assert nthOr(s.counters, 3, Counter(id: 0, x: 0.0, y: 0.0, n: 0)).n == 13
|
|
228
223
|
assert {nthOr(s.drifters, 0, newDrifter(0, Color.White, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)).x - 20.0}.abs() < 0.000001
|
|
229
224
|
first := nthOr(s.spinners, 0, newSpinner(0))
|
compiler/game/web-test/fixtures/stress_state_view_deep.plum
CHANGED
|
@@ -97,12 +97,10 @@ fun newState(sheet: Texture, white: Texture) -> State =
|
|
|
97
97
|
# --- step: pure ---
|
|
98
98
|
|
|
99
99
|
fun stepDrifter(d: Drifter) -> Drifter =
|
|
100
|
-
|
|
100
|
+
movedX := d.x + d.dx
|
|
101
|
-
|
|
101
|
+
movedY := d.y + d.dy
|
|
102
|
-
if
|
|
102
|
+
nx := if movedX > Float(WorldW) then movedX - Float(WorldW) else movedX
|
|
103
|
-
nx = nx - Float(WorldW)
|
|
104
|
-
if
|
|
103
|
+
ny := if movedY > Float(WorldH) then movedY - Float(WorldH) else movedY
|
|
105
|
-
ny = ny - Float(WorldH)
|
|
106
104
|
Drifter(id: d.id, x: nx, y: ny, w: d.w, h: d.h, dx: d.dx, dy: d.dy, color: d.color)
|
|
107
105
|
|
|
108
106
|
fun stepDancer(d: Dancer, dt: Float) -> Dancer =
|
|
@@ -180,9 +178,7 @@ fun main(io: Io) -> World =
|
|
|
180
178
|
World(state: newState(gfx.loadTexture(io, "sprites.png"), whiteTexture(io)))
|
|
181
179
|
|
|
182
180
|
test "ten frames of step run"
|
|
183
|
-
s := newState(Texture(id: 0), Texture(id: 0))
|
|
181
|
+
s := 0.until(10).fold(newState(Texture(id: 0), Texture(id: 0)), |current, _| step(current, 0.016))
|
|
184
|
-
for i := range 10
|
|
185
|
-
s = step(s, 0.016)
|
|
186
182
|
assert nthOr(s.counters, 3, Counter(id: 0, x: 0.0, y: 0.0, n: 0)).n == 13
|
|
187
183
|
assert {nthOr(s.drifters, 0, newDrifter(0, Color.White, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0)).x - 20.0}.abs() < 0.000001
|
|
188
184
|
assert {nthOr(s.dancers, 0, newDancer(0, 0)).rotation - 0.192}.abs() < 0.000001
|
compiler/wasm-codegen/tests/wasm_codegen/codegen_tests.rs
CHANGED
|
@@ -333,7 +333,7 @@ fn tailIfWithoutElseIsAClearError() {
|
|
|
333
333
|
let src = "\
|
|
334
334
|
fun bad(n: Int) -> Int =
|
|
335
335
|
if n < 0
|
|
336
|
-
|
|
336
|
+
1
|
|
337
337
|
";
|
|
338
338
|
let source = parse(src);
|
|
339
339
|
let err = compileSource(&source)
|
|
@@ -1030,7 +1030,7 @@ import std/Result
|
|
|
1030
1030
|
enum NopeError = Nope | Other
|
|
1031
1031
|
|
|
1032
1032
|
fun make() -> Result[Int, NopeError] =
|
|
1033
|
-
|
|
1033
|
+
Err(Nope)
|
|
1034
1034
|
|
|
1035
1035
|
fun f() -> Result[Str, NopeError] =
|
|
1036
1036
|
s := \"v={make()?}\"
|
|
@@ -1038,7 +1038,7 @@ fun f() -> Result[Str, NopeError] =
|
|
|
1038
1038
|
|
|
1039
1039
|
fun main() -> Int =
|
|
1040
1040
|
r := f()
|
|
1041
|
-
|
|
1041
|
+
1
|
|
1042
1042
|
";
|
|
1043
1043
|
assert_eq!(runMerged::<i64>("question_interp", src), 1);
|
|
1044
1044
|
}
|
|
@@ -1060,7 +1060,7 @@ struct B(m: Int) =
|
|
|
1060
1060
|
fun main() -> Int =
|
|
1061
1061
|
a := A(n: 4)
|
|
1062
1062
|
s := \"v={a.c().b()}\"
|
|
1063
|
-
|
|
1063
|
+
s.length()
|
|
1064
1064
|
";
|
|
1065
1065
|
assert_eq!(runMerged::<i64>("interp_chain", src), 4);
|
|
1066
1066
|
}
|
|
@@ -1123,11 +1123,13 @@ fun mapList[T, U](xs: Slice[T], cb: fn(T) -> U) -> Slice[U] =
|
|
|
1123
1123
|
result.push(cb(x))
|
|
1124
1124
|
result
|
|
1125
1125
|
|
|
1126
|
+
struct Reduced[U](mut value: U)
|
|
1127
|
+
|
|
1126
1128
|
fun reduceList[T, U](xs: Slice[T], acc: U, cb: fn(U, T) -> U) -> U =
|
|
1127
|
-
result := acc
|
|
1129
|
+
result := Reduced(value: acc)
|
|
1128
1130
|
for x := range xs
|
|
1129
|
-
result = cb(result, x)
|
|
1131
|
+
result.value = cb(result.value, x)
|
|
1130
|
-
result
|
|
1132
|
+
result.value
|
|
1131
1133
|
|
|
1132
1134
|
fun retainList[T](xs: Slice[T], keep: fn(T) -> Bool) -> Slice[T] =
|
|
1133
1135
|
result := Slice[T]()
|
|
@@ -1405,8 +1407,9 @@ fun main() -> Int =
|
|
|
1405
1407
|
b := l.get(1).unwrapOr(true)
|
|
1406
1408
|
c := l.get(2).unwrapOr(false)
|
|
1407
1409
|
if a && !b && c && l.length() == 3
|
|
1410
|
+
1
|
|
1408
|
-
|
|
1411
|
+
else
|
|
1409
|
-
|
|
1412
|
+
0
|
|
1410
1413
|
";
|
|
1411
1414
|
assert_eq!(runMerged::<i64>("bool_list", src), 1);
|
|
1412
1415
|
}
|
|
@@ -1427,8 +1430,9 @@ fun same[T](a: T, b: T) -> Bool =
|
|
|
1427
1430
|
|
|
1428
1431
|
fun main() -> Int =
|
|
1429
1432
|
if show(true) == \"true\" && show(false) == \"false\" && same(true, true) && !same(true, false)
|
|
1433
|
+
1
|
|
1430
|
-
|
|
1434
|
+
else
|
|
1431
|
-
|
|
1435
|
+
0
|
|
1432
1436
|
";
|
|
1433
1437
|
assert_eq!(runMerged::<i64>("bool_boxed", src), 1);
|
|
1434
1438
|
}
|
|
@@ -1448,7 +1452,7 @@ fun yes(ok: Bool) -> Result[Bool, NopeError] =
|
|
|
1448
1452
|
if ok
|
|
1449
1453
|
Ok(true)
|
|
1450
1454
|
else
|
|
1451
|
-
|
|
1455
|
+
Err(Nope)
|
|
1452
1456
|
|
|
1453
1457
|
fun chain(ok: Bool) -> Result[Bool, NopeError] =
|
|
1454
1458
|
v := yes(ok)?
|
|
@@ -1459,8 +1463,9 @@ fun main() -> Int =
|
|
|
1459
1463
|
b := chain(false).unwrapOr(true)
|
|
1460
1464
|
p := Bool.parse(\"true\").unwrapOr(false)
|
|
1461
1465
|
if !a && b && p
|
|
1466
|
+
1
|
|
1462
|
-
|
|
1467
|
+
else
|
|
1463
|
-
|
|
1468
|
+
0
|
|
1464
1469
|
";
|
|
1465
1470
|
assert_eq!(runMerged::<i64>("bool_try", src), 1);
|
|
1466
1471
|
}
|
|
@@ -1550,8 +1555,9 @@ import std/Str
|
|
|
1550
1555
|
fun main() -> Int =
|
|
1551
1556
|
t := true
|
|
1552
1557
|
if t.toStr() == \"true\"
|
|
1558
|
+
1
|
|
1553
|
-
|
|
1559
|
+
else
|
|
1554
|
-
|
|
1560
|
+
0
|
|
1555
1561
|
";
|
|
1556
1562
|
let dir = std::env::temp_dir().join(format!("plum_t3_bool_noimport_{}", std::process::id()));
|
|
1557
1563
|
std::fs::create_dir_all(&dir).unwrap();
|
|
@@ -1572,7 +1578,7 @@ fn int_tostr_without_import_is_rejected() {
|
|
|
1572
1578
|
let src = "\
|
|
1573
1579
|
fun main() -> Str =
|
|
1574
1580
|
n := 5
|
|
1575
|
-
|
|
1581
|
+
n.toStr()
|
|
1576
1582
|
";
|
|
1577
1583
|
let source = parse(src);
|
|
1578
1584
|
assert!(
|
|
@@ -1644,8 +1650,9 @@ fun main() -> Int =
|
|
|
1644
1650
|
intNum := Num(add: |a, b| a + b, zero: 0)
|
|
1645
1651
|
floatNum := Num(add: |a, b| a + b, zero: 0.0)
|
|
1646
1652
|
if sumWith([1, 2, 3].toSlice(), intNum) == 6 && sumWith([1.5, 2.5].toSlice(), floatNum) == 4.0
|
|
1653
|
+
1
|
|
1647
|
-
|
|
1654
|
+
else
|
|
1648
|
-
|
|
1655
|
+
0
|
|
1649
1656
|
",
|
|
1650
1657
|
STD_HEAD, LIST_FNS
|
|
1651
1658
|
);
|
|
@@ -1692,7 +1699,7 @@ fn funcTypes(bytes: &[u8]) -> Vec<(Vec<wasmparser::ValType>, Vec<wasmparser::Val
|
|
|
1692
1699
|
fn value_multi_value_return() {
|
|
1693
1700
|
use wasmparser::ValType::F64;
|
|
1694
1701
|
let src = format!(
|
|
1695
|
-
"{VEC2}fun make(x: Float) -> Vec2 =\n Vec2(x: x, y: x * 2.0)\n\nfun main() -> Int =\n v := make(3.0)\n if v.y == 6.0\n
|
|
1702
|
+
"{VEC2}fun make(x: Float) -> Vec2 =\n Vec2(x: x, y: x * 2.0)\n\nfun main() -> Int =\n v := make(3.0)\n if v.y == 6.0\n 1\n else\n 0\n"
|
|
1696
1703
|
);
|
|
1697
1704
|
let bytes = assertValid(&src);
|
|
1698
1705
|
assert!(
|
|
@@ -1708,7 +1715,7 @@ fn value_multi_value_return() {
|
|
|
1708
1715
|
fn value_nested_flatten() {
|
|
1709
1716
|
use wasmparser::ValType::F64;
|
|
1710
1717
|
let src = format!(
|
|
1711
|
-
"{VEC2}struct Rect(pos: Vec2, size: Vec2)\n\nfun area(r: Rect) -> Float =\n r.size.x * r.size.y\n\nfun main() -> Int =\n r := Rect(pos: Vec2(x: 1.0, y: 2.0), size: Vec2(x: 3.0, y: 4.0))\n s := r\n s.pos.x = 5.0\n if area(r) == 12.0 && r.pos.x == 1.0 && s.pos.x == 5.0\n
|
|
1718
|
+
"{VEC2}struct Rect(pos: Vec2, size: Vec2)\n\nfun area(r: Rect) -> Float =\n r.size.x * r.size.y\n\nfun main() -> Int =\n r := Rect(pos: Vec2(x: 1.0, y: 2.0), size: Vec2(x: 3.0, y: 4.0))\n s := r\n s.pos.x = 5.0\n if area(r) == 12.0 && r.pos.x == 1.0 && s.pos.x == 5.0\n 1\n else\n 0\n"
|
|
1712
1719
|
);
|
|
1713
1720
|
let bytes = assertValid(&src);
|
|
1714
1721
|
assert!(
|
|
@@ -1723,7 +1730,7 @@ fn value_nested_flatten() {
|
|
|
1723
1730
|
#[test]
|
|
1724
1731
|
fn extern_value_multi_value() {
|
|
1725
1732
|
let src = format!(
|
|
1726
|
-
"{VEC2}struct Color(r: Int, g: Int)\n\nprimitive Io =\n extern fun mousePos(self) -> Vec2\n extern fun tint(self) -> Color\n extern fun move(self, p: Vec2, c: Color)\n\nfun main(io: Io) -> Int =\n p := io.mousePos()\n c := io.tint()\n io.move(p, c)\n if p.x == 1.5 && c.g == 0 - 7\n
|
|
1733
|
+
"{VEC2}struct Color(r: Int, g: Int)\n\nprimitive Io =\n extern fun mousePos(self) -> Vec2\n extern fun tint(self) -> Color\n extern fun move(self, p: Vec2, c: Color)\n\nfun main(io: Io) -> Int =\n p := io.mousePos()\n c := io.tint()\n io.move(p, c)\n if p.x == 1.5 && c.g == 0 - 7\n 1\n else\n 0\n"
|
|
1727
1734
|
);
|
|
1728
1735
|
let source = parse(&src);
|
|
1729
1736
|
let bytes = compileSource(&source).unwrap_or_else(|e| panic!("compile failed: {e}"));
|
|
@@ -1820,7 +1827,7 @@ fun main() -> Int =
|
|
|
1820
1827
|
#[test]
|
|
1821
1828
|
fn value_nested_pattern() {
|
|
1822
1829
|
let src = format!(
|
|
1823
|
-
"{VEC2}struct Rect(pos: Vec2, size: Vec2)\n\nfun f(r: Rect) -> Float =\n match r\n Rect(Vec2(px, py), size) => px + py * 10.0 + size.x * 100.0 + size.y * 1000.0\n\nfun g(v: Vec2) -> Float =\n match v\n Vec2(a, b) => a - b\n\nfun main() -> Int =\n r := Rect(pos: Vec2(x: 1.0, y: 2.0), size: Vec2(x: 3.0, y: 4.0))\n if f(r) == 4321.0 && g(r.pos) == 0.0 - 1.0\n
|
|
1830
|
+
"{VEC2}struct Rect(pos: Vec2, size: Vec2)\n\nfun f(r: Rect) -> Float =\n match r\n Rect(Vec2(px, py), size) => px + py * 10.0 + size.x * 100.0 + size.y * 1000.0\n\nfun g(v: Vec2) -> Float =\n match v\n Vec2(a, b) => a - b\n\nfun main() -> Int =\n r := Rect(pos: Vec2(x: 1.0, y: 2.0), size: Vec2(x: 3.0, y: 4.0))\n if f(r) == 4321.0 && g(r.pos) == 0.0 - 1.0\n 1\n else\n 0\n"
|
|
1824
1831
|
);
|
|
1825
1832
|
assert_eq!(runMerged::<i64>("value_nested_pattern", &src), 1);
|
|
1826
1833
|
}
|
|
@@ -1830,7 +1837,7 @@ fn value_nested_pattern_mixed() {
|
|
|
1830
1837
|
// Whole-value binding next to a nested destructure, and a heap container
|
|
1831
1838
|
// (class-like enum) holding a value-enum field.
|
|
1832
1839
|
let src = format!(
|
|
1833
|
-
"{VEC2}struct Rect(pos: Vec2, size: Vec2)\n\nstruct Player(pos: Vec2, hp: Float)\n\nfun f(r: Rect) -> Float =\n match r\n Rect(p, Vec2(w, h)) => p.x + p.y * 10.0 + w * 100.0 + h * 1000.0\n\nfun g(p: Player) -> Float =\n match p\n Player(Vec2(x, y), hp) => x + y * 10.0 + hp * 100.0\n\nfun main() -> Int =\n r := Rect(pos: Vec2(x: 1.0, y: 2.0), size: Vec2(x: 3.0, y: 4.0))\n p := Player(pos: Vec2(x: 1.0, y: 2.0), hp: 3.0)\n if f(r) == 4321.0 && g(p) == 321.0\n
|
|
1840
|
+
"{VEC2}struct Rect(pos: Vec2, size: Vec2)\n\nstruct Player(pos: Vec2, hp: Float)\n\nfun f(r: Rect) -> Float =\n match r\n Rect(p, Vec2(w, h)) => p.x + p.y * 10.0 + w * 100.0 + h * 1000.0\n\nfun g(p: Player) -> Float =\n match p\n Player(Vec2(x, y), hp) => x + y * 10.0 + hp * 100.0\n\nfun main() -> Int =\n r := Rect(pos: Vec2(x: 1.0, y: 2.0), size: Vec2(x: 3.0, y: 4.0))\n p := Player(pos: Vec2(x: 1.0, y: 2.0), hp: 3.0)\n if f(r) == 4321.0 && g(p) == 321.0\n 1\n else\n 0\n"
|
|
1834
1841
|
);
|
|
1835
1842
|
assert_eq!(runMerged::<i64>("value_nested_pattern_mixed", &src), 1);
|
|
1836
1843
|
}
|
|
@@ -1855,13 +1862,15 @@ fn countOps(ops: &[String], prefix: &str) -> usize {
|
|
|
1855
1862
|
fn for_over_list_no_closure() {
|
|
1856
1863
|
let form = format!(
|
|
1857
1864
|
"{STD_HEAD}\
|
|
1865
|
+
struct Count(mut n: Int)
|
|
1866
|
+
|
|
1858
1867
|
fun main() -> Int =
|
|
1859
1868
|
units := [1, 2, 3, 4]
|
|
1860
|
-
alive := 0
|
|
1869
|
+
alive := Count(n: 0)
|
|
1861
1870
|
for u := range units
|
|
1862
1871
|
if u > 2
|
|
1863
|
-
alive = alive + 1
|
|
1872
|
+
alive.n = alive.n + 1
|
|
1864
|
-
alive
|
|
1873
|
+
alive.n
|
|
1865
1874
|
"
|
|
1866
1875
|
);
|
|
1867
1876
|
let (bytes, _) = compiledKeys("formfilter", &form);
|
|
@@ -1904,11 +1913,13 @@ fn range_int_loop_is_tight() {
|
|
|
1904
1913
|
// `for i := range n` is a plain `while`: no call for the iteration itself.
|
|
1905
1914
|
let src = format!(
|
|
1906
1915
|
"{}\
|
|
1916
|
+
struct Count(mut n: Int)
|
|
1917
|
+
|
|
1907
1918
|
fun main() -> Int =
|
|
1908
|
-
total := 0
|
|
1919
|
+
total := Count(n: 0)
|
|
1909
1920
|
for i := range 10
|
|
1910
|
-
total = total + i
|
|
1921
|
+
total.n = total.n + i
|
|
1911
|
-
total
|
|
1922
|
+
total.n
|
|
1912
1923
|
",
|
|
1913
1924
|
STD_HEAD
|
|
1914
1925
|
);
|
|
@@ -2044,8 +2055,9 @@ struct Box(v: Result[Int, NegError])
|
|
|
2044
2055
|
|
|
2045
2056
|
fun f(n: Int) -> Result[Int, NegError] =
|
|
2046
2057
|
if n < 0
|
|
2047
|
-
|
|
2058
|
+
Err(Neg)
|
|
2059
|
+
else
|
|
2048
|
-
|
|
2060
|
+
Ok(n)
|
|
2049
2061
|
|
|
2050
2062
|
fun main() -> Int =
|
|
2051
2063
|
a := Box(v: f(6))
|
|
@@ -2125,8 +2137,9 @@ enum NegError = Neg | Other
|
|
|
2125
2137
|
|
|
2126
2138
|
fun chk(n: Int) -> Result[Int, NegError] =
|
|
2127
2139
|
if n < 0
|
|
2128
|
-
|
|
2140
|
+
Err(Neg)
|
|
2141
|
+
else
|
|
2129
|
-
|
|
2142
|
+
Ok(n)
|
|
2130
2143
|
|
|
2131
2144
|
fun total(xs: Slice[Result[Int, NegError]]) -> Int =
|
|
2132
2145
|
a := xs.at(0).unwrapOr(0)
|
|
@@ -2688,8 +2701,9 @@ fun main() -> Int =
|
|
|
2688
2701
|
before := mem.size()
|
|
2689
2702
|
old := mem.grow(2)
|
|
2690
2703
|
if ok && before == 16 * 65536 && old == 16 && mem.size() == 18 * 65536 && mem.grow(100000) == -1
|
|
2704
|
+
1
|
|
2691
|
-
|
|
2705
|
+
else
|
|
2692
|
-
|
|
2706
|
+
0
|
|
2693
2707
|
";
|
|
2694
2708
|
assert_eq!(runMerged::<i64>("mem_roundtrip", src), 1);
|
|
2695
2709
|
}
|
|
@@ -2847,9 +2861,9 @@ fn hasCopyingWith(bytes: &[u8]) -> bool {
|
|
|
2847
2861
|
|
|
2848
2862
|
#[test]
|
|
2849
2863
|
fn array_with_proved_local_writes_in_place() {
|
|
2850
|
-
// `a
|
|
2864
|
+
// `a.with(..)` handing over a fresh local: the checker runs `withUnique`, which only sets.
|
|
2851
2865
|
let src = format!(
|
|
2852
|
-
"{WITH_HEAD}fun
|
|
2866
|
+
"{WITH_HEAD}fun fresh() -> Array[Int] =\n a := Array.filled(4, 0)\n a.with(3, 6)\n\nfun main() -> Int =\n fresh().get(3)\n"
|
|
2853
2867
|
);
|
|
2854
2868
|
let bytes = compiledFbip("awproved", &src, true);
|
|
2855
2869
|
assert!(hasFn(&bytes, "withUnique"), "{:?}", bodyNames(&bytes));
|
|
@@ -2870,17 +2884,13 @@ const VEC_SRC: &str = "struct Vec(cells: Array[Int], n: Int) =
|
|
|
2870
2884
|
fun append(self, x: Int) -> Vec = Vec(..self, cells: self.cells.with(self.n, x), n: self.n + 1)
|
|
2871
2885
|
fun get(self, i: Int) -> Int = self.cells.get(i)
|
|
2872
2886
|
|
|
2873
|
-
fun fill(k: Int) -> Vec =
|
|
2874
|
-
|
|
2887
|
+
fun fillTwo() -> Vec = Vec(cells: Array.filled(8, 0), n: 0).append(0).append(2)
|
|
2875
|
-
for i := range k
|
|
2876
|
-
v = v.append(i * 2)
|
|
2877
|
-
v
|
|
2878
2888
|
";
|
|
2879
2889
|
|
|
2880
2890
|
#[test]
|
|
2881
2891
|
fn array_with_in_a_unique_struct_field_writes_in_place() {
|
|
2882
2892
|
// `Vec(..self, cells: self.cells.with(..))` of a unique `self`: no copy of the array.
|
|
2883
|
-
let src = format!("{WITH_HEAD}{VEC_SRC}\nfun main() -> Int =\n
|
|
2893
|
+
let src = format!("{WITH_HEAD}{VEC_SRC}\nfun main() -> Int =\n fillTwo().append(4).append(6).get(3)\n");
|
|
2884
2894
|
let bytes = compiledFbip("awvec", &src, true);
|
|
2885
2895
|
assert!(hasFn(&bytes, "withUnique"), "{:?}", bodyNames(&bytes));
|
|
2886
2896
|
assert!(!hasCopyingWith(&bytes), "{:?}", bodyNames(&bytes));
|
|
@@ -2896,7 +2906,7 @@ fn array_with_in_a_unique_struct_field_writes_in_place() {
|
|
|
2896
2906
|
fn array_with_unproved_site_copies() {
|
|
2897
2907
|
// The old array is still held by `b`: the site must copy (`resize` allocates and copies).
|
|
2898
2908
|
let src = format!(
|
|
2899
|
-
"{WITH_HEAD}fun main() -> Int =\n a := Array.filled(4, 0)\n b := a\n
|
|
2909
|
+
"{WITH_HEAD}fun main() -> Int =\n a := Array.filled(4, 0)\n b := a\n c := a.with(1, 5)\n b.get(1) * 10 + c.get(1)\n"
|
|
2900
2910
|
);
|
|
2901
2911
|
let bytes = compiledFbip("awunproved", &src, true);
|
|
2902
2912
|
assert!(!hasFn(&bytes, "withUnique"), "{:?}", bodyNames(&bytes));
|
|
@@ -2909,7 +2919,7 @@ fn array_with_unproved_site_copies() {
|
|
|
2909
2919
|
fn array_with_in_a_shared_struct_copies() {
|
|
2910
2920
|
// Two holders of the vector: its array is not written in place.
|
|
2911
2921
|
let src = format!(
|
|
2912
|
-
"{WITH_HEAD}{VEC_SRC}\nfun main() -> Int =\n v :=
|
|
2922
|
+
"{WITH_HEAD}{VEC_SRC}\nfun main() -> Int =\n v := fillTwo()\n w := v\n x := v.append(9)\n w.get(2) * 10 + x.get(2)\n"
|
|
2913
2923
|
);
|
|
2914
2924
|
let bytes = compiledFbip("awshared", &src, true);
|
|
2915
2925
|
assert!(!hasFn(&bytes, "withUnique"), "{:?}", bodyNames(&bytes));
|
compiler/wasm-codegen/tests/wasm_codegen/e2e_types/type_param_static.rs
CHANGED
|
@@ -12,7 +12,7 @@ fun build[T](n: Int) -> T = T.make(n)
|
|
|
12
12
|
|
|
13
13
|
fun main() -> Int =
|
|
14
14
|
p: Pt := build(3)
|
|
15
|
-
|
|
15
|
+
p.x
|
|
16
16
|
";
|
|
17
17
|
let errs = checkErrors("missing", src);
|
|
18
18
|
assert!(
|
|
@@ -44,7 +44,7 @@ import std/Str
|
|
|
44
44
|
|
|
45
45
|
fun main() -> Int =
|
|
46
46
|
j := 4.toJson()
|
|
47
|
-
|
|
47
|
+
0
|
|
48
48
|
";
|
|
49
49
|
let errs = match load("ext_noimport", src) {
|
|
50
50
|
Err(e) => vec![e],
|
compiler/wasm-codegen/tests/wasm_codegen/examples_test.rs
CHANGED
|
@@ -170,7 +170,7 @@ fn const_table_1000_entries() {
|
|
|
170
170
|
for i in 0..1000 {
|
|
171
171
|
src.push_str(&format!(" out.push({})\n", i * i));
|
|
172
172
|
}
|
|
173
|
-
src.push_str("
|
|
173
|
+
src.push_str(" out\n\nfun probe() -> Int =\n E999 + Double\n");
|
|
174
174
|
let dir = std::env::temp_dir().join(format!("plum_const_table_{}", std::process::id()));
|
|
175
175
|
std::fs::create_dir_all(&dir).unwrap();
|
|
176
176
|
let path = dir.join("t.plum");
|
compiler/wasm-codegen/tests/wasm_codegen/fusion_alloc_test.rs
CHANGED
|
@@ -167,13 +167,13 @@ fn expected(n: i64) -> i64 {
|
|
|
167
167
|
|
|
168
168
|
fn buildList(n: i64) -> String {
|
|
169
169
|
format!(
|
|
170
|
-
"fun build(
|
|
170
|
+
"fun build(i: Int, xs: List[Int]) -> List[Int] =\n if i > 0 then build(i - 1, Cons(head: i, tail: xs)) else xs\n\n"
|
|
171
171
|
) + &format!("# n = {n}\n")
|
|
172
172
|
}
|
|
173
173
|
|
|
174
174
|
fn listPipeline(n: i64) -> String {
|
|
175
175
|
format!(
|
|
176
|
-
"{PRE}{}fun total(xs: List[Int]) -> Int = xs.map(|x| x * 2).filter(|x| x % 3 != 0).fold(0, |a, x| a + x)\n\nfun main() -> Int =
|
|
176
|
+
"{PRE}{}fun total(xs: List[Int]) -> Int = xs.map(|x| x * 2).filter(|x| x % 3 != 0).fold(0, |a, x| a + x)\n\nfun main() -> Int =\n xs := build({n}, Nil)\n if xs.length() > 0 then total(xs) else 0\n",
|
|
177
177
|
buildList(n)
|
|
178
178
|
)
|
|
179
179
|
}
|
|
@@ -263,12 +263,12 @@ fn million_pipeline_timed() {
|
|
|
263
263
|
let list = load("million", &listPipeline(n));
|
|
264
264
|
let buildOnly = load(
|
|
265
265
|
"million_build",
|
|
266
|
-
&format!("{PRE}{}fun main() -> Int = build({n}).length()\n", buildList(n)),
|
|
266
|
+
&format!("{PRE}{}fun main() -> Int = build({n}, Nil).length()\n", buildList(n)),
|
|
267
267
|
);
|
|
268
268
|
let slice = load(
|
|
269
269
|
"million_slice",
|
|
270
270
|
&format!(
|
|
271
|
-
"{PRE}
|
|
271
|
+
"{PRE}struct Total(mut value: Int)\n\nfun main() -> Int =\n xs := Slice()\n for i := range {n}\n xs.push(i + 1)\n total := Total(value: 0)\n for x := range xs\n y := x * 2\n if y % 3 != 0\n total.value = total.value + y\n total.value\n"
|
|
272
272
|
),
|
|
273
273
|
);
|
|
274
274
|
let eng = engine(true);
|
docs/plans/2026-11-06-simpler-language.md
CHANGED
|
@@ -191,7 +191,7 @@ e.g. a struct with a method named `push` no longer blocks another type's `push`
|
|
|
191
191
|
**Consumes:** the proof and purity key methods by the checker's receiver type, so `Ship.update` and `Orb.update` are separate; struct and enum spreads and reuse prove in the shapes the pilot failed on (T11)
|
|
192
192
|
**Produces:** a local moved into a field or returned counts as handed over, so `S(..s, ship: s.ship.update(dt))` reuses both when `s` is unique
|
|
193
193
|
e.g. `--explain-fusion` shows `S(..s, ship: s.ship.update(dt))` in place
|
|
194
|
-
**Notes:** Last use is flow-sensitive across `if` and `match` branches. A closure capture or a read after the move stays shared.
|
|
194
|
+
**Notes:** With loops gone, these are the shapes that replace `v = v.f()`, and the proof must cover them, found in T9: a rebinding `w := v.f()` where `v` isn't used again; a `fold` accumulator (`xs.fold(s, |acc, x| acc.bump(x))`, including after closure specialisation); a tail-recursive argument (`loop(s.bump(), i + 1)`). At T9 they all copy: `tests/fbip_probes/struct_spread.plum` went from 1 to 8 ms, `slice_shapes` s10 was lost, and some `tests/fbip` aliasing programs now reject by default. Restore them, and record those timings. Last use is flow-sensitive across `if` and `match` branches. A closure capture or a read after the move stays shared.
|
|
195
195
|
**Test code:** hostile programs: read after the move in one branch; move in a nested spread; capture before the move; the same value moved into two fields.
|
|
196
196
|
|
|
197
197
|
### T13 shared and unique callers · Complexity 7 · Est. context 90k
|
docs/plans/2026-11-06-simpler-language.t9-notes.md
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
# T9 notes: what later tasks must do
|
|
2
|
+
|
|
3
|
+
## For T10: tests to delete with the forms they test
|
|
4
|
+
|
|
5
|
+
**Plum files, whole:** tests/fbip/vec*.plum (with the pilot `Vec`); tests/wasm_codegen/e2e_tail/{break_in_match_arm_direct, break_in_if_in_match_arm_in_while, break_in_else_if_chain_in_while, continue_in_if_in_match_arm_in_while, nested_loops_with_match_in_inner}; e2e_mem/unique_spread_reassign_local; e2e_records/reassign_enum_local_across_variants.
|
|
6
|
+
|
|
7
|
+
**Plum tests, parts of a file:** for_next `rangeCtl`; shadowed_effects `incRebound`, `incLocal`; block_closures `captured`; block_values_passes `retInBlock`; examples: `break_continue_return_semantics` (control_flow, sequences), `probe break/continue` (arrays), `capturing closure snapshots...` (closures), `union_local_reassigned` (structs_records).
|
|
8
|
+
|
|
9
|
+
**Rust tests:**
|
|
10
|
+
- checker_tests: first_use_assign_is_an_error, reassign_after_declare_is_ok, local_named_like_a_global_fn_counts_as_declared, param_named_like_a_global_fn_can_be_reassigned, assign_to_a_global_fn_name_is_an_error
|
|
11
|
+
- closure_types: closure_reassigned_in_loop_keeps_its_type
|
|
12
|
+
- closure_spec: capture_read_after_mutation, capture_reassigned_between_closure_and_call
|
|
13
|
+
- uniqueness: returned_parameter_is_fresh_for_a_unique_argument, returned_parameter_that_was_stored_is_not_fresh, returned_parameter_handed_a_shared_value_elsewhere_is_not_unique, method_handed_over_in_a_while_loop_is_unique, returned_parameter_in_a_loop_keeps_the_hand_over_unique, loop_keeping_the_previous_value_not_unique, loop_value_bound_outside_from_a_shared_source_not_unique
|
|
14
|
+
- unique_inplace: unique_spread_reassign_same_var
|
|
15
|
+
- rename: plainAssign; tail: return_expr_tail
|
|
16
|
+
- formatter: doesNotCollapseMultiStatementOrReturnOnlyMatchCaseBodies
|
|
17
|
+
- compile_tests: returnBodySuggestedByTheParseErrorHintCompiles
|
|
18
|
+
|
|
19
|
+
**One line goes, the rest stays:** question_tests::bare_value_into_option_arg_and_annotation_errors (`o = 1`); parser_test::plainAssignmentsHaveNoType (`x = 1`).
|
|
20
|
+
|
|
21
|
+
**Change together:** the parser hint and loader_test::parseErrorBlamesTheTokenNotTheWholeFunction, which asserts the hint says `return`.
|
|
22
|
+
|
|
23
|
+
**Keep:** `while` with a `mut` field in formatter_test, if_and_while_bindings_parse, the `?`-in-`while` rejection, block_values_passes `whileCond`, seq_with_optional_next.
|
|
24
|
+
|
|
25
|
+
The uniqueness tests above are about loops and hand-over. Their aliasing ideas must survive in new forms: `tests/fbip` keeps them, and T12 re-adds the proofs.
|
|
26
|
+
|
|
27
|
+
**Also:** same-scope `:=` shadowing is an error, so a rebinding can't be ported to the same name. `State(..newState(), ...)` is rejected, since a spread needs a variable.
|
|
28
|
+
|
|
29
|
+
## For T11 to T13: the proof's gap
|
|
30
|
+
|
|
31
|
+
`uniqueness.rs` treats only `v = g(v)` as a hand-over, and T10 removes that form. These replace it and don't prove today: `w := g(v)`; `g(v)` as a nested argument; `bump(touch(fresh))` through a pass-through function; a `fold` accumulator; a tail-recursive state loop (`bumped(Cell(..c, ...), n - 1)` gives "read after the spread"). Non-tail recursion keeps the proof but overflows at 100k.
|
|
32
|
+
|
|
33
|
+
Measured: `tests/fbip_probes/struct_spread.plum` went from 1 to 8 ms. `slice_shapes` s10 lost its proof. Some `tests/fbip` aliasing programs now reject by default: three moved to tests/fbip/array_in_place.plum. `array_names` uses an `if` merge in place of a fresh-then-shared rebind.
|
|
34
|
+
|
|
35
|
+
**Reshaped to keep a proof, so restore the loop form once the proof handles it:**
|
|
36
|
+
- no_fbip_test: ten nested `bump`
|
|
37
|
+
- array_with_proved_local_writes_in_place: one `with`, not a loop (T10 deletes `Array.with`)
|
|
38
|
+
- array_with_in_a_unique_struct_field_writes_in_place, array_with_in_a_shared_struct_copies: `fillTwo().append..`
|
|
39
|
+
- step_called_only_with_unique_args_is_unique, self_spread_in_method_unique
|
|
40
|
+
|
|
41
|
+
**Reshaped for fusion:** list_pipeline_loop_has_no_cons_alloc and map_single_pass_one_alloc_per_cell: `main` reads the result twice, so fusion doesn't absorb the whole program.
|
|
42
|
+
|
|
43
|
+
## Open
|
|
44
|
+
|
|
45
|
+
- Public helper methods: each `while` became a helper, and methods can't be private: `Map.probeFor`, `Slice.sameFrom` and others are visible. Decide on visibility later.
|
|
46
|
+
- `Range.find$cN` reports impure in `--explain-fusion`. It looks legitimate, but the cause isn't confirmed.
|
|
47
|
+
- No speed check yet of the migrated std against the old compiler.
|
examples/arrays.plum
CHANGED
|
@@ -15,27 +15,17 @@ import std/Slice
|
|
|
15
15
|
# unboxed wasm arrays (i64 / f64 / i32 elements), not boxed anyref arrays.
|
|
16
16
|
fun sumInts(n: Int) -> Int =
|
|
17
17
|
a := Array.filled(n, 3)
|
|
18
|
-
total := 0
|
|
19
|
-
for i := range n
|
|
20
|
-
|
|
18
|
+
0.until(n).fold(0, |total, i| total + a.get(i))
|
|
21
|
-
return total
|
|
22
19
|
|
|
23
20
|
fun sumFloats(n: Int) -> Float =
|
|
24
21
|
a := Array.filled(n, 0.5)
|
|
25
|
-
total := 0.0
|
|
26
|
-
for i := range n
|
|
27
|
-
|
|
22
|
+
0.until(n).fold(0.0, |total, i| total + a.get(i))
|
|
28
|
-
return total
|
|
29
23
|
|
|
30
24
|
fun countTrue(n: Int) -> Int =
|
|
31
25
|
a := Array.filled(n, false)
|
|
32
26
|
a.set(1, true)
|
|
33
27
|
a.set(3, true)
|
|
34
|
-
c := 0
|
|
35
|
-
|
|
28
|
+
0.until(n).fold(0, |count, i| if a.get(i) then count + 1 else count)
|
|
36
|
-
if a.get(i)
|
|
37
|
-
c = c + 1
|
|
38
|
-
return c
|
|
39
29
|
|
|
40
30
|
# ==== struct arrays ====
|
|
41
31
|
|
|
@@ -64,6 +54,8 @@ struct Holder(arr: Array[Vec2])
|
|
|
64
54
|
|
|
65
55
|
struct Named(label: Str, v: Vec2)
|
|
66
56
|
|
|
57
|
+
struct Vec2Sum(mut x: Float, mut y: Float)
|
|
58
|
+
|
|
67
59
|
fun bump(h: Holder) =
|
|
68
60
|
h.arr.set(0, Vec2(x: 77.0, y: 88.0))
|
|
69
61
|
|
|
@@ -102,9 +94,7 @@ test "List[Vec2] push/get/set round-trips across growth"
|
|
|
102
94
|
assert l.get(99)!.y == 198.0
|
|
103
95
|
|
|
104
96
|
test "nested struct (Rect of Vec2s) in an Array"
|
|
105
|
-
a := Array[Rect]()
|
|
106
|
-
a =
|
|
97
|
+
a := Array[Rect]().push(Rect(pos: Vec2(x: 1.0, y: 2.0), size: Vec2(x: 3.0, y: 4.0))).push(Rect(pos: Vec2(x: 5.0, y: 6.0), size: Vec2(x: 7.0, y: 8.0)))
|
|
107
|
-
a = a.push(Rect(pos: Vec2(x: 5.0, y: 6.0), size: Vec2(x: 7.0, y: 8.0)))
|
|
108
98
|
assert a.length() == 2
|
|
109
99
|
r := a.get(1)
|
|
110
100
|
assert r.pos.x == 5.0
|
|
@@ -117,9 +107,7 @@ test "nested struct (Rect of Vec2s) in an Array"
|
|
|
117
107
|
assert a.get(0).area() == 30.0
|
|
118
108
|
|
|
119
109
|
test "struct with Int, Bool and Float leaves"
|
|
120
|
-
a := Array[Tile]()
|
|
121
|
-
a = a.push(Tile(id: 7, solid: true, weight: 1.5))
|
|
122
|
-
a =
|
|
110
|
+
a := Array[Tile]().push(Tile(id: 7, solid: true, weight: 1.5)).push(Tile(id: 9, solid: false, weight: 2.5))
|
|
123
111
|
assert a.get(0).id == 7
|
|
124
112
|
assert a.get(0).solid
|
|
125
113
|
assert a.get(0).weight == 1.5
|
|
@@ -136,23 +124,22 @@ test "copy semantics: a value read from the array is a copy until set"
|
|
|
136
124
|
l := Slice[Vec2]()
|
|
137
125
|
l.push(Vec2(x: 1.0, y: 1.0))
|
|
138
126
|
v := l.get(0)!
|
|
139
|
-
|
|
127
|
+
changed := Vec2(..v, x: 99.0)
|
|
140
128
|
assert l.get(0)!.x == 1.0
|
|
141
129
|
w := l.get(0)!
|
|
142
130
|
_ := l.set(0, Vec2(x: 5.0, y: 5.0))!
|
|
143
131
|
assert w.x == 1.0
|
|
144
132
|
assert l.get(0)!.x == 5.0
|
|
145
|
-
assert
|
|
133
|
+
assert changed.x == 99.0
|
|
146
134
|
|
|
147
135
|
test "for x := range over Array[Vec2]"
|
|
148
136
|
a := Array[Vec2]().push(Vec2(x: 1.0, y: 10.0)).push(Vec2(x: 2.0, y: 20.0)).push(Vec2(x: 3.0, y: 30.0))
|
|
149
|
-
|
|
137
|
+
sum := Vec2Sum(x: 0.0, y: 0.0)
|
|
150
|
-
sy := 0.0
|
|
151
138
|
for v := range a
|
|
152
|
-
|
|
139
|
+
sum.x = sum.x + v.x
|
|
153
|
-
|
|
140
|
+
sum.y = sum.y + v.y
|
|
154
|
-
assert
|
|
141
|
+
assert sum.x == 6.0
|
|
155
|
-
assert
|
|
142
|
+
assert sum.y == 60.0
|
|
156
143
|
|
|
157
144
|
test "Array.filled with a struct"
|
|
158
145
|
a := Array.filled(5, Vec2(x: 2.0, y: 3.0))
|
|
@@ -177,9 +164,7 @@ test "a List[Vec2] inside a record field"
|
|
|
177
164
|
assert w.points.get(0)!.y == 2.0
|
|
178
165
|
|
|
179
166
|
test "probe push growth, shrink, single leaf"
|
|
180
|
-
a := Array[One]()
|
|
181
|
-
for i := range 50
|
|
182
|
-
|
|
167
|
+
a := 0.until(50).fold(Array[One](), |grown, i| grown.push(One(n: i)))
|
|
183
168
|
assert a.length() == 50
|
|
184
169
|
assert a.get(49).n == 49
|
|
185
170
|
b := a.resize(10)
|
|
@@ -189,14 +174,12 @@ test "probe push growth, shrink, single leaf"
|
|
|
189
174
|
assert c.length() == 0
|
|
190
175
|
|
|
191
176
|
test "probe Byte/Bool leaves"
|
|
192
|
-
a := Array[Flags]()
|
|
193
|
-
a =
|
|
177
|
+
a := Array[Flags]().push(Flags(a: false, b: true, c: 3))
|
|
194
178
|
assert a.get(0).b
|
|
195
179
|
assert a.get(0).c == 3
|
|
196
180
|
|
|
197
181
|
test "probe three-level nested"
|
|
198
|
-
a := Array[L3]()
|
|
199
|
-
a =
|
|
182
|
+
a := Array[L3]().push(L3(r: Rect(pos: Vec2(x: 1.0, y: 2.0), size: Vec2(x: 3.0, y: 4.0)), k: 9))
|
|
200
183
|
assert a.get(0).r.size.y == 4.0
|
|
201
184
|
assert a.get(0).k == 9
|
|
202
185
|
assert a.get(0).r.area() == 12.0
|
|
@@ -236,8 +219,7 @@ test "probe break/continue"
|
|
|
236
219
|
assert s == 4.0
|
|
237
220
|
|
|
238
221
|
test "probe Str leaf"
|
|
239
|
-
a := Array[Named]()
|
|
240
|
-
a =
|
|
222
|
+
a := Array[Named]().push(Named(label: "hi", v: Vec2(x: 1.0, y: 2.0)))
|
|
241
223
|
assert a.get(0).label == "hi"
|
|
242
224
|
|
|
243
225
|
test "Byte struct fields: copy, compare, store"
|
examples/basics.plum
CHANGED
|
@@ -57,7 +57,7 @@ fun isEven(n: Int) -> Bool =
|
|
|
57
57
|
# ==== functions ====
|
|
58
58
|
|
|
59
59
|
# Functions: top-level function declarations, typed arguments, default
|
|
60
|
-
# parameters, list-literal arguments,
|
|
60
|
+
# parameters, list-literal arguments, and recursion.
|
|
61
61
|
|
|
62
62
|
fun addInts(a: Int, b: Int) -> Int =
|
|
63
63
|
a + b
|
|
@@ -68,11 +68,14 @@ fun greet() =
|
|
|
68
68
|
fun withDefault(a: Int, step: Int = 1) -> Int =
|
|
69
69
|
a + step
|
|
70
70
|
|
|
71
|
+
# A `for` runs side effects, so its running total lives in a `mut` field.
|
|
72
|
+
struct Tally(mut total: Int, mut i: Int)
|
|
73
|
+
|
|
71
74
|
fun sumAll(nums: List[Int]) -> Int =
|
|
72
|
-
|
|
75
|
+
tally := Tally(total: 0, i: 0)
|
|
73
76
|
for v := range nums
|
|
74
|
-
total = total + v
|
|
77
|
+
tally.total = tally.total + v
|
|
75
|
-
total
|
|
78
|
+
tally.total
|
|
76
79
|
|
|
77
80
|
fun wrap[T](value: T) -> Bool =
|
|
78
81
|
true
|
|
@@ -90,8 +93,9 @@ fun usePair() -> Bool =
|
|
|
90
93
|
|
|
91
94
|
fun factorial(x: Int) -> Int =
|
|
92
95
|
if x < 2
|
|
96
|
+
1
|
|
93
|
-
|
|
97
|
+
else
|
|
94
|
-
|
|
98
|
+
x * factorial(x - 1)
|
|
95
99
|
|
|
96
100
|
# Regression test: `double(...)` (an all-lowercase, no-uppercase, no-underscore
|
|
97
101
|
# callee) used to fail to parse at all — `var_identifier` and `fn_identifier`
|
|
@@ -104,12 +108,11 @@ fun combineList(prefix: Int, rest: List[Int]) -> Int =
|
|
|
104
108
|
prefix
|
|
105
109
|
|
|
106
110
|
fun weighted(nums: List[Int]) -> Int =
|
|
107
|
-
|
|
111
|
+
tally := Tally(total: 0, i: 0)
|
|
108
|
-
i := 0
|
|
109
112
|
for v := range nums
|
|
110
|
-
total = total + i * v
|
|
113
|
+
tally.total = tally.total + tally.i * v
|
|
111
|
-
i = i + 1
|
|
114
|
+
tally.i = tally.i + 1
|
|
112
|
-
total
|
|
115
|
+
tally.total
|
|
113
116
|
|
|
114
117
|
# ==== testing tests ====
|
|
115
118
|
|
examples/control_flow.plum
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
# Control flow:
|
|
1
|
+
# Control flow: `if` and `match` choose a value, `for` and `while` run side
|
|
2
|
+
# effects, `fold` and recursion carry state from step to step.
|
|
2
3
|
import std/Str
|
|
3
4
|
import std/Int
|
|
4
5
|
import std/Float
|
|
@@ -9,31 +10,45 @@ import std/List
|
|
|
9
10
|
|
|
10
11
|
# ==== control flow ====
|
|
11
12
|
|
|
12
|
-
#
|
|
13
|
+
# `if` and `match` are expressions: a function's value is its last expression,
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
# so each branch ends in the value it produces.
|
|
15
|
-
total := 0
|
|
16
|
-
for i := range limit
|
|
17
|
-
if i == 3
|
|
18
|
-
continue
|
|
19
|
-
if i == 8
|
|
20
|
-
break
|
|
21
|
-
total = total + i
|
|
22
|
-
return total
|
|
23
15
|
|
|
24
16
|
fun classify(n: Int) -> Str =
|
|
25
17
|
if n < 0
|
|
26
|
-
|
|
18
|
+
"negative"
|
|
27
19
|
else if n == 0
|
|
28
|
-
|
|
20
|
+
"zero"
|
|
29
21
|
else
|
|
30
|
-
|
|
22
|
+
"positive"
|
|
23
|
+
|
|
24
|
+
# `for` and `while cond` are for side effects: the body acts through a `mut`
|
|
25
|
+
# field or `io`. Local names can't be reassigned, so a loop that needs to count
|
|
26
|
+
# keeps its count in a field.
|
|
27
|
+
struct Tally(mut n: Int)
|
|
31
28
|
|
|
32
29
|
fun countdown(start: Int) -> Int =
|
|
33
|
-
|
|
30
|
+
left := Tally(n: start)
|
|
34
|
-
while
|
|
31
|
+
while left.n > 0
|
|
32
|
+
left.n = left.n - 1
|
|
33
|
+
left.n
|
|
34
|
+
|
|
35
|
+
# `fold` carries state through a loop without a field: each step gets the total
|
|
36
|
+
# so far and returns the next one. A branch that returns `total` unchanged skips
|
|
37
|
+
# an element.
|
|
38
|
+
fun sumSkippingThree(limit: Int) -> Int =
|
|
39
|
+
0.until(limit).fold(0, |total, i|
|
|
35
|
-
i =
|
|
40
|
+
if i == 3
|
|
41
|
+
total
|
|
42
|
+
else
|
|
36
|
-
|
|
43
|
+
total + i
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
# Recursion carries state as arguments, and stops by not recursing further.
|
|
47
|
+
fun sumDigits(n: Int, total: Int) -> Int =
|
|
48
|
+
if n == 0
|
|
49
|
+
total
|
|
50
|
+
else
|
|
51
|
+
sumDigits(n / 10, total + n % 10)
|
|
37
52
|
|
|
38
53
|
fun placeholder() =
|
|
39
54
|
todo
|
|
@@ -65,7 +80,7 @@ fun classifySign(n: Int) -> Int =
|
|
|
65
80
|
fun describeMagnitude(n: Int) -> Int =
|
|
66
81
|
match n
|
|
67
82
|
0 =>
|
|
68
|
-
|
|
83
|
+
100
|
|
69
84
|
x => x * 2
|
|
70
85
|
|
|
71
86
|
enum TailOption = TailSome(v: Int) | TailNone
|
|
@@ -81,75 +96,90 @@ fun checkPositiveAndReturn(n: Int) -> Int =
|
|
|
81
96
|
|
|
82
97
|
# ==== loops ====
|
|
83
98
|
|
|
84
|
-
# Loops: `
|
|
99
|
+
# Loops: `for ... := range` over ranges and collections, with `if` in the body
|
|
85
|
-
#
|
|
100
|
+
# to filter, and `find` to stop at the first match.
|
|
86
101
|
|
|
87
102
|
fun sumList(xs: Slice[Int]) -> Int =
|
|
88
|
-
|
|
103
|
+
sum := Tally(n: 0)
|
|
89
104
|
for x := range xs
|
|
90
|
-
|
|
105
|
+
sum.n = sum.n + x
|
|
91
|
-
|
|
106
|
+
sum.n
|
|
92
107
|
|
|
93
108
|
fun firstOver(xs: Slice[Int], limit: Int) -> Int =
|
|
109
|
+
hit := 0.until(xs.length()).find(|i| xs.at(i) > limit)
|
|
110
|
+
match hit
|
|
94
|
-
|
|
111
|
+
Some(i) => xs.at(i)
|
|
95
|
-
|
|
112
|
+
None => -1
|
|
96
|
-
return x
|
|
97
|
-
return -1
|
|
98
113
|
|
|
99
114
|
# ==== control flow tests ====
|
|
100
115
|
|
|
101
|
-
test "tail match
|
|
116
|
+
test "tail match runs correctly"
|
|
102
117
|
assert bindExample(5) == 5
|
|
103
118
|
|
|
104
|
-
test "tail if
|
|
119
|
+
test "tail if runs correctly"
|
|
105
120
|
assert abs(-7) == 7
|
|
106
121
|
|
|
107
|
-
test "tail if nested inside match arm
|
|
122
|
+
test "tail if nested inside match arm runs correctly"
|
|
108
123
|
assert classifySign(-5) == -1
|
|
109
124
|
|
|
110
|
-
test "tail match mixing
|
|
125
|
+
test "tail match mixing block and bare expr arms runs correctly"
|
|
111
126
|
assert describeMagnitude(21) == 42
|
|
112
127
|
|
|
113
|
-
test "tail enum match
|
|
128
|
+
test "tail enum match runs correctly"
|
|
114
129
|
assert unwrapTailOr(TailSome(9), 0) == 9
|
|
115
130
|
|
|
116
131
|
test "assert passes through on true"
|
|
117
132
|
assert checkPositiveAndReturn(5) == 5
|
|
118
133
|
|
|
134
|
+
test "if chain chooses a value"
|
|
135
|
+
assert classify(-1) == "negative"
|
|
136
|
+
assert classify(0) == "zero"
|
|
137
|
+
assert classify(1) == "positive"
|
|
138
|
+
|
|
139
|
+
test "while cond runs until the condition is false"
|
|
140
|
+
assert countdown(3) == 0
|
|
141
|
+
assert countdown(0) == 0
|
|
142
|
+
|
|
143
|
+
test "fold carries state, a branch can skip an element"
|
|
144
|
+
assert sumSkippingThree(5) == 7
|
|
145
|
+
|
|
146
|
+
test "recursion carries state as arguments"
|
|
147
|
+
assert sumDigits(1234, 0) == 10
|
|
148
|
+
|
|
119
149
|
# ==== loops tests ====
|
|
120
150
|
|
|
121
151
|
test "plain_iterable_for: List and Array"
|
|
122
152
|
xs := [1, 2, 3, 4].toSlice()
|
|
123
153
|
assert sumList(xs) == 10
|
|
124
154
|
arr := Array.filled(3, 7)
|
|
125
|
-
total := 0
|
|
155
|
+
total := Tally(n: 0)
|
|
126
156
|
for a := range arr
|
|
127
|
-
total = total + a
|
|
157
|
+
total.n = total.n + a
|
|
128
|
-
assert total == 21
|
|
158
|
+
assert total.n == 21
|
|
129
159
|
|
|
130
160
|
test "index alongside the element: range over the length"
|
|
131
161
|
xs := [10, 20, 30].toSlice()
|
|
132
|
-
acc := 0
|
|
162
|
+
acc := Tally(n: 0)
|
|
133
163
|
for i := range xs.length()
|
|
134
|
-
acc = acc + i * xs.at(i)
|
|
164
|
+
acc.n = acc.n + i * xs.at(i)
|
|
135
|
-
assert acc == 80
|
|
165
|
+
assert acc.n == 80
|
|
136
166
|
|
|
137
167
|
test "if_in_body_keeps_when_true: if cond keeps the element, false skips it"
|
|
138
168
|
xs := [1, 2, 3, 4, 5, 6].toSlice()
|
|
139
|
-
total := 0
|
|
169
|
+
total := Tally(n: 0)
|
|
140
170
|
for x := range xs
|
|
141
171
|
if x % 2 == 0
|
|
142
|
-
total = total + x
|
|
172
|
+
total.n = total.n + x
|
|
143
|
-
assert total == 12
|
|
173
|
+
assert total.n == 12
|
|
144
174
|
|
|
145
175
|
test "bind_in_body: name := expr is visible to the rest of the body"
|
|
146
176
|
xs := [1, 2, 3, 4].toSlice()
|
|
147
|
-
total := 0
|
|
177
|
+
total := Tally(n: 0)
|
|
148
178
|
for x := range xs
|
|
149
179
|
sq := x * x
|
|
150
180
|
if sq > 9
|
|
151
|
-
total = total + sq
|
|
181
|
+
total.n = total.n + sq
|
|
152
|
-
assert total == 16
|
|
182
|
+
assert total.n == 16
|
|
153
183
|
|
|
154
184
|
test "break_continue_return_semantics"
|
|
155
185
|
xs := [1, 2, 3, 4, 5].toSlice()
|
|
@@ -164,75 +194,78 @@ test "break_continue_return_semantics"
|
|
|
164
194
|
assert firstOver(xs, 3) == 4
|
|
165
195
|
assert firstOver(xs, 9) == -1
|
|
166
196
|
|
|
197
|
+
test "find stops at the first match"
|
|
198
|
+
xs := [1, 2, 3, 4, 5].toSlice()
|
|
199
|
+
assert firstOver(xs, 3) == 4
|
|
200
|
+
assert firstOver(xs, 9) == -1
|
|
201
|
+
|
|
167
202
|
test "nested iterable loops"
|
|
168
203
|
xs := [1, 2].toSlice()
|
|
169
204
|
ys := [10, 20].toSlice()
|
|
170
|
-
total := 0
|
|
205
|
+
total := Tally(n: 0)
|
|
171
206
|
for x := range xs
|
|
172
207
|
for y := range ys
|
|
173
|
-
total = total + x * y
|
|
208
|
+
total.n = total.n + x * y
|
|
174
|
-
assert total == 90
|
|
209
|
+
assert total.n == 90
|
|
175
210
|
|
|
176
|
-
test "range_int: 0 to n-1, empty for 0
|
|
211
|
+
test "range_int: 0 to n-1, empty for 0"
|
|
177
|
-
total := 0
|
|
212
|
+
total := Tally(n: 0)
|
|
178
213
|
for i := range 5
|
|
179
|
-
total = total + i
|
|
214
|
+
total.n = total.n + i
|
|
180
|
-
assert total == 10
|
|
215
|
+
assert total.n == 10
|
|
181
|
-
|
|
216
|
+
runs := Tally(n: 0)
|
|
182
217
|
for _ := range 0
|
|
183
|
-
n = n + 1
|
|
218
|
+
runs.n = runs.n + 1
|
|
184
|
-
assert n == 0
|
|
219
|
+
assert runs.n == 0
|
|
185
|
-
odd := 0
|
|
220
|
+
odd := Tally(n: 0)
|
|
186
221
|
for i := range 6
|
|
187
|
-
if i % 2 =
|
|
222
|
+
if i % 2 != 0
|
|
188
|
-
continue
|
|
189
|
-
|
|
223
|
+
odd.n = odd.n + i
|
|
190
|
-
assert odd == 9
|
|
224
|
+
assert odd.n == 9
|
|
191
225
|
|
|
192
226
|
test "range_until_to_downTo: bounds incl. empty ranges"
|
|
193
|
-
a := 0
|
|
227
|
+
a := Tally(n: 0)
|
|
194
228
|
for i := range 2.until(6)
|
|
195
|
-
a = a + i
|
|
229
|
+
a.n = a.n + i
|
|
196
|
-
assert a == 14
|
|
230
|
+
assert a.n == 14
|
|
197
|
-
b := 0
|
|
231
|
+
b := Tally(n: 0)
|
|
198
232
|
for i := range 1.to(4)
|
|
199
|
-
b = b + i
|
|
233
|
+
b.n = b.n + i
|
|
200
|
-
assert b == 10
|
|
234
|
+
assert b.n == 10
|
|
201
|
-
c := 0
|
|
235
|
+
c := Tally(n: 0)
|
|
202
236
|
for i := range 4.downTo(1)
|
|
203
|
-
c = c * 10 + i
|
|
237
|
+
c.n = c.n * 10 + i
|
|
204
|
-
assert c == 4321
|
|
238
|
+
assert c.n == 4321
|
|
205
|
-
|
|
239
|
+
runs := Tally(n: 0)
|
|
206
240
|
for _ := range 5.until(5)
|
|
207
|
-
n = n + 1
|
|
241
|
+
runs.n = runs.n + 1
|
|
208
242
|
for _ := range 1.downTo(2)
|
|
209
|
-
n = n + 1
|
|
243
|
+
runs.n = runs.n + 1
|
|
210
244
|
for _ := range 3.to(2)
|
|
211
|
-
n = n + 1
|
|
245
|
+
runs.n = runs.n + 1
|
|
212
|
-
assert n == 0
|
|
246
|
+
assert runs.n == 0
|
|
213
247
|
|
|
214
248
|
test "range_stepBy: stepBy on until and to"
|
|
215
|
-
total := 0
|
|
249
|
+
total := Tally(n: 0)
|
|
216
250
|
for i := range 0.until(10).stepBy(3)
|
|
217
|
-
total = total + i
|
|
251
|
+
total.n = total.n + i
|
|
218
|
-
assert total == 18
|
|
252
|
+
assert total.n == 18
|
|
219
253
|
for i := range 1.to(10).stepBy(4)
|
|
220
|
-
total = total + i
|
|
254
|
+
total.n = total.n + i
|
|
221
|
-
assert total == 18 + 15
|
|
255
|
+
assert total.n == 18 + 15
|
|
222
256
|
|
|
223
257
|
test "range_filter: an if in the body filters an Int range"
|
|
224
|
-
evens := 0
|
|
258
|
+
evens := Tally(n: 0)
|
|
225
259
|
for i := range 0.until(10)
|
|
226
260
|
if i % 2 == 0
|
|
227
|
-
evens = evens + 1
|
|
261
|
+
evens.n = evens.n + 1
|
|
228
|
-
assert evens == 5
|
|
262
|
+
assert evens.n == 5
|
|
229
|
-
down := 0
|
|
263
|
+
down := Tally(n: 0)
|
|
230
264
|
for i := range 9.downTo(0).stepBy(4)
|
|
231
|
-
down = down * 10 + i
|
|
265
|
+
down.n = down.n * 10 + i
|
|
232
|
-
assert down == 951
|
|
266
|
+
assert down.n == 951
|
|
233
|
-
|
|
267
|
+
kept := Tally(n: 0)
|
|
234
268
|
for i := range 0.until(6)
|
|
235
|
-
if i
|
|
269
|
+
if i >= 3
|
|
236
|
-
continue
|
|
237
|
-
|
|
270
|
+
kept.n = kept.n + i
|
|
238
|
-
assert
|
|
271
|
+
assert kept.n == 12
|
examples/errors.plum
CHANGED
|
@@ -37,8 +37,9 @@ enum PositiveError =
|
|
|
37
37
|
fun parsePositive(s: Str) -> Result[Int, PositiveError] =
|
|
38
38
|
n := Int.parse(s).mapErr(|e| BadNumber)?
|
|
39
39
|
if n < 0
|
|
40
|
-
|
|
40
|
+
Err(NegativeNum)
|
|
41
|
+
else
|
|
41
|
-
|
|
42
|
+
Ok(n)
|
|
42
43
|
|
|
43
44
|
# `?` returns the first error on the way out and skips the rest of the function.
|
|
44
45
|
fun sumTwo(a: Str, b: Str) -> Result[Int, PositiveError] =
|
|
@@ -121,10 +122,11 @@ enum AgeError =
|
|
|
121
122
|
fun parseAge(s: Str) -> Result[Int, AgeError] =
|
|
122
123
|
n := Int.parse(s).mapErr(|e| NotANumber)?
|
|
123
124
|
if n < 0
|
|
124
|
-
|
|
125
|
+
Err(Negative)
|
|
125
|
-
if n > 150
|
|
126
|
+
else if n > 150
|
|
126
|
-
|
|
127
|
+
Err(TooOld)
|
|
128
|
+
else
|
|
127
|
-
|
|
129
|
+
Ok(n)
|
|
128
130
|
|
|
129
131
|
# Handling errors with `match`: one arm per case (or `_`), and every arm
|
|
130
132
|
# produces the same type.
|
|
@@ -159,8 +161,9 @@ struct Neg(value: Int)
|
|
|
159
161
|
|
|
160
162
|
fun checkNonNeg(n: Int) -> Result[Unit, Neg] =
|
|
161
163
|
if n < 0
|
|
162
|
-
|
|
164
|
+
Err(Neg(value: n))
|
|
165
|
+
else
|
|
163
|
-
|
|
166
|
+
Ok(Unit)
|
|
164
167
|
|
|
165
168
|
fun passCheck(n: Int) -> Result[Unit, Neg] =
|
|
166
169
|
checkNonNeg(n)
|
|
@@ -171,28 +174,33 @@ fun runChecked(n: Int) -> Result[Int, Neg] =
|
|
|
171
174
|
|
|
172
175
|
fun negOrSeven(n: Int) -> Result[Int, Neg] =
|
|
173
176
|
if n < 0
|
|
174
|
-
|
|
177
|
+
Err(Neg(value: n))
|
|
178
|
+
else
|
|
175
|
-
|
|
179
|
+
Ok(n)
|
|
176
180
|
|
|
177
181
|
fun lookup(m: Map[Int, Result[Int, Neg]]) -> Int =
|
|
178
182
|
v := m.get(1).unwrapOr(negOrSeven(-1))
|
|
179
183
|
v.unwrapOr(0)
|
|
180
184
|
|
|
181
|
-
# `?` on a type parameter's static method, bound with `:=`
|
|
185
|
+
# `?` on a type parameter's static method, bound with `:=` in a recursive helper:
|
|
182
|
-
# operand's type is `Result[T, _]`, so it is checked per instantiation (here
|
|
186
|
+
# the operand's type is `Result[T, _]`, so it is checked per instantiation (here
|
|
183
187
|
# `T = Age`).
|
|
184
188
|
struct Age(n: Int) =
|
|
185
189
|
fun parse(s: Str) -> Result[Age, PositiveError] =
|
|
186
190
|
n := parsePositive(s)?
|
|
187
|
-
|
|
191
|
+
Ok(Age(n))
|
|
188
192
|
|
|
189
193
|
struct Ages[T](items: Slice[T]) =
|
|
190
194
|
fun parseAll(xs: Slice[Str]) -> Result[Ages[T], PositiveError] =
|
|
195
|
+
Ages.parseFrom(xs, 0, Slice[T]())
|
|
196
|
+
|
|
197
|
+
fun parseFrom(xs: Slice[Str], i: Int, out: Slice[T]) -> Result[Ages[T], PositiveError] =
|
|
191
|
-
|
|
198
|
+
if i == xs.length()
|
|
192
|
-
|
|
199
|
+
Ok(Ages(out))
|
|
200
|
+
else
|
|
193
|
-
v := T.parse(
|
|
201
|
+
v := T.parse(xs.at(i))?
|
|
194
202
|
out.push(v)
|
|
195
|
-
|
|
203
|
+
Ages.parseFrom(xs, i + 1, out)
|
|
196
204
|
|
|
197
205
|
fun parseAges(xs: Slice[Str]) -> Result[Ages[Age], PositiveError] = Ages.parseAll(xs)
|
|
198
206
|
|
|
@@ -200,7 +208,8 @@ fun parseAges(xs: Slice[Str]) -> Result[Ages[Age], PositiveError] = Ages.parseAl
|
|
|
200
208
|
|
|
201
209
|
# `Option[T]` is `Some(value)` or `None`. A present value is always written
|
|
202
210
|
# `Some(v)`. An option is handled with `unwrapOr(d)`, `if Some(x) := opt`,
|
|
211
|
+
# `while Some(x) := opt` (the body moves on through a `mut` field), `opt!`
|
|
203
|
-
#
|
|
212
|
+
# (crashes on `None`), `isSome()` and `isNone()`
|
|
204
213
|
# (`==` on an Option is an error: it would compare identity, not contents).
|
|
205
214
|
#
|
|
206
215
|
# `opt.map(f)` reaches into an option without unwrapping it and passes `None`
|
|
@@ -213,13 +222,15 @@ struct Holder(mut item: Option[Box], mut count: Option[Int])
|
|
|
213
222
|
|
|
214
223
|
fun maybeBox(c: Bool) -> Option[Box] =
|
|
215
224
|
if c
|
|
216
|
-
|
|
225
|
+
Some(Box(label: "in"))
|
|
226
|
+
else
|
|
217
|
-
|
|
227
|
+
None
|
|
218
228
|
|
|
219
229
|
fun maybeInt(c: Bool) -> Option[Int] =
|
|
220
230
|
if c
|
|
221
|
-
|
|
231
|
+
Some(7)
|
|
232
|
+
else
|
|
222
|
-
|
|
233
|
+
None
|
|
223
234
|
|
|
224
235
|
fun orFive(n: Option[Int] = None) -> Int =
|
|
225
236
|
n.unwrapOr(5)
|
|
@@ -239,10 +250,11 @@ fun chainDefault(a: Option[Int], b: Option[Int]) -> Int =
|
|
|
239
250
|
# A Result whose success value is an Option.
|
|
240
251
|
fun fallible(n: Int) -> Result[Option[Int], Str] =
|
|
241
252
|
if n < 0
|
|
242
|
-
|
|
253
|
+
Err("bad")
|
|
243
|
-
if n == 0
|
|
254
|
+
else if n == 0
|
|
244
|
-
|
|
255
|
+
Ok(None)
|
|
256
|
+
else
|
|
245
|
-
|
|
257
|
+
Ok(Some(n))
|
|
246
258
|
|
|
247
259
|
fun viaQuestion(n: Int) -> Result[Int, Str] =
|
|
248
260
|
v := fallible(n)?
|
|
@@ -251,6 +263,8 @@ fun viaQuestion(n: Int) -> Result[Int, Str] =
|
|
|
251
263
|
fun nextItem(items: Slice[Int], i: Int) -> Option[Int] =
|
|
252
264
|
items.get(i)
|
|
253
265
|
|
|
266
|
+
struct Walk(mut total: Int, mut i: Int)
|
|
267
|
+
|
|
254
268
|
fun sparse() -> Slice[Option[Int]] =
|
|
255
269
|
xs: List[Option[Int]] := [Some(1), None, Some(3)]
|
|
256
270
|
xs.toSlice()
|
|
@@ -277,16 +291,16 @@ fun somePerson() -> Option[Person] =
|
|
|
277
291
|
Some(Person(name: "Ann", age: 3))
|
|
278
292
|
|
|
279
293
|
fun noPerson() -> Option[Person] =
|
|
280
|
-
|
|
294
|
+
None
|
|
281
295
|
|
|
282
296
|
fun noTarget() -> Option[Target] =
|
|
283
|
-
|
|
297
|
+
None
|
|
284
298
|
|
|
285
299
|
fun maybeCount(n: Option[Int] = None) -> Option[Int] =
|
|
286
300
|
n
|
|
287
301
|
|
|
288
302
|
fun noUser() -> Option[User] =
|
|
289
|
-
|
|
303
|
+
None
|
|
290
304
|
|
|
291
305
|
fun targetName(u: Option[User]) -> Option[Str] =
|
|
292
306
|
u.andThen(|v| v.target).map(|t| t.name)
|
|
@@ -315,7 +329,7 @@ test "mapErr maps the error to another type"
|
|
|
315
329
|
else
|
|
316
330
|
assert false
|
|
317
331
|
|
|
318
|
-
test "question mark binds a declaration from a type parameter's static method in a
|
|
332
|
+
test "question mark binds a declaration from a type parameter's static method in a recursive helper"
|
|
319
333
|
ages := parseAges(["3", "4"].toSlice())!
|
|
320
334
|
assert ages.items.length() == 2
|
|
321
335
|
assert ages.items.get(1)!.n == 4
|
|
@@ -380,13 +394,16 @@ test "a list of results nests one level per access"
|
|
|
380
394
|
test "a Unit result propagates with question mark and is handled with if Err"
|
|
381
395
|
assert runChecked(4).unwrapOr(0) == 4
|
|
382
396
|
assert runChecked(-1).unwrapOr(5) == 5
|
|
383
|
-
hits := 0
|
|
384
|
-
if Err(e) := passCheck(1)
|
|
397
|
+
passing := if Err(e) := passCheck(1)
|
|
398
|
+
1
|
|
385
|
-
|
|
399
|
+
else
|
|
400
|
+
0
|
|
386
|
-
if Err(e) := passCheck(-1)
|
|
401
|
+
failing := if Err(e) := passCheck(-1)
|
|
387
402
|
assert e.value == -1
|
|
403
|
+
10
|
|
388
|
-
|
|
404
|
+
else
|
|
405
|
+
0
|
|
389
|
-
assert
|
|
406
|
+
assert passing + failing == 10
|
|
390
407
|
|
|
391
408
|
test "a Map can hold results as values"
|
|
392
409
|
m: Map[Int, Result[Int, Neg]] := Map.empty()
|
|
@@ -435,17 +452,16 @@ test "unwrapOr_chain"
|
|
|
435
452
|
assert chainDefault(Some(1), Some(2)) == 1
|
|
436
453
|
|
|
437
454
|
test "if_bind_option"
|
|
438
|
-
r := 0
|
|
439
|
-
if Some(x) := maybeInt(true)
|
|
455
|
+
present := if Some(x) := maybeInt(true)
|
|
440
|
-
|
|
456
|
+
x
|
|
441
457
|
else
|
|
442
|
-
|
|
458
|
+
-1
|
|
443
|
-
assert
|
|
459
|
+
assert present == 7
|
|
444
|
-
if Some(y) := maybeInt(false)
|
|
460
|
+
absent := if Some(y) := maybeInt(false)
|
|
445
|
-
|
|
461
|
+
y
|
|
446
462
|
else
|
|
447
|
-
|
|
463
|
+
-1
|
|
448
|
-
assert
|
|
464
|
+
assert absent == -1
|
|
449
465
|
assert {if Some(v) := maybeInt(true) then v + 1 else 0} == 8
|
|
450
466
|
assert {if Some(w) := maybeInt(false) then w + 1 else 0} == 0
|
|
451
467
|
if Some(b) := maybeBox(true)
|
|
@@ -453,12 +469,11 @@ test "if_bind_option"
|
|
|
453
469
|
|
|
454
470
|
test "while_bind_option"
|
|
455
471
|
items := [3, 2, 1].toSlice()
|
|
456
|
-
|
|
472
|
+
walk := Walk(total: 0, i: 0)
|
|
457
|
-
i := 0
|
|
458
|
-
while Some(v) := nextItem(items, i)
|
|
473
|
+
while Some(v) := nextItem(items, walk.i)
|
|
459
|
-
total = total + v
|
|
474
|
+
walk.total = walk.total + v
|
|
460
|
-
i = i + 1
|
|
475
|
+
walk.i = walk.i + 1
|
|
461
|
-
assert total == 6
|
|
476
|
+
assert walk.total == 6
|
|
462
477
|
|
|
463
478
|
test "map_over_option_flattens_with_andThen"
|
|
464
479
|
assert maybeBox(true).map(|b| b.label)! == "in"
|
|
@@ -484,14 +499,12 @@ test "int_min_literal"
|
|
|
484
499
|
assert Int.Min == -0x8000_0000_0000_0000
|
|
485
500
|
assert sumBig() == -1
|
|
486
501
|
|
|
487
|
-
test "
|
|
502
|
+
test "an option local or field holding None or a value"
|
|
488
|
-
o :=
|
|
503
|
+
o: Option[Int] := None
|
|
489
|
-
o = None
|
|
490
504
|
assert o.isNone()
|
|
491
|
-
|
|
505
|
+
o4: Option[Int] := Some(4)
|
|
492
|
-
assert
|
|
506
|
+
assert o4! == 4
|
|
493
|
-
b :=
|
|
507
|
+
b: Option[Box] := None
|
|
494
|
-
b = None
|
|
495
508
|
assert b.isNone()
|
|
496
509
|
h := Holder(item: Some(Box(label: "x")), count: Some(2))
|
|
497
510
|
h.count = None
|
examples/functional.plum
CHANGED
|
@@ -367,26 +367,28 @@ fun demoScene() -> Node =
|
|
|
367
367
|
fun sumEvensMethods(xs: Chain[Int]) -> Int =
|
|
368
368
|
xs.filter(|x| x % 2 == 0).foldl(0, |a, x| a + x)
|
|
369
369
|
|
|
370
|
+
# A loop body acts through a `mut` field, so that's where a running total lives.
|
|
371
|
+
struct EvenSum(mut total: Int, mut rest: Chain[Int])
|
|
372
|
+
|
|
373
|
+
struct Built(mut items: Chain[Int])
|
|
374
|
+
|
|
370
375
|
# 2. A `for` over the list (it has `next`), with an `if` in the body as the filter.
|
|
371
376
|
fun sumEvensFor(xs: Chain[Int]) -> Int =
|
|
372
|
-
|
|
377
|
+
sum := EvenSum(total: 0, rest: xs)
|
|
373
378
|
for x := range xs
|
|
374
379
|
if x % 2 == 0
|
|
375
|
-
total = total + x
|
|
380
|
+
sum.total = sum.total + x
|
|
376
|
-
total
|
|
381
|
+
sum.total
|
|
377
382
|
|
|
378
|
-
# 3. A `while` loop
|
|
383
|
+
# 3. A `while` loop whose cursor is a `mut` field, taking the list apart with a
|
|
384
|
+
# pattern. It stops when the pattern no longer matches.
|
|
379
385
|
fun sumEvensWhile(xs: Chain[Int]) -> Int =
|
|
380
|
-
|
|
386
|
+
sum := EvenSum(total: 0, rest: xs)
|
|
381
|
-
cur := xs
|
|
382
|
-
while true
|
|
383
|
-
|
|
387
|
+
while Link(h, t) := sum.rest
|
|
384
|
-
|
|
388
|
+
if h % 2 == 0
|
|
385
|
-
|
|
389
|
+
sum.total = sum.total + h
|
|
386
|
-
|
|
390
|
+
sum.rest = t
|
|
387
|
-
else
|
|
388
|
-
break
|
|
389
|
-
total
|
|
391
|
+
sum.total
|
|
390
392
|
|
|
391
393
|
# Tail recursive: the recursive call is the last thing done, so it is a jump.
|
|
392
394
|
fun countUp(xs: Chain[Int], n: Int) -> Int = match xs
|
|
@@ -541,10 +543,10 @@ test "loops build lists too"
|
|
|
541
543
|
seen := Slice()
|
|
542
544
|
xs.each(|x| seen.push(x * 2))
|
|
543
545
|
assert seen.length() == 4
|
|
544
|
-
out
|
|
546
|
+
out := Built(items: Empty)
|
|
545
547
|
for x := range xs
|
|
546
548
|
if x > 1
|
|
547
|
-
out = cons(x * 2, out)
|
|
549
|
+
out.items = cons(x * 2, out.items)
|
|
548
|
-
doubled := out.reverse()
|
|
550
|
+
doubled := out.items.reverse()
|
|
549
551
|
assert joinInts(doubled, ",") == joinInts(xs.filter(|x| x > 1).map(|x| x * 2), ",")
|
|
550
552
|
assert joinInts(doubled, ",") == "4,6,8"
|
examples/games/game_loop.plum
CHANGED
|
@@ -29,31 +29,33 @@ struct Stats(spawned: Int, removed: Int)
|
|
|
29
29
|
# Move every particle one step; remove the dead by swap-remove (the last
|
|
30
30
|
# particle takes the freed slot, so the list never shifts). Returns how many died.
|
|
31
31
|
fun step(ps: Slice[Particle], dt: Float) -> Int =
|
|
32
|
-
|
|
32
|
+
stepFrom(ps, dt, 0, 0)
|
|
33
|
+
|
|
33
|
-
|
|
34
|
+
# `step` from slot `i` on, with `removed` dead so far.
|
|
35
|
+
fun stepFrom(ps: Slice[Particle], dt: Float, i: Int, removed: Int) -> Int =
|
|
34
|
-
|
|
36
|
+
if i >= ps.length()
|
|
37
|
+
removed
|
|
38
|
+
else
|
|
35
39
|
p := ps.at(i)
|
|
36
|
-
|
|
40
|
+
moved := Particle(..p, pos: p.pos.add(p.vel.mul(dt)), life: p.life - 1)
|
|
37
|
-
p.life = p.life - 1
|
|
38
|
-
if
|
|
41
|
+
if moved.life <= 0
|
|
39
42
|
last := ps.at(ps.length() - 1)
|
|
40
43
|
_ := ps.set(i, last)!
|
|
41
44
|
_ := ps.pop()!
|
|
42
|
-
|
|
45
|
+
stepFrom(ps, dt, i, removed + 1)
|
|
43
46
|
else
|
|
44
|
-
_ := ps.set(i,
|
|
47
|
+
_ := ps.set(i, moved)!
|
|
45
|
-
|
|
48
|
+
stepFrom(ps, dt, i + 1, removed)
|
|
46
|
-
removed
|
|
47
49
|
|
|
48
50
|
fun run(frames: Int, ps: Slice[Particle]) -> Stats =
|
|
49
|
-
spawned := 0
|
|
50
|
-
removed := 0
|
|
51
|
-
|
|
51
|
+
0.until(frames).fold(Stats(spawned: 0, removed: 0), |stats, frame|
|
|
52
|
-
if frame % 10 == 0
|
|
52
|
+
spawned := if frame % 10 == 0
|
|
53
53
|
ps.push(Particle(pos: Vec2(x: 0.0, y: 0.0), vel: Vec2(x: 2.0, y: 1.0), life: 25))
|
|
54
|
-
spawned
|
|
54
|
+
stats.spawned + 1
|
|
55
|
+
else
|
|
55
|
-
|
|
56
|
+
stats.spawned
|
|
56
|
-
|
|
57
|
+
Stats(spawned: spawned, removed: stats.removed + step(ps, 0.5))
|
|
58
|
+
)
|
|
57
59
|
|
|
58
60
|
fun handle(world: Map[Int, Entity], e: Event) =
|
|
59
61
|
match e
|
|
@@ -75,11 +77,8 @@ test "100 frames: particles spawn, move and are swap-removed when dead"
|
|
|
75
77
|
test "survivors have moved and aged the expected amount"
|
|
76
78
|
ps := Slice[Particle]()
|
|
77
79
|
_ := run(100, ps)
|
|
78
|
-
x := 0.0
|
|
79
|
-
life := 0
|
|
80
|
-
for p := range ps
|
|
81
|
-
|
|
80
|
+
x := 0.until(ps.length()).fold(0.0, |total, i| total + ps.at(i).pos.x)
|
|
82
|
-
|
|
81
|
+
life := 0.until(ps.length()).fold(0, |total, i| total + ps.at(i).life)
|
|
83
82
|
# spawned on frames 80 and 90: 20 and 10 steps of +1.0, 5 and 15 life left
|
|
84
83
|
assert x == 30.0
|
|
85
84
|
assert life == 20
|
examples/games/kids/balloon_pop.plum
CHANGED
|
@@ -165,9 +165,10 @@ struct State(balloons: List[Balloon], rng: Random, pops: Int, clock: Float, part
|
|
|
165
165
|
# Balloons `i` and up, spread out below the screen.
|
|
166
166
|
fun fill(self, i: Int) -> State =
|
|
167
167
|
if i >= Count
|
|
168
|
-
|
|
168
|
+
self
|
|
169
|
+
else
|
|
169
|
-
|
|
170
|
+
sp := Balloon.spawn(BalloonId + i, self.rng, Float(i) * 110.0 - ArenaH * 0.6)
|
|
170
|
-
|
|
171
|
+
State(..self, balloons: self.balloons.append([sp.balloon]), rng: sp.rng).fill(i + 1)
|
|
171
172
|
|
|
172
173
|
fun update(self, msg: Msg) -> State = match msg
|
|
173
174
|
Tick(dt) => self.tick(dt)
|
|
@@ -182,21 +183,23 @@ struct State(balloons: List[Balloon], rng: Random, pops: Int, clock: Float, part
|
|
|
182
183
|
# A balloon that is gone comes back at the bottom with a new colour.
|
|
183
184
|
fun renew(self, b: Balloon) -> State =
|
|
184
185
|
if !b.done(self.clock)
|
|
185
|
-
|
|
186
|
+
self
|
|
187
|
+
else
|
|
186
|
-
|
|
188
|
+
sp := Balloon.spawn(b.id, self.rng, 0.0)
|
|
187
|
-
|
|
189
|
+
State(..self, balloons: self.balloons.map(|x| if x.id == b.id then sp.balloon else x), rng: sp.rng)
|
|
188
190
|
|
|
189
191
|
fun pop(self, id: Int) -> State =
|
|
190
192
|
hit := self.balloons.fold(false, |found, b| found || {b.id == id && !b.popped()})
|
|
191
193
|
if !hit
|
|
192
|
-
|
|
194
|
+
self
|
|
195
|
+
else
|
|
193
|
-
|
|
196
|
+
pops := self.pops + 1
|
|
194
|
-
|
|
197
|
+
State(
|
|
195
|
-
|
|
198
|
+
..self,
|
|
196
|
-
|
|
199
|
+
pops: pops,
|
|
197
|
-
|
|
200
|
+
partyAt: if pops % PartyEvery == 0 then self.clock else self.partyAt,
|
|
198
|
-
|
|
201
|
+
balloons: self.balloons.map(|b| if b.id == id then Balloon(..b, x: b.swayX(self.clock), poppedAt: self.clock) else b)
|
|
199
|
-
|
|
202
|
+
)
|
|
200
203
|
|
|
201
204
|
fun partying(self) -> Bool =
|
|
202
205
|
self.clock - self.partyAt < PartyTime
|
|
@@ -210,8 +213,11 @@ struct State(balloons: List[Balloon], rng: Random, pops: Int, clock: Float, part
|
|
|
210
213
|
view := self.render()
|
|
211
214
|
if Some(id) := view.hitId(x, y)
|
|
212
215
|
if Some(msg) := view.msgOf(id)
|
|
213
|
-
|
|
216
|
+
self.update(msg)
|
|
217
|
+
else
|
|
214
|
-
|
|
218
|
+
self
|
|
219
|
+
else
|
|
220
|
+
self
|
|
215
221
|
|
|
216
222
|
# --- render ---
|
|
217
223
|
|
|
@@ -248,22 +254,23 @@ struct State(balloons: List[Balloon], rng: Random, pops: Int, clock: Float, part
|
|
|
248
254
|
|
|
249
255
|
fun renderParty(self) -> SceneNode[Msg] =
|
|
250
256
|
if !self.partying()
|
|
251
|
-
|
|
257
|
+
nothing()
|
|
258
|
+
else
|
|
252
|
-
|
|
259
|
+
age := self.clock - self.partyAt
|
|
253
|
-
|
|
260
|
+
p := Anim.at(partyAnim(), age)
|
|
254
|
-
|
|
261
|
+
Group(
|
|
255
|
-
|
|
262
|
+
transform: Identity,
|
|
256
|
-
|
|
263
|
+
children: [
|
|
257
|
-
|
|
264
|
+
Group(transform: Identity, children: confetti(0, age)),
|
|
258
|
-
|
|
265
|
+
Group(
|
|
259
|
-
|
|
266
|
+
transform: Compose(first: Translate(dx: ArenaW / 2.0, dy: 250.0), then: Scale(sx: p.scaleX, sy: p.scaleY)),
|
|
260
|
-
|
|
267
|
+
children: [
|
|
261
|
-
|
|
268
|
+
centred("Hooray!", 0.0, -40.0, 72.0, Color.rgb(0.95, 0.3, 0.5)),
|
|
262
|
-
|
|
269
|
+
centred("{self.pops} balloons!", 0.0, 40.0, 40.0, Color.rgb(0.15, 0.15, 0.3))
|
|
263
|
-
|
|
270
|
+
]
|
|
264
|
-
|
|
271
|
+
)
|
|
265
|
-
|
|
272
|
+
]
|
|
266
|
-
|
|
273
|
+
)
|
|
267
274
|
|
|
268
275
|
# 30 confetti squares falling from the top, `age` seconds into the party.
|
|
269
276
|
fun confetti(i: Int, age: Float) -> List[SceneNode[Msg]] =
|
|
@@ -312,11 +319,9 @@ struct Balloons(state: State, input: Input, popSound: Sound, partySound: Sound,
|
|
|
312
319
|
# pop and a fanfare when a party starts. Returns the game with the next state.
|
|
313
320
|
fun update(self, io: Io, dt: Float) -> Balloons =
|
|
314
321
|
input := self.input.poll(io)
|
|
315
|
-
|
|
322
|
+
before := self.state.update(Resize(w: gfx.screenWidth(io), h: gfx.screenHeight(io)))
|
|
316
|
-
before := s
|
|
317
323
|
inputs := input.events
|
|
318
|
-
for i := range inputs.length()
|
|
319
|
-
|
|
324
|
+
s := 0.until(inputs.length()).fold(before, |current, i| current.update(Event(input: inputs.at(i))))
|
|
320
325
|
if s.pops > before.pops
|
|
321
326
|
self.popSound.play(io, 0.9)
|
|
322
327
|
if s.partyAt != before.partyAt
|
|
@@ -382,9 +387,8 @@ test "a popped balloon comes back at the bottom"
|
|
|
382
387
|
assert first(t).y > ArenaH
|
|
383
388
|
|
|
384
389
|
test "every tenth pop starts a party that ends"
|
|
385
|
-
|
|
390
|
+
fresh := newState()
|
|
386
|
-
for i := range 9
|
|
387
|
-
|
|
391
|
+
s := State(..fresh, pops: 9)
|
|
388
392
|
assert !s.partying()
|
|
389
393
|
t := withCentred(s).update(Pop(id: BalloonId))
|
|
390
394
|
assert t.pops == 10
|
examples/games/orb_run.plum
CHANGED
|
@@ -95,25 +95,24 @@ fun overlaps(ax: Float, ay: Float, ra: Float, bx: Float, by: Float, rb: Float) -
|
|
|
95
95
|
|
|
96
96
|
# A random spot at least `gap` away from (ax, ay), drifting at up to `speed`.
|
|
97
97
|
fun spawn(rng: Random, ax: Float, ay: Float, gap: Float, speed: Float) -> Spawned =
|
|
98
|
-
margin := 30.0
|
|
99
|
-
r := rng
|
|
100
|
-
x := 0.0
|
|
101
|
-
y := 0.0
|
|
102
|
-
tries := 0
|
|
103
|
-
while tries < 20
|
|
104
|
-
|
|
98
|
+
spot := findSpot(rng, ax, ay, gap, 20)
|
|
105
|
-
|
|
99
|
+
dh := spot.rng.nextFloat()
|
|
106
|
-
r = dy.rng
|
|
107
|
-
x = margin + dx.value * {ArenaW - 2.0 * margin}
|
|
108
|
-
y = margin + dy.value * {ArenaH - 2.0 * margin}
|
|
109
|
-
if math.hypot(x - ax, y - ay) >= gap
|
|
110
|
-
break
|
|
111
|
-
tries = tries + 1
|
|
112
|
-
dh := r.nextFloat()
|
|
113
100
|
dp := dh.rng.nextFloat()
|
|
114
101
|
heading := dh.value * Float.Tau
|
|
115
102
|
pace := speed * {0.4 + 0.6 * dp.value}
|
|
116
|
-
Spawned(at: Motion(x: x, y: y, vx: math.cos(heading) * pace, vy: math.sin(heading) * pace), rng: dp.rng)
|
|
103
|
+
Spawned(at: Motion(x: spot.at.x, y: spot.at.y, vx: math.cos(heading) * pace, vy: math.sin(heading) * pace), rng: dp.rng)
|
|
104
|
+
|
|
105
|
+
# A random spot at least `gap` away from (ax, ay); after `tries` draws the last one stands.
|
|
106
|
+
fun findSpot(rng: Random, ax: Float, ay: Float, gap: Float, tries: Int) -> Spawned =
|
|
107
|
+
margin := 30.0
|
|
108
|
+
dx := rng.nextFloat()
|
|
109
|
+
dy := dx.rng.nextFloat()
|
|
110
|
+
x := margin + dx.value * {ArenaW - 2.0 * margin}
|
|
111
|
+
y := margin + dy.value * {ArenaH - 2.0 * margin}
|
|
112
|
+
if tries <= 1 || math.hypot(x - ax, y - ay) >= gap
|
|
113
|
+
Spawned(at: Motion(x: x, y: y, vx: 0.0, vy: 0.0), rng: dy.rng)
|
|
114
|
+
else
|
|
115
|
+
findSpot(dy.rng, ax, ay, gap, tries - 1)
|
|
117
116
|
|
|
118
117
|
# `b` moved for `dt` seconds and bounced off the arena walls; `b` itself is unchanged.
|
|
119
118
|
fun driftOf(b: Motion, r: Float, dt: Float) -> Motion =
|
|
@@ -410,10 +409,11 @@ struct State(
|
|
|
410
409
|
fun onInput(self, e: InputEvent) -> State =
|
|
411
410
|
s := State(..self, held: self.held.after(e))
|
|
412
411
|
if e.isPointer()
|
|
413
|
-
|
|
412
|
+
s.feed(e)
|
|
414
|
-
if s.over && isRestartKey(e)
|
|
413
|
+
else if s.over && isRestartKey(e)
|
|
415
|
-
|
|
414
|
+
s.restarted(s.rng.nextInt().value)
|
|
415
|
+
else
|
|
416
|
-
|
|
416
|
+
s
|
|
417
417
|
|
|
418
418
|
# The steering axes from the held arrow or WASD keys.
|
|
419
419
|
fun axisX(self) -> Float =
|
|
@@ -428,11 +428,12 @@ struct State(
|
|
|
428
428
|
fun feed(self, input: InputEvent) -> State =
|
|
429
429
|
po := input.position()
|
|
430
430
|
if po.isNone()
|
|
431
|
-
|
|
431
|
+
self
|
|
432
|
+
else
|
|
432
|
-
|
|
433
|
+
pos := po.unwrap()
|
|
433
|
-
|
|
434
|
+
view := self.render()
|
|
434
|
-
|
|
435
|
+
r := ui.step(self.ui, input, view.hitId(pos.x, pos.y))
|
|
435
|
-
|
|
436
|
+
ui.messages(view, r.events).fold(State(..self, ui: r.ui), |s, m| s.update(m))
|
|
436
437
|
|
|
437
438
|
# --- render ---
|
|
438
439
|
|
|
@@ -534,10 +535,8 @@ struct OrbGame(state: State, input: Input, pickupSound: Sound, hitSound: Sound,
|
|
|
534
535
|
fun update(self, io: Io, dt: Float) -> OrbGame =
|
|
535
536
|
polled := self.input.poll(io)
|
|
536
537
|
s := self.state.update(Resize(w: gfx.screenWidth(io), h: gfx.screenHeight(io)))
|
|
537
|
-
clicked := s
|
|
538
538
|
inputs := polled.events
|
|
539
|
-
for i := range inputs.length()
|
|
540
|
-
|
|
539
|
+
clicked := 0.until(inputs.length()).fold(s, |current, i| current.update(Event(input: inputs.at(i))))
|
|
541
540
|
if s.over && !clicked.over
|
|
542
541
|
self.startMusic(io)
|
|
543
542
|
OrbGame(..self, state: clicked, input: polled)
|
|
@@ -589,6 +588,13 @@ fun near(a: Float, b: Float) -> Bool =
|
|
|
589
588
|
fun tickOf(s: State, ax: Float, ay: Float, dt: Float) -> State =
|
|
590
589
|
s.update(Tick(dt: dt, ax: ax, ay: ay))
|
|
591
590
|
|
|
591
|
+
# `n` ticks in a row with the same steering.
|
|
592
|
+
fun ticksOf(s: State, n: Int, ax: Float, ay: Float, dt: Float) -> State =
|
|
593
|
+
0.until(n).fold(s, |current, _| tickOf(current, ax, ay, dt))
|
|
594
|
+
|
|
595
|
+
# How many frames the ship was hidden and shown while ticking `state`.
|
|
596
|
+
struct Blinks(state: State, hidden: Int, shown: Int)
|
|
597
|
+
|
|
592
598
|
fun away() -> Motion =
|
|
593
599
|
Motion(x: 5000.0, y: 0.0, vx: 0.0, vy: 0.0)
|
|
594
600
|
|
|
@@ -626,15 +632,12 @@ test "steering accelerates and drag slows the ship"
|
|
|
626
632
|
assert s.vx > 0.0
|
|
627
633
|
assert s.x > ArenaW / 2.0
|
|
628
634
|
fast := s.vx
|
|
629
|
-
t := s
|
|
630
|
-
for i := range 30
|
|
631
|
-
|
|
635
|
+
t := 0.until(30).fold(s, |current, _| current.update(0.0, 0.0, 0.1))
|
|
632
636
|
assert t.vx < fast / 4.0
|
|
633
637
|
|
|
634
638
|
test "the ship stops at the arena walls"
|
|
635
|
-
|
|
639
|
+
start := Ship(x: 20.0, y: 20.0, vx: 0.0, vy: 0.0, rotation: 0.0, hurt: 0.0)
|
|
636
|
-
for i := range 20
|
|
637
|
-
|
|
640
|
+
s := 0.until(20).fold(start, |current, _| current.update(-1.0, -1.0, 0.1))
|
|
638
641
|
assert s.x == ShipRadius
|
|
639
642
|
assert s.y == ShipRadius
|
|
640
643
|
assert s.vx == 0.0
|
|
@@ -671,28 +674,28 @@ test "collecting an orb scores and respawns it elsewhere"
|
|
|
671
674
|
test "a mine costs one life and makes the ship flash"
|
|
672
675
|
e := emptyState()
|
|
673
676
|
s := withMine(e, 0, onShip(e))
|
|
674
|
-
|
|
677
|
+
hit := tickOf(s, 0.0, 0.0, 0.016)
|
|
675
|
-
assert
|
|
678
|
+
assert hit.lives == StartLives - 1
|
|
676
|
-
assert
|
|
679
|
+
assert hit.ship.hurt > 0.0
|
|
677
|
-
|
|
680
|
+
start := Blinks(state: withMine(hit, 0, away()), hidden: 0, shown: 0)
|
|
678
|
-
hidden := 0
|
|
679
|
-
shown := 0
|
|
680
|
-
|
|
681
|
+
blinks := 0.until(100).fold(start, |b, _|
|
|
681
|
-
|
|
682
|
+
next := tickOf(b.state, 0.0, 0.0, 0.016)
|
|
682
|
-
if countNodes(
|
|
683
|
+
if countNodes(next.renderShip()) == 1
|
|
683
|
-
|
|
684
|
+
Blinks(state: next, hidden: b.hidden + 1, shown: b.shown)
|
|
684
685
|
else
|
|
686
|
+
Blinks(state: next, hidden: b.hidden, shown: b.shown + 1)
|
|
687
|
+
)
|
|
685
|
-
|
|
688
|
+
t := blinks.state
|
|
686
|
-
assert hidden > 0
|
|
689
|
+
assert blinks.hidden > 0
|
|
687
|
-
assert shown > 0
|
|
690
|
+
assert blinks.shown > 0
|
|
688
691
|
assert t.ship.hurt == 0.0
|
|
689
692
|
assert countNodes(t.renderShip()) == 3
|
|
690
693
|
assert t.lives == StartLives - 1
|
|
691
694
|
|
|
692
695
|
test "the ship is briefly safe after a hit"
|
|
693
696
|
e := emptyState()
|
|
694
|
-
|
|
697
|
+
hit := tickOf(withMine(e, 0, onShip(e)), 0.0, 0.0, 0.016)
|
|
695
|
-
t = tickOf(
|
|
698
|
+
t := tickOf(hit, 0.0, 0.0, 0.016)
|
|
696
699
|
assert t.lives == StartLives - 1
|
|
697
700
|
|
|
698
701
|
test "the game ends at zero lives and then ignores steps"
|
|
@@ -710,11 +713,10 @@ test "the game ends at zero lives and then ignores steps"
|
|
|
710
713
|
test "after game over nothing moves but the animations keep running"
|
|
711
714
|
e := emptyState()
|
|
712
715
|
s := State(..e, lives: 1)
|
|
713
|
-
|
|
716
|
+
ended := tickOf(withMine(s, 0, Motion(x: s.ship.x, y: s.ship.y, vx: 10.0, vy: 0.0)), 0.0, 0.0, 0.016)
|
|
714
|
-
assert
|
|
717
|
+
assert ended.over
|
|
715
|
-
mine :=
|
|
718
|
+
mine := ended.mines.at(0)
|
|
716
|
-
for i := range 10
|
|
717
|
-
|
|
719
|
+
t := ticksOf(ended, 10, 1.0, 0.0, 0.1)
|
|
718
720
|
after := t.mines.at(0)
|
|
719
721
|
assert after.at.x == mine.at.x
|
|
720
722
|
assert after.rotation == mine.rotation
|
|
@@ -749,11 +751,8 @@ test "more mines appear as the score grows and restart removes them"
|
|
|
749
751
|
assert r.mines.length() == 3
|
|
750
752
|
|
|
751
753
|
test "the same seed plays out the same way"
|
|
752
|
-
a := newState(11)
|
|
753
|
-
b := newState(11)
|
|
754
|
-
for i := range 120
|
|
755
|
-
|
|
754
|
+
a := ticksOf(newState(11), 120, 1.0, -1.0, 0.016)
|
|
756
|
-
|
|
755
|
+
b := ticksOf(newState(11), 120, 1.0, -1.0, 0.016)
|
|
757
756
|
assert a.ship.x == b.ship.x
|
|
758
757
|
assert a.score == b.score
|
|
759
758
|
assert a.mines.at(0).at.x == b.mines.at(0).at.x
|
|
@@ -799,14 +798,12 @@ test "update_is_pure"
|
|
|
799
798
|
assert s.ship.x == x
|
|
800
799
|
|
|
801
800
|
test "freezing at game over keeps the orb and mine animation clocks"
|
|
802
|
-
t := emptyState()
|
|
803
|
-
for i := range 5
|
|
804
|
-
|
|
801
|
+
running := ticksOf(emptyState(), 5, 0.0, 0.0, 0.1)
|
|
805
|
-
orbClock :=
|
|
802
|
+
orbClock := running.orbs.at(0).clock
|
|
806
|
-
mineClock :=
|
|
803
|
+
mineClock := running.mines.at(0).clock
|
|
807
804
|
assert orbClock > 0.4
|
|
808
|
-
|
|
805
|
+
lastLife := State(..running, lives: 1)
|
|
809
|
-
t = tickOf(withMine(
|
|
806
|
+
t := tickOf(withMine(lastLife, 1, onShip(lastLife)), 0.0, 0.0, 0.1)
|
|
810
807
|
assert t.over
|
|
811
808
|
assert t.orbs.at(0).clock > orbClock
|
|
812
809
|
assert t.mines.at(0).clock > mineClock
|
examples/generics.plum
CHANGED
|
@@ -34,14 +34,14 @@ fun wrap[T](x: T) -> Result[T, GenError] =
|
|
|
34
34
|
|
|
35
35
|
fun viaQ[T](x: T) -> Result[T, GenError] =
|
|
36
36
|
y := wrap(x)?
|
|
37
|
-
|
|
37
|
+
Ok(y)
|
|
38
38
|
|
|
39
39
|
fun failing() -> Result[Int, GenError] =
|
|
40
|
-
|
|
40
|
+
Err(Boom)
|
|
41
41
|
|
|
42
42
|
fun viaQFailing() -> Result[Int, GenError] =
|
|
43
43
|
y := failing()?
|
|
44
|
-
|
|
44
|
+
Ok(y)
|
|
45
45
|
|
|
46
46
|
fun pick[T](x: T, f: fn(T) -> T) -> T =
|
|
47
47
|
f(x)
|
|
@@ -54,7 +54,7 @@ fun readDeep(outer: Slice[Slice[Map[Str, Int]]], r: Int, c: Int, key: Str) -> Re
|
|
|
54
54
|
row := outer.get(r).okOr(OutOfBounds)?
|
|
55
55
|
m := row.get(c).okOr(OutOfBounds)?
|
|
56
56
|
v := m.get(key).okOr(KeyNotFound)?
|
|
57
|
-
|
|
57
|
+
Ok(v)
|
|
58
58
|
|
|
59
59
|
struct Circle(r: Float) =
|
|
60
60
|
|
|
@@ -84,10 +84,7 @@ fun nameOf[T](s: T) -> Str =
|
|
|
84
84
|
struct Num[T](add: fn(T, T) -> T, zero: T)
|
|
85
85
|
|
|
86
86
|
fun sumWith[T](xs: Slice[T], num: Num[T]) -> T =
|
|
87
|
-
acc := num.zero
|
|
88
|
-
for x := range xs
|
|
89
|
-
|
|
87
|
+
0.until(xs.length()).fold(num.zero, |acc, i| num.add(acc, xs.at(i)))
|
|
90
|
-
acc
|
|
91
88
|
|
|
92
89
|
# --- generic records and enums ----------------------------------------------
|
|
93
90
|
|
|
@@ -130,10 +127,9 @@ struct Crate[T](value: T) =
|
|
|
130
127
|
struct Chain[T](v: T, next: Option[Chain[T]]) =
|
|
131
128
|
|
|
132
129
|
fun last(self) -> T =
|
|
133
|
-
|
|
130
|
+
match self.next
|
|
134
|
-
|
|
131
|
+
Some(n) => n.last()
|
|
135
|
-
|
|
132
|
+
None => self.v
|
|
136
|
-
cur.v
|
|
137
133
|
|
|
138
134
|
enum Stash[T] =
|
|
139
135
|
| Stashed(v: T)
|
|
@@ -267,10 +263,9 @@ test "a duck-typed generic fn dispatches statically per instantiation"
|
|
|
267
263
|
shapes.push(Circle(r: 2.0))
|
|
268
264
|
shapes.push(Circle(r: 3.0))
|
|
269
265
|
names := Slice[Str]()
|
|
270
|
-
total := 0.0
|
|
271
266
|
for s := range shapes
|
|
272
267
|
names.push(s.name())
|
|
273
|
-
|
|
268
|
+
total := 0.until(shapes.length()).fold(0.0, |sum, i| sum + shapes.at(i).area())
|
|
274
269
|
assert names.join(",") == "circle,circle"
|
|
275
270
|
assert total == 39.0
|
|
276
271
|
|
examples/json.plum
CHANGED
|
@@ -17,7 +17,7 @@ struct House(name: Str, rooms: Int, tags: Slice[Str], annex: Slice[House]) =
|
|
|
17
17
|
rooms := Int.fromJson(j.field("rooms")?)?
|
|
18
18
|
tags := tagsOf(j.field("tags")?)?
|
|
19
19
|
annex := annexOf(j.field("annex")?)?
|
|
20
|
-
|
|
20
|
+
Ok(House(name: name, rooms: rooms, tags: tags, annex: annex))
|
|
21
21
|
|
|
22
22
|
fun tagsOf(j: Json) -> Result[Slice[Str], JsonError] = j.into()
|
|
23
23
|
|
examples/maps.plum
CHANGED
|
@@ -17,10 +17,8 @@ test "maps_mixed_pair_and_arrow"
|
|
|
17
17
|
|
|
18
18
|
test "Map[Int, MapPoint] struct values"
|
|
19
19
|
m: Map[Int, MapPoint] := Map.empty()
|
|
20
|
-
i := 0
|
|
21
|
-
|
|
20
|
+
for i := range 500
|
|
22
21
|
m.set(i, MapPoint(x: Float(i), y: Float(i) * 2.0))
|
|
23
|
-
i = i + 1
|
|
24
22
|
v := m.get(499)!
|
|
25
23
|
assert v.x == 499.0
|
|
26
24
|
assert v.y == 998.0
|
examples/match.plum
CHANGED
|
@@ -145,10 +145,11 @@ struct Soldier(hp: Int, shape: Shape)
|
|
|
145
145
|
|
|
146
146
|
fun area(s: Shape) -> Float =
|
|
147
147
|
if Circle(r) := s
|
|
148
|
-
|
|
148
|
+
3.0 * r * r
|
|
149
149
|
else if Rect(w, h) := s
|
|
150
|
-
|
|
150
|
+
w * h
|
|
151
|
+
else
|
|
151
|
-
|
|
152
|
+
0.0
|
|
152
153
|
|
|
153
154
|
fun wide(s: Shape) -> Str =
|
|
154
155
|
if Rect(w, h) := s, w > h
|
|
@@ -179,29 +180,32 @@ fun shapeOf(u: Soldier) -> Str =
|
|
|
179
180
|
else
|
|
180
181
|
"other"
|
|
181
182
|
|
|
183
|
+
struct JobRun(mut state: Job, mut steps: Int)
|
|
184
|
+
|
|
182
185
|
fun countDown(j: Job) -> Int =
|
|
183
|
-
steps := 0
|
|
184
|
-
|
|
186
|
+
run := JobRun(state: j, steps: 0)
|
|
185
|
-
while Busy(n) := state
|
|
187
|
+
while Busy(n) := run.state
|
|
186
|
-
steps = steps + 1
|
|
188
|
+
run.steps = run.steps + 1
|
|
187
189
|
if n > 1
|
|
188
|
-
state = Busy(left: n - 1)
|
|
190
|
+
run.state = Busy(left: n - 1)
|
|
189
191
|
else
|
|
190
|
-
state = Idle
|
|
192
|
+
run.state = Idle
|
|
191
|
-
steps
|
|
193
|
+
run.steps
|
|
192
194
|
|
|
193
195
|
fun sumXY(v: Vec2) -> Int =
|
|
194
196
|
if Vec2(x, y) := v
|
|
195
|
-
|
|
197
|
+
x + y
|
|
198
|
+
else
|
|
196
|
-
|
|
199
|
+
0
|
|
197
200
|
|
|
198
201
|
enum SmallError = TooBig | NotNum
|
|
199
202
|
|
|
200
203
|
fun parseSmall(s: Str) -> Result[Int, SmallError] =
|
|
201
204
|
n := Int.parse(s).mapErr(|e| NotNum)?
|
|
202
205
|
if n > 9
|
|
203
|
-
|
|
206
|
+
Err(TooBig)
|
|
207
|
+
else
|
|
204
|
-
|
|
208
|
+
Ok(n)
|
|
205
209
|
|
|
206
210
|
fun describe(s: Str) -> Str =
|
|
207
211
|
match parseSmall(s)
|
|
@@ -211,15 +215,17 @@ fun describe(s: Str) -> Str =
|
|
|
211
215
|
|
|
212
216
|
fun firstSome(a: Option[Int], b: Option[Int]) -> Int =
|
|
213
217
|
if Some(x) := a
|
|
214
|
-
|
|
218
|
+
x
|
|
215
219
|
else if Some(y) := b
|
|
216
|
-
|
|
220
|
+
y + 1
|
|
221
|
+
else
|
|
217
|
-
|
|
222
|
+
-1
|
|
218
223
|
|
|
219
224
|
fun bigOnly(a: Option[Int]) -> Int =
|
|
220
225
|
if Some(x) := a, x > 3
|
|
226
|
+
x
|
|
221
|
-
|
|
227
|
+
else
|
|
222
|
-
|
|
228
|
+
0
|
|
223
229
|
|
|
224
230
|
# ---- guards: `when <expr>` ----
|
|
225
231
|
|
examples/recursion.plum
CHANGED
|
@@ -52,6 +52,8 @@ fun dec(n: Int) -> Int =
|
|
|
52
52
|
|
|
53
53
|
struct Counter(n: Int, total: Int)
|
|
54
54
|
|
|
55
|
+
struct Tally(mut n: Int)
|
|
56
|
+
|
|
55
57
|
fun spin(c: Counter) -> Counter =
|
|
56
58
|
if c.n == 0 then c else spin(Counter(..c, n: c.n - 1, total: c.total + 2))
|
|
57
59
|
|
|
@@ -62,10 +64,11 @@ enum DownError = Neg
|
|
|
62
64
|
|
|
63
65
|
fun down(n: Int) -> Result[Int, DownError] =
|
|
64
66
|
if n < 0
|
|
65
|
-
|
|
67
|
+
Err(Neg)
|
|
66
|
-
if n == 0
|
|
68
|
+
else if n == 0
|
|
67
|
-
|
|
69
|
+
Ok(5)
|
|
70
|
+
else
|
|
68
|
-
|
|
71
|
+
down(n - 1)
|
|
69
72
|
|
|
70
73
|
# ==== cons list ====
|
|
71
74
|
|
|
@@ -173,14 +176,14 @@ test "reverse_append"
|
|
|
173
176
|
assert ys.equals([1, 2, 3, 4])
|
|
174
177
|
|
|
175
178
|
test "for_range"
|
|
176
|
-
total := 0
|
|
179
|
+
total := Tally(n: 0)
|
|
177
180
|
for x := range [1, 2, 3, 4]
|
|
178
|
-
total = total + x
|
|
181
|
+
total.n = total.n + x
|
|
179
|
-
assert total == 10
|
|
182
|
+
assert total.n == 10
|
|
180
|
-
big := 0
|
|
183
|
+
big := Tally(n: 0)
|
|
181
184
|
for x := range range(0, 1000000)
|
|
182
|
-
big = big + x
|
|
185
|
+
big.n = big.n + x
|
|
183
|
-
assert big == 499999500000
|
|
186
|
+
assert big.n == 499999500000
|
|
184
187
|
|
|
185
188
|
test "to_slice_round_trip"
|
|
186
189
|
s := [1, 2, 3].map(|x| x * 2).toSlice()
|
examples/sequences.plum
CHANGED
|
@@ -40,6 +40,9 @@ fun army() -> Slice[Soldier] =
|
|
|
40
40
|
Soldier(name: "f", team: 2, hp: 8.0, x: 1.0, y: 0.0),
|
|
41
41
|
].toSlice()
|
|
42
42
|
|
|
43
|
+
# A `for` body acts through a `mut` field, so that's where a running total lives.
|
|
44
|
+
struct Tally(mut n: Int)
|
|
45
|
+
|
|
43
46
|
# A user iterator: from, from - 1, ... 1.
|
|
44
47
|
struct Countdown(from: Int) =
|
|
45
48
|
|
|
@@ -51,86 +54,91 @@ struct TakeList[T](src: List[T], n: Int) =
|
|
|
51
54
|
|
|
52
55
|
fun next(self) -> Option[Pair[T, TakeList[T]]] =
|
|
53
56
|
if self.n <= 0
|
|
54
|
-
|
|
57
|
+
None
|
|
58
|
+
else
|
|
55
|
-
|
|
59
|
+
step := self.src.next()?
|
|
56
|
-
|
|
60
|
+
Some(Pair(first: step.first, second: TakeList(src: step.second, n: self.n - 1)))
|
|
57
61
|
|
|
58
62
|
# The 8 neighbours of (x, y), as one list of (x, y) pairs.
|
|
59
63
|
fun neighbours(x: Int, y: Int) -> List[Pair[Int, Int]] =
|
|
60
|
-
|
|
64
|
+
none: List[Pair[Int, Int]] := Nil
|
|
61
|
-
|
|
65
|
+
found := 0.until(3).fold(none, |cells, dx|
|
|
62
|
-
|
|
66
|
+
0.until(3).fold(cells, |row, dy|
|
|
63
67
|
if dx != 1 || dy != 1
|
|
64
|
-
|
|
68
|
+
Cons(head: Pair(first: x + dx - 1, second: y + dy - 1), tail: row)
|
|
69
|
+
else
|
|
70
|
+
row
|
|
71
|
+
)
|
|
72
|
+
)
|
|
65
|
-
|
|
73
|
+
found.reverse()
|
|
66
74
|
|
|
67
75
|
fun firstBig(xs: Slice[Int]) -> Int =
|
|
76
|
+
hit := 0.until(xs.length()).find(|i| xs.at(i) > 10)
|
|
77
|
+
match hit
|
|
68
|
-
|
|
78
|
+
Some(i) => xs.at(i)
|
|
69
|
-
|
|
79
|
+
None => -1
|
|
70
|
-
return x
|
|
71
|
-
return -1
|
|
72
80
|
|
|
73
81
|
# ==== iterator tests ====
|
|
74
82
|
|
|
75
83
|
test "a user type with next is a for source"
|
|
76
|
-
seen := 0
|
|
84
|
+
seen := Tally(n: 0)
|
|
77
85
|
for t := range Countdown(from: 4)
|
|
78
|
-
seen = seen * 10 + t
|
|
86
|
+
seen.n = seen.n * 10 + t
|
|
79
|
-
assert seen == 4321
|
|
87
|
+
assert seen.n == 4321
|
|
80
|
-
|
|
88
|
+
runs := Tally(n: 0)
|
|
81
89
|
for _ := range Countdown(from: 0)
|
|
82
|
-
n = n + 1
|
|
90
|
+
runs.n = runs.n + 1
|
|
83
|
-
assert n == 0
|
|
91
|
+
assert runs.n == 0
|
|
84
92
|
|
|
85
93
|
test "if and bind in the body over an iterator"
|
|
86
|
-
total := 0
|
|
94
|
+
total := Tally(n: 0)
|
|
87
95
|
for t := range Countdown(from: 6)
|
|
88
96
|
if t % 2 == 0
|
|
89
97
|
sq := t * t
|
|
90
|
-
total = total + sq
|
|
98
|
+
total.n = total.n + sq
|
|
91
|
-
assert total == 36 + 16 + 4
|
|
99
|
+
assert total.n == 36 + 16 + 4
|
|
92
100
|
|
|
93
101
|
test "a lazy adapter stops early"
|
|
94
|
-
|
|
102
|
+
seen := Tally(n: 0)
|
|
95
103
|
for x := range TakeList(src: [5, 6, 7, 8], n: 2)
|
|
96
|
-
n = n * 10 + x
|
|
104
|
+
seen.n = seen.n * 10 + x
|
|
97
|
-
assert n == 56
|
|
105
|
+
assert seen.n == 56
|
|
98
106
|
|
|
99
107
|
test "lists and ranges"
|
|
100
|
-
|
|
108
|
+
fromList := Tally(n: 0)
|
|
101
109
|
for x := range [1, 2, 3]
|
|
102
|
-
n = n * 10 + x
|
|
110
|
+
fromList.n = fromList.n * 10 + x
|
|
103
|
-
assert n == 123
|
|
111
|
+
assert fromList.n == 123
|
|
104
|
-
|
|
112
|
+
fromRange := Tally(n: 0)
|
|
105
113
|
for i := range 1.to(9).stepBy(4)
|
|
106
|
-
|
|
114
|
+
fromRange.n = fromRange.n * 10 + i
|
|
107
|
-
assert
|
|
115
|
+
assert fromRange.n == 159
|
|
108
116
|
|
|
109
117
|
test "neighbours: a list of pairs"
|
|
110
|
-
count := 0
|
|
118
|
+
count := Tally(n: 0)
|
|
111
|
-
sum := 0
|
|
119
|
+
sum := Tally(n: 0)
|
|
112
120
|
for p := range neighbours(5, 5)
|
|
113
121
|
if p.first > 5
|
|
114
|
-
count = count + 1
|
|
122
|
+
count.n = count.n + 1
|
|
115
|
-
sum = sum + p.second
|
|
123
|
+
sum.n = sum.n + p.second
|
|
116
|
-
assert count == 3
|
|
124
|
+
assert count.n == 3
|
|
117
|
-
assert sum == 15
|
|
125
|
+
assert sum.n == 15
|
|
118
126
|
|
|
119
127
|
test "Map entries, keys and values"
|
|
120
128
|
m := [1 => 10, 2 => 20, 3 => 30]
|
|
121
|
-
total := 0
|
|
129
|
+
total := Tally(n: 0)
|
|
122
130
|
for e := range m.entries()
|
|
123
|
-
total = total + e.first * e.second
|
|
131
|
+
total.n = total.n + e.first * e.second
|
|
124
|
-
assert total == 140
|
|
132
|
+
assert total.n == 140
|
|
125
|
-
big := 0
|
|
133
|
+
big := Tally(n: 0)
|
|
126
134
|
for k := range m.keys()
|
|
127
135
|
if k > 1
|
|
128
|
-
big = big + 1
|
|
136
|
+
big.n = big.n + 1
|
|
129
|
-
assert big == 2
|
|
137
|
+
assert big.n == 2
|
|
130
|
-
vs := 0
|
|
138
|
+
vs := Tally(n: 0)
|
|
131
139
|
for v := range m.values()
|
|
132
|
-
vs = vs + v
|
|
140
|
+
vs.n = vs.n + v
|
|
133
|
-
assert vs == 60
|
|
141
|
+
assert vs.n == 60
|
|
134
142
|
|
|
135
143
|
test "break_continue_return_semantics"
|
|
136
144
|
xs := [1, 2, 3, 4, 5].toSlice()
|
|
@@ -145,16 +153,22 @@ test "break_continue_return_semantics"
|
|
|
145
153
|
assert firstBig([1, 20, 30].toSlice()) == 20
|
|
146
154
|
assert firstBig(xs) == -1
|
|
147
155
|
|
|
156
|
+
test "find stops at the first big element"
|
|
157
|
+
assert firstBig([1, 20, 30].toSlice()) == 20
|
|
158
|
+
assert firstBig([1, 2].toSlice()) == -1
|
|
159
|
+
|
|
148
160
|
# ---- chains: several stages in one loop ----
|
|
149
161
|
|
|
150
162
|
test "sum of hp of my team: filter + map + sum"
|
|
151
163
|
units := army()
|
|
152
164
|
me := units.at(0)
|
|
153
|
-
total := 0.0
|
|
165
|
+
total := 0.until(units.length()).fold(0.0, |sum, i|
|
|
154
|
-
|
|
166
|
+
u := units.at(i)
|
|
155
167
|
if u.team == me.team
|
|
156
|
-
|
|
168
|
+
sum + u.hp
|
|
157
|
-
|
|
169
|
+
else
|
|
170
|
+
sum
|
|
171
|
+
)
|
|
158
172
|
assert total == 21.0
|
|
159
173
|
|
|
160
174
|
test "names of living units within range: filter + map + filter + map"
|
|
@@ -175,10 +189,8 @@ test "names of living units within range: filter + map + filter + map"
|
|
|
175
189
|
test "first enemy in range: filter + find"
|
|
176
190
|
units := army()
|
|
177
191
|
me := units.at(0)
|
|
178
|
-
|
|
192
|
+
hit := 0.until(units.length()).find(|i|
|
|
179
|
-
|
|
193
|
+
u := units.at(i)
|
|
180
|
-
if u.team != me.team
|
|
181
|
-
|
|
194
|
+
u.team != me.team && dist2(u.x, u.y, me.x, me.y) < 4.0
|
|
182
|
-
first = u
|
|
183
|
-
|
|
195
|
+
)
|
|
184
|
-
assert
|
|
196
|
+
assert units.at(hit!).name == "b"
|
examples/structs_records.plum
CHANGED
|
@@ -62,15 +62,13 @@ fun viaMatch(v: Vec2) -> Float =
|
|
|
62
62
|
Vec2(x, y) => x + y
|
|
63
63
|
|
|
64
64
|
fun sumAll(vs: List[Vec2]) -> Float =
|
|
65
|
-
t := 0.0
|
|
66
|
-
for v := range vs
|
|
67
|
-
|
|
65
|
+
vs.fold(0.0, |t, v| t + v.x)
|
|
68
|
-
t
|
|
69
66
|
|
|
70
67
|
fun opt(c: Bool) -> Option[Vec2] =
|
|
71
68
|
if c
|
|
72
|
-
|
|
69
|
+
Some(Vec2(x: 1.0, y: 1.0))
|
|
70
|
+
else
|
|
73
|
-
|
|
71
|
+
None
|
|
74
72
|
|
|
75
73
|
fun chain(c: Bool) -> Option[Vec2] =
|
|
76
74
|
v := opt(c)?
|
|
@@ -107,11 +105,10 @@ struct Wrapper(inner: Cat, tag: Int) =
|
|
|
107
105
|
struct LoopBox(v: Int)
|
|
108
106
|
|
|
109
107
|
fun sumLoopBoxes() -> Int =
|
|
110
|
-
total := 0
|
|
111
|
-
|
|
108
|
+
0.until(5).fold(0, |total, i|
|
|
112
109
|
b := LoopBox(v: i)
|
|
113
|
-
total
|
|
110
|
+
total + b.v
|
|
114
|
-
|
|
111
|
+
)
|
|
115
112
|
|
|
116
113
|
fun methodsTotal() -> Int =
|
|
117
114
|
c := Cat(name: "Whiskers", age: 3)
|
|
@@ -160,10 +157,11 @@ enum S1 = E1 | E2
|
|
|
160
157
|
|
|
161
158
|
fun fail(n: Int) -> Result[Int, S1] =
|
|
162
159
|
if n == 1
|
|
163
|
-
|
|
160
|
+
Err(E1)
|
|
164
|
-
if n == 2
|
|
161
|
+
else if n == 2
|
|
165
|
-
|
|
162
|
+
Err(E2)
|
|
163
|
+
else
|
|
166
|
-
|
|
164
|
+
Ok(n)
|
|
167
165
|
|
|
168
166
|
fun asInt(w: V2) -> Int =
|
|
169
167
|
match w
|
|
@@ -172,20 +170,21 @@ fun asInt(w: V2) -> Int =
|
|
|
172
170
|
|
|
173
171
|
fun payload(w: V2) -> Int =
|
|
174
172
|
if A1(n) := w
|
|
173
|
+
n
|
|
175
|
-
|
|
174
|
+
else
|
|
176
|
-
|
|
175
|
+
0
|
|
177
176
|
|
|
178
177
|
fun isB2(w: V2) -> Int =
|
|
179
178
|
if B2 := w
|
|
179
|
+
1
|
|
180
|
-
|
|
180
|
+
else
|
|
181
|
-
|
|
181
|
+
0
|
|
182
182
|
|
|
183
183
|
# ==== annotated locals ====
|
|
184
184
|
|
|
185
185
|
# Annotated local declarations: `name: Type := value` fixes the local's type to
|
|
186
186
|
# the annotation. The value converts to it exactly as it would for a parameter of
|
|
187
|
-
# that type (a variant into its enum, `Some(x)` or `None` into an `Option[T]`)
|
|
187
|
+
# that type (a variant into its enum, `Some(x)` or `None` into an `Option[T]`).
|
|
188
|
-
# a later `name = value` is checked and converted against the same type.
|
|
189
188
|
|
|
190
189
|
enum Shape = Circle(r: Float) | Region(w: Float, h: Float)
|
|
191
190
|
|
|
@@ -198,8 +197,9 @@ fun area(s: Shape) -> Float =
|
|
|
198
197
|
|
|
199
198
|
fun parseLocal(n: Int) -> Result[Int, LocalError] =
|
|
200
199
|
if n < 0
|
|
201
|
-
|
|
200
|
+
Err(Bad)
|
|
201
|
+
else
|
|
202
|
-
|
|
202
|
+
Ok(n * 2)
|
|
203
203
|
|
|
204
204
|
fun twice(x: Int) -> Int = x * 2
|
|
205
205
|
|
|
@@ -413,8 +413,6 @@ test "optional_local_present"
|
|
|
413
413
|
test "optional_local_none"
|
|
414
414
|
o: Option[Int] := None
|
|
415
415
|
assert o.unwrapOr(9) == 9
|
|
416
|
-
o = Some(5)
|
|
417
|
-
assert o.unwrapOr(9) == 5
|
|
418
416
|
|
|
419
417
|
test "list_local"
|
|
420
418
|
xs: Slice[Shape] := Slice[Shape]()
|
examples/unions.plum
CHANGED
|
@@ -124,11 +124,12 @@ enum PosError = Neg
|
|
|
124
124
|
|
|
125
125
|
fun pos(n: Int) -> Result[Slice[Jv], PosError] =
|
|
126
126
|
if n < 0
|
|
127
|
-
|
|
127
|
+
Err(Neg)
|
|
128
|
+
else
|
|
128
|
-
|
|
129
|
+
xs := Slice[Jv]()
|
|
129
|
-
|
|
130
|
+
xs.push(JInt(v: n))
|
|
130
|
-
|
|
131
|
+
xs.push(JStr(v: "a"))
|
|
131
|
-
|
|
132
|
+
Ok(xs)
|
|
132
133
|
|
|
133
134
|
fun posLen(n: Int) -> Result[Int, PosError] =
|
|
134
135
|
xs := pos(n)?
|
|
@@ -143,9 +144,7 @@ test "list_of_shapes"
|
|
|
143
144
|
xs := Slice[Shape]()
|
|
144
145
|
xs.push(Circle(r: 1.0))
|
|
145
146
|
xs.push(Rectangle(w: 2.0, h: 3.0))
|
|
146
|
-
sum := 0.0
|
|
147
|
-
for x := range xs
|
|
148
|
-
|
|
147
|
+
sum := 0.until(xs.length()).fold(0.0, |acc, i| acc + xs.at(i).area())
|
|
149
148
|
assert sum == 9.0
|
|
150
149
|
|
|
151
150
|
test "tree_generic"
|
|
@@ -188,9 +187,7 @@ test "variants_with_for"
|
|
|
188
187
|
xs.push(JInt(v: 1))
|
|
189
188
|
xs.push(JStr(v: "a"))
|
|
190
189
|
xs.push(JFloat(v: 2.5))
|
|
191
|
-
n := 0
|
|
192
|
-
for x := range xs
|
|
193
|
-
|
|
190
|
+
n := 0.until(xs.length()).fold(0, |acc, i| acc + describeJv(xs.at(i)).length())
|
|
194
191
|
assert n == 3 + 5 + 5
|
|
195
192
|
|
|
196
193
|
test "tags and per-tag values"
|
std/game/input.plum
CHANGED
|
@@ -229,7 +229,7 @@ struct Input(held: Held, events: Slice[InputEvent], pos: Vec2, wheel: Float) =
|
|
|
229
229
|
|
|
230
230
|
# Went down this frame.
|
|
231
231
|
fun isKeyPressed(self, key: Int) -> Bool =
|
|
232
|
-
0.
|
|
232
|
+
0.until(self.events.length()).find(|i| self.events.at(i).isKeyDownOf(key)).isSome()
|
|
233
233
|
|
|
234
234
|
# The left mouse button, or a touch.
|
|
235
235
|
fun isMouseDown(self) -> Bool =
|
|
@@ -248,10 +248,10 @@ struct Input(held: Held, events: Slice[InputEvent], pos: Vec2, wheel: Float) =
|
|
|
248
248
|
Pointer(pos: self.pos, down: self.held.mouse, pressed: self.pointerPressed(), released: self.pointerReleased())
|
|
249
249
|
|
|
250
250
|
fun pointerPressed(self) -> Bool =
|
|
251
|
-
0.
|
|
251
|
+
0.until(self.events.length()).find(|i| self.events.at(i).isPointerDown()).isSome()
|
|
252
252
|
|
|
253
253
|
fun pointerReleased(self) -> Bool =
|
|
254
|
-
0.
|
|
254
|
+
0.until(self.events.length()).find(|i| self.events.at(i).isPointerUp()).isSome()
|
|
255
255
|
|
|
256
256
|
# What happened to a UI element: pressed on it, released after a press on it,
|
|
257
257
|
# clicked (pressed and released on it), or the pointer entered or left it.
|
|
@@ -318,6 +318,18 @@ test "input agrees for the whole frame"
|
|
|
318
318
|
assert st.mousePos().x == 3.0
|
|
319
319
|
assert st.pointer().pressed
|
|
320
320
|
|
|
321
|
+
test "only this frame's events count, not what lies past them"
|
|
322
|
+
all := [KeyDown(key: Key.KeyA), KeyDown(key: Key.KeyB), PointerDown(x: 1.0, y: 1.0), PointerUp(x: 1.0, y: 1.0)].toSlice()
|
|
323
|
+
held := Held(mouse: false, keys: [Key.KeyA].toSlice())
|
|
324
|
+
one := Input(held: held, events: all.slice(0, 1), pos: Vec2(x: 0.0, y: 0.0), wheel: 0.0)
|
|
325
|
+
assert one.isKeyPressed(Key.KeyA)
|
|
326
|
+
assert !one.isKeyPressed(Key.KeyB)
|
|
327
|
+
downOnly := Input(held: held, events: all.slice(1, 2), pos: Vec2(x: 0.0, y: 0.0), wheel: 0.0)
|
|
328
|
+
assert !downOnly.pointerPressed()
|
|
329
|
+
beforeUp := Input(held: held, events: all.slice(2, 3), pos: Vec2(x: 0.0, y: 0.0), wheel: 0.0)
|
|
330
|
+
assert beforeUp.pointerPressed()
|
|
331
|
+
assert !beforeUp.pointerReleased()
|
|
332
|
+
|
|
321
333
|
test "key and mouse constants use GLFW numbering"
|
|
322
334
|
assert Key.KeyW == 87
|
|
323
335
|
assert Key.Left == 263
|
tests/core/for_next.plum
CHANGED
|
@@ -7,6 +7,8 @@ import std/Option
|
|
|
7
7
|
# whose type has `next(self) -> Option[Pair[T, Self]]` iterates (None = done, else
|
|
8
8
|
# `Some(Pair(value, rest))`, the iterator rebound to `rest` each step).
|
|
9
9
|
|
|
10
|
+
struct Tally(mut acc: Int)
|
|
11
|
+
|
|
10
12
|
struct Countdown(n: Int) =
|
|
11
13
|
fun next(self) -> Option[Pair[Int, Countdown]] =
|
|
12
14
|
if self.n == 0 then None else Some(Pair(first: self.n, second: Countdown(n: self.n - 1)))
|
|
@@ -16,16 +18,16 @@ struct Up(i: Int, n: Int) =
|
|
|
16
18
|
if self.i >= self.n then None else Some(Pair(first: self.i, second: Up(i: self.i + 1, n: self.n)))
|
|
17
19
|
|
|
18
20
|
fun rangeInt() -> Int =
|
|
19
|
-
|
|
21
|
+
t := Tally(acc: 0)
|
|
20
22
|
for i := range 3
|
|
21
|
-
acc = acc * 10 + i + 1
|
|
23
|
+
t.acc = t.acc * 10 + i + 1
|
|
22
|
-
acc
|
|
24
|
+
t.acc
|
|
23
25
|
|
|
24
26
|
fun rangeList() -> Int =
|
|
25
|
-
|
|
27
|
+
t := Tally(acc: 0)
|
|
26
28
|
for x := range [5, 6]
|
|
27
|
-
acc = acc * 10 + x
|
|
29
|
+
t.acc = t.acc * 10 + x
|
|
28
|
-
acc
|
|
30
|
+
t.acc
|
|
29
31
|
|
|
30
32
|
fun listNext() -> Int =
|
|
31
33
|
xs := [5, 6]
|
|
@@ -35,10 +37,10 @@ fun listNext() -> Int =
|
|
|
35
37
|
p.first * 100 + q.first * 10 + {if r.isNone() then 1 else 0}
|
|
36
38
|
|
|
37
39
|
fun rangeUser() -> Int =
|
|
38
|
-
|
|
40
|
+
t := Tally(acc: 0)
|
|
39
41
|
for x := range Countdown(n: 3)
|
|
40
|
-
acc = acc * 10 + x
|
|
42
|
+
t.acc = t.acc * 10 + x
|
|
41
|
-
acc
|
|
43
|
+
t.acc
|
|
42
44
|
|
|
43
45
|
fun rangeCtl() -> Int =
|
|
44
46
|
acc := 0
|
tests/fbip/aliasing.plum
CHANGED
|
@@ -56,9 +56,8 @@ fun both(c: Cell) -> Pair[Cell, Cell] = Pair(c, bump(c))
|
|
|
56
56
|
|
|
57
57
|
fun branchy(c: Cell, flag: Bool) -> Int =
|
|
58
58
|
d := c
|
|
59
|
-
if flag
|
|
60
|
-
|
|
59
|
+
e := if flag then bump(d) else d
|
|
61
|
-
c.n +
|
|
60
|
+
c.n + e.n
|
|
62
61
|
|
|
63
62
|
fun matched(c: Cell) -> Int =
|
|
64
63
|
match c
|
|
@@ -90,8 +89,7 @@ fun fresher(lg: Slice[Int]) -> Cell =
|
|
|
90
89
|
|
|
91
90
|
fun viaParam(fresher: fn(Slice[Int]) -> Cell, lg: Slice[Int]) -> Cell =
|
|
92
91
|
y := fresher(lg)
|
|
93
|
-
|
|
92
|
+
Cell(..y, n: 9)
|
|
94
|
-
y
|
|
95
93
|
|
|
96
94
|
struct Maker(k: Int) =
|
|
97
95
|
fun made(self, lg: Slice[Int]) -> Cell =
|
|
@@ -112,12 +110,6 @@ fun keepAndSet(xs: Slice[Array[Int]], a: Array[Int]) -> Array[Int] =
|
|
|
112
110
|
xs.push(a)
|
|
113
111
|
a.with(0, 7)
|
|
114
112
|
|
|
115
|
-
fun doubled(n: Int) -> Array[Int] =
|
|
116
|
-
a := Array.filled(n, 1)
|
|
117
|
-
for i := range n
|
|
118
|
-
a = a.with(i, a.get(i) * 2 + i)
|
|
119
|
-
a
|
|
120
|
-
|
|
121
113
|
fun lastWith(n: Int) -> Array[Int] =
|
|
122
114
|
a := Array.filled(n, 3)
|
|
123
115
|
a.set(0, 4)
|
|
@@ -125,10 +117,10 @@ fun lastWith(n: Int) -> Array[Int] =
|
|
|
125
117
|
|
|
126
118
|
fun twoDecls(flag: Bool) -> Array[Int] =
|
|
127
119
|
a := Array.filled(2, 0)
|
|
128
|
-
if flag
|
|
129
|
-
|
|
120
|
+
chosen := if flag then a.with(0, 1) else a
|
|
130
|
-
b :=
|
|
121
|
+
b := chosen
|
|
131
|
-
|
|
122
|
+
written := chosen.with(1, 5)
|
|
123
|
+
assert written.get(1) == 5
|
|
132
124
|
b
|
|
133
125
|
|
|
134
126
|
# ---- tests ----
|
|
@@ -187,11 +179,11 @@ test "captured by a closure before the update"
|
|
|
187
179
|
|
|
188
180
|
test "captured by a closure after the update"
|
|
189
181
|
a := Cell(label: "a", n: 0)
|
|
190
|
-
|
|
182
|
+
once := bump(a)
|
|
191
|
-
f := ||
|
|
183
|
+
f := || once.n
|
|
192
|
-
|
|
184
|
+
twiceOver := bump(once)
|
|
193
185
|
assert f() == 1
|
|
194
|
-
assert
|
|
186
|
+
assert twiceOver.n == 2
|
|
195
187
|
|
|
196
188
|
test "returned alongside another reference"
|
|
197
189
|
a := Cell(label: "a", n: 0)
|
|
@@ -231,10 +223,9 @@ test "a shared value passed through a method"
|
|
|
231
223
|
test "a shared value passed through a method then handed over in a loop"
|
|
232
224
|
a := Cell(label: "a", n: 0)
|
|
233
225
|
v := a.same()
|
|
234
|
-
for i := range 3
|
|
235
|
-
|
|
226
|
+
last := 0.until(3).fold(v, |cur, i| cur.bumped())
|
|
236
227
|
assert a.n == 0
|
|
237
|
-
assert
|
|
228
|
+
assert last.n == 3
|
|
238
229
|
|
|
239
230
|
test "a method that stores self then spreads it"
|
|
240
231
|
s := Stored(label: "s", n: 0, log: Slice[Stored]())
|
|
@@ -273,10 +264,11 @@ test "a generic function instantiated with both kinds"
|
|
|
273
264
|
test "a loop keeping the previous value"
|
|
274
265
|
v := Cell(label: "v", n: 0)
|
|
275
266
|
prev := Slice[Cell]()
|
|
276
|
-
|
|
267
|
+
last := 0.until(3).fold(v, |cur, i|
|
|
277
|
-
prev.push(
|
|
268
|
+
prev.push(cur)
|
|
278
|
-
|
|
269
|
+
cur.bumped()
|
|
270
|
+
)
|
|
279
|
-
assert
|
|
271
|
+
assert last.n == 3
|
|
280
272
|
assert prev.at(0).n == 0
|
|
281
273
|
assert prev.at(1).n == 1
|
|
282
274
|
assert prev.at(2).n == 2
|
|
@@ -285,9 +277,8 @@ test "a loop handing over a value bound outside it while another holder keeps it
|
|
|
285
277
|
start := Cell(label: "s", n: 0)
|
|
286
278
|
keepIt := Holder(a: start, b: start)
|
|
287
279
|
v := start
|
|
288
|
-
|
|
280
|
+
last := 0.until(3).fold(v, |cur, i| bump(cur))
|
|
289
|
-
v = bump(v)
|
|
290
|
-
assert
|
|
281
|
+
assert last.n == 3
|
|
291
282
|
assert start.n == 0
|
|
292
283
|
assert keepIt.a.n == 0
|
|
293
284
|
|
|
@@ -341,9 +332,8 @@ test "a hand-over loop in a field keeps an earlier snapshot"
|
|
|
341
332
|
|
|
342
333
|
test "a fresh value handed over repeatedly is correct"
|
|
343
334
|
v := Cell(label: "f", n: 0)
|
|
344
|
-
for i := range 4
|
|
345
|
-
|
|
335
|
+
last := 0.until(4).fold(v, |cur, i| cur.bumped())
|
|
346
|
-
assert
|
|
336
|
+
assert last.n == 4
|
|
347
337
|
w := twice(shared("w", 10))
|
|
348
338
|
assert w.n == 12
|
|
349
339
|
|
|
@@ -358,21 +348,22 @@ test "a value that is both unique and shared at call sites"
|
|
|
358
348
|
test "a returned parameter that was stored then updated"
|
|
359
349
|
xs := Slice[Cell]()
|
|
360
350
|
v := Cell(label: "v", n: 0)
|
|
361
|
-
|
|
351
|
+
stored := reg(xs, v)
|
|
362
|
-
w := bump(
|
|
352
|
+
w := bump(stored)
|
|
363
353
|
assert xs.at(0).n == 0
|
|
364
354
|
assert w.n == 1
|
|
365
355
|
|
|
366
356
|
test "a returned parameter handed over in a loop keeps a stored snapshot"
|
|
367
357
|
xs := Slice[Cell]()
|
|
368
358
|
v := Cell(label: "v", n: 0)
|
|
369
|
-
|
|
359
|
+
last := 0.until(3).fold(v, |cur, i|
|
|
370
|
-
|
|
360
|
+
stored := reg(xs, cur)
|
|
371
|
-
|
|
361
|
+
bump(stored)
|
|
362
|
+
)
|
|
372
363
|
assert xs.at(0).n == 0
|
|
373
364
|
assert xs.at(1).n == 1
|
|
374
365
|
assert xs.at(2).n == 2
|
|
375
|
-
assert
|
|
366
|
+
assert last.n == 3
|
|
376
367
|
|
|
377
368
|
test "a returned parameter changed through a mut field with an alias kept"
|
|
378
369
|
a := Cell(label: "a", n: 0)
|
|
@@ -402,9 +393,9 @@ test "a local closure named like a fresh fn"
|
|
|
402
393
|
cap := Cell(label: "s", n: 0)
|
|
403
394
|
fresher := |l| cap
|
|
404
395
|
y := fresher(lg)
|
|
405
|
-
|
|
396
|
+
z := Cell(..y, n: 7)
|
|
406
397
|
assert cap.n == 0
|
|
407
|
-
assert
|
|
398
|
+
assert z.n == 7
|
|
408
399
|
|
|
409
400
|
test "a fn-typed parameter named like a fresh fn"
|
|
410
401
|
lg := Slice[Int]()
|
|
@@ -419,26 +410,26 @@ test "a closure field named like a fresh method"
|
|
|
419
410
|
cap := Cell(label: "s", n: 0)
|
|
420
411
|
h := Hook(made: |l| cap)
|
|
421
412
|
y := h.made(lg)
|
|
422
|
-
|
|
413
|
+
z := Cell(..y, n: 7)
|
|
423
414
|
assert cap.n == 0
|
|
424
|
-
assert
|
|
415
|
+
assert z.n == 7
|
|
425
416
|
assert Maker(k: 1).made(lg).n == 100
|
|
426
417
|
|
|
427
418
|
test "array: alias then with"
|
|
428
419
|
a := Array.filled(3, 0)
|
|
429
420
|
b := a
|
|
430
|
-
|
|
421
|
+
written := a.with(1, 5)
|
|
431
422
|
assert b.get(1) == 0
|
|
432
|
-
assert
|
|
423
|
+
assert written.get(1) == 5
|
|
433
424
|
|
|
434
425
|
test "array: alias taken after with"
|
|
435
426
|
a := Array.filled(3, 0)
|
|
436
|
-
|
|
427
|
+
once := a.with(1, 5)
|
|
437
|
-
b :=
|
|
428
|
+
b := once
|
|
438
|
-
|
|
429
|
+
twiceOver := once.with(2, 6)
|
|
439
430
|
assert b.get(2) == 0
|
|
440
431
|
assert b.get(1) == 5
|
|
441
|
-
assert
|
|
432
|
+
assert twiceOver.get(2) == 6
|
|
442
433
|
|
|
443
434
|
test "array: with without a rebind leaves the receiver alone"
|
|
444
435
|
a := Array.filled(3, 0)
|
|
@@ -461,9 +452,8 @@ test "array: returned from a helper and kept"
|
|
|
461
452
|
r := firstTo9(z)
|
|
462
453
|
assert z.get(0) == 0
|
|
463
454
|
assert r.get(0) == 9
|
|
464
|
-
|
|
455
|
+
written := makeZeros(2).with(1, 4)
|
|
465
|
-
y = y.with(1, 4)
|
|
466
|
-
assert
|
|
456
|
+
assert written.get(1) == 4
|
|
467
457
|
|
|
468
458
|
test "array: passed to a helper that stores it"
|
|
469
459
|
xs := Slice[Array[Int]]()
|
|
@@ -477,15 +467,16 @@ test "array: stored in a slice before the write"
|
|
|
477
467
|
xs := Slice[Array[Int]]()
|
|
478
468
|
a := Array.filled(2, 0)
|
|
479
469
|
xs.push(a)
|
|
480
|
-
|
|
470
|
+
written := a.with(0, 3)
|
|
481
471
|
assert xs.at(0).get(0) == 0
|
|
482
|
-
assert
|
|
472
|
+
assert written.get(0) == 3
|
|
483
473
|
|
|
484
474
|
test "array: captured by a closure before the write"
|
|
485
475
|
a := Array.filled(2, 0)
|
|
486
476
|
f := || a.get(0)
|
|
487
|
-
|
|
477
|
+
written := a.with(0, 5)
|
|
488
478
|
assert f() == 5 || f() == 0
|
|
479
|
+
assert written.get(0) == 5
|
|
489
480
|
b := Array.filled(2, 0)
|
|
490
481
|
g := || b.get(0)
|
|
491
482
|
c := b.with(0, 5)
|
|
@@ -495,27 +486,22 @@ test "array: captured by a closure before the write"
|
|
|
495
486
|
test "array: a loop keeping the previous array"
|
|
496
487
|
a := Array.filled(3, 0)
|
|
497
488
|
snaps := Slice[Array[Int]]()
|
|
498
|
-
|
|
489
|
+
last := 0.until(3).fold(a, |cur, i|
|
|
499
|
-
snaps.push(
|
|
490
|
+
snaps.push(cur)
|
|
500
|
-
|
|
491
|
+
cur.with(i, i + 1)
|
|
492
|
+
)
|
|
501
493
|
assert snaps.at(0).get(0) == 0
|
|
502
494
|
assert snaps.at(1).get(0) == 1 && snaps.at(1).get(1) == 0
|
|
503
495
|
assert snaps.at(2).get(1) == 2 && snaps.at(2).get(2) == 0
|
|
504
|
-
assert
|
|
496
|
+
assert last.get(2) == 3
|
|
505
497
|
|
|
506
498
|
test "array: a loop with the previous kept in a variable"
|
|
507
499
|
a := Array.filled(3, 0)
|
|
508
|
-
prev := a
|
|
509
|
-
|
|
500
|
+
# state: first is the previous array, second the current one
|
|
510
|
-
prev = a
|
|
511
|
-
|
|
501
|
+
end := 0.until(3).fold(Pair(a, a), |st, i| Pair(st.second, st.second.with(i, 1)))
|
|
512
|
-
assert
|
|
502
|
+
assert end.first.get(2) == 0
|
|
513
|
-
assert
|
|
503
|
+
assert end.first.get(1) == 1
|
|
514
|
-
assert
|
|
504
|
+
assert end.second.get(2) == 1
|
|
515
|
-
|
|
516
|
-
test "array: a unique loop gives the copying answer"
|
|
517
|
-
a := doubled(4)
|
|
518
|
-
assert a.get(0) == 2 && a.get(1) == 3 && a.get(2) == 4 && a.get(3) == 5
|
|
519
505
|
|
|
520
506
|
test "array: with as the last expression"
|
|
521
507
|
a := lastWith(3)
|
|
@@ -533,54 +519,44 @@ test "array: through a nested with"
|
|
|
533
519
|
assert a.get(0) == 0 && a.get(1) == 0
|
|
534
520
|
assert b.get(0) == 5
|
|
535
521
|
|
|
536
|
-
test "array: with, then the old array read in the argument"
|
|
537
|
-
a := Array.filled(3, 1)
|
|
538
|
-
a = a.with(0, a.get(1) + a.get(2))
|
|
539
|
-
a = a.with(1, a.get(0) * 10)
|
|
540
|
-
assert a.get(0) == 2 && a.get(1) == 20 && a.get(2) == 1
|
|
541
|
-
|
|
542
522
|
test "array: from push and resize"
|
|
543
523
|
a := Array[Int]().push(1).push(2)
|
|
544
524
|
b := a
|
|
545
|
-
|
|
525
|
+
written := a.with(0, 9)
|
|
546
526
|
assert b.get(0) == 1
|
|
527
|
+
assert written.get(0) == 9
|
|
547
528
|
e := Array[Int]().push(4).with(0, 6)
|
|
548
529
|
assert e.get(0) == 6
|
|
549
|
-
|
|
530
|
+
sized := Array[Int]().resize(2).with(1, 3)
|
|
550
|
-
r = r.with(1, 3)
|
|
551
|
-
assert
|
|
531
|
+
assert sized.get(1) == 3 && sized.length() == 2
|
|
552
532
|
|
|
553
533
|
test "array: an empty array stays empty"
|
|
554
534
|
a := Array[Int]()
|
|
555
535
|
b := a
|
|
556
|
-
|
|
536
|
+
resized := a.resize(0)
|
|
557
|
-
assert
|
|
537
|
+
assert resized.length() == 0
|
|
558
538
|
assert b.length() == 0
|
|
559
539
|
|
|
560
540
|
test "array: bytes"
|
|
561
541
|
a := Array.filled(3, Byte(7))
|
|
562
542
|
b := a
|
|
563
|
-
|
|
543
|
+
written := a.with(1, Byte(200))
|
|
564
544
|
assert b.get(1).toInt() == 7
|
|
565
|
-
assert
|
|
545
|
+
assert written.get(1).toInt() == 200
|
|
566
|
-
assert
|
|
546
|
+
assert written.get(2).toInt() == 7
|
|
567
547
|
|
|
568
548
|
test "array: vec2 elements"
|
|
569
549
|
a := Array.filled(3, Vec2(x: 1.0, y: 2.0))
|
|
570
550
|
b := a
|
|
571
|
-
|
|
551
|
+
written := a.with(1, Vec2(x: 5.0, y: 6.0))
|
|
572
552
|
assert b.get(1).x == 1.0 && b.get(1).y == 2.0
|
|
573
|
-
assert
|
|
553
|
+
assert written.get(1).x == 5.0 && written.get(1).y == 6.0
|
|
574
|
-
assert
|
|
554
|
+
assert written.get(0).y == 2.0
|
|
575
|
-
c := Array.filled(2, Vec2(x: 0.0, y: 0.0))
|
|
576
|
-
for i := range 2
|
|
577
|
-
c = c.with(i, Vec2(x: Float(i), y: 3.0))
|
|
578
|
-
assert c.get(1).x == 1.0 && c.get(0).y == 3.0
|
|
579
555
|
|
|
580
556
|
test "array: vec2 stored in a field and a slice"
|
|
581
557
|
a := Array.filled(2, Vec2(x: 1.0, y: 1.0))
|
|
582
558
|
xs := Slice[Array[Vec2]]()
|
|
583
559
|
xs.push(a)
|
|
584
|
-
|
|
560
|
+
written := a.with(0, Vec2(x: 9.0, y: 9.0))
|
|
585
561
|
assert xs.at(0).get(0).x == 1.0
|
|
586
|
-
assert
|
|
562
|
+
assert written.get(0).x == 9.0
|
tests/fbip/array_in_place.plum
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import std/Int
|
|
2
|
+
import std/Float
|
|
3
|
+
import std/Array
|
|
4
|
+
import std/game/Vec2
|
|
5
|
+
|
|
6
|
+
# The hand-overs from aliasing.plum that are proved in place. They live apart because the proof
|
|
7
|
+
# resolves `with` by name, and the shared `with`s in aliasing.plum would stop it here too.
|
|
8
|
+
# Each test asserts the copying answer, so a wrong in-place write changes it.
|
|
9
|
+
|
|
10
|
+
fun doubledUpTo(n: Int, k: Int) -> Array[Int] =
|
|
11
|
+
if k == 0
|
|
12
|
+
Array.filled(n, 1)
|
|
13
|
+
else
|
|
14
|
+
a := doubledUpTo(n, k - 1)
|
|
15
|
+
a.with(k - 1, a.get(k - 1) * 2 + k - 1)
|
|
16
|
+
|
|
17
|
+
fun doubled(n: Int) -> Array[Int] = doubledUpTo(n, n)
|
|
18
|
+
|
|
19
|
+
fun sumOfOthersFirst() -> Array[Int] =
|
|
20
|
+
a := Array.filled(3, 1)
|
|
21
|
+
a.with(0, a.get(1) + a.get(2))
|
|
22
|
+
|
|
23
|
+
fun tenTimesFirstSecond() -> Array[Int] =
|
|
24
|
+
a := sumOfOthersFirst()
|
|
25
|
+
a.with(1, a.get(0) * 10)
|
|
26
|
+
|
|
27
|
+
fun threesUpTo(k: Int) -> Array[Vec2] =
|
|
28
|
+
if k == 0
|
|
29
|
+
Array.filled(2, Vec2(x: 0.0, y: 0.0))
|
|
30
|
+
else
|
|
31
|
+
threesUpTo(k - 1).with(k - 1, Vec2(x: Float(k - 1), y: 3.0))
|
|
32
|
+
|
|
33
|
+
test "array: a unique loop gives the copying answer"
|
|
34
|
+
a := doubled(4)
|
|
35
|
+
assert a.get(0) == 2 && a.get(1) == 3 && a.get(2) == 4 && a.get(3) == 5
|
|
36
|
+
|
|
37
|
+
test "array: with, then the old array read in the argument"
|
|
38
|
+
a := tenTimesFirstSecond()
|
|
39
|
+
assert a.get(0) == 2 && a.get(1) == 20 && a.get(2) == 1
|
|
40
|
+
|
|
41
|
+
test "array: vec2 elements handed over"
|
|
42
|
+
c := threesUpTo(2)
|
|
43
|
+
assert c.get(1).x == 1.0 && c.get(0).y == 3.0
|
tests/fbip/array_names.plum
CHANGED
|
@@ -11,16 +11,21 @@ struct Bag(items: Array[Int]) =
|
|
|
11
11
|
test "array: a user type with its own with and push"
|
|
12
12
|
bag := Bag(items: Array.filled(2, 0))
|
|
13
13
|
a := bag.with(0, 1)
|
|
14
|
-
|
|
14
|
+
written := a.with(0, 9)
|
|
15
|
+
assert written.get(0) == 9
|
|
15
16
|
assert bag.items.get(0) == 0
|
|
16
17
|
b := Array.filled(2, 0)
|
|
17
|
-
|
|
18
|
+
fresh := b.with(0, 1)
|
|
18
|
-
assert
|
|
19
|
+
assert fresh.get(0) == 1
|
|
19
20
|
|
|
20
21
|
test "array: bound to the result of a user push that returns shared storage"
|
|
21
22
|
bag := Bag(items: Array.filled(2, 0))
|
|
22
|
-
a := Array.filled(2, 5)
|
|
23
|
-
a = bag.push(1)
|
|
23
|
+
a := bag.push(1)
|
|
24
|
-
|
|
24
|
+
written := a.with(0, 9)
|
|
25
|
-
assert
|
|
25
|
+
assert written.get(0) == 9
|
|
26
26
|
assert bag.items.get(0) == 0
|
|
27
|
+
# a name that is fresh on one path and holds shared storage on the other
|
|
28
|
+
picked := if bag.items.length() == 0 then Array.filled(2, 5) else bag.push(1)
|
|
29
|
+
over := picked.with(1, 9)
|
|
30
|
+
assert over.get(1) == 9
|
|
31
|
+
assert bag.items.get(1) == 0
|
tests/fbip/array_with.plum
CHANGED
|
@@ -2,6 +2,7 @@ import std/Int
|
|
|
2
2
|
import std/Float
|
|
3
3
|
import std/Array
|
|
4
4
|
import std/Slice
|
|
5
|
+
import std/Pair
|
|
5
6
|
import std/game/Vec2
|
|
6
7
|
|
|
7
8
|
# `Array.with` on arrays held in struct fields. A `with` writes in place only when the struct is
|
|
@@ -20,16 +21,13 @@ struct Vec(cells: Array[Int], n: Int) =
|
|
|
20
21
|
|
|
21
22
|
fun newVec(cap: Int) -> Vec = Vec(cells: Array.filled(cap, 0), n: 0)
|
|
22
23
|
|
|
23
|
-
fun
|
|
24
|
+
fun fillUpTo(cap: Int, k: Int) -> Vec =
|
|
24
|
-
|
|
25
|
+
if k == 0 then newVec(cap) else fillUpTo(cap, k - 1).append({k - 1} * 3)
|
|
26
|
+
|
|
25
|
-
|
|
27
|
+
fun fillVec(n: Int) -> Vec = fillUpTo(n, n)
|
|
26
|
-
v = v.append(i * 3)
|
|
27
|
-
v
|
|
28
28
|
|
|
29
29
|
fun bumpAll(v: Vec) -> Vec =
|
|
30
|
-
for i := range v.length()
|
|
31
|
-
|
|
30
|
+
0.until(v.length()).fold(v, |cur, i| cur.put(i, cur.get(i) + 1))
|
|
32
|
-
v
|
|
33
31
|
|
|
34
32
|
# ---- a `mut` field rebuilt with an assign-back ----
|
|
35
33
|
|
|
@@ -86,11 +84,13 @@ struct WVec(wcells: Array[Vec2], wn: Int) =
|
|
|
86
84
|
fun wput(self, i: Int, p: Vec2) -> WVec = WVec(..self, wcells: self.wcells.with(i, p))
|
|
87
85
|
fun wget(self, i: Int) -> Vec2 = self.wcells.get(i)
|
|
88
86
|
|
|
89
|
-
fun
|
|
87
|
+
fun gridUpTo(n: Int, k: Int) -> WVec =
|
|
88
|
+
if k == 0
|
|
90
|
-
|
|
89
|
+
WVec(wcells: Array.filled(n, Vec2(x: 0.0, y: 0.0)), wn: n)
|
|
91
|
-
|
|
90
|
+
else
|
|
92
|
-
|
|
91
|
+
gridUpTo(n, k - 1).wput(k - 1, Vec2(x: Float(k - 1), y: Float(k - 1) * 2.0))
|
|
92
|
+
|
|
93
|
-
|
|
93
|
+
fun grid(n: Int) -> WVec = gridUpTo(n, n)
|
|
94
94
|
|
|
95
95
|
struct Outer(mut inner: Vec, tag: Int)
|
|
96
96
|
|
|
@@ -112,9 +112,9 @@ test "vec: put through a helper"
|
|
|
112
112
|
test "vec: the old vector kept while the new one is built"
|
|
113
113
|
v := fillVec(3)
|
|
114
114
|
old := v
|
|
115
|
-
|
|
115
|
+
w := v.put(0, 99)
|
|
116
116
|
assert old.get(0) == 0
|
|
117
|
-
assert
|
|
117
|
+
assert w.get(0) == 99
|
|
118
118
|
|
|
119
119
|
test "vec: kept by a helper argument"
|
|
120
120
|
a := fillVec(3)
|
|
@@ -138,9 +138,9 @@ test "mut field: assign-back rebuild"
|
|
|
138
138
|
test "alias of the field kept before the update"
|
|
139
139
|
v := AVec(lanes: Array.filled(3, 0), an: 3)
|
|
140
140
|
d := v.lanesOf()
|
|
141
|
-
|
|
141
|
+
w := v.aput(0, 9)
|
|
142
142
|
assert d.get(0) == 0
|
|
143
|
-
assert
|
|
143
|
+
assert w.aget(0) == 9
|
|
144
144
|
|
|
145
145
|
test "alias of the field read directly"
|
|
146
146
|
v := AVec(lanes: Array.filled(3, 0), an: 3)
|
|
@@ -151,19 +151,18 @@ test "alias of the field read directly"
|
|
|
151
151
|
|
|
152
152
|
test "two structs sharing one array"
|
|
153
153
|
arr := Array.filled(3, 0)
|
|
154
|
-
a := SVec(slots: arr, sn: 0)
|
|
155
154
|
b := SVec(slots: arr, sn: 0)
|
|
156
|
-
|
|
155
|
+
written := SVec(slots: arr, sn: 0).sput(0, 5)
|
|
157
156
|
assert b.sget(0) == 0
|
|
158
157
|
assert arr.get(0) == 0
|
|
159
|
-
assert
|
|
158
|
+
assert written.sget(0) == 5
|
|
160
159
|
|
|
161
160
|
test "the vector itself aliased"
|
|
162
161
|
v := EVec(ecells: Array.filled(3, 0), en: 0)
|
|
163
162
|
w := v
|
|
164
|
-
|
|
163
|
+
u := v.eput(1, 6)
|
|
165
164
|
assert w.eget(1) == 0
|
|
166
|
-
assert
|
|
165
|
+
assert u.eget(1) == 6
|
|
167
166
|
|
|
168
167
|
test "the vector captured by a closure"
|
|
169
168
|
v := CVec(ccells: Array.filled(3, 0), cn: 0)
|
|
@@ -183,32 +182,31 @@ test "the vector stored by a helper then updated"
|
|
|
183
182
|
test "a loop keeping every previous vector"
|
|
184
183
|
snaps := Slice[HVec]()
|
|
185
184
|
v := HVec(hcells: Array.filled(3, 0), hn: 0)
|
|
186
|
-
|
|
185
|
+
last := 0.until(3).fold(v, |cur, i|
|
|
187
|
-
snaps.push(
|
|
186
|
+
snaps.push(cur)
|
|
188
|
-
|
|
187
|
+
cur.hput(i, i + 1)
|
|
188
|
+
)
|
|
189
189
|
assert snaps.at(0).hget(0) == 0
|
|
190
190
|
assert snaps.at(1).hget(0) == 1 && snaps.at(1).hget(1) == 0
|
|
191
191
|
assert snaps.at(2).hget(1) == 2 && snaps.at(2).hget(2) == 0
|
|
192
|
-
assert
|
|
192
|
+
assert last.hget(2) == 3
|
|
193
193
|
|
|
194
194
|
test "a loop where the previous vector lives in a variable"
|
|
195
195
|
prev := IVec(icells: Array.filled(3, 0), iprev: 0)
|
|
196
196
|
v := prev
|
|
197
|
-
|
|
197
|
+
# state: first is the previous vector, second the current one
|
|
198
|
-
prev = v
|
|
199
|
-
|
|
198
|
+
end := 0.until(3).fold(Pair(prev, v), |st, i| Pair(st.second, st.second.iput(i, 1)))
|
|
200
|
-
assert
|
|
199
|
+
assert end.first.iget(1) == 1 && end.first.iget(2) == 0
|
|
201
|
-
assert
|
|
200
|
+
assert end.second.iget(2) == 1
|
|
202
201
|
|
|
203
202
|
test "vec2 elements"
|
|
204
203
|
v := PVec(pcells: Array.filled(3, Vec2(x: 1.0, y: 2.0)), pn: 3)
|
|
205
204
|
w := v
|
|
206
|
-
|
|
205
|
+
once := v.pput(1, Vec2(x: 5.0, y: 6.0))
|
|
207
206
|
assert w.pget(1).x == 1.0 && w.pget(1).y == 2.0
|
|
208
|
-
assert
|
|
207
|
+
assert once.pget(1).x == 5.0 && once.pget(1).y == 6.0 && once.pget(0).y == 2.0
|
|
209
|
-
for i := range 3
|
|
210
|
-
|
|
208
|
+
all := 0.until(3).fold(once, |cur, i| cur.pput(i, Vec2(x: Float(i), y: 0.5)))
|
|
211
|
-
assert
|
|
209
|
+
assert all.pget(2).x == 2.0 && all.pget(2).y == 0.5
|
|
212
210
|
|
|
213
211
|
test "another field of the spread reads the array after the write"
|
|
214
212
|
v := QVec(qcells: Array.filled(3, 4), qn: 0)
|
|
@@ -218,10 +216,9 @@ test "another field of the spread reads the array after the write"
|
|
|
218
216
|
assert v.qget(1) == 4
|
|
219
217
|
|
|
220
218
|
test "another field reads the old element"
|
|
221
|
-
|
|
219
|
+
w := RVec(rcells: Array.filled(3, 4), rn: 0).rput(1, 9)
|
|
222
|
-
v = v.rput(1, 9)
|
|
223
|
-
assert
|
|
220
|
+
assert w.rn == 4
|
|
224
|
-
assert
|
|
221
|
+
assert w.rget(1) == 9
|
|
225
222
|
|
|
226
223
|
test "vec2 elements written in place"
|
|
227
224
|
g := grid(4)
|
tests/fbip/fip.plum
CHANGED
|
@@ -12,12 +12,11 @@ fip fun plainSum(a: Int, b: Int) -> Int = a + b
|
|
|
12
12
|
# a fresh array written once
|
|
13
13
|
fip fun freshWrite() -> Array[Int] = Array.filled(3, 0).with(1, 7)
|
|
14
14
|
|
|
15
|
-
# a local array written
|
|
15
|
+
# a local array written once per step of a recursion
|
|
16
|
-
fip fun
|
|
16
|
+
fip fun squaresUpTo(n: Int, k: Int) -> Array[Int] =
|
|
17
|
+
if k == 0 then Array.filled(n, 0) else squaresUpTo(n, k - 1).with(k - 1, {k - 1} * {k - 1})
|
|
18
|
+
|
|
17
|
-
|
|
19
|
+
fun squares(n: Int) -> Array[Int] = squaresUpTo(n, n)
|
|
18
|
-
for i := range n
|
|
19
|
-
a = a.with(i, i * i)
|
|
20
|
-
a
|
|
21
20
|
|
|
22
21
|
# a spread of a parameter every caller builds fresh
|
|
23
22
|
struct Pt(px: Int, py: Int)
|
|
@@ -25,21 +24,17 @@ struct Pt(px: Int, py: Int)
|
|
|
25
24
|
fip fun movePt(p: Pt, dx: Int) -> Pt = Pt(..p, px: p.px + dx)
|
|
26
25
|
|
|
27
26
|
fun walkPt(n: Int) -> Pt =
|
|
28
|
-
|
|
27
|
+
if n == 0 then Pt(px: 0, py: 5) else movePt(walkPt(n - 1), 2)
|
|
29
|
-
for i := range n
|
|
30
|
-
p = movePt(p, 2)
|
|
31
|
-
p
|
|
32
28
|
|
|
33
|
-
# a fip method: self handed
|
|
29
|
+
# a fip method: self handed on from the recursive call
|
|
34
30
|
struct Bag(items: Array[Int], count: Int) =
|
|
35
31
|
fip fun bagAdd(self, i: Int, x: Int) -> Bag = Bag(..self, items: self.items.with(i, x), count: self.count + 1)
|
|
36
32
|
fun bagAt(self, i: Int) -> Int = self.items.get(i)
|
|
37
33
|
|
|
38
|
-
fun
|
|
34
|
+
fun fillBagUpTo(n: Int, k: Int) -> Bag =
|
|
39
|
-
|
|
35
|
+
if k == 0 then Bag(items: Array.filled(n, 0), count: 0) else fillBagUpTo(n, k - 1).bagAdd(k - 1, k + 9)
|
|
40
|
-
|
|
36
|
+
|
|
41
|
-
|
|
37
|
+
fun fillBag(n: Int) -> Bag = fillBagUpTo(n, n)
|
|
42
|
-
b
|
|
43
38
|
|
|
44
39
|
# a fip fun whose update sits in a closure-free helper chain
|
|
45
40
|
fip fun bumpAt(xs: Array[Int], i: Int) -> Array[Int] = xs.with(i, xs.get(i) + 1)
|
tests/fbip/slice_shapes.plum
CHANGED
|
@@ -43,9 +43,7 @@ fun grow4(v: Slice[Int], x: Int) -> Slice[Int] =
|
|
|
43
43
|
v
|
|
44
44
|
|
|
45
45
|
fun s4(n: Int) -> Int =
|
|
46
|
-
v := Slice[Int]()
|
|
46
|
+
v := 0.until(n).fold(Slice[Int](), |cur, i| grow4(cur, i))
|
|
47
|
-
for i := range n
|
|
48
|
-
v = grow4(v, i)
|
|
49
47
|
v.length() + v.at(n - 1)
|
|
50
48
|
|
|
51
49
|
fun s5(n: Int) -> Int =
|
|
@@ -101,9 +99,7 @@ fun step10(s: State10, x: Int) -> State10 =
|
|
|
101
99
|
State10(..s, tick: s.tick + 1)
|
|
102
100
|
|
|
103
101
|
fun s10(n: Int) -> Int =
|
|
104
|
-
s := State10(items: Slice[Int](), tick: 0)
|
|
102
|
+
s := 0.until(n).fold(State10(items: Slice[Int](), tick: 0), |cur, i| step10(cur, i))
|
|
105
|
-
for i := range n
|
|
106
|
-
s = step10(s, i)
|
|
107
103
|
s.items.length() + s.items.at(n - 1) + s.tick
|
|
108
104
|
|
|
109
105
|
struct World11(items: Slice[Int], frame: Int)
|
|
@@ -113,74 +109,43 @@ fun frame11(w: World11, n: Int) -> World11 =
|
|
|
113
109
|
w.items.putAt(i, w.items.takeAt(i) + 1)
|
|
114
110
|
World11(..w, frame: w.frame + 1)
|
|
115
111
|
|
|
112
|
+
fun framesUpTo11(v: Slice[Int], n: Int, f: Int) -> World11 =
|
|
113
|
+
if f == 0 then World11(items: v, frame: 0) else frame11(framesUpTo11(v, n, f - 1), n)
|
|
114
|
+
|
|
116
115
|
fun s11(n: Int, frames: Int) -> Int =
|
|
117
116
|
v := Slice[Int]()
|
|
118
117
|
for i := range n
|
|
119
118
|
v.push(0)
|
|
120
|
-
w :=
|
|
119
|
+
w := framesUpTo11(v, n, frames)
|
|
121
|
-
for f := range frames
|
|
122
|
-
w = frame11(w, n)
|
|
123
120
|
w.items.at(0) + w.items.at(n - 1) + w.frame
|
|
124
121
|
|
|
125
122
|
# the persistent alternative to Slice: a List, rebuilt each frame by `map`
|
|
126
123
|
fun s11list(n: Int, frames: Int) -> Int =
|
|
127
|
-
|
|
124
|
+
empty: List[Int] := Nil
|
|
128
|
-
for i := range n
|
|
129
|
-
|
|
125
|
+
zeros := 0.until(n).fold(empty, |l, i| Cons(head: 0, tail: l))
|
|
130
|
-
for f := range frames
|
|
131
|
-
|
|
126
|
+
bumped := 0.until(frames).fold(zeros, |l, f| l.map(|x| x + 1))
|
|
132
|
-
|
|
127
|
+
bumped.at(0) + bumped.at(n - 1) + frames
|
|
133
128
|
|
|
134
|
-
fun
|
|
129
|
+
fun timed(io: Io, label: Str, work: fn() -> Int) -> Unit =
|
|
135
|
-
n := 100000
|
|
136
130
|
t := Time.now(io).toMillis()
|
|
137
|
-
r :=
|
|
131
|
+
r := work()
|
|
138
132
|
u := Time.now(io).toMillis()
|
|
139
|
-
io.printLn("
|
|
133
|
+
io.printLn("{label} result={r} ms={u - t}")
|
|
134
|
+
|
|
140
|
-
|
|
135
|
+
fun main(io: Io) =
|
|
141
|
-
|
|
136
|
+
n := 100000
|
|
142
|
-
|
|
137
|
+
timed(io, "slice shape 1", || s1(n))
|
|
143
|
-
|
|
138
|
+
timed(io, "slice shape 2", || s2(n))
|
|
144
|
-
t = Time.now(io).toMillis()
|
|
145
|
-
r = s3(n)
|
|
146
|
-
u = Time.now(io).toMillis()
|
|
147
|
-
|
|
139
|
+
timed(io, "slice shape 3", || s3(n))
|
|
148
|
-
t = Time.now(io).toMillis()
|
|
149
|
-
r = s4(n)
|
|
150
|
-
u = Time.now(io).toMillis()
|
|
151
|
-
|
|
140
|
+
timed(io, "slice shape 4", || s4(n))
|
|
152
|
-
t = Time.now(io).toMillis()
|
|
153
|
-
r = s5(n)
|
|
154
|
-
u = Time.now(io).toMillis()
|
|
155
|
-
|
|
141
|
+
timed(io, "slice shape 5", || s5(n))
|
|
156
|
-
t = Time.now(io).toMillis()
|
|
157
|
-
r = s6(n)
|
|
158
|
-
u = Time.now(io).toMillis()
|
|
159
|
-
|
|
142
|
+
timed(io, "slice shape 6", || s6(n))
|
|
160
|
-
t = Time.now(io).toMillis()
|
|
161
|
-
r = s7(n)
|
|
162
|
-
u = Time.now(io).toMillis()
|
|
163
|
-
|
|
143
|
+
timed(io, "slice shape 7", || s7(n))
|
|
164
|
-
t = Time.now(io).toMillis()
|
|
165
|
-
r = s8(n)
|
|
166
|
-
u = Time.now(io).toMillis()
|
|
167
|
-
|
|
144
|
+
timed(io, "slice shape 8", || s8(n))
|
|
168
|
-
t = Time.now(io).toMillis()
|
|
169
|
-
r = s9(n / 2)
|
|
170
|
-
u = Time.now(io).toMillis()
|
|
171
|
-
|
|
145
|
+
timed(io, "slice shape 9", || s9(n / 2))
|
|
172
|
-
t = Time.now(io).toMillis()
|
|
173
|
-
r = s10(n)
|
|
174
|
-
u = Time.now(io).toMillis()
|
|
175
|
-
|
|
146
|
+
timed(io, "slice shape 10", || s10(n))
|
|
176
|
-
t = Time.now(io).toMillis()
|
|
177
|
-
r = s11(20000, 200)
|
|
178
|
-
u = Time.now(io).toMillis()
|
|
179
|
-
|
|
147
|
+
timed(io, "slice shape 11", || s11(20000, 200))
|
|
180
|
-
t = Time.now(io).toMillis()
|
|
181
|
-
r = s11list(20000, 200)
|
|
182
|
-
u = Time.now(io).toMillis()
|
|
183
|
-
|
|
148
|
+
timed(io, "list map shape 11", || s11list(20000, 200))
|
|
184
149
|
|
|
185
150
|
test "slice shapes agree with the vec shapes"
|
|
186
151
|
assert s1(1000) == 2499
|
tests/fbip/spread_ro_method.plum
CHANGED
|
@@ -22,19 +22,16 @@ fun runIt(f: fn() -> Int) -> Int = f()
|
|
|
22
22
|
fun roClosure(v: RoC) -> RoC = RoC(..v, ccells: v.ccells.with(0, 9), ctotal: runIt(|| v.csum0()))
|
|
23
23
|
|
|
24
24
|
test "a read-only method of self inside the spread sees the old array"
|
|
25
|
-
|
|
25
|
+
w := RoA(rcells: Array.filled(3, 5), rtotal: 0).rupd()
|
|
26
|
-
v = v.rupd()
|
|
27
|
-
assert
|
|
26
|
+
assert w.rcells.get(0) == 9
|
|
28
|
-
assert
|
|
27
|
+
assert w.rtotal == 5
|
|
29
28
|
|
|
30
29
|
test "a read-only method of the spread source from a free fn"
|
|
31
|
-
|
|
30
|
+
w := roUpd(RoB(qcells: Array.filled(3, 5), qtotal: 0))
|
|
32
|
-
v = roUpd(v)
|
|
33
|
-
assert
|
|
31
|
+
assert w.qcells.get(0) == 9
|
|
34
|
-
assert
|
|
32
|
+
assert w.qtotal == 5
|
|
35
33
|
|
|
36
34
|
test "a closure reading the spread source"
|
|
37
|
-
|
|
35
|
+
w := roClosure(RoC(ccells: Array.filled(3, 5), ctotal: 0))
|
|
38
|
-
v = roClosure(v)
|
|
39
|
-
assert
|
|
36
|
+
assert w.ccells.get(0) == 9
|
|
40
|
-
assert
|
|
37
|
+
assert w.ctotal == 5
|
tests/fbip_probes/array_set.plum
CHANGED
|
@@ -12,10 +12,7 @@ fun run(n: Int) -> Int =
|
|
|
12
12
|
a := Array[Int]().resize(1000)
|
|
13
13
|
for i := range n
|
|
14
14
|
a.set(i % 1000, i)
|
|
15
|
-
total := 0
|
|
16
|
-
for j := range 1000
|
|
17
|
-
|
|
15
|
+
0.until(1000).fold(0, |total, j| total + a.get(j))
|
|
18
|
-
total
|
|
19
16
|
|
|
20
17
|
fun main(io: Io) =
|
|
21
18
|
t0 := Time.now(io).toMillis()
|
tests/fbip_probes/map_set_get.plum
CHANGED
|
@@ -13,10 +13,7 @@ fun run(n: Int) -> Int =
|
|
|
13
13
|
m: Map[Int, Int] := Map.empty()
|
|
14
14
|
for i := range n
|
|
15
15
|
m.set(i, i * 2)
|
|
16
|
-
total := 0
|
|
17
|
-
for i := range n
|
|
18
|
-
|
|
16
|
+
0.until(n).fold(0, |total, i| total + m.get(i).unwrapOr(0))
|
|
19
|
-
total
|
|
20
17
|
|
|
21
18
|
fun main(io: Io) =
|
|
22
19
|
t0 := Time.now(io).toMillis()
|
tests/fbip_probes/struct_spread.plum
CHANGED
|
@@ -10,9 +10,7 @@ struct Cell(label: Str, n: Int, m: Int)
|
|
|
10
10
|
fun size() -> Int = 1000000
|
|
11
11
|
|
|
12
12
|
fun run(count: Int) -> Int =
|
|
13
|
-
c := Cell(label: "cell", n: 0, m: 0)
|
|
13
|
+
c := 0.until(count).fold(Cell(label: "cell", n: 0, m: 0), |cur, i| Cell(..cur, n: cur.n + 1))
|
|
14
|
-
for i := range count
|
|
15
|
-
c = Cell(..c, n: c.n + 1)
|
|
16
14
|
c.n + c.m
|
|
17
15
|
|
|
18
16
|
fun main(io: Io) =
|
tests/param_defaults.plum
CHANGED
|
@@ -9,6 +9,7 @@ import std/Option
|
|
|
9
9
|
|
|
10
10
|
struct Cell(n: Int, label: Str)
|
|
11
11
|
struct Flat(n: Int)
|
|
12
|
+
struct Tally(mut n: Int)
|
|
12
13
|
struct Box[T](items: List[T], label: Str)
|
|
13
14
|
|
|
14
15
|
enum Shape =
|
|
@@ -16,10 +17,10 @@ enum Shape =
|
|
|
16
17
|
| Rect(w: Int, label: Str)
|
|
17
18
|
|
|
18
19
|
fun times3(d: Int) -> Int =
|
|
19
|
-
|
|
20
|
+
t := Tally(n: 0)
|
|
20
21
|
for i := range 3
|
|
21
|
-
|
|
22
|
+
t.n = t.n + d
|
|
22
|
-
|
|
23
|
+
t.n
|
|
23
24
|
|
|
24
25
|
fun withInt(d: Int = 7) -> Int = times3(d)
|
|
25
26
|
fun withStr(d: Str = "abc") -> Int = times3(d.length())
|
|
@@ -44,10 +45,8 @@ fun withNullary(d: Shape = Dot) -> Int =
|
|
|
44
45
|
|
|
45
46
|
# the parameter is rebuilt with a spread
|
|
46
47
|
fun respread(d: Cell = Cell(n: 1, label: "a")) -> Int =
|
|
47
|
-
|
|
48
|
+
first := Cell(..d, n: d.n + 1)
|
|
48
|
-
for i := range 3
|
|
49
|
-
|
|
49
|
+
0.until(3).fold(first, |c, i| Cell(..c, n: c.n + 1)).n
|
|
50
|
-
d.n
|
|
51
50
|
|
|
52
51
|
# a default that calls a fn, a generic fn, and an expression
|
|
53
52
|
fun made() -> Int = times3(2)
|
tests/syntax/block_values_passes.plum
CHANGED
|
@@ -170,10 +170,10 @@ fun alternating(n: Int) -> Int = sumList(upTo(n))
|
|
|
170
170
|
|
|
171
171
|
fun loopInBlock(n: Int) -> Int =
|
|
172
172
|
v := if n > 0
|
|
173
|
-
|
|
173
|
+
t := Tally(n: 0)
|
|
174
174
|
for i := range n
|
|
175
|
-
|
|
175
|
+
t.n = t.n + i
|
|
176
|
-
|
|
176
|
+
t.n
|
|
177
177
|
else
|
|
178
178
|
0 - 1
|
|
179
179
|
v
|
|
@@ -185,17 +185,15 @@ fun retInBlock(n: Int) -> Int =
|
|
|
185
185
|
n * 2
|
|
186
186
|
v + 1
|
|
187
187
|
|
|
188
|
-
fun
|
|
188
|
+
fun blockInLoop(n: Int) -> Int =
|
|
189
|
-
acc := 0
|
|
190
|
-
|
|
189
|
+
t := Tally(n: 0)
|
|
191
|
-
|
|
190
|
+
for i := range n
|
|
192
|
-
i = i + 1
|
|
193
|
-
d := if i == 3
|
|
191
|
+
d := if i + 1 == 3
|
|
194
192
|
0
|
|
195
193
|
else
|
|
196
|
-
i
|
|
194
|
+
i + 1
|
|
197
|
-
|
|
195
|
+
t.n = t.n + d
|
|
198
|
-
|
|
196
|
+
t.n
|
|
199
197
|
|
|
200
198
|
fun condBlock(n: Int) -> Bool =
|
|
201
199
|
n > 0 && { if n > 5
|
|
@@ -221,13 +219,13 @@ fun capture2(n: Int, xs: List[Int]) -> List[Int] =
|
|
|
221
219
|
v
|
|
222
220
|
|
|
223
221
|
fun whileCond(n: Int) -> Int =
|
|
224
|
-
|
|
222
|
+
t := Tally(n: 0)
|
|
225
|
-
while { if
|
|
223
|
+
while { if t.n < n
|
|
226
224
|
true
|
|
227
225
|
else
|
|
228
226
|
false }
|
|
229
|
-
|
|
227
|
+
t.n = t.n + 1
|
|
230
|
-
|
|
228
|
+
t.n
|
|
231
229
|
|
|
232
230
|
fun patNames(o: Option[Int], p: Option[Int]) -> Int =
|
|
233
231
|
a := match o
|
|
@@ -368,7 +366,7 @@ test "loops, return, closures and conditional positions"
|
|
|
368
366
|
assert loopInBlock(0) == 0 - 1
|
|
369
367
|
assert retInBlock(9) == 99
|
|
370
368
|
assert retInBlock(2) == 5
|
|
371
|
-
assert
|
|
369
|
+
assert blockInLoop(5) == 1 + 2 + 4 + 5
|
|
372
370
|
assert condBlock(7)
|
|
373
371
|
assert !condBlock(3)
|
|
374
372
|
assert capture(3) == 16
|
tests/syntax/question.plum
CHANGED
|
@@ -83,12 +83,14 @@ fun discarded(n: Int) -> Result[Int, Oops] =
|
|
|
83
83
|
_ := half(n)?
|
|
84
84
|
Ok(n)
|
|
85
85
|
|
|
86
|
-
fun
|
|
86
|
+
fun sumHalves(i: Int, n: Int, total: Int) -> Result[Int, Oops] =
|
|
87
|
-
total := 0
|
|
88
|
-
|
|
87
|
+
if i >= n
|
|
88
|
+
Ok(total)
|
|
89
|
+
else
|
|
89
90
|
h := half(i * 2)?
|
|
90
|
-
|
|
91
|
+
sumHalves(i + 1, n, total + h)
|
|
92
|
+
|
|
91
|
-
|
|
93
|
+
fun inLoop(n: Int) -> Result[Int, Oops] = sumHalves(0, n, 0)
|
|
92
94
|
|
|
93
95
|
test "chains"
|
|
94
96
|
assert val(quarter(8)) == 2
|
tests/wasm_codegen/e2e_tail/question_in_tail_not_tail_call.plum
CHANGED
|
@@ -5,9 +5,10 @@ enum NegError = Neg | Other
|
|
|
5
5
|
|
|
6
6
|
fun down(n: Int) -> Result[Int, NegError] =
|
|
7
7
|
if n == 0
|
|
8
|
-
|
|
8
|
+
Ok(5)
|
|
9
|
+
else
|
|
9
|
-
|
|
10
|
+
v := down(n - 1)?
|
|
10
|
-
|
|
11
|
+
Ok(v)
|
|
11
12
|
|
|
12
13
|
fun runMain() -> Int = down(100).unwrapOr(99)
|
|
13
14
|
|
tests/wasm_codegen/e2e_tail/result_mutual_million.plum
CHANGED
|
@@ -5,13 +5,15 @@ enum NegError = Neg | Other
|
|
|
5
5
|
|
|
6
6
|
fun ping(n: Int) -> Result[Int, NegError] =
|
|
7
7
|
if n == 0
|
|
8
|
-
|
|
8
|
+
Ok(1)
|
|
9
|
+
else
|
|
9
|
-
|
|
10
|
+
pong(n - 1)
|
|
10
11
|
|
|
11
12
|
fun pong(n: Int) -> Result[Int, NegError] =
|
|
12
13
|
if n == 0
|
|
13
|
-
|
|
14
|
+
Ok(2)
|
|
15
|
+
else
|
|
14
|
-
|
|
16
|
+
ping(n - 1)
|
|
15
17
|
|
|
16
18
|
fun runMain() -> Int = ping(1000000).unwrapOr(99)
|
|
17
19
|
|
tests/wasm_codegen/e2e_tail/result_self_loop_million.plum
CHANGED
|
@@ -5,10 +5,11 @@ enum NegError = Neg | Other
|
|
|
5
5
|
|
|
6
6
|
fun down(n: Int) -> Result[Int, NegError] =
|
|
7
7
|
if n < 0
|
|
8
|
-
|
|
8
|
+
Err(Neg)
|
|
9
|
-
if n == 0
|
|
9
|
+
else if n == 0
|
|
10
|
-
|
|
10
|
+
Ok(5)
|
|
11
|
+
else
|
|
11
|
-
|
|
12
|
+
down(n - 1)
|
|
12
13
|
|
|
13
14
|
fun runMain() -> Int = down(1000000).unwrapOr(99)
|
|
14
15
|
|
tests/wasm_codegen/e2e_tail/result_self_loop_returns_err.plum
CHANGED
|
@@ -5,8 +5,9 @@ enum NegError = Neg | Other
|
|
|
5
5
|
|
|
6
6
|
fun down(n: Int) -> Result[Int, NegError] =
|
|
7
7
|
if n == 0
|
|
8
|
-
|
|
8
|
+
Err(Neg)
|
|
9
|
+
else
|
|
9
|
-
|
|
10
|
+
down(n - 1)
|
|
10
11
|
|
|
11
12
|
fun runMain() -> Int = down(1000000).unwrapOr(42)
|
|
12
13
|
|
tests/wasm_codegen/e2e_types/chained_err_value_is_first.plum
CHANGED
|
@@ -6,8 +6,9 @@ struct Pt(first: Int, second: Int)
|
|
|
6
6
|
|
|
7
7
|
fun pick(n: Int) -> Result[Int, Int] =
|
|
8
8
|
if n > 0
|
|
9
|
-
|
|
9
|
+
Ok(n)
|
|
10
|
+
else
|
|
10
|
-
|
|
11
|
+
Err(100 + n)
|
|
11
12
|
|
|
12
13
|
fun f(x: Int, y: Int, z: Int) -> Result[Int, Int] =
|
|
13
14
|
p := pick(x)?
|
tests/wasm_codegen/e2e_types/chained_question_returns_first_err.plum
CHANGED
|
@@ -6,8 +6,9 @@ struct Pt(first: Int, second: Int)
|
|
|
6
6
|
|
|
7
7
|
fun pick(n: Int) -> Result[Int, Int] =
|
|
8
8
|
if n > 0
|
|
9
|
-
|
|
9
|
+
Ok(n)
|
|
10
|
+
else
|
|
10
|
-
|
|
11
|
+
Err(100 + n)
|
|
11
12
|
|
|
12
13
|
fun f(x: Int, y: Int, z: Int) -> Result[Int, Int] =
|
|
13
14
|
p := pick(x)?
|
tests/wasm_codegen/e2e_types/nested_generic.plum
CHANGED
|
@@ -9,12 +9,12 @@ fun buildAll[T](ns: Slice[Int]) -> Slice[T] =
|
|
|
9
9
|
out := Slice[T]()
|
|
10
10
|
for n := range ns
|
|
11
11
|
out.push(T.make(n))
|
|
12
|
-
|
|
12
|
+
out
|
|
13
13
|
|
|
14
14
|
fun runMain() -> Int =
|
|
15
15
|
ps: Slice[Pt] := buildAll([1, 2, 3].toSlice())
|
|
16
16
|
p := ps.get(2).unwrapOr(Pt(x: 0))
|
|
17
|
-
|
|
17
|
+
p.x
|
|
18
18
|
|
|
19
19
|
# moved from e2e_types::type_param_static::nested_generic
|
|
20
20
|
test "nested generic"
|
tests/wasm_codegen/e2e_types/ok_unit_and_expression_position.plum
CHANGED
|
@@ -8,8 +8,9 @@ fun g(n: Int) -> Option[Int] = if n > 0 then Some(n) else None
|
|
|
8
8
|
|
|
9
9
|
fun u(n: Int) -> Result[Unit, Int] =
|
|
10
10
|
if n > 0
|
|
11
|
-
|
|
11
|
+
Ok(Unit)
|
|
12
|
+
else
|
|
12
|
-
|
|
13
|
+
Err(n)
|
|
13
14
|
|
|
14
15
|
fun h(n: Int) -> Option[Int] = Some(1 + g(n)?)
|
|
15
16
|
|
tests/wasm_codegen/e2e_types/seq_with_optional_next.plum
CHANGED
|
@@ -1,15 +1,16 @@
|
|
|
1
1
|
import std/Int
|
|
2
2
|
import std/Option
|
|
3
3
|
|
|
4
|
+
struct Cursor(mut i: Int, mut total: Int)
|
|
5
|
+
|
|
4
6
|
struct Seq[T](next: fn(Int) -> Option[T]) =
|
|
5
7
|
fun upTo(n: Int) -> Seq[Int] = Seq(next: |i| {if i < n then Some(i * 2) else None})
|
|
6
8
|
fun sum(self) -> Int =
|
|
7
|
-
|
|
9
|
+
c := Cursor(i: 0, total: 0)
|
|
8
|
-
i := 0
|
|
9
|
-
while Some(v) := self.next(i)
|
|
10
|
+
while Some(v) := self.next(c.i)
|
|
10
|
-
total = total + v
|
|
11
|
+
c.total = c.total + v
|
|
11
|
-
i = i + 1
|
|
12
|
+
c.i = c.i + 1
|
|
12
|
-
total
|
|
13
|
+
c.total
|
|
13
14
|
|
|
14
15
|
fun runMain() -> Int =
|
|
15
16
|
Seq.upTo(4).sum()
|
tests/wasm_codegen/e2e_types/static_on_builtin.plum
CHANGED
|
@@ -4,7 +4,7 @@ fun maxOf[T](n: Int) -> T = T.Max
|
|
|
4
4
|
|
|
5
5
|
fun runMain() -> Int =
|
|
6
6
|
m: Int := maxOf(1)
|
|
7
|
-
|
|
7
|
+
if m == Int.Max then 1 else 0
|
|
8
8
|
|
|
9
9
|
# moved from e2e_types::type_param_static::static_on_builtin
|
|
10
10
|
test "static on builtin"
|
tests/wasm_codegen/e2e_types/static_on_builtin_method_and_json_cycle.plum
CHANGED
|
@@ -10,7 +10,7 @@ fun runMain() -> Int =
|
|
|
10
10
|
r: Result[Int, NumError] := parseIt("41")
|
|
11
11
|
n := r.unwrapOr(0)
|
|
12
12
|
j := n.toJson()
|
|
13
|
-
|
|
13
|
+
n + 1
|
|
14
14
|
|
|
15
15
|
# moved from e2e_types::type_param_static::static_on_builtin_method_and_json_cycle
|
|
16
16
|
test "static on builtin method and json cycle"
|
tests/wasm_codegen/e2e_types/static_on_type_param.plum
CHANGED
|
@@ -7,7 +7,7 @@ fun build[T](n: Int) -> T = T.make(n)
|
|
|
7
7
|
|
|
8
8
|
fun runMain() -> Int =
|
|
9
9
|
p: Pt := build(3)
|
|
10
|
-
|
|
10
|
+
p.x
|
|
11
11
|
|
|
12
12
|
# moved from e2e_types::type_param_static::static_on_type_param
|
|
13
13
|
test "static on type param"
|
tests/wasm_codegen/e2e_types/user_type_shadows_std_pair_imported_by_std.plum
CHANGED
|
@@ -4,9 +4,7 @@ struct Pair(a: Int, b: Int) =
|
|
|
4
4
|
fun sum(self) -> Int = self.a + self.b
|
|
5
5
|
|
|
6
6
|
fun runMain() -> Int =
|
|
7
|
-
p := Pair(a: 3, b: 4)
|
|
8
|
-
for i := range 3
|
|
9
|
-
|
|
7
|
+
p := 0.until(3).fold(Pair(a: 3, b: 4), |q, i| Pair(a: q.a + i, b: q.b))
|
|
10
8
|
p.a * 10 + p.sum()
|
|
11
9
|
|
|
12
10
|
# moved from e2e_types::list_and_shadow::user_type_shadows_std_pair_imported_by_std
|