From c07e0bcb3eaf7d7bfcd430dc66b901022a3ae4ab Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Wed, 3 Jun 2026 19:51:27 +0200 Subject: [PATCH] Fixed constructor args #60 --- crates/codegen/src/expr.rs | 6 +++++- crates/codegen/src/top_level.rs | 31 +++++++++++++++++++++++++++++-- crates/parser/src/ast/expr.rs | 14 ++++++++++++++ mist-test/src/main.mist | 6 +++++- 4 files changed, 53 insertions(+), 4 deletions(-) diff --git a/crates/codegen/src/expr.rs b/crates/codegen/src/expr.rs index 9e38e67..0de2c59 100644 --- a/crates/codegen/src/expr.rs +++ b/crates/codegen/src/expr.rs @@ -38,7 +38,11 @@ impl GenRust for Literal { Self::Tuple(values) => { cg.add("("); - for val in values { + for (i, val) in values.iter().enumerate() { + if i > 0 { + cg.add(", "); + } + val.gen_rust(ctx, cg); } diff --git a/crates/codegen/src/top_level.rs b/crates/codegen/src/top_level.rs index 871155e..8547fa1 100644 --- a/crates/codegen/src/top_level.rs +++ b/crates/codegen/src/top_level.rs @@ -196,12 +196,21 @@ impl GenRust for (&Vec>, &Spanned) { cg.add_indented(&format!("this.constructor(")); - for (i, param) in self.1.item.params.0.iter().enumerate() { + for (i, param) in self + .1 + .item + .params + .0 + .iter() + .map(|v| expr_pattern(&v.name)) + .enumerate() + { if i > 0 { cg.add(", "); } - param.name.gen_rust(ctx, cg); + ctx.expr_ensure_semicolon = false; + param.gen_rust(ctx, cg); } cg.addln(");"); @@ -237,6 +246,24 @@ impl GenRust for (&Vec>, &Spanned) { } } +pub fn expr_pattern(pat: &Pattern) -> Expression { + match pat { + Pattern::Literal(v) => Expression::Literal(v.clone()), + Pattern::Path(_, v) => Expression::Path(v.clone().into()), + Pattern::Tuple(items) => Expression::Literal(Literal::Tuple( + items.iter().map(|v| expr_pattern(&v)).collect(), + )), + Pattern::NamedTuple(path, items) => Expression::Fix { + initial: Box::new(Expression::Path(path.clone().into())), + prefixes: Vec::new(), + postfixes: vec![Postfix::Call( + items.iter().map(|v| expr_pattern(&v)).collect(), + )], + }, + Pattern::Struct(_, _) => unimplemented!(), + } +} + impl GenRust for TopLevelKind { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { match self { diff --git a/crates/parser/src/ast/expr.rs b/crates/parser/src/ast/expr.rs index 5aba972..2146b38 100644 --- a/crates/parser/src/ast/expr.rs +++ b/crates/parser/src/ast/expr.rs @@ -100,3 +100,17 @@ impl Expression { } } } + +impl From for ExprPath { + fn from(path: Path) -> Self { + Self( + path.0 + .into_iter() + .map(|v| ExprPathSegment { + ident: v, + generics: None, + }) + .collect(), + ) + } +} diff --git a/mist-test/src/main.mist b/mist-test/src/main.mist index a6546d1..994f30e 100644 --- a/mist-test/src/main.mist +++ b/mist-test/src/main.mist @@ -1,3 +1,7 @@ +class Bro { + constructor([str*, str*] [idk, bro]) {} +} + void main() { test_package::hello(); -} \ No newline at end of file +}