From c083a547d79a677e89d2207db441c45bfb894063 Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Mon, 4 May 2026 19:43:49 +0200 Subject: [PATCH] Macro calls --- parser/src/ast.rs | 1 + parser/src/grammar.pest | 7 +++++++ parser/src/lib.rs | 2 ++ src/codegen.rs | 4 ++++ test/src/main.mist | 4 +++- 5 files changed, 17 insertions(+), 1 deletion(-) diff --git a/parser/src/ast.rs b/parser/src/ast.rs index d33557a..2dd6699 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -66,6 +66,7 @@ pub enum TopLevel { pub enum Postfix { FieldAccess(String), Call(Vec), + MacroCall(String), StructCall(HashMap), Index(Expression), Binary(BinaryOp, Expression), diff --git a/parser/src/grammar.pest b/parser/src/grammar.pest index fc0110c..ef05032 100644 --- a/parser/src/grammar.pest +++ b/parser/src/grammar.pest @@ -79,6 +79,11 @@ path_type = { static_path ~ ("<" ~ (type_expr ~ ("," ~ type_expr)*)? ~ ">")? } ref_type = { mutable? ~ "*" } type_expr = { (path_type | tuple_type) ~ ref_type* } +macro_inner_v = { + "(" ~ macro_inner_v* ~ ")" + | (!("(" | ")") ~ ANY) +} + primary = { integer | float @@ -90,6 +95,7 @@ primary = { postfix = { field_px | call_px + | macro_call_px | struct_px | index_px | binary_px @@ -97,6 +103,7 @@ postfix = { field_px = { "." ~ identifier } call_px = { "(" ~ (expr ~ ("," ~ expr)*)? ~ ")" } +macro_call_px = { "!" ~ "(" ~ macro_inner_v* ~ ")" } struct_px = { "{" ~ (struct_field_decl ~ ("," ~ struct_field_decl)*)? ~ "}" } index_px = { "[" ~ expr ~ "]" } binary_px = { bin_op ~ expr } diff --git a/parser/src/lib.rs b/parser/src/lib.rs index e8d2340..204dbb7 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -362,6 +362,8 @@ impl From> for Postfix { Postfix::Binary(op, Expression::from(inner.next().unwrap())) } + Rule::macro_call_px => Postfix::MacroCall(pair.into_inner().as_str().to_string()), + _ => unimplemented!("Postfix parsing not implemented yet {:?}", pair.as_rule()), } } diff --git a/src/codegen.rs b/src/codegen.rs index 34b9571..64ade01 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -144,6 +144,10 @@ impl PostfixChain for [Postfix] { format!("{}({})", result, args) } + Postfix::MacroCall(inner) => { + format!("{}!({})", result, inner) + } + Postfix::StructCall(fields) => { let fields = fields .iter() diff --git a/test/src/main.mist b/test/src/main.mist index 9f2489e..aa71749 100644 --- a/test/src/main.mist +++ b/test/src/main.mist @@ -1 +1,3 @@ -i32 add(i32* a, i32* b) {} \ No newline at end of file +void main() { + println!("Hello, World!"); +} \ No newline at end of file