AST and codegen

This commit is contained in:
2026-05-10 17:08:58 +02:00
parent 22b1b377f3
commit 81d0286226
2 changed files with 40 additions and 3 deletions
+9
View File
@@ -74,6 +74,7 @@ pub enum TopLevelKind {
ModAttribute, ModAttribute,
Import(Path), Import(Path),
Mod(Identifier), Mod(Identifier),
ImplDecl(ImplDecl),
EnumDecl { EnumDecl {
visibility: Visibility, visibility: Visibility,
name: Identifier, name: Identifier,
@@ -192,6 +193,14 @@ pub enum Statement {
Continue, Continue,
} }
#[derive(Debug, Clone, Serialize)]
pub struct ImplDecl {
pub generics: Generics,
pub target: TypeExpr,
pub trait_: Option<TypeExpr>,
pub methods: Vec<FunctionDecl>,
}
#[derive(Debug, Clone, Serialize)] #[derive(Debug, Clone, Serialize)]
pub struct VarDecl { pub struct VarDecl {
pub mutable: bool, pub mutable: bool,
+31 -3
View File
@@ -1,8 +1,8 @@
use parser::ast::{ use parser::ast::{
Attribute, BinaryOp, Block, EnumItem, Expression, FieldDecl, FunctionDecl, Generic, Generics, Attribute, BinaryOp, Block, EnumItem, Expression, FieldDecl, FunctionDecl, Generic, Generics,
Identifier, Literal, Path, Pattern, Postfix, Prefix, Statement, StatementBranch, TopLevel, Identifier, ImplDecl, Literal, Path, Pattern, Postfix, Prefix, Statement, StatementBranch,
TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, VarDecl, VarDeclStmt, TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, VarDecl,
Visibility, VarDeclStmt, Visibility,
}; };
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
@@ -315,6 +315,7 @@ impl ToRust for TopLevelKind {
Self::Import(path) => cg.addln(&format!("use {};", path.get_rust())), Self::Import(path) => cg.addln(&format!("use {};", path.get_rust())),
Self::Mod(id) => cg.addln(&format!("mod {};", id.get_rust())), Self::Mod(id) => cg.addln(&format!("mod {};", id.get_rust())),
Self::FunctionDecl(decl) => decl.to_rust(cg), Self::FunctionDecl(decl) => decl.to_rust(cg),
Self::ImplDecl(impl_) => impl_.to_rust(cg),
Self::StructDecl { Self::StructDecl {
visibility, visibility,
name, name,
@@ -615,6 +616,33 @@ impl ToRust for FunctionDecl {
} }
} }
impl ToRust for ImplDecl {
fn to_rust(&self, cg: &mut RustCodegen) {
if let Some(trait_) = &self.trait_ {
cg.add_indentedln(&format!(
"impl{} {} for {} {{",
self.generics.get_rust(),
trait_.get_rust(),
self.target.get_rust()
));
} else {
cg.add_indentedln(&format!(
"impl{} {} {{",
self.generics.get_rust(),
self.target.get_rust()
));
}
cg.indent += 1;
for method in &self.methods {
method.to_rust(cg);
}
cg.indent -= 1;
cg.add_indentedln("}");
}
}
impl GetRust for VarDecl { impl GetRust for VarDecl {
fn get_rust(&self) -> String { fn get_rust(&self) -> String {
let ty = self let ty = self