plum
git clone https://git.pyrossh.dev/plum
A statically typed, imperative programming language inspired by rust, python
8dda3df
— Peter John
2026-11-10T15:51:27+05:30
Parser: binary literals of several digits, and a match of conditions
- compiler/checker/tests/checker/diagnostic_tests.rs +29 -0
- compiler/core/src/parser.rs +36 -0
- compiler/tooling/tree-sitter-plum/grammar.js +1 -1
- compiler/tooling/tree-sitter-plum/src/grammar.json +0 -0
- compiler/tooling/tree-sitter-plum/src/parser.c +0 -0
- compiler/tooling/tree-sitter-plum/test/corpus/literals.txt +73 -3
- docs/language.md +10 -0
- tests/syntax/binary_literals_and_guard_match.plum +68 -0
compiler/checker/tests/checker/diagnostic_tests.rs
CHANGED
|
@@ -271,3 +271,32 @@ fn aRenamedSiblingDeclarationKeepsItsSourceNameInMessages() {
|
|
|
271
271
|
assert!(shown.contains("assign 'd': type mismatch"), "{shown}");
|
|
272
272
|
assert_eq!(es[0].line, 6, "{es:?}");
|
|
273
273
|
}
|
|
274
|
+
|
|
275
|
+
/// A `match` of conditions is `match true` with guarded `_` arms; without a last `| _` it is
|
|
276
|
+
/// an error at the `match`.
|
|
277
|
+
#[test]
|
|
278
|
+
fn aMatchOfConditionsNeedsACatchAllArm() {
|
|
279
|
+
let es = errors("fun f(n: Int) -> Int = match\n | n > 90 => 3\n | n > 50 => 2\n");
|
|
280
|
+
assert!(
|
|
281
|
+
es.iter().any(|e| e.message.contains("a `match` of conditions needs a last `| _ =>` arm") && (e.line, e.column) == (1, 24)),
|
|
282
|
+
"{es:?}"
|
|
283
|
+
);
|
|
284
|
+
let mut ok = plum_test_support::parseRawSource("fun f(n: Int) -> Int = match\n | n > 90 => 3\n | n > 50 => 2\n | _ => 0\n");
|
|
285
|
+
plum_core::lower::frontEndDiag(&mut ok).expect("front end");
|
|
286
|
+
checkSource(&ok).expect("a match of conditions with a last `| _` checks");
|
|
287
|
+
// A condition that isn't a Bool is a type error like any guard's.
|
|
288
|
+
let es = errors("fun f(n: Int) -> Int = match\n | n => 3\n | _ => 0\n");
|
|
289
|
+
assert!(!es.is_empty(), "{es:?}");
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/// `0b1010` is one integer, wherever it is written.
|
|
293
|
+
#[test]
|
|
294
|
+
fn aBinaryLiteralOfSeveralDigitsIsOneInteger() {
|
|
295
|
+
let mut ok = plum_test_support::parseRawSource(
|
|
296
|
+
"fun g(n: Int) -> Int = n\n\nfun f(n: Int) -> Int = match n\n 0b10 => g(0b1010)\n _ => {0b11_01}\n",
|
|
297
|
+
);
|
|
298
|
+
plum_core::lower::frontEndDiag(&mut ok).expect("front end");
|
|
299
|
+
checkSource(&ok).expect("binary literals");
|
|
300
|
+
let dump = format!("{:?}", ok.items);
|
|
301
|
+
assert!(dump.contains("Int(10)") && dump.contains("Int(13)") && dump.contains("Int(2)"), "{dump}");
|
|
302
|
+
}
|
compiler/core/src/parser.rs
CHANGED
|
@@ -1011,6 +1011,42 @@ impl<'a> AstParser<'a> {
|
|
|
1011
1011
|
fn parseMatch(&self, node: Node) -> Match {
|
|
1012
1012
|
// match: "match" commaSep1(expression) "is" case+
|
|
1013
1013
|
let named: Vec<Node> = self.namedChildren(node);
|
|
1014
|
+
// A guard `match` has no subject: `| cond => e` runs the first arm whose condition
|
|
1015
|
+
// holds, and `| _ => e` any case. It is `match true` with `_ when cond => e` arms, so
|
|
1016
|
+
// the checker asks for the closing `| _` like any other missing case.
|
|
1017
|
+
if named.iter().any(|n| n.kind() == "guard_case") {
|
|
1018
|
+
let last = named.iter().rev().find(|n| n.kind() == "guard_case");
|
|
1019
|
+
if last.is_some_and(|n| n.child_by_field_name("guard").is_some_and(|g| g.kind() == "expression")) {
|
|
1020
|
+
let pos = node.start_position();
|
|
1021
|
+
self.litErrors.borrow_mut().push((
|
|
1022
|
+
pos.row + 1,
|
|
1023
|
+
pos.column + 1,
|
|
1024
|
+
"a `match` of conditions needs a last `| _ =>` arm for when none holds".to_string(),
|
|
1025
|
+
));
|
|
1026
|
+
}
|
|
1027
|
+
let cases: Vec<Case> = named
|
|
1028
|
+
.iter()
|
|
1029
|
+
.filter(|n| n.kind() == "guard_case")
|
|
1030
|
+
.map(|n| {
|
|
1031
|
+
let guard = n
|
|
1032
|
+
.child_by_field_name("guard")
|
|
1033
|
+
.filter(|g| g.kind() == "expression")
|
|
1034
|
+
.map(|g| self.parseExpression(self.unwrapExprNode(g)));
|
|
1035
|
+
let body = n
|
|
1036
|
+
.child_by_field_name("body")
|
|
1037
|
+
.map(|b| {
|
|
1038
|
+
if b.kind() == "body" {
|
|
1039
|
+
self.parseBlock(b)
|
|
1040
|
+
} else {
|
|
1041
|
+
self.inlineBody(self.unwrapExprNode(b))
|
|
1042
|
+
}
|
|
1043
|
+
})
|
|
1044
|
+
.unwrap_or(Block { stmts: vec![] });
|
|
1045
|
+
Case { patterns: vec![CasePattern::Wildcard], guard, body }
|
|
1046
|
+
})
|
|
1047
|
+
.collect();
|
|
1048
|
+
return Match { subjects: vec![Expr::BoolLit(true)], cases };
|
|
1049
|
+
}
|
|
1014
1050
|
let split = named
|
|
1015
1051
|
.iter()
|
|
1016
1052
|
.position(|n| n.kind() == "case")
|
compiler/tooling/tree-sitter-plum/grammar.js
CHANGED
|
@@ -23,7 +23,7 @@ const PREC = {
|
|
|
23
23
|
|
|
24
24
|
const DEC_DIGITS = token(sep1(/[0-9]+/, /_+/));
|
|
25
25
|
const HEX_DIGITS = token(sep1(/[0-9a-fA-F]+/, /_+/));
|
|
26
|
-
const BIN_DIGITS = token(sep1(/[01]/, /_+/));
|
|
26
|
+
const BIN_DIGITS = token(sep1(/[01]+/, /_+/));
|
|
27
27
|
const REAL_EXPONENT = token(seq(/[eE]/, optional(/[+-]/), DEC_DIGITS));
|
|
28
28
|
|
|
29
29
|
module.exports = grammar({
|
compiler/tooling/tree-sitter-plum/src/grammar.json
CHANGED
|
Binary file
|
compiler/tooling/tree-sitter-plum/src/parser.c
CHANGED
|
Binary file
|
compiler/tooling/tree-sitter-plum/test/corpus/literals.txt
CHANGED
|
@@ -61,9 +61,6 @@ fun main() =
|
|
|
61
61
|
(expression
|
|
62
62
|
(primary_expression
|
|
63
63
|
(integer))))
|
|
64
|
-
(expression
|
|
65
|
-
(primary_expression
|
|
66
|
-
(integer)))
|
|
67
64
|
(assign
|
|
68
65
|
(var_identifier)
|
|
69
66
|
(expression
|
|
@@ -136,3 +133,76 @@ fun main() =
|
|
|
136
133
|
(integer)))))))))))
|
|
137
134
|
(comment)
|
|
138
135
|
(comment))))
|
|
136
|
+
|
|
137
|
+
================================================================================
|
|
138
|
+
binary literals of several digits are one integer
|
|
139
|
+
================================================================================
|
|
140
|
+
|
|
141
|
+
fun bits(n: Int) -> Int =
|
|
142
|
+
x := 0b1010
|
|
143
|
+
y := g(0b10, 0B1_0)
|
|
144
|
+
zs := [0b11, 1]
|
|
145
|
+
match n
|
|
146
|
+
0b101 => {0b110}
|
|
147
|
+
_ => 0
|
|
148
|
+
|
|
149
|
+
--------------------------------------------------------------------------------
|
|
150
|
+
|
|
151
|
+
(source
|
|
152
|
+
(fn
|
|
153
|
+
(fn_identifier)
|
|
154
|
+
(param
|
|
155
|
+
(var_identifier)
|
|
156
|
+
(type
|
|
157
|
+
(type_identifier)))
|
|
158
|
+
(type
|
|
159
|
+
(type_identifier))
|
|
160
|
+
(body
|
|
161
|
+
(assign
|
|
162
|
+
(var_identifier)
|
|
163
|
+
(expression
|
|
164
|
+
(primary_expression
|
|
165
|
+
(integer))))
|
|
166
|
+
(assign
|
|
167
|
+
(var_identifier)
|
|
168
|
+
(expression
|
|
169
|
+
(primary_expression
|
|
170
|
+
(fn_call
|
|
171
|
+
(var_identifier)
|
|
172
|
+
(fn_argument_list
|
|
173
|
+
(expression
|
|
174
|
+
(primary_expression
|
|
175
|
+
(integer)))
|
|
176
|
+
(expression
|
|
177
|
+
(primary_expression
|
|
178
|
+
(integer))))))))
|
|
179
|
+
(assign
|
|
180
|
+
(var_identifier)
|
|
181
|
+
(expression
|
|
182
|
+
(primary_expression
|
|
183
|
+
(list_literal
|
|
184
|
+
(expression
|
|
185
|
+
(primary_expression
|
|
186
|
+
(integer)))
|
|
187
|
+
(expression
|
|
188
|
+
(primary_expression
|
|
189
|
+
(integer)))))))
|
|
190
|
+
(expression
|
|
191
|
+
(match
|
|
192
|
+
(expression
|
|
193
|
+
(primary_expression
|
|
194
|
+
(var_identifier)))
|
|
195
|
+
(case
|
|
196
|
+
(case_pattern
|
|
197
|
+
(integer))
|
|
198
|
+
(expression
|
|
199
|
+
(primary_expression
|
|
200
|
+
(parenthesized_expression
|
|
201
|
+
(expression
|
|
202
|
+
(primary_expression
|
|
203
|
+
(integer)))))))
|
|
204
|
+
(case
|
|
205
|
+
(case_pattern)
|
|
206
|
+
(expression
|
|
207
|
+
(primary_expression
|
|
208
|
+
(integer)))))))))
|
docs/language.md
CHANGED
|
@@ -272,6 +272,16 @@ The same form works as `while Pattern := value`.
|
|
|
272
272
|
|
|
273
273
|
Use `match` when every case needs handling, and `if` when only one or two do. Only `match` is checked for missing cases.
|
|
274
274
|
|
|
275
|
+
**A `match` of conditions** has no value after `match`: each arm is `| condition => value`, the first one that holds runs, and the last arm is `| _ =>`.
|
|
276
|
+
|
|
277
|
+
```plum
|
|
278
|
+
fun sign(n: Int) -> Str =
|
|
279
|
+
match
|
|
280
|
+
| n < 0 => "negative"
|
|
281
|
+
| n == 0 => "zero"
|
|
282
|
+
| _ => "positive"
|
|
283
|
+
```
|
|
284
|
+
|
|
275
285
|
## Option and Result
|
|
276
286
|
|
|
277
287
|
Plum has no `null` and no exceptions.
|
tests/syntax/binary_literals_and_guard_match.plum
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
# Two parser bugs found on the self-hosting branch (its KNOWN_DIFFS.md).
|
|
2
|
+
# A binary literal of several digits was read as its first digit and then a second number
|
|
3
|
+
# (`x := 0b1010` made `x` 1), and a parse error anywhere but at a statement's end.
|
|
4
|
+
# A `match` of conditions (`| cond => e` arms, no subject) had every arm read as a subject.
|
|
5
|
+
import std/Int
|
|
6
|
+
import std/Str
|
|
7
|
+
import std/collections/List
|
|
8
|
+
|
|
9
|
+
fun id(n: Int) -> Int = n
|
|
10
|
+
|
|
11
|
+
fun bits(n: Int) -> Int = match n
|
|
12
|
+
0b10 => 1
|
|
13
|
+
0b1111 => 2
|
|
14
|
+
_ => 0
|
|
15
|
+
|
|
16
|
+
fun sign(a: Int) -> Str =
|
|
17
|
+
match
|
|
18
|
+
| a < 0 => "neg"
|
|
19
|
+
| a == 0 => "zero"
|
|
20
|
+
| _ => "pos"
|
|
21
|
+
|
|
22
|
+
fun grade(n: Int) -> Int = match
|
|
23
|
+
| n > 90 => 3
|
|
24
|
+
| n > 50 =>
|
|
25
|
+
half := n / 2
|
|
26
|
+
half - 24
|
|
27
|
+
| _ => 0
|
|
28
|
+
|
|
29
|
+
struct Tally(mut n: Int)
|
|
30
|
+
|
|
31
|
+
fun bump(t: Tally, by: Int) =
|
|
32
|
+
match
|
|
33
|
+
| by > 10 =>
|
|
34
|
+
t.n = t.n + 10
|
|
35
|
+
| by > 0 =>
|
|
36
|
+
t.n = t.n + by
|
|
37
|
+
| _ =>
|
|
38
|
+
t.n = 0
|
|
39
|
+
|
|
40
|
+
test "a binary literal of several digits is one number"
|
|
41
|
+
x := 0b1010
|
|
42
|
+
assert x == 10
|
|
43
|
+
assert 0b0 == 0 && 0b1 == 1 && 0b10 == 2 && 0B11 == 3
|
|
44
|
+
assert 0b1111_0000 == 240 && 0b1_0 == 2
|
|
45
|
+
assert id(0b101) == 5
|
|
46
|
+
assert [0b11, 0b100].length() == 2
|
|
47
|
+
assert {0b110} + 1 == 7
|
|
48
|
+
assert 0b111111111111111111111111111111111111111111111111111111111111111 == 9223372036854775807
|
|
49
|
+
|
|
50
|
+
test "a binary literal as a pattern"
|
|
51
|
+
assert bits(2) == 1 && bits(15) == 2 && bits(3) == 0
|
|
52
|
+
|
|
53
|
+
test "a match of conditions runs the first arm that holds"
|
|
54
|
+
assert sign(-5) == "neg" && sign(0) == "zero" && sign(9) == "pos"
|
|
55
|
+
assert grade(95) == 3 && grade(60) == 6 && grade(1) == 0
|
|
56
|
+
k := match
|
|
57
|
+
| sign(1) == "pos" => 7
|
|
58
|
+
| _ => 9
|
|
59
|
+
assert k == 7
|
|
60
|
+
|
|
61
|
+
test "a match of conditions as a statement"
|
|
62
|
+
t := Tally(n: 0)
|
|
63
|
+
bump(t, 3)
|
|
64
|
+
assert t.n == 3
|
|
65
|
+
bump(t, 50)
|
|
66
|
+
assert t.n == 13
|
|
67
|
+
bump(t, -1)
|
|
68
|
+
assert t.n == 0
|