From fa4a7ee4c768e3b3a8a9be0f0ef254209ae41425 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Sun, 7 Jun 2026 03:36:02 +0200 Subject: [PATCH] Super and super --- crates/codegen/src/class_decl.rs | 16 ++++++++++++++++ crates/codegen/src/expr.rs | 26 +++++++++++++++++++++++++- crates/codegen/src/lib.rs | 6 ++++++ mist-test/src/items/class_test.mist | 2 +- 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/crates/codegen/src/class_decl.rs b/crates/codegen/src/class_decl.rs index c8d9fbe..c00bbb1 100644 --- a/crates/codegen/src/class_decl.rs +++ b/crates/codegen/src/class_decl.rs @@ -16,6 +16,10 @@ pub fn class_decl( constructor: &Spanned, items: &Vec, ) { + if let Some(inherits) = inherits { + ctx.expr_super = Some(get_type_path(inherits)); + } + // Struct decl cg.addln(&format!( "{}struct {}{} {{", @@ -200,6 +204,8 @@ pub fn class_decl( cg.indent -= 1; cg.addln("}"); } + + ctx.expr_super = None; } impl GenRust @@ -394,3 +400,13 @@ pub fn gen_method_point(method: &FunctionDecl, ctx: &mut Context, cg: &mut RustC cg.indent -= 1; cg.add_indentedln("}"); } + +pub fn get_type_path(ty: &TypeExpr) -> Path { + match ty { + TypeExpr::Path(p, _) => p.clone(), + TypeExpr::Dyn(v) => get_type_path(v), + TypeExpr::Ref { ty, .. } => get_type_path(ty), + TypeExpr::UnsafePtr { ty, .. } => get_type_path(ty), + _ => unimplemented!(), + } +} diff --git a/crates/codegen/src/expr.rs b/crates/codegen/src/expr.rs index fbfdfe8..9cf5171 100644 --- a/crates/codegen/src/expr.rs +++ b/crates/codegen/src/expr.rs @@ -12,6 +12,30 @@ impl GetRust for ExprPath { .collect::>() .join("::") } + + fn get_rust_ctx(&self, cx: &mut Context) -> String { + let mut a = self.clone(); + + if let Some(path) = &cx.expr_super { + if self.0[0].ident.0 == "super" { + a.0[0].ident.0 = String::from("self._super"); + } else if self.0[0].ident.0 == "Super" { + a.0[0].ident = path.0[0].clone(); + + if path.0.len() > 1 { + a.0.splice( + 1..1, + path.0.clone().into_iter().skip(1).map(|v| ExprPathSegment { + ident: v, + generics: None, + }), + ); + } + } + } + + a.get_rust() + } } impl GetRust for ExprPathSegment { @@ -63,7 +87,7 @@ impl GenRust for Expression { }; match self { - Expression::Path(path) => cg.add(&path.get_rust()), + Expression::Path(path) => cg.add(&path.get_rust_ctx(ctx)), Expression::Literal(literal) => literal.gen_rust(ctx, cg), Expression::Statement(stmt) => stmt.gen_rust(ctx, cg), Expression::Array(values) => { diff --git a/crates/codegen/src/lib.rs b/crates/codegen/src/lib.rs index ea63d97..a1d74cb 100644 --- a/crates/codegen/src/lib.rs +++ b/crates/codegen/src/lib.rs @@ -7,6 +7,7 @@ use mist_parser::ast::*; pub struct Context { pub expr_ensure_semicolon: bool, + pub expr_super: Option, } pub trait GenRust { @@ -15,6 +16,10 @@ pub trait GenRust { pub trait GetRust { fn get_rust(&self) -> String; + fn get_rust_ctx(&self, cx: &mut Context) -> String { + let _ = cx; + self.get_rust() + } } #[derive(Default)] @@ -57,6 +62,7 @@ impl RustCodegen { pub fn generate(&mut self, toplevels: Vec) -> String { let mut ctx = Context { expr_ensure_semicolon: true, + expr_super: None, }; for tl in toplevels { diff --git a/mist-test/src/items/class_test.mist b/mist-test/src/items/class_test.mist index 71cbfcf..9d88134 100644 --- a/mist-test/src/items/class_test.mist +++ b/mist-test/src/items/class_test.mist @@ -13,7 +13,7 @@ pub class Animal { pub class Dog : Animal { constructor() { unsafe { - std::ptr::write(&mut self._super, Animal::new()); + std::ptr::write(&mut super, Super::new()); } }