From 28d4d784f23f5481b665cf182a0d87b5187e9588 Mon Sep 17 00:00:00 2001 From: Klesti Selimaj Date: Sun, 10 May 2026 17:35:33 +0200 Subject: [PATCH] Class impl --- mist-test/src/logger.mist | 6 ++++++ parser/src/ast.rs | 8 +++++++- parser/src/lib.rs | 16 +++++++++++++++- src/codegen.rs | 32 +++++++++++++++++++++++++------- 4 files changed, 53 insertions(+), 9 deletions(-) diff --git a/mist-test/src/logger.mist b/mist-test/src/logger.mist index 65dbe78..f6b55c5 100644 --- a/mist-test/src/logger.mist +++ b/mist-test/src/logger.mist @@ -58,4 +58,10 @@ public class Logger { } } } + + impl fmt::Display { + std::fmt::Result fmt(self*, std::fmt::Formatter<'_> mut* f) { + return write!(f, "logger ({})", self.prefix); + } + } } diff --git a/parser/src/ast.rs b/parser/src/ast.rs index abcce46..5f78fd4 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -94,10 +94,16 @@ pub enum TopLevelKind { generics: Generics, fields: Vec, constructor: ClassConstructor, - items: Vec, + items: Vec, }, } +#[derive(Debug, Clone, Serialize)] +pub enum ClassItem { + Method(FunctionDecl), + ImplDecl(ImplDecl), +} + #[derive(Debug, Clone, Serialize, Default)] pub struct Generics(pub Vec); diff --git a/parser/src/lib.rs b/parser/src/lib.rs index c26d368..d3861e2 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -289,7 +289,7 @@ impl From> for TopLevelKind { .map(FieldDeclStmt::from) .collect(), constructor: ClassConstructor::from(inner.next().unwrap()), - items: inner.into_iter().map(FunctionDecl::from).collect(), + items: inner.into_iter().map(ClassItem::from).collect(), }, Rule::enum_decl => TopLevelKind::EnumDecl { @@ -310,6 +310,20 @@ impl From> for TopLevelKind { } } +impl From> for ClassItem { + fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { + let rule = pair.as_rule(); + + match rule { + Rule::impl_decl | Rule::impl_for_decl => ClassItem::ImplDecl(ImplDecl::from(pair)), + + Rule::method => ClassItem::Method(FunctionDecl::from(pair)), + + _ => unimplemented!("{rule:#?}"), + } + } +} + impl From> for ImplDecl { fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { let rule = pair.as_rule(); diff --git a/src/codegen.rs b/src/codegen.rs index 304a904..3df7404 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -1,8 +1,8 @@ use parser::ast::{ - Attribute, BinaryOp, Block, EnumItem, Expression, FieldDecl, FunctionDecl, Generic, Generics, - Identifier, ImplDecl, Literal, Path, Pattern, Postfix, Prefix, Statement, StatementBranch, - TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, VarDecl, - VarDeclStmt, Visibility, + Attribute, BinaryOp, Block, ClassItem, EnumItem, Expression, FieldDecl, FunctionDecl, Generic, + Generics, Identifier, ImplDecl, Literal, Path, Pattern, Postfix, Prefix, Statement, + StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, + VarDecl, VarDeclStmt, Visibility, }; // --------------------------------------------------------------------------- @@ -364,7 +364,7 @@ impl ToRust for TopLevelKind { generics, fields, constructor, - items: methods, + items, } => { // Struct decl cg.addln(&format!( @@ -458,12 +458,30 @@ impl ToRust for TopLevelKind { cg.indent -= 1; cg.add_indentedln("}\n"); - for method in methods { - method.to_rust(cg); + for item in items { + match item { + ClassItem::ImplDecl(_) => {} + ClassItem::Method(method) => method.to_rust(cg), + } } cg.indent -= 1; cg.addln("}\n"); + + for item in items { + match item { + ClassItem::ImplDecl(impl_) => { + let mut impl_ = impl_.clone(); + + impl_.trait_ = Some(impl_.target); + impl_.target = + TypeExpr(TypeExprKind::Path(Path(vec![name.clone()])), Vec::new()); + + impl_.to_rust(cg); + } + ClassItem::Method(_) => {} + } + } } } }