plum

#treesitter#compiler#wasm

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

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


d54833d — Peter John 2026-11-10T11:24:43+05:30
wasm codegen: string and float literal match patterns
compiler/wasm-codegen/src/matching.rs CHANGED
@@ -487,13 +487,9 @@ pub(crate) fn compileCasePositions(
487
487
  )
488
488
  }
489
489
  }
490
- ast::CasePattern::Int(n) => {
491
- if subject_vt != ValType::I64 {
492
- return Err("codegen: integer match pattern against a non-Int subject".to_string());
490
+ ast::CasePattern::Int(_) | ast::CasePattern::Float(_) | ast::CasePattern::String(_) => {
493
- }
494
491
  Instruction::LocalGet(scratch_local).encode(body);
495
- Instruction::I64Const(*n).encode(body);
492
+ emitLiteralTest(pat, subject_vt, "subject", body, ctx, state)?;
496
- Instruction::I64Eq.encode(body);
497
493
  Instruction::If(blockTypeFor(result_vt)).encode(body);
498
494
  let _tg = TailDepth::enter(&ctx.tail_depth);
499
495
  compileCasePositions(
@@ -520,12 +516,6 @@ pub(crate) fn compileCasePositions(
520
516
  Instruction::End.encode(body);
521
517
  Ok(())
522
518
  }
523
- ast::CasePattern::String(_) => {
524
- Err("codegen: string match patterns are not yet supported".to_string())
525
- }
526
- ast::CasePattern::Float(_) => {
527
- Err("codegen: float match patterns are not yet supported".to_string())
528
- }
529
519
  ast::CasePattern::Class { name, fields, .. } => compileVariantConstructorArm(
530
520
  pat,
531
521
  name,
@@ -1008,10 +998,7 @@ pub(crate) fn compileFieldPatterns(
1008
998
  Instruction::End.encode(body);
1009
999
  Ok(())
1010
1000
  }
1011
- ast::CasePattern::Int(n) => {
1012
- if inst_vt != ValType::I64 {
1013
- return Err("codegen: integer match pattern against a non-Int field".to_string());
1001
+ ast::CasePattern::Int(_) | ast::CasePattern::Float(_) | ast::CasePattern::String(_) => {
1014
- }
1015
1002
  emitContainerField(
1016
1003
  body,
1017
1004
  ctx,
@@ -1020,8 +1007,7 @@ pub(crate) fn compileFieldPatterns(
1020
1007
  fpos,
1021
1008
  leaf_map,
1022
1009
  );
1023
- Instruction::I64Const(*n).encode(body);
1010
+ emitLiteralTest(pat, inst_vt, "field", body, ctx, state)?;
1024
- Instruction::I64Eq.encode(body);
1025
1011
  Instruction::If(blockTypeFor(result_vt)).encode(body);
1026
1012
  let _tg = TailDepth::enter(&ctx.tail_depth);
1027
1013
  compileFieldPatterns(
@@ -1054,12 +1040,6 @@ pub(crate) fn compileFieldPatterns(
1054
1040
  Instruction::End.encode(body);
1055
1041
  Ok(())
1056
1042
  }
1057
- ast::CasePattern::String(_) => {
1058
- Err("codegen: string match patterns are not yet supported".to_string())
1059
- }
1060
- ast::CasePattern::Float(_) => {
1061
- Err("codegen: float match patterns are not yet supported".to_string())
1062
- }
1063
1043
  ast::CasePattern::Class {
1064
1044
  name,
1065
1045
  fields: inner_fields,
@@ -1425,3 +1405,46 @@ pub(crate) fn compilePatternBind(
1425
1405
  state,
1426
1406
  )
1427
1407
  }
1408
+
1409
+ /// With the matched value (`vt`) on the stack, leaves whether it equals the literal
1410
+ /// pattern `pat`: `i64.eq` for an `Int`, `f64.eq` for a `Float` (so `0.0` matches `-0.0`
1411
+ /// and nothing matches NaN, as with `==`), and `Str.eq` (content equality) for a string.
1412
+ /// `what` names the matched thing in an error ("subject" or "field").
1413
+ pub(crate) fn emitLiteralTest(
1414
+ pat: &ast::CasePattern,
1415
+ vt: ValType,
1416
+ what: &str,
1417
+ body: &mut Vec<u8>,
1418
+ ctx: &LocalCtx,
1419
+ state: &mut ModuleState,
1420
+ ) -> Result<(), String> {
1421
+ match pat {
1422
+ ast::CasePattern::Int(n) => {
1423
+ if vt != ValType::I64 {
1424
+ return Err(format!("codegen: integer match pattern against a non-Int {what}"));
1425
+ }
1426
+ Instruction::I64Const(*n).encode(body);
1427
+ Instruction::I64Eq.encode(body);
1428
+ }
1429
+ ast::CasePattern::Float(x) => {
1430
+ if vt != ValType::F64 {
1431
+ return Err(format!("codegen: float match pattern against a non-Float {what}"));
1432
+ }
1433
+ Instruction::F64Const(*x).encode(body);
1434
+ Instruction::F64Eq.encode(body);
1435
+ }
1436
+ ast::CasePattern::String(text) => {
1437
+ if !matches!(vt, ValType::Ref(_)) {
1438
+ return Err(format!("codegen: string match pattern against a non-Str {what}"));
1439
+ }
1440
+ let eq = *ctx
1441
+ .func_ids
1442
+ .get("Str::eq")
1443
+ .ok_or_else(|| "codegen: a string match pattern requires `Str.eq` (import std/Str)".to_string())?;
1444
+ compileStaticString(text, body, state);
1445
+ Instruction::Call(eq).encode(body);
1446
+ }
1447
+ _ => return Err("internal codegen error: not a literal pattern".to_string()),
1448
+ }
1449
+ Ok(())
1450
+ }
compiler/wasm-codegen/tests/wasm_codegen/codegen_tests.rs CHANGED
@@ -257,12 +257,11 @@ fn stringInterpolationOfAFloatIsAClearError() {
257
257
  assert!(err.contains("Float"), "got: {}", err);
258
258
  }
259
259
 
260
+ /// String and float literal patterns compile (`tests/core/literal_patterns.plum` runs them).
260
261
  #[test]
261
- fn matchStringPatternIsAClearError() {
262
- let src = "fun main(a: Str) -> Int =\n match a\n \"x\" =>\n 1\n _ =>\n 0\n";
263
- let source = parse(src);
264
- let err = compileSource(&source).expect_err("string match patterns are not yet supported");
265
- assert!(err.contains("string match"), "got: {}", err);
262
+ fn matchStringAndFloatPatternsCompile() {
263
+ let src = "import std/Int\nimport std/Str\nimport std/Float\n\nfun pick(a: Str, x: Float) -> Int =\n match a\n \"x\" => 1\n _ =>\n match x\n 0.5 => 2\n _ => 0\n\nfun main() -> Int =\n pick(\"x\", 0.0) + pick(\"y\", 0.5) * 10\n";
264
+ assert_eq!(plum_test_support::runInt("strfloatpat", src), 21);
266
265
  }
267
266
 
268
267
  // The pass-through-on-true half of this used to also live here, migrated to
tests/core/literal_patterns.plum ADDED
@@ -0,0 +1,57 @@
1
+ import std/Int
2
+ import std/Str
3
+ import std/Float
4
+ import std/Option
5
+
6
+ # String and float literals as match patterns: a string matches by content (`Str.eq`),
7
+ # a float by `f64.eq` (so `0.0` matches `-0.0`), both as the whole subject and inside a
8
+ # payload.
9
+
10
+ fun command(s: Str) -> Int =
11
+ match s
12
+ "go" => 1
13
+ "stop" => 2
14
+ "" => 3
15
+ _ => 0
16
+
17
+ fun scale(x: Float) -> Int =
18
+ match x
19
+ 0.0 => 0
20
+ 0.5 => 1
21
+ 1.5 => 2
22
+ _ => 9
23
+
24
+ fun greeting(o: Option[Str]) -> Str =
25
+ match o
26
+ Some("hi") => "hello"
27
+ Some(other) => other
28
+ None => "none"
29
+
30
+ fun half(o: Option[Float]) -> Bool =
31
+ match o
32
+ Some(0.5) => true
33
+ _ => false
34
+
35
+ test "a string pattern matches by content"
36
+ assert command("go") == 1
37
+ assert command("stop") == 2
38
+ assert command("") == 3
39
+ assert command("g" + "o") == 1
40
+ assert command("going") == 0
41
+
42
+ test "a float pattern matches by value"
43
+ assert scale(0.0) == 0
44
+ assert scale(-0.0) == 0
45
+ assert scale(0.5) == 1
46
+ assert scale(1.5) == 2
47
+ assert scale(2.0) == 9
48
+
49
+ test "a string pattern inside a payload"
50
+ assert greeting(Some("hi")) == "hello"
51
+ assert greeting(Some("yo")) == "yo"
52
+ assert greeting(None) == "none"
53
+
54
+ test "a float pattern inside a payload"
55
+ assert half(Some(0.5))
56
+ assert !half(Some(0.25))
57
+ assert !half(None)