Super and super
This commit is contained in:
@@ -16,6 +16,10 @@ pub fn class_decl(
|
||||
constructor: &Spanned<ClassConstructor>,
|
||||
items: &Vec<ClassItem>,
|
||||
) {
|
||||
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!(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,30 @@ impl GetRust for ExprPath {
|
||||
.collect::<Vec<_>>()
|
||||
.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) => {
|
||||
|
||||
@@ -7,6 +7,7 @@ use mist_parser::ast::*;
|
||||
|
||||
pub struct Context {
|
||||
pub expr_ensure_semicolon: bool,
|
||||
pub expr_super: Option<Path>,
|
||||
}
|
||||
|
||||
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<TopLevel>) -> String {
|
||||
let mut ctx = Context {
|
||||
expr_ensure_semicolon: true,
|
||||
expr_super: None,
|
||||
};
|
||||
|
||||
for tl in toplevels {
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user