Super and super

This commit is contained in:
2026-06-07 03:36:02 +02:00
parent e70e813497
commit fa4a7ee4c7
4 changed files with 48 additions and 2 deletions
+16
View File
@@ -16,6 +16,10 @@ pub fn class_decl(
constructor: &Spanned<ClassConstructor>, constructor: &Spanned<ClassConstructor>,
items: &Vec<ClassItem>, items: &Vec<ClassItem>,
) { ) {
if let Some(inherits) = inherits {
ctx.expr_super = Some(get_type_path(inherits));
}
// Struct decl // Struct decl
cg.addln(&format!( cg.addln(&format!(
"{}struct {}{} {{", "{}struct {}{} {{",
@@ -200,6 +204,8 @@ pub fn class_decl(
cg.indent -= 1; cg.indent -= 1;
cg.addln("}"); cg.addln("}");
} }
ctx.expr_super = None;
} }
impl GenRust impl GenRust
@@ -394,3 +400,13 @@ pub fn gen_method_point(method: &FunctionDecl, ctx: &mut Context, cg: &mut RustC
cg.indent -= 1; cg.indent -= 1;
cg.add_indentedln("}"); 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!(),
}
}
+25 -1
View File
@@ -12,6 +12,30 @@ impl GetRust for ExprPath {
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join("::") .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 { impl GetRust for ExprPathSegment {
@@ -63,7 +87,7 @@ impl GenRust for Expression {
}; };
match self { 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::Literal(literal) => literal.gen_rust(ctx, cg),
Expression::Statement(stmt) => stmt.gen_rust(ctx, cg), Expression::Statement(stmt) => stmt.gen_rust(ctx, cg),
Expression::Array(values) => { Expression::Array(values) => {
+6
View File
@@ -7,6 +7,7 @@ use mist_parser::ast::*;
pub struct Context { pub struct Context {
pub expr_ensure_semicolon: bool, pub expr_ensure_semicolon: bool,
pub expr_super: Option<Path>,
} }
pub trait GenRust { pub trait GenRust {
@@ -15,6 +16,10 @@ pub trait GenRust {
pub trait GetRust { pub trait GetRust {
fn get_rust(&self) -> String; fn get_rust(&self) -> String;
fn get_rust_ctx(&self, cx: &mut Context) -> String {
let _ = cx;
self.get_rust()
}
} }
#[derive(Default)] #[derive(Default)]
@@ -57,6 +62,7 @@ impl RustCodegen {
pub fn generate(&mut self, toplevels: Vec<TopLevel>) -> String { pub fn generate(&mut self, toplevels: Vec<TopLevel>) -> String {
let mut ctx = Context { let mut ctx = Context {
expr_ensure_semicolon: true, expr_ensure_semicolon: true,
expr_super: None,
}; };
for tl in toplevels { for tl in toplevels {
+1 -1
View File
@@ -13,7 +13,7 @@ pub class Animal {
pub class Dog : Animal { pub class Dog : Animal {
constructor() { constructor() {
unsafe { unsafe {
std::ptr::write(&mut self._super, Animal::new()); std::ptr::write(&mut super, Super::new());
} }
} }