From c7ada7202928aad4ccabc01cedd4ca5544691180 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Thu, 7 May 2026 23:33:27 +0200 Subject: [PATCH] Match patterns --- parser/src/ast.rs | 1 + parser/src/grammar.pest | 10 +++++++--- parser/src/lib.rs | 7 ++++--- src/codegen.rs | 1 + test/src/main.mist | 3 +++ 5 files changed, 16 insertions(+), 6 deletions(-) diff --git a/parser/src/ast.rs b/parser/src/ast.rs index c20da68..70c3516 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -97,6 +97,7 @@ pub enum Pattern { NamedTuple(Path, Vec), Struct(Path, Vec), Tuple(Vec), + Literal(Literal), Path(Path), Id(Identifier), } diff --git a/parser/src/grammar.pest b/parser/src/grammar.pest index ec4edac..4e67169 100644 --- a/parser/src/grammar.pest +++ b/parser/src/grammar.pest @@ -16,7 +16,7 @@ static_path = { identifier ~ ("::" ~ identifier)* } tuple_pattern = { "(" ~ (identifier ~ ("," ~ identifier)*)? ~ ")" } named_tuple_pattern = { static_path ~ "(" ~ (identifier ~ ("," ~ identifier)*)? ~ ")" } struct_pattern = { static_path ~ "{" ~ (identifier ~ ("," ~ identifier)*)? ~ "}" } -pattern = _{ named_tuple_pattern | tuple_pattern | struct_pattern | identifier } +pattern = _{ named_tuple_pattern | tuple_pattern | struct_pattern | literal | static_path } export = { "public" } mutable = { "mut" } @@ -64,7 +64,7 @@ mod_attribute = { } meta = { - static_path ~ ("=" ~ primary | "(" ~ meta_list? ~ ")")? + static_path ~ ("=" ~ literal | "(" ~ meta_list? ~ ")")? } meta_list = { @@ -164,11 +164,15 @@ macro_inner_v = { tuple = { "(" ~ (expr ~ ("," ~ expr)*)? ~ ")" } -primary = { +literal = { float | integer | boolean | string_lit +} + +primary = { + literal | static_path | tuple } diff --git a/parser/src/lib.rs b/parser/src/lib.rs index fbec5d8..e7f87f0 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -400,6 +400,7 @@ impl From> for Literal { match rule { Rule::primary => Self::from(inner.next().unwrap()), + Rule::literal => Self::from(inner.next().unwrap()), Rule::integer => Literal::Int(pair.as_str().parse::().unwrap()), Rule::float => Literal::Float(pair.as_str().parse::().unwrap()), Rule::boolean => Literal::Bool(pair.as_str().parse::().unwrap()), @@ -436,9 +437,7 @@ impl From> for Expression { } Rule::primary => Expression::from(inner.next().unwrap()), Rule::static_path => Expression::Path(Path::from(pair)), - Rule::integer | Rule::float | Rule::boolean | Rule::string_lit | Rule::tuple => { - Expression::Literal(Literal::from(pair)) - } + Rule::literal => Expression::Literal(Literal::from(pair)), _ => unimplemented!("{rule:#?}"), } } @@ -549,6 +548,8 @@ impl From> for Pattern { inner.map(Identifier::from).collect(), ), + Rule::literal => Pattern::Literal(Literal::from(pair)), + Rule::identifier => Pattern::Id(Identifier::from(pair)), Rule::static_path => Pattern::Path(Path::from(pair)), diff --git a/src/codegen.rs b/src/codegen.rs index 48a079b..70d2836 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -666,6 +666,7 @@ impl GetRust for Pattern { match self { Self::Id(id) => id.get_rust(), Self::Path(path) => path.get_rust(), + Self::Literal(lit) => lit.get_rust(), Self::Struct(path, ids) => format!( "{} {{{}}}", path.get_rust(), diff --git a/test/src/main.mist b/test/src/main.mist index 13cc58e..602dd59 100644 --- a/test/src/main.mist +++ b/test/src/main.mist @@ -9,6 +9,9 @@ void main() { match (MyEnum::Tuple(10, String::new())) { MyEnum::Tuple(i, _) => { println!("{i}"); + } + MyEnum::Named => { + } _ => {} }