diff --git a/codegen/src/expr.rs b/codegen/src/expr.rs index ec21e89..1729525 100644 --- a/codegen/src/expr.rs +++ b/codegen/src/expr.rs @@ -87,6 +87,7 @@ impl GenRust for Expression { } => { cg.add(&prefixes.get_rust()); initial.gen_rust(ctx, cg); + cg.add(&Some(prefixes).get_rust()); for postfix in postfixes { postfix.gen_rust(ctx, cg); } @@ -126,7 +127,7 @@ impl GetRust for Vec { } } -impl GetRust for Option> { +impl GetRust for Option<&Vec> { fn get_rust(&self) -> String { if let Some(prefixes) = self { prefixes diff --git a/codegen/src/statement.rs b/codegen/src/statement.rs index 4178c08..e2a1ecb 100644 --- a/codegen/src/statement.rs +++ b/codegen/src/statement.rs @@ -7,6 +7,7 @@ use crate::{GenRust, GetRust, RustCodegen}; impl GenRust for Block { fn gen_rust(&self, ctx: &mut Context, cg: &mut RustCodegen) { for stmt in &self.0 { + ctx.expr_ensure_semicolon = true; cg.add_indented(""); stmt.gen_rust(ctx, cg); } @@ -14,7 +15,6 @@ impl GenRust for Block { if let Some(soft_return) = &self.1 { ctx.expr_ensure_semicolon = false; soft_return.gen_rust(ctx, cg); - ctx.expr_ensure_semicolon = true; } } } diff --git a/codegen/src/top_level.rs b/codegen/src/top_level.rs index af80bbc..a5c54a9 100644 --- a/codegen/src/top_level.rs +++ b/codegen/src/top_level.rs @@ -197,7 +197,7 @@ impl GenRust for (&Vec>, &ClassConstructor) { } } - cg.add_indentedln(&format!("this.construct_class(")); + cg.add_indentedln(&format!("this.constructor(")); for (i, param) in self.1.params.0.iter().enumerate() { if i > 0 { @@ -207,22 +207,30 @@ impl GenRust for (&Vec>, &ClassConstructor) { param.name.gen_rust(ctx, cg); } - cg.add(")"); + cg.add(");"); cg.add_indentedln("this"); cg.indent -= 1; cg.add_indentedln("}\n"); + let mut constructor_params = vec![VarDecl { + mutable: false, + name: Pattern::Id(Identifier(String::from("self"))), + type_: Some(TypeExpr( + TypeExprKind::Path(Path(vec![Identifier(String::from("Self"))])), + vec![TypePostfix::RefMut], + )), + }]; + + constructor_params.append(&mut self.1.params.0.clone()); + FunctionDecl { visibility: self.1.visibility.clone(), name: Identifier(String::from("constructor")), generics: self.1.generics.clone(), - params: self.1.params.clone(), - return_type: TypeExpr( - TypeExprKind::Path(Path(vec![Identifier(String::from("Self"))])), - Vec::new(), - ), + params: ParamList(constructor_params), + return_type: TypeExpr::no_px(TypeExprKind::Tuple(Vec::new())), body: Some(self.1.body.clone()), } .gen_rust(ctx, cg);