plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
83ae9ce
— Peter John
2026-11-10T15:22:22+05:30
No + on Str: build strings with interpolation
- compiler/checker/src/infer.rs +6 -0
- compiler/checker/tests/checker/main.rs +1 -0
- compiler/checker/tests/checker/str_plus_tests.rs +73 -0
- compiler/core/src/tests_lower.rs +8 -6
- compiler/wasm-codegen/src/construct.rs +4 -4
- compiler/wasm-codegen/tests/wasm_codegen/codegen_tests.rs +1 -1
- compiler/wasm-codegen/tests/wasm_codegen/dispatch_test.rs +2 -2
- compiler/wasm-codegen/tests/wasm_codegen/literals_test.rs +2 -2
- docs/language.md +2 -0
- docs/std.md +1 -1
- examples/functional.plum +3 -3
- examples/generics.plum +4 -4
- examples/maps.plum +1 -1
- examples/traits.plum +1 -1
- std/Decimal.plum +6 -6
- std/Float.plum +4 -4
- std/Int.plum +3 -3
- std/Os.plum +14 -14
- std/Slice.plum +1 -1
- std/Str.plum +18 -18
- std/Time.plum +3 -4
- std/Uuid.plum +2 -4
- std/collections/BPlusTree.plum +1 -1
- std/collections/BTree.plum +1 -1
- std/collections/Hamt.plum +2 -2
- std/collections/IntervalTree.plum +1 -1
- std/collections/KdTree.plum +1 -1
- std/collections/LeftistHeap.plum +1 -1
- std/collections/List.plum +1 -1
- std/collections/Map.plum +2 -2
- std/collections/PairingHeap.plum +1 -1
- std/collections/Quadtree.plum +1 -1
- std/collections/RadixTree.plum +1 -1
- std/collections/Trie.plum +1 -1
- std/encoding/Base64.plum +1 -1
- std/encoding/Json.plum +5 -5
- std/testing.plum +1 -1
- tests/core/literal_patterns.plum +1 -1
- tests/core/str_no_plus.plum +51 -0
- tests/modules/constants.plum +1 -1
- tests/std/json.plum +5 -5
- tests/traits/statics_and_type_name.plum +2 -2
- tests/traits/tags.plum +1 -1
compiler/checker/src/infer.rs
CHANGED
|
@@ -374,6 +374,12 @@ pub(crate) fn inferExprInner(
|
|
|
374
374
|
if matches!(lt, PlumType::TParam(_)) && lt != rt {
|
|
375
375
|
return Ok(PlumType::TVar("_".to_string()));
|
|
376
376
|
}
|
|
377
|
+
// `Str` has no `+`. `Int + Str` stays a plain operand mismatch, reported at the `Str`.
|
|
378
|
+
let isStr = |t: &PlumType| matches!(resolveRecorded(t), PlumType::TNamed(ref n) if n == ast::STR_TYPE);
|
|
379
|
+
let leftOpen = matches!(resolveRecorded(<), PlumType::TVar(_));
|
|
380
|
+
if matches!(b.op, ast::BinOp::Add) && (isStr(<) || (leftOpen && isStr(&rt))) {
|
|
381
|
+
return Err(blame(expr, "Str has no +; use interpolation, \"{a}{b}\"".to_string()));
|
|
382
|
+
}
|
|
377
383
|
if isOperatorOperand(<) {
|
|
378
384
|
return inferOperatorCall(
|
|
379
385
|
expr, <, sym, method, Some(&b.right), env, ctx, expected,
|
compiler/checker/tests/checker/main.rs
CHANGED
|
@@ -11,5 +11,6 @@ mod fip_test;
|
|
|
11
11
|
mod fusion_pipeline_test;
|
|
12
12
|
mod inline_variants_tests;
|
|
13
13
|
mod question_tests;
|
|
14
|
+
mod str_plus_tests;
|
|
14
15
|
mod syntax_tests;
|
|
15
16
|
mod traits_tests;
|
compiler/checker/tests/checker/str_plus_tests.rs
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
#![allow(non_snake_case)]
|
|
2
|
+
//! `Str` has no `+`: strings are built with interpolation, `"{a}{b}"`.
|
|
3
|
+
use plum_checker::checkSource;
|
|
4
|
+
|
|
5
|
+
fn run(body: &str) -> Result<(), Vec<plum_checker::types::CheckError>> {
|
|
6
|
+
let mut source = plum_test_support::parse(&format!("{body}\n"));
|
|
7
|
+
plum_core::lower::frontEndDiag(&mut source).expect("front end");
|
|
8
|
+
checkSource(&source)
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
fn strPlusError(body: &str) -> plum_checker::types::CheckError {
|
|
12
|
+
let es = run(body).expect_err(&format!("expected an error:\n{body}"));
|
|
13
|
+
es.iter()
|
|
14
|
+
.find(|e| e.message.contains("Str has no +"))
|
|
15
|
+
.cloned()
|
|
16
|
+
.unwrap_or_else(|| panic!("{body}\nno Str + error in {:?}", es.iter().map(|x| &x.message).collect::<Vec<_>>()))
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
#[test]
|
|
20
|
+
fn str_plus_str_names_interpolation() {
|
|
21
|
+
let e = strPlusError("fun f(a: Str, b: Str) -> Str =\n a + b");
|
|
22
|
+
assert!(e.message.contains("Str has no +; use interpolation, \"{a}{b}\""), "{e:?}");
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/// The error is at the `+` expression, not at the enclosing statement or fn.
|
|
26
|
+
#[test]
|
|
27
|
+
fn the_error_is_at_the_plus_expression() {
|
|
28
|
+
let e = strPlusError("fun f(a: Str, b: Str) -> Int =\n n := {a + b}.length()\n n");
|
|
29
|
+
assert_eq!((e.line, e.column), (2, 9), "{e:?}");
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
#[test]
|
|
33
|
+
fn str_plus_int_names_interpolation() {
|
|
34
|
+
let e = strPlusError("fun f(a: Str, n: Int) -> Str =\n a + n");
|
|
35
|
+
assert!(e.message.contains("use interpolation"), "{e:?}");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
#[test]
|
|
39
|
+
fn an_untyped_closure_param_plus_str_names_interpolation() {
|
|
40
|
+
let e = strPlusError("fun f(s: Str) -> Str =\n g := |y| y + s\n g(s)");
|
|
41
|
+
assert!(e.message.contains("use interpolation"), "{e:?}");
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
#[test]
|
|
45
|
+
fn literal_plus_literal_names_interpolation() {
|
|
46
|
+
strPlusError("fun f() -> Str =\n \"x=\" + \"y\"");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/// `Int + Str` stays a plain operand mismatch, reported at the `Str`.
|
|
50
|
+
#[test]
|
|
51
|
+
fn int_plus_str_is_an_operand_mismatch() {
|
|
52
|
+
let es = run("fun f() -> Int =\n 1 + \"a\"").expect_err("mismatch");
|
|
53
|
+
assert!(es.iter().any(|e| e.message.contains("binary op")), "{es:?}");
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/// A generic fn that adds its operands fails where it is used with `Str`.
|
|
57
|
+
#[test]
|
|
58
|
+
fn a_generic_plus_instantiated_with_str_is_an_error() {
|
|
59
|
+
let es = run("fun plus[T](a: T, b: T) -> T =\n a + b\n\nfun main() -> Str =\n plus(\"a\", \"b\")").expect_err("no + on Str");
|
|
60
|
+
assert!(es.iter().any(|e| e.message.contains("Str has no +") || e.message.contains("'add'")), "{es:?}");
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
#[test]
|
|
64
|
+
fn numeric_plus_is_untouched() {
|
|
65
|
+
run("fun f(a: Int, b: Int) -> Int =\n a + b").unwrap();
|
|
66
|
+
run("fun g(a: Float, b: Float) -> Float =\n a + b").unwrap();
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
#[test]
|
|
70
|
+
fn interpolation_and_concat_type_check() {
|
|
71
|
+
run("fun f(a: Str, n: Int) -> Str =\n \"x={a}, {n + 1}, {{braces}}, \\\"q\\\"\"").unwrap();
|
|
72
|
+
run("fun f(a: Str, b: Str) -> Str =\n a.concat(b)").unwrap();
|
|
73
|
+
}
|
compiler/core/src/tests_lower.rs
CHANGED
|
@@ -183,7 +183,7 @@ fn desugarTestsToFns(source: &crate::ast::Source) -> (crate::ast::Source, Vec<(S
|
|
|
183
183
|
|
|
184
184
|
/// Recursively rewrites every `assert cond` (`cond` carrying its own literal
|
|
185
185
|
/// source text, captured at parse time — see `crate::ast::Check`) into
|
|
186
|
-
/// `if !cond { __report =
|
|
186
|
+
/// `if !cond { __report = "{__report}<text>\n" }`, descending into nested
|
|
187
187
|
/// control-flow blocks so an `assert` inside a `test`'s own
|
|
188
188
|
/// `if`/`while`/`for`/`match` still records into the same hidden accumulator.
|
|
189
189
|
fn desugarAssertStmts(stmts: &[crate::ast::Stmt], counter: &mut usize) -> Vec<crate::ast::Stmt> {
|
|
@@ -222,11 +222,13 @@ fn desugarAssertStmt(stmt: &crate::ast::Stmt, counter: &mut usize) -> Vec<crate:
|
|
|
222
222
|
let appendReport = crate::ast::Stmt::Assign(crate::ast::Assign {
|
|
223
223
|
isSynthetic: false,
|
|
224
224
|
targets: vec![crate::ast::AssignTarget::Field(Box::new(crate::ast::Expr::Var("__report".to_string())), REPORT_FIELD.to_string())],
|
|
225
|
+
// `"{__report.text}{message}"`: `Str` has no `+`.
|
|
225
|
-
values: vec![crate::ast::Expr::
|
|
226
|
+
values: vec![crate::ast::Expr::String(crate::ast::StringExpr {
|
|
227
|
+
parts: vec![
|
|
226
|
-
|
|
228
|
+
crate::ast::StringPart::Interp(reportField()),
|
|
227
|
-
left: reportField(),
|
|
228
|
-
|
|
229
|
+
crate::ast::StringPart::Interp(assertFailureMessage(c, &lhs, &rhs)),
|
|
230
|
+
],
|
|
229
|
-
})
|
|
231
|
+
})],
|
|
230
232
|
declare: false,
|
|
231
233
|
ty: None,
|
|
232
234
|
});
|
compiler/wasm-codegen/src/construct.rs
CHANGED
|
@@ -178,9 +178,9 @@ pub(crate) fn compileStaticString(text: &str, body: &mut Vec<u8>, state: &mut Mo
|
|
|
178
178
|
/// Every part becomes a string-pointer-valued expression (static text via
|
|
179
179
|
/// `compileStaticString`; `Str`/`Int`/`Bool`/`Float` interpolated values
|
|
180
180
|
/// converted at runtime — `Float` via `Float.toStr`, see the `TFloat` arm
|
|
181
|
-
/// below). Two parts are joined with the `Str.
|
|
181
|
+
/// below). Two parts are joined with the `Str.concat` method; three or more go into one
|
|
182
182
|
/// `array<Str>` for `strJoin`, which copies every part once into a buffer of the summed
|
|
183
|
-
/// length (left-folding `Str.
|
|
183
|
+
/// length (left-folding `Str.concat` would copy the growing prefix again at every part).
|
|
184
184
|
pub(crate) fn compileInterpolatedString(
|
|
185
185
|
s: &ast::StringExpr,
|
|
186
186
|
body: &mut Vec<u8>,
|
|
@@ -272,8 +272,8 @@ pub(crate) fn compileInterpolatedString(
|
|
|
272
272
|
}
|
|
273
273
|
}
|
|
274
274
|
if !first && join.is_none() {
|
|
275
|
-
let add = *ctx.func_ids.get("Str::
|
|
275
|
+
let add = *ctx.func_ids.get("Str::concat").ok_or_else(|| {
|
|
276
|
-
"codegen: string interpolation requires `Str.
|
|
276
|
+
"codegen: string interpolation requires `Str.concat` (import std/Str)".to_string()
|
|
277
277
|
})?;
|
|
278
278
|
Instruction::Call(add).encode(body);
|
|
279
279
|
}
|
compiler/wasm-codegen/tests/wasm_codegen/codegen_tests.rs
CHANGED
|
@@ -1164,7 +1164,7 @@ fn one_copy_per_distinct_type() {
|
|
|
1164
1164
|
"{}{}\
|
|
1165
1165
|
fun main() -> Int =
|
|
1166
1166
|
a := mapList([1, 2].toSlice(), |n| n + 1)
|
|
1167
|
-
b := mapList([\"x\", \"y\"].toSlice(), |s|
|
|
1167
|
+
b := mapList([\"x\", \"y\"].toSlice(), |s| \"{{s}}!\")
|
|
1168
1168
|
a.length() + b.length()
|
|
1169
1169
|
",
|
|
1170
1170
|
STD_HEAD, LIST_FNS
|
compiler/wasm-codegen/tests/wasm_codegen/dispatch_test.rs
CHANGED
|
@@ -141,11 +141,11 @@ struct Ball(id: Int, n: Int) is Actor[Int] =
|
|
|
141
141
|
|
|
142
142
|
struct Menu(id: Int) is Screen[Str, Int] =
|
|
143
143
|
fun key(self) -> Int = self.id
|
|
144
|
-
fun title(self, s: Str) -> Str = \"menu \"
|
|
144
|
+
fun title(self, s: Str) -> Str = \"menu {s}\"
|
|
145
145
|
|
|
146
146
|
struct Play(id: Int, score: Int) is Screen[Str, Int] =
|
|
147
147
|
fun key(self) -> Int = self.id + self.score
|
|
148
|
-
fun title(self, s: Str) -> Str = \"play \"
|
|
148
|
+
fun title(self, s: Str) -> Str = \"play {s}\"
|
|
149
149
|
fun react(self, m: Int) -> Play = Play(..self, score: self.score + m)
|
|
150
150
|
|
|
151
151
|
enum Dot is Actor[Int] =
|
compiler/wasm-codegen/tests/wasm_codegen/literals_test.rs
CHANGED
|
@@ -23,7 +23,7 @@ fun tag(n: Int) -> Str =
|
|
|
23
23
|
if n == 0 then \"same text\" else if n == 1 then \"same text\" else \"other\"
|
|
24
24
|
|
|
25
25
|
fun main() -> Str =
|
|
26
|
-
tag(0)
|
|
26
|
+
\"{tag(0)}{tag(1)}same text{tag(2)}\"
|
|
27
27
|
";
|
|
28
28
|
|
|
29
29
|
/// A literal written three times is one segment, and each use still reads it.
|
|
@@ -55,7 +55,7 @@ fn names(bytes: &[u8]) -> Vec<String> {
|
|
|
55
55
|
out
|
|
56
56
|
}
|
|
57
57
|
|
|
58
|
-
/// Three or more parts build one buffer with `strJoin`; two parts
|
|
58
|
+
/// Three or more parts build one buffer with `strJoin`; two parts call `Str.concat`.
|
|
59
59
|
#[test]
|
|
60
60
|
fn longInterpolationJoinsOnce() {
|
|
61
61
|
let long = "import std/Int\nimport std/Str\n\nfun show(a: Str, n: Int) -> Str =\n \"<{a}:{n}>\"\n\nfun main() -> Str =\n show(\"k\", 42)\n";
|
docs/language.md
CHANGED
|
@@ -70,6 +70,8 @@ fun greet(name: Str) -> Str =
|
|
|
70
70
|
"Hello, {name}! Your name has {name.length()} letters."
|
|
71
71
|
```
|
|
72
72
|
|
|
73
|
+
`Str` has no `+`: build strings with interpolation, `"{a}{b}"` rather than `a + b` (`a + b` on a `Str` is a type error that says so), or call `a.concat(b)`. `Int`, `Float`, `Bool` and any type with a `toStr` method interpolate directly. Write `{{` and `}}` for literal braces.
|
|
74
|
+
|
|
73
75
|
**Grouping** uses `{ }`, because `( )` is only for calls:
|
|
74
76
|
|
|
75
77
|
```plum
|
docs/std.md
CHANGED
|
@@ -71,7 +71,7 @@ fun main() =
|
|
|
71
71
|
- **Unicode:** `length` counts bytes; `runeLength` and `runeAt` count characters. `runeAt(i)` is an `Option[Str]`, `None` past the last character.
|
|
72
72
|
- **Bytes:** `byteAt(i)` and `get(i)` crash with the index and the length out of range, so a substring never reads its source's bytes outside it.
|
|
73
73
|
|
|
74
|
-
To build a string in steps, use `Buffer`: `write`, `writeByte`, `toStr`.
|
|
74
|
+
`Str` has no `+`: join strings with interpolation, `"{a}{b}"`, or `a.concat(b)`. To build a string in steps, use `Buffer`: `write`, `writeByte`, `toStr`.
|
|
75
75
|
|
|
76
76
|
## Sequences
|
|
77
77
|
|
examples/functional.plum
CHANGED
|
@@ -495,7 +495,7 @@ test "scene count and draw"
|
|
|
495
495
|
assert scene.count() == 5
|
|
496
496
|
cmds := scene.draw(identityXf())
|
|
497
497
|
assert cmds.length() == 3
|
|
498
|
-
kinds := cmds.foldr("", |c, acc| c.kind
|
|
498
|
+
kinds := cmds.foldr("", |c, acc| "{c.kind} {acc}")
|
|
499
499
|
assert kinds == "sprite label button "
|
|
500
500
|
btn := cmds.foldl(cmd("none", 0, identityXf()), |a, c| if c.id == 5 then c else a)
|
|
501
501
|
assert btn.x == 110.0
|
|
@@ -522,9 +522,9 @@ test "scene click routes an event"
|
|
|
522
522
|
if Link(e, _) := r.events
|
|
523
523
|
assert e.id == 5
|
|
524
524
|
assert e.kind == Clicked
|
|
525
|
-
kinds := r.node.draw(identityXf()).foldr("", |c, acc| c.kind
|
|
525
|
+
kinds := r.node.draw(identityXf()).foldr("", |c, acc| "{c.kind} {acc}")
|
|
526
526
|
assert kinds == "sprite label button-down "
|
|
527
|
-
old := scene.draw(identityXf()).foldr("", |c, acc| c.kind
|
|
527
|
+
old := scene.draw(identityXf()).foldr("", |c, acc| "{c.kind} {acc}")
|
|
528
528
|
assert old == "sprite label button "
|
|
529
529
|
miss := click(scene, 0.0, 0.0)
|
|
530
530
|
assert miss.events.isEmpty()
|
examples/generics.plum
CHANGED
|
@@ -242,9 +242,9 @@ test "closures nested three deep capture Int and Str locals through generic para
|
|
|
242
242
|
s := "z"
|
|
243
243
|
r := pick(10, |x| pick(x, |y| pick(y, |z| z + k)))
|
|
244
244
|
assert r == 15
|
|
245
|
-
t := pick("a", |x| pick(x, |y| pick(y, |z| z
|
|
245
|
+
t := pick("a", |x| pick(x, |y| pick(y, |z| "{z}{s}")))
|
|
246
246
|
assert t == "az"
|
|
247
|
-
both := pick(1, |x| pick("q", |y| y
|
|
247
|
+
both := pick(1, |x| pick("q", |y| "{y}{s}").length() + x + k)
|
|
248
248
|
assert both == 8
|
|
249
249
|
assert twice(|x| twice(|y| y * 2, x) + k, 1) == 41
|
|
250
250
|
|
|
@@ -271,7 +271,7 @@ test "one generic fn serves Int, Str and a class in the same program"
|
|
|
271
271
|
c := id(Circle(r: 1.0))
|
|
272
272
|
assert c.name() == "circle"
|
|
273
273
|
assert id(8) + 1 == 9
|
|
274
|
-
assert id("a")
|
|
274
|
+
assert "{id("a")}b" == "ab"
|
|
275
275
|
|
|
276
276
|
test "== on T works for Int and Str"
|
|
277
277
|
assert same(2, 2)
|
|
@@ -284,7 +284,7 @@ test "== on T works for Int and Str"
|
|
|
284
284
|
test "num_record_passed_explicitly"
|
|
285
285
|
intNum := Num(add: |a, b| a + b, zero: 0)
|
|
286
286
|
floatNum := Num(add: |a, b| a + b, zero: 0.0)
|
|
287
|
-
strNum := Num(add: |a, b| a
|
|
287
|
+
strNum := Num(add: |a, b| a.concat(b), zero: "")
|
|
288
288
|
assert sumWith([1, 2, 3].toSlice(), intNum) == 6
|
|
289
289
|
assert sumWith([1.5, 2.5].toSlice(), floatNum) == 4.0
|
|
290
290
|
assert sumWith(["a", "b"].toSlice(), strNum) == "ab"
|
examples/maps.plum
CHANGED
|
@@ -44,7 +44,7 @@ test "old versions stay as they were"
|
|
|
44
44
|
test "walks go in key order"
|
|
45
45
|
m := [30 => "c", 10 => "a", 20 => "b"]
|
|
46
46
|
assert m.keys().toStr() == "[10,20,30]"
|
|
47
|
-
assert m.fold("", |acc, k, v| acc
|
|
47
|
+
assert m.fold("", |acc, k, v| "{acc}{v}") == "abc"
|
|
48
48
|
assert m.min()!.first == 10 && m.max()!.second == "c"
|
|
49
49
|
assert m.atLeast(15)!.first == 20 && m.below(10).isNone()
|
|
50
50
|
total := Slice[Int]()
|
examples/traits.plum
CHANGED
|
@@ -72,7 +72,7 @@ struct Counter(n: Int, count: Int) is Reducer[Int] =
|
|
|
72
72
|
fun seen(self) -> Int = self.count
|
|
73
73
|
|
|
74
74
|
struct Words(text: Str) is Reducer[Str] =
|
|
75
|
-
fun update(self, msg: Str) -> Words = Words(text: self.text
|
|
75
|
+
fun update(self, msg: Str) -> Words = Words(text: "{self.text}{msg}")
|
|
76
76
|
|
|
77
77
|
fun replay[S: Reducer[M], M](s: S, msgs: List[M]) -> S = msgs.fold(s, |a, m| a.update(m))
|
|
78
78
|
|
std/Decimal.plum
CHANGED
|
@@ -130,16 +130,16 @@ struct Decimal(digits: Str, exp: Int, neg: Bool) =
|
|
|
130
130
|
body := if self.exp == 0 && n <= 21
|
|
131
131
|
ds
|
|
132
132
|
else if k >= n && k <= 21
|
|
133
|
-
ds
|
|
133
|
+
"{ds}{"0".repeat(k - n)}"
|
|
134
134
|
else if k > 0 && k <= 21
|
|
135
|
-
ds.sub(0, k)
|
|
135
|
+
"{ds.sub(0, k)}.{ds.sub(k, n)}"
|
|
136
136
|
else if k > -6 && k <= 0
|
|
137
|
-
"0."
|
|
137
|
+
"0.{"0".repeat(0 - k)}{ds}"
|
|
138
138
|
else
|
|
139
|
-
mant := if n == 1 then ds else ds.sub(0, 1)
|
|
139
|
+
mant := if n == 1 then ds else "{ds.sub(0, 1)}.{ds.sub(1, n)}"
|
|
140
140
|
x10 := k - 1
|
|
141
|
-
|
|
141
|
+
"{mant}e{if x10 < 0 then "-" else "+"}{if x10 < 0 then 0 - x10 else x10}"
|
|
142
|
-
if self.neg then "-"
|
|
142
|
+
if self.neg then "-{body}" else body
|
|
143
143
|
|
|
144
144
|
# True for 0 and -0.
|
|
145
145
|
fun isZero(self) -> Bool = self.digits == "0"
|
std/Float.plum
CHANGED
|
@@ -208,8 +208,8 @@ primitive Float =
|
|
|
208
208
|
if self < 0.0f then "-Infinity" else "Infinity"
|
|
209
209
|
else if self >= 9223372036854775808.0f || self <= -9223372036854775808.0f
|
|
210
210
|
d := Decimal.exact(self)
|
|
211
|
-
s := d.digits
|
|
211
|
+
s := "{d.digits}{"0".repeat(d.exp)}.0"
|
|
212
|
-
if d.neg then "-"
|
|
212
|
+
if d.neg then "-{s}" else s
|
|
213
213
|
else
|
|
214
214
|
neg := self < 0.0f
|
|
215
215
|
abs_val := if neg then -self else self
|
|
@@ -219,5 +219,5 @@ primitive Float =
|
|
|
219
219
|
carry := scaled >= 1000000
|
|
220
220
|
whole_part := if carry then int_part + 1 else int_part
|
|
221
221
|
frac := if carry then 0 else scaled
|
|
222
|
-
s := Int.intToStr(whole_part)
|
|
222
|
+
s := "{Int.intToStr(whole_part)}.{Int.trimmedFracDigits(frac, 6)}"
|
|
223
|
-
if neg then "-"
|
|
223
|
+
if neg then "-{s}" else s
|
std/Int.plum
CHANGED
|
@@ -178,7 +178,7 @@ primitive Int =
|
|
|
178
178
|
if n == 0
|
|
179
179
|
"0"
|
|
180
180
|
else if n < 0
|
|
181
|
-
"-
|
|
181
|
+
"-{digitsToStr(-{n / 10})}{digitChar(-{n % 10})}"
|
|
182
182
|
else
|
|
183
183
|
digitsToStr(n)
|
|
184
184
|
|
|
@@ -190,7 +190,7 @@ primitive Int =
|
|
|
190
190
|
if n == 0
|
|
191
191
|
""
|
|
192
192
|
else
|
|
193
|
-
digitsToStr(n / 10)
|
|
193
|
+
"{digitsToStr(n / 10)}{digitChar(n % 10)}"
|
|
194
194
|
|
|
195
195
|
fun digitChar(d: Int) -> Str =
|
|
196
196
|
match d
|
|
@@ -213,7 +213,7 @@ primitive Int =
|
|
|
213
213
|
if place_value == 0
|
|
214
214
|
""
|
|
215
215
|
else
|
|
216
|
-
digitChar(n / place_value % 10)
|
|
216
|
+
"{digitChar(n / place_value % 10)}{fixedFracDigits(n, place_value / 10)}"
|
|
217
217
|
|
|
218
218
|
fun pow10(n: Int) -> Int =
|
|
219
219
|
if n <= 0
|
std/Os.plum
CHANGED
|
@@ -83,7 +83,7 @@ primitive Os =
|
|
|
83
83
|
fun appendFile(io: Io, path: Str, data: Str) -> Result[Unit, IoError] =
|
|
84
84
|
if exists(io, path)
|
|
85
85
|
existing := readFile(io, path)?
|
|
86
|
-
writeFile(io, path, existing
|
|
86
|
+
writeFile(io, path, "{existing}{data}")
|
|
87
87
|
else
|
|
88
88
|
writeFile(io, path, data)
|
|
89
89
|
|
|
@@ -118,7 +118,7 @@ primitive Os =
|
|
|
118
118
|
# retrying on the (astronomically unlikely) chance of a collision, and
|
|
119
119
|
# returns the path actually created.
|
|
120
120
|
fun mkdtemp(io: Io, prefix: Str) -> Result[Str, IoError] =
|
|
121
|
-
path := prefix
|
|
121
|
+
path := "{prefix}{Uuid.v4(Random.seeded(io.randomSeed())).value}"
|
|
122
122
|
if exists(io, path)
|
|
123
123
|
mkdtemp(io, prefix)
|
|
124
124
|
else
|
|
@@ -190,7 +190,7 @@ primitive Os =
|
|
|
190
190
|
|
|
191
191
|
test "writeFile/readFile/appendFile/remove round-trip on a real file"
|
|
192
192
|
dir := Os.mkdtemp(io, "/tmp/plum_os_test_")!
|
|
193
|
-
path :=
|
|
193
|
+
path := "{dir}/a.txt"
|
|
194
194
|
Os.writeFile(io, path, "hello")!
|
|
195
195
|
assert Os.readFile(io, path)! == "hello"
|
|
196
196
|
Os.appendFile(io, path, " world")!
|
|
@@ -202,9 +202,9 @@ test "writeFile/readFile/appendFile/remove round-trip on a real file"
|
|
|
202
202
|
|
|
203
203
|
test "copyFile/cp/rename move and duplicate file contents correctly"
|
|
204
204
|
dir := Os.mkdtemp(io, "/tmp/plum_os_test_")!
|
|
205
|
-
a :=
|
|
205
|
+
a := "{dir}/a.txt"
|
|
206
|
-
b :=
|
|
206
|
+
b := "{dir}/b.txt"
|
|
207
|
-
c :=
|
|
207
|
+
c := "{dir}/c.txt"
|
|
208
208
|
Os.writeFile(io, a, "data")!
|
|
209
209
|
Os.copyFile(io, a, b)!
|
|
210
210
|
assert Os.readFile(io, b)! == "data"
|
|
@@ -218,17 +218,17 @@ test "copyFile/cp/rename move and duplicate file contents correctly"
|
|
|
218
218
|
test "readdir lists the entries created in a fresh directory"
|
|
219
219
|
dir := Os.mkdtemp(io, "/tmp/plum_os_test_")!
|
|
220
220
|
assert Os.readdir(io, dir)!.length() == 0
|
|
221
|
-
Os.writeFile(io,
|
|
221
|
+
Os.writeFile(io, "{dir}/only.txt", "x")!
|
|
222
222
|
names := Os.readdir(io, dir)!
|
|
223
223
|
assert names.length() == 1
|
|
224
224
|
assert names.join(",") == "only.txt"
|
|
225
|
-
Os.remove(io,
|
|
225
|
+
Os.remove(io, "{dir}/only.txt")!
|
|
226
226
|
Os.rmdir(io, dir)!
|
|
227
227
|
|
|
228
228
|
test "symlink/readlink/realpath resolve a linked file correctly"
|
|
229
229
|
dir := Os.mkdtemp(io, "/tmp/plum_os_test_")!
|
|
230
|
-
target :=
|
|
230
|
+
target := "{dir}/target.txt"
|
|
231
|
-
link_path :=
|
|
231
|
+
link_path := "{dir}/link.txt"
|
|
232
232
|
Os.writeFile(io, target, "linked")!
|
|
233
233
|
Os.symlink(io, target, link_path)!
|
|
234
234
|
assert Os.readlink(io, link_path)! == target
|
|
@@ -240,7 +240,7 @@ test "symlink/readlink/realpath resolve a linked file correctly"
|
|
|
240
240
|
|
|
241
241
|
test "truncate resizes a file's contents"
|
|
242
242
|
dir := Os.mkdtemp(io, "/tmp/plum_os_test_")!
|
|
243
|
-
path :=
|
|
243
|
+
path := "{dir}/a.txt"
|
|
244
244
|
Os.writeFile(io, path, "hello world")!
|
|
245
245
|
Os.truncate(io, path, 5)!
|
|
246
246
|
assert Os.readFile(io, path)! == "hello"
|
|
@@ -249,7 +249,7 @@ test "truncate resizes a file's contents"
|
|
|
249
249
|
|
|
250
250
|
test "chmod changes a file's permission bits"
|
|
251
251
|
dir := Os.mkdtemp(io, "/tmp/plum_os_test_")!
|
|
252
|
-
path :=
|
|
252
|
+
path := "{dir}/a.txt"
|
|
253
253
|
Os.writeFile(io, path, "x")!
|
|
254
254
|
Os.chmod(io, path, 384)! # 0o600
|
|
255
255
|
Os.remove(io, path)!
|
|
@@ -257,8 +257,8 @@ test "chmod changes a file's permission bits"
|
|
|
257
257
|
|
|
258
258
|
test "link creates a second name for the same file"
|
|
259
259
|
dir := Os.mkdtemp(io, "/tmp/plum_os_test_")!
|
|
260
|
-
a :=
|
|
260
|
+
a := "{dir}/a.txt"
|
|
261
|
-
b :=
|
|
261
|
+
b := "{dir}/b.txt"
|
|
262
262
|
Os.writeFile(io, a, "shared")!
|
|
263
263
|
Os.link(io, a, b)!
|
|
264
264
|
assert Os.readFile(io, b)! == "shared"
|
std/Slice.plum
CHANGED
|
@@ -301,7 +301,7 @@ struct Slice[T](mut data: Array[T], mut start: Int, mut size: Int, mut cap: Int)
|
|
|
301
301
|
res.toStr()
|
|
302
302
|
|
|
303
303
|
fun toStr(self) -> Str =
|
|
304
|
-
"[
|
|
304
|
+
"[{self.join(",")}]"
|
|
305
305
|
|
|
306
306
|
# Stops the program: index `i` is out of range for a length `n` (`what` names the call).
|
|
307
307
|
fun outOfRange[T](what: Str, i: Int, n: Int) -> T =
|
std/Str.plum
CHANGED
|
@@ -155,12 +155,10 @@ struct Str(data: Array[Byte], start: Int, len: Int) =
|
|
|
155
155
|
fun endsWith(self, search: Str) -> Bool =
|
|
156
156
|
matchesAt(self, search, self.length() - search.length(), search.length())
|
|
157
157
|
|
|
158
|
+
# `a.concat(b)`, and what a two-part interpolation `"{a}{b}"` calls: one exact-length
|
|
159
|
+
# allocation and two bulk copies. Returns an operand unchanged when the other is empty
|
|
160
|
+
# (Strs are immutable). `Str` has no `+`.
|
|
158
|
-
fun concat(self,
|
|
161
|
+
fun concat(self, o: Str) -> Str =
|
|
159
|
-
self + other
|
|
160
|
-
|
|
161
|
-
# `a + b`: one exact-length allocation and two bulk copies. Returns an
|
|
162
|
-
# operand unchanged when the other is empty (Strs are immutable).
|
|
163
|
-
fun add(self, o: Str) -> Str =
|
|
164
162
|
n := self.length()
|
|
165
163
|
m := o.length()
|
|
166
164
|
if m == 0
|
|
@@ -358,7 +356,7 @@ struct Str(data: Array[Byte], start: Int, len: Int) =
|
|
|
358
356
|
if self.length() == 0
|
|
359
357
|
self
|
|
360
358
|
else
|
|
361
|
-
self.get(0).upperCase()
|
|
359
|
+
"{self.get(0).upperCase()}{self.sub(1, self.length()).toLower()}"
|
|
362
360
|
|
|
363
361
|
fun kebabCase(self) -> Str =
|
|
364
362
|
caseConvert(self, "-", 1)
|
|
@@ -370,7 +368,7 @@ struct Str(data: Array[Byte], start: Int, len: Int) =
|
|
|
370
368
|
if self.length() == 0
|
|
371
369
|
self
|
|
372
370
|
else
|
|
373
|
-
self.get(0).toLower()
|
|
371
|
+
"{self.get(0).toLower()}{self.sub(1, self.length())}"
|
|
374
372
|
|
|
375
373
|
fun upperCase(self) -> Str =
|
|
376
374
|
mapCase(self, true)
|
|
@@ -379,7 +377,7 @@ struct Str(data: Array[Byte], start: Int, len: Int) =
|
|
|
379
377
|
if self.length() == 0
|
|
380
378
|
self
|
|
381
379
|
else
|
|
382
|
-
self.get(0).upperCase()
|
|
380
|
+
"{self.get(0).upperCase()}{self.sub(1, self.length())}"
|
|
383
381
|
|
|
384
382
|
fun startCase(self) -> Str =
|
|
385
383
|
caseConvert(self, " ", 4)
|
|
@@ -455,7 +453,7 @@ struct Str(data: Array[Byte], start: Int, len: Int) =
|
|
|
455
453
|
else if length <= omission.length()
|
|
456
454
|
omission.sub(0, length)
|
|
457
455
|
else
|
|
458
|
-
self.sub(0, runeBoundary(self, length - omission.length()))
|
|
456
|
+
"{self.sub(0, runeBoundary(self, length - omission.length()))}{omission}"
|
|
459
457
|
|
|
460
458
|
fun unescape(self) -> Str =
|
|
461
459
|
unescapeHtml(self)
|
|
@@ -529,11 +527,11 @@ struct Str(data: Array[Byte], start: Int, len: Int) =
|
|
|
529
527
|
if cp < 0x80
|
|
530
528
|
byteToStr(cp)
|
|
531
529
|
else if cp < 0x800
|
|
532
|
-
byteToStr(0xC0 | {cp >> 6})
|
|
530
|
+
"{byteToStr(0xC0 | {cp >> 6})}{byteToStr(0x80 | {cp & 0x3F})}"
|
|
533
531
|
else if cp < 0x10000
|
|
534
|
-
byteToStr(0xE0 | {cp >> 12})
|
|
532
|
+
"{byteToStr(0xE0 | {cp >> 12})}{byteToStr(0x80 | {{cp >> 6} & 0x3F})}{byteToStr(0x80 | {cp & 0x3F})}"
|
|
535
533
|
else
|
|
536
|
-
byteToStr(0xF0 | {cp >> 18})
|
|
534
|
+
"{byteToStr(0xF0 | {cp >> 18})}{byteToStr(0x80 | {{cp >> 12} & 0x3F})}{byteToStr(0x80 | {{cp >> 6} & 0x3F})}{byteToStr(0x80 | {cp & 0x3F})}"
|
|
537
535
|
|
|
538
536
|
# The largest byte index at most `i` that starts a character (or is the end): backs up
|
|
539
537
|
# over continuation bytes (`10xxxxxx`).
|
|
@@ -825,7 +823,7 @@ test "sub of a 200000-byte string is a single copy"
|
|
|
825
823
|
assert s.slice(-5, 200000) == "aaaaa"
|
|
826
824
|
|
|
827
825
|
test "reverse of a 100000-char string"
|
|
828
|
-
r :=
|
|
826
|
+
r := "b{"a".repeat(99999)}".reverse()
|
|
829
827
|
assert r.length() == 100000
|
|
830
828
|
assert r.byteAt(99999) == 98
|
|
831
829
|
assert r.byteAt(0) == 97
|
|
@@ -884,8 +882,9 @@ test "deburr strips Latin-1 and Latin Extended-A diacritics"
|
|
|
884
882
|
test "Str edge cases: empties, sub bounds, replaceAll growth, split ends, utf8 reverse"
|
|
885
883
|
b := Buffer()
|
|
886
884
|
assert b.toStr() == ""
|
|
885
|
+
assert "{b.toStr()}{b.toStr()}" == ""
|
|
887
|
-
assert b.toStr()
|
|
886
|
+
assert b.toStr().concat(b.toStr()) == ""
|
|
888
|
-
assert
|
|
887
|
+
assert "".concat("").length() == 0
|
|
889
888
|
assert "hello".sub(5, 5) == ""
|
|
890
889
|
assert "hello".sub(-3, 2) == "he"
|
|
891
890
|
assert "hello".sub(3, 99) == "lo"
|
|
@@ -915,8 +914,9 @@ test "substrings are views: nested, compared, hashed and concatenated by content
|
|
|
915
914
|
assert n.byteAt(0) == 110
|
|
916
915
|
assert n == s.sub(12, 15)
|
|
917
916
|
assert n.hash() == "new".hash()
|
|
918
|
-
assert
|
|
917
|
+
assert "{n}!" == "new!"
|
|
918
|
+
assert n.concat("!") == "new!"
|
|
919
|
-
assert "<
|
|
919
|
+
assert "<{w.sub(0, 5)}>" == "<brave>"
|
|
920
920
|
assert w.sub(5, 2) == ""
|
|
921
921
|
assert w.indexOf("new") == 6
|
|
922
922
|
assert w.reverse() == "wen evarb"
|
std/Time.plum
CHANGED
|
@@ -76,10 +76,9 @@ struct Time(value: Int) =
|
|
|
76
76
|
# ISO 8601 / RFC 3339, always in UTC (`Z`): `YYYY-MM-DDTHH:MM:SS.mmmZ`.
|
|
77
77
|
fun toStr(self) -> Str =
|
|
78
78
|
d := self.date()
|
|
79
|
-
d.year.toStr().padStart("0", 4) + "-" + d.month.toStr().padStart("0", 2)
|
|
80
|
-
|
|
79
|
+
ymd := "{d.year.toStr().padStart("0", 4)}-{d.month.toStr().padStart("0", 2)}-{d.day.toStr().padStart("0", 2)}"
|
|
81
|
-
|
|
80
|
+
hms := "{self.hour().toStr().padStart("0", 2)}:{self.minute().toStr().padStart("0", 2)}:{self.second().toStr().padStart("0", 2)}"
|
|
82
|
-
|
|
81
|
+
"{ymd}T{hms}.{self.millisecond().toStr().padStart("0", 3)}Z"
|
|
83
82
|
|
|
84
83
|
# Days-since-epoch to proleptic Gregorian year/month/day, via Howard
|
|
85
84
|
# Hinnant's `civil_from_days` (http://howardhinnant.github.io/date_algorithms.html) —
|
std/Uuid.plum
CHANGED
|
@@ -85,9 +85,7 @@ struct Uuid(value: Str) =
|
|
|
85
85
|
time_hi_and_version := { a & 0x0FFF } | 0x4000
|
|
86
86
|
clock_seq := { { b >> 48 } & 0x3FFF } | 0x8000
|
|
87
87
|
node := b & 0xFFFFFFFFFFFF
|
|
88
|
-
s := hexDigits(time_low, 8) + "-" + hexDigits(time_mid, 4) + "-"
|
|
89
|
-
|
|
88
|
+
s := "{hexDigits(time_low, 8)}-{hexDigits(time_mid, 4)}-{hexDigits(time_hi_and_version, 4)}-{hexDigits(clock_seq, 4)}-{hexDigits(node, 12)}"
|
|
90
|
-
+ hexDigits(node, 12)
|
|
91
89
|
Rolled(value: Uuid(value: s), rng: db.rng)
|
|
92
90
|
|
|
93
91
|
# The numeric value (0-15) of an ASCII hex digit byte, or -1 if it isn't one.
|
|
@@ -122,7 +120,7 @@ struct Uuid(value: Str) =
|
|
|
122
120
|
if count <= 0
|
|
123
121
|
""
|
|
124
122
|
else
|
|
125
|
-
hexDigits(n >> 4, count - 1)
|
|
123
|
+
"{hexDigits(n >> 4, count - 1)}{hexDigit(n)}"
|
|
126
124
|
|
|
127
125
|
test "nil is the all-zero UUID and is recognized as such"
|
|
128
126
|
assert Uuid.nil().toStr() == "00000000-0000-0000-0000-000000000000"
|
std/collections/BPlusTree.plum
CHANGED
|
@@ -226,7 +226,7 @@ struct BPlusTree[K, V](root: BpNode[K, V], size: Int, order: Int, before: fn(K,
|
|
|
226
226
|
|
|
227
227
|
# `[k0: v0, k1: v1]` in key order.
|
|
228
228
|
fun toStr(self) -> Str =
|
|
229
|
-
"[
|
|
229
|
+
"[{self.entries().join(", ")}]"
|
|
230
230
|
|
|
231
231
|
# A node of a `BPlusTree`: a leaf of entries (keys ascending), or an inner node of
|
|
232
232
|
# `kids` with one separator key between each two.
|
std/collections/BTree.plum
CHANGED
|
@@ -183,7 +183,7 @@ struct BTree[K, V](root: BtNode[K, V], size: Int, t: Int, before: fn(K, K) -> Bo
|
|
|
183
183
|
|
|
184
184
|
# `[1: a, 2: b]`, in key order.
|
|
185
185
|
fun toStr(self) -> Str =
|
|
186
|
-
"[
|
|
186
|
+
"[{self.entries().join(", ")}]"
|
|
187
187
|
|
|
188
188
|
# The keys node by node: a leaf is `(1 2)`, a branch `((1) 2 (3))`.
|
|
189
189
|
fun shape(self) -> Str =
|
std/collections/Hamt.plum
CHANGED
|
@@ -94,7 +94,7 @@ struct Hamt[K, V](root: HamtNode[K, V], size: Int) =
|
|
|
94
94
|
|
|
95
95
|
# `[k: v, ...]` in trie order; `[]` when empty.
|
|
96
96
|
fun toStr(self) -> Str =
|
|
97
|
-
"[
|
|
97
|
+
"[{self.entries().join(", ")}]"
|
|
98
98
|
|
|
99
99
|
# A trie node. A branch's `bitmap` has bit i set when it has a child for 5-bit slot i;
|
|
100
100
|
# `kids` holds just those children, in slot order. Keys whose full hashes are equal share
|
|
@@ -482,7 +482,7 @@ test "keys, values, entries and toStr see every entry once"
|
|
|
482
482
|
one: Hamt[Str, Int] := Hamt.empty()
|
|
483
483
|
assert one.set("x", 1).toStr() == "[x: 1]"
|
|
484
484
|
assert m.toStr().length() > 0
|
|
485
|
-
assert m.toStr() == "[
|
|
485
|
+
assert m.toStr() == "[{es.map(|e| "{e.first}: {e.second}").join(", ")}]"
|
|
486
486
|
|
|
487
487
|
test "forced collisions: keys with a bad hash"
|
|
488
488
|
m0: Hamt[HamtBadKey, Int] := Hamt.empty()
|
std/collections/IntervalTree.plum
CHANGED
|
@@ -84,7 +84,7 @@ struct IntervalTree[V](root: IvBranch[V], size: Int) =
|
|
|
84
84
|
|
|
85
85
|
# The intervals in order: `[[1, 5]: a, [2, 3]: b]`.
|
|
86
86
|
fun toStr(self) -> Str =
|
|
87
|
-
"[
|
|
87
|
+
"[{self.entries().join(", ")}]"
|
|
88
88
|
|
|
89
89
|
# One stored interval `[lo, hi]` and its value.
|
|
90
90
|
struct IvEntry[V](lo: Int, hi: Int, value: V) =
|
std/collections/KdTree.plum
CHANGED
|
@@ -151,7 +151,7 @@ struct KdTree[V](root: KdNode[V], size: Int) =
|
|
|
151
151
|
|
|
152
152
|
# The entries as `[(1.0, 2.0): a, (3.0, 4.0): b]`, in `entries` order.
|
|
153
153
|
fun toStr(self) -> Str =
|
|
154
|
-
"[
|
|
154
|
+
"[{self.entries().map(|e| "({e.first.x}, {e.first.y}): {e.second.toStr()}").join(", ")}]"
|
|
155
155
|
|
|
156
156
|
# The nodes of a `KdTree`: `KdLeaf`, or an entry with the subtrees on either side of
|
|
157
157
|
# its split line. The split axis is not stored; it comes from the depth.
|
std/collections/LeftistHeap.plum
CHANGED
|
@@ -91,7 +91,7 @@ struct LeftistHeap[T](root: LhTree[T], size: Int, before: fn(T, T) -> Bool) =
|
|
|
91
91
|
|
|
92
92
|
# The elements smallest first: `[1,2,3]`.
|
|
93
93
|
fun toStr(self) -> Str =
|
|
94
|
-
"[
|
|
94
|
+
"[{self.toList().join(",")}]"
|
|
95
95
|
|
|
96
96
|
# A leftist tree: empty, or an element above two subtrees with the node's rank
|
|
97
97
|
# (1 + the rank of its right subtree; an empty tree has rank 0).
|
std/collections/List.plum
CHANGED
|
@@ -107,7 +107,7 @@ enum List[T] =
|
|
|
107
107
|
res.toStr()
|
|
108
108
|
|
|
109
109
|
fun toStr(self) -> Str =
|
|
110
|
-
"[
|
|
110
|
+
"[{self.join(",")}]"
|
|
111
111
|
|
|
112
112
|
# `==` and `!=`: the elements compare equal, in order (see `equals`).
|
|
113
113
|
fun eq(self, other: List[T]) -> Bool = self.equals(other)
|
std/collections/Map.plum
CHANGED
|
@@ -669,7 +669,7 @@ fun mapModelGet(xs: List[Pair[Int, Int]], k: Int) -> Option[Int] =
|
|
|
669
669
|
xs.find(|e| e.first == k).map(|e| e.second)
|
|
670
670
|
|
|
671
671
|
fun mapModelStr(xs: List[Pair[Int, Int]]) -> Str =
|
|
672
|
-
"[
|
|
672
|
+
"[{xs.map(|e| "{e.first} => {e.second}").join(", ")}]"
|
|
673
673
|
|
|
674
674
|
# Whether the entry `e` has the key `k`, or both are None.
|
|
675
675
|
fun mapSameKey(e: Option[Pair[Int, Int]], k: Option[Int]) -> Bool = match e
|
|
@@ -966,7 +966,7 @@ test "set operations agree with a filter over pseudo-random maps, and stay balan
|
|
|
966
966
|
|
|
967
967
|
test "walks go in key order: fold, each, keys, values, entries, for, toStr"
|
|
968
968
|
m := [3 => "c", 1 => "a", 2 => "b"]
|
|
969
|
-
assert m.fold("", |acc, k, v|
|
|
969
|
+
assert m.fold("", |acc, k, v| "{acc}{k}{v}") == "1a2b3c"
|
|
970
970
|
seen := Slice[Int]()
|
|
971
971
|
m.each(|k, v| seen.push(k))
|
|
972
972
|
assert seen.toStr() == "[1,2,3]"
|
std/collections/PairingHeap.plum
CHANGED
|
@@ -93,7 +93,7 @@ struct PairingHeap[T](root: PhTree[T], size: Int, before: fn(T, T) -> Bool) =
|
|
|
93
93
|
|
|
94
94
|
# The elements smallest first: `[1,2,3]`.
|
|
95
95
|
fun toStr(self) -> Str =
|
|
96
|
-
"[
|
|
96
|
+
"[{self.toList().join(",")}]"
|
|
97
97
|
|
|
98
98
|
# A pairing tree: empty, or an element above its child trees (never `PhEmpty`).
|
|
99
99
|
enum PhTree[T] =
|
std/collections/Quadtree.plum
CHANGED
|
@@ -87,7 +87,7 @@ struct Quadtree[V](root: QuadNode[V], bounds: Aabb, capacity: Int, maxDepth: Int
|
|
|
87
87
|
|
|
88
88
|
# The entries as `[(1.0, 2.0): a, (3.0, 4.0): b]`, in `entries` order.
|
|
89
89
|
fun toStr(self) -> Str =
|
|
90
|
-
"[
|
|
90
|
+
"[{self.entries().map(|e| "({e.first.x}, {e.first.y}): {e.second.toStr()}").join(", ")}]"
|
|
91
91
|
|
|
92
92
|
# The nodes of a `Quadtree`: a leaf of entries (newest first) with their count, or
|
|
93
93
|
# four quarters (numbered as in `quadQuadrantOf`) with the total count below. A
|
std/collections/RadixTree.plum
CHANGED
|
@@ -111,7 +111,7 @@ struct RadixTree[V](root: RadixNode[V], size: Int) =
|
|
|
111
111
|
|
|
112
112
|
# `[a: 1, ab: 2]`, in sorted key order.
|
|
113
113
|
fun toStr(self) -> Str =
|
|
114
|
-
"[
|
|
114
|
+
"[{self.entries().join(", ")}]"
|
|
115
115
|
|
|
116
116
|
# How many bytes of `b`, from index `c` on, match `a` from index `ai + c` on, plus
|
|
117
117
|
# `c` (call with `c` = 0). Tail call.
|
std/collections/Trie.plum
CHANGED
|
@@ -108,7 +108,7 @@ struct Trie[V](root: TrieNode[V], size: Int) =
|
|
|
108
108
|
|
|
109
109
|
# `[a: 1, ab: 2]`, in sorted key order.
|
|
110
110
|
fun toStr(self) -> Str =
|
|
111
|
-
"[
|
|
111
|
+
"[{self.entries().join(", ")}]"
|
|
112
112
|
|
|
113
113
|
# A node of a `Trie`: the value of the key that ends here, if any, and the
|
|
114
114
|
# children, one per next byte.
|
std/encoding/Base64.plum
CHANGED
|
@@ -160,7 +160,7 @@ test "decode: the inverse, whitespace and missing padding allowed, other bytes a
|
|
|
160
160
|
assert Base64.decode("Zm9v*").isErr()
|
|
161
161
|
|
|
162
162
|
test "the URL alphabet: - and _ for + and /, no padding unless asked"
|
|
163
|
-
data := Str.byteToStr(251)
|
|
163
|
+
data := "{Str.byteToStr(251)}{Str.byteToStr(255)}{Str.byteToStr(190)}"
|
|
164
164
|
assert Base64.encode(data) == "+/++"
|
|
165
165
|
assert Base64.encodeUrl(data) == "-_--"
|
|
166
166
|
assert Base64.encodeUrl("f") == "Zg" && Base64.encodeUrl("f", true) == "Zg=="
|
std/encoding/Json.plum
CHANGED
|
@@ -68,9 +68,9 @@ enum Json =
|
|
|
68
68
|
JsonNull => "null"
|
|
69
69
|
JsonBool(value) => if value then "true" else "false"
|
|
70
70
|
JsonNum(value) => numberText(value)
|
|
71
|
-
JsonStr(value) => "\"
|
|
71
|
+
JsonStr(value) => "\"{escapeJsonStr(value)}\""
|
|
72
|
-
JsonArr(items) => "[
|
|
72
|
+
JsonArr(items) => "[{items.join(",")}]"
|
|
73
|
-
JsonObj(fields) => "{{
|
|
73
|
+
JsonObj(fields) => "{{{jsonMapToStr(fields)}}}"
|
|
74
74
|
|
|
75
75
|
# ---- encode / decode API ----
|
|
76
76
|
fun encode[T](x: T) -> Str = x.toJson().toStr()
|
|
@@ -205,7 +205,7 @@ enum Json =
|
|
|
205
205
|
escapeFrom(s, buf, i + step)
|
|
206
206
|
|
|
207
207
|
fun hexByteStr(b: Int) -> Str =
|
|
208
|
-
hexDigitChar(b / 16)
|
|
208
|
+
"{hexDigitChar(b / 16)}{hexDigitChar(b % 16)}"
|
|
209
209
|
|
|
210
210
|
fun hexDigitChar(d: Int) -> Str =
|
|
211
211
|
match d
|
|
@@ -741,7 +741,7 @@ test "round-trips a 100000 character string"
|
|
|
741
741
|
buf := Buffer()
|
|
742
742
|
for i := range 100000
|
|
743
743
|
buf.write("a")
|
|
744
|
-
doc := "\"
|
|
744
|
+
doc := "\"{buf.toStr()}\""
|
|
745
745
|
v := Json.fromStr(doc)!
|
|
746
746
|
assert v.asStr()!.length() == 100000
|
|
747
747
|
assert v.toStr().length() == 100002
|
std/testing.plum
CHANGED
|
@@ -85,7 +85,7 @@ test "expectTrue and expectFalse"
|
|
|
85
85
|
|
|
86
86
|
test "expectEq and expectNotEq"
|
|
87
87
|
assert expectEq(2 + 2, 4)
|
|
88
|
-
assert expectEq("ab"
|
|
88
|
+
assert expectEq("ab".concat("c"), "abc")
|
|
89
89
|
assert expectNotEq(2 + 2, 5)
|
|
90
90
|
|
|
91
91
|
test "list equality via the Slice.equals method"
|
tests/core/literal_patterns.plum
CHANGED
|
@@ -36,7 +36,7 @@ test "a string pattern matches by content"
|
|
|
36
36
|
assert command("go") == 1
|
|
37
37
|
assert command("stop") == 2
|
|
38
38
|
assert command("") == 3
|
|
39
|
-
assert command("g"
|
|
39
|
+
assert command("g".concat("o")) == 1
|
|
40
40
|
assert command("going") == 0
|
|
41
41
|
|
|
42
42
|
test "a float pattern matches by value"
|
tests/core/str_no_plus.plum
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
# `Str` has no `+`: strings are built with interpolation, `"{a}{b}"`, or `a.concat(b)`.
|
|
2
|
+
import std/Int
|
|
3
|
+
import std/Float
|
|
4
|
+
import std/Bool
|
|
5
|
+
import std/Str
|
|
6
|
+
import std/Time
|
|
7
|
+
import std/Slice
|
|
8
|
+
import std/collections/List
|
|
9
|
+
|
|
10
|
+
struct Words(text: Str) =
|
|
11
|
+
fun toStr(self) -> Str = "<{self.text}>"
|
|
12
|
+
|
|
13
|
+
test "interpolation takes expressions, calls with string arguments and other types"
|
|
14
|
+
s := "ab"
|
|
15
|
+
n := 41
|
|
16
|
+
assert "{s}{s}" == "abab"
|
|
17
|
+
assert "x={s}" == "x=ab"
|
|
18
|
+
assert "{n + 1}" == "42"
|
|
19
|
+
assert "<{s.padStart("-", 4)}>" == "<--ab>"
|
|
20
|
+
assert "{n > 1}|{1.5}|{-n}" == "true|1.5|-41"
|
|
21
|
+
assert "{n}" == n.toStr()
|
|
22
|
+
assert "{Words(text: s)}!" == "<ab>!"
|
|
23
|
+
|
|
24
|
+
test "braces and quotes inside interpolated literals"
|
|
25
|
+
s := "ab"
|
|
26
|
+
assert "{{{s}}}" == "{{ab}}"
|
|
27
|
+
assert "{{}}".length() == 2
|
|
28
|
+
assert "\"{s}\"" == "\"ab\""
|
|
29
|
+
assert "{s}".length() == 2
|
|
30
|
+
|
|
31
|
+
test "concat is an ordinary method"
|
|
32
|
+
a := "ab"
|
|
33
|
+
assert a.concat("cd") == "abcd"
|
|
34
|
+
assert a.concat("") == "ab"
|
|
35
|
+
assert "".concat(a) == "ab"
|
|
36
|
+
assert a.concat(a).concat(a).length() == 6
|
|
37
|
+
assert ["x", "y", "z"].fold("", |acc, s| acc.concat(s)) == "xyz"
|
|
38
|
+
assert ["x", "y", "z"].join("") == "xyz"
|
|
39
|
+
|
|
40
|
+
test "rewritten std functions keep their output"
|
|
41
|
+
assert Time(value: 86400000 + 3723004).toStr() == "1970-01-02T01:02:03.004Z"
|
|
42
|
+
assert [1, 2, 3].toStr() == "[1,2,3]"
|
|
43
|
+
assert [1, 2, 3].toSlice().toStr() == "[1,2,3]"
|
|
44
|
+
m := -12
|
|
45
|
+
assert m.toStr() == "-12"
|
|
46
|
+
assert 2.5.toStr() == "2.5"
|
|
47
|
+
assert {-0.25}.toStr() == "-0.25"
|
|
48
|
+
assert "hello".capitalize() == "Hello"
|
|
49
|
+
assert "Hello".lowerFirst() == "hello"
|
|
50
|
+
assert "hello".upperFirst() == "Hello"
|
|
51
|
+
assert "hello world".truncate(8) == "hello..."
|
tests/modules/constants.plum
CHANGED
|
@@ -11,7 +11,7 @@ First = 4
|
|
|
11
11
|
ArenaW = 800.0
|
|
12
12
|
ArenaArea = ArenaW * ArenaH
|
|
13
13
|
ArenaH = 600.0
|
|
14
|
-
Greeting = "hello "
|
|
14
|
+
Greeting = "hello ".concat("there")
|
|
15
15
|
Boxes = [Second, Third]
|
|
16
16
|
|
|
17
17
|
test "constants read each other in dependency order"
|
tests/std/json.plum
CHANGED
|
@@ -69,12 +69,12 @@ test "invalid numbers are errors: leading zeros, a bare sign or point, out of ra
|
|
|
69
69
|
assert text == "number out of range"
|
|
70
70
|
|
|
71
71
|
test "nesting past MaxJsonDepth is an error, not a stack overflow"
|
|
72
|
-
ok := "[".repeat(1000)
|
|
72
|
+
ok := "{"[".repeat(1000)}{"]".repeat(1000)}"
|
|
73
73
|
assert Json.fromStr(ok).isOk()
|
|
74
|
-
assert bad("[".repeat(1001)
|
|
74
|
+
assert bad("{"[".repeat(1001)}{"]".repeat(1001)}")
|
|
75
|
-
assert bad("[".repeat(200000)
|
|
75
|
+
assert bad("{"[".repeat(200000)}{"]".repeat(200000)}")
|
|
76
|
-
assert bad("{{\"a\":".repeat(1001)
|
|
76
|
+
assert bad("{"{{\"a\":".repeat(1001)}1{"}}".repeat(1001)}")
|
|
77
|
-
wide := "["
|
|
77
|
+
wide := "[{"[],".repeat(5000)}[]]"
|
|
78
78
|
assert Json.fromStr(wide)!.asList()!.length() == 5001
|
|
79
79
|
match Json.parse("[".repeat(2000))
|
|
80
80
|
Parsed(v) =>
|
tests/traits/statics_and_type_name.plum
CHANGED
|
@@ -43,6 +43,6 @@ test "a trait's static default is each implementer's, and an override wins"
|
|
|
43
43
|
|
|
44
44
|
test "through dispatch, Self.name() is the value's own type's static"
|
|
45
45
|
xs: List[Named] := [PlayScreen(id: 1), Own(id: 2)]
|
|
46
|
-
assert xs.map(|n| n.label()).
|
|
46
|
+
assert xs.map(|n| n.label()).join("") == "<PlayScreen><mine>"
|
|
47
47
|
ks: List[Kind[Int]] := [Box[Int](id: 1), Plain(id: 2)]
|
|
48
|
-
assert ks.map(|k| k.kindOf()).
|
|
48
|
+
assert ks.map(|k| k.kindOf()).join("") == "BoxPlain"
|
tests/traits/tags.plum
CHANGED
|
@@ -22,7 +22,7 @@ struct B(n: Int, m: Int) is Part[Int] =
|
|
|
22
22
|
|
|
23
23
|
struct C(s: Str) is Part[Int] =
|
|
24
24
|
fun weight(self) -> Int = self.s.length()
|
|
25
|
-
fun heavier(self, by: Int) -> C = C(s: self.s
|
|
25
|
+
fun heavier(self, by: Int) -> C = C(s: "{self.s}x")
|
|
26
26
|
|
|
27
27
|
struct D(n: Int) is Part[Int] =
|
|
28
28
|
fun weight(self) -> Int = 4 * self.n
|