plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
a1aafdc
— Peter John
2026-11-01T20:48:14+05:30
T7 review: Bool is a builtin union member; unions + try/for/test regression tests
plum-core/src/unions.rs
CHANGED
|
@@ -9,7 +9,7 @@ use std::cell::{Cell, RefCell};
|
|
|
9
9
|
use std::collections::HashMap;
|
|
10
10
|
|
|
11
11
|
/// Types the compiler provides without an item in the source.
|
|
12
|
-
const BUILTIN_MEMBERS: &[&str] = &["Int", "Float", "Byte"];
|
|
12
|
+
const BUILTIN_MEMBERS: &[&str] = &["Int", "Float", "Byte", "Bool"];
|
|
13
13
|
|
|
14
14
|
/// `Shape__Circle`: the hidden variant that wraps member `Circle` of `Shape`.
|
|
15
15
|
pub fn hiddenVariant(enumName: &str, member: &str) -> String {
|
plum-examples/unions.plum
CHANGED
|
@@ -61,6 +61,28 @@ fun make(big: Bool) -> Shape =
|
|
|
61
61
|
else
|
|
62
62
|
Circle(r: 1.0)
|
|
63
63
|
|
|
64
|
+
enum Jb = BNull | Bool | Float | Str | List[Jb]
|
|
65
|
+
|
|
66
|
+
fun showB(j: Jb) -> Str =
|
|
67
|
+
match j
|
|
68
|
+
BNull => "null"
|
|
69
|
+
Bool => if j then "T" else "F"
|
|
70
|
+
Float => "f"
|
|
71
|
+
Str => "s"
|
|
72
|
+
List => "l"
|
|
73
|
+
|
|
74
|
+
fun pos(n: Int) -> !List[Jv] =
|
|
75
|
+
if n < 0
|
|
76
|
+
return error.Neg
|
|
77
|
+
xs := List[Jv]()
|
|
78
|
+
xs.push(n)
|
|
79
|
+
xs.push("a")
|
|
80
|
+
xs
|
|
81
|
+
|
|
82
|
+
fun posLen(n: Int) -> !Int =
|
|
83
|
+
xs := try pos(n)
|
|
84
|
+
xs.length()
|
|
85
|
+
|
|
64
86
|
test "struct_member_copy"
|
|
65
87
|
s := make(false)
|
|
66
88
|
if Circle := s
|
|
@@ -116,3 +138,28 @@ test "auto_dispatch"
|
|
|
116
138
|
assert make(true).area() == 100.0
|
|
117
139
|
assert make(false).area() == 3.0
|
|
118
140
|
assert "{make(false)}" == "circle"
|
|
141
|
+
|
|
142
|
+
test "bool_member"
|
|
143
|
+
assert showB(true) == "T"
|
|
144
|
+
assert showB(false) == "F"
|
|
145
|
+
assert showB(1.5) == "f"
|
|
146
|
+
assert "{showB(true)}" == "T"
|
|
147
|
+
|
|
148
|
+
test "union_in_try"
|
|
149
|
+
assert {posLen(3) catch 0} == 2
|
|
150
|
+
assert {posLen(-1) catch 0} == 0
|
|
151
|
+
ys := pos(3) catch List[Jv]()
|
|
152
|
+
assert describe(ys) == "list"
|
|
153
|
+
|
|
154
|
+
test "union_with_for"
|
|
155
|
+
xs := List[Jv]()
|
|
156
|
+
xs.push(1)
|
|
157
|
+
xs.push("a")
|
|
158
|
+
xs.push(2.5)
|
|
159
|
+
n := 0
|
|
160
|
+
for x := range xs
|
|
161
|
+
n = n + describe(x).length()
|
|
162
|
+
assert n == 3 + 5 + 5
|
|
163
|
+
|
|
164
|
+
test "union_toplevel_plain"
|
|
165
|
+
assert describe(JvNull) == "null"
|
plum-wasm-codegen/tests/unions_e2e_test.rs
CHANGED
|
@@ -187,3 +187,22 @@ fun go(p: Pair) -> Int =
|
|
|
187
187
|
"{errs:?}"
|
|
188
188
|
);
|
|
189
189
|
}
|
|
190
|
+
|
|
191
|
+
#[test]
|
|
192
|
+
fn bool_is_a_member() {
|
|
193
|
+
let src = withDefs(
|
|
194
|
+
"\
|
|
195
|
+
enum Jb = BNull | Bool | Float
|
|
196
|
+
|
|
197
|
+
fun code(j: Jb) -> Int =
|
|
198
|
+
match j
|
|
199
|
+
BNull => 0
|
|
200
|
+
Bool => if j then 1 else 2
|
|
201
|
+
Float => 3
|
|
202
|
+
|
|
203
|
+
fun main() -> Int =
|
|
204
|
+
code(true) * 100 + code(false) * 10 + code(1.5)
|
|
205
|
+
",
|
|
206
|
+
);
|
|
207
|
+
assert_eq!(runInt("boolmember", &src), 123);
|
|
208
|
+
}
|