From 5be6ecb44e5d0f91394d6cdecf481f02975f017b Mon Sep 17 00:00:00 2001 From: Selimaj Dev Date: Tue, 5 May 2026 21:54:43 +0200 Subject: [PATCH] Deduped methods --- parser/src/ast.rs | 18 +++++++++--------- parser/src/lib.rs | 38 ++++---------------------------------- src/codegen.rs | 39 +++++---------------------------------- 3 files changed, 18 insertions(+), 77 deletions(-) diff --git a/parser/src/ast.rs b/parser/src/ast.rs index 12d7b36..7e4dc1b 100644 --- a/parser/src/ast.rs +++ b/parser/src/ast.rs @@ -15,6 +15,12 @@ pub enum TypePostfix { RefMut, } +#[derive(Debug, Clone, Serialize)] +pub enum Visibility { + Public, + Private, +} + #[derive(Debug, Clone, Serialize)] pub enum Attribute { /// #[test] @@ -67,19 +73,13 @@ pub enum TopLevelKind { name: String, fields: FieldList, }, - FunctionDecl { - export: bool, - name: String, - params: ParamList, - return_type: TypeExpr, - body: Block, - }, + FunctionDecl(FunctionDecl), ClassDecl { export: bool, name: String, fields: Vec, constructor: ClassConstructor, - methods: Vec, + methods: Vec, }, } @@ -91,7 +91,7 @@ pub struct ClassConstructor { } #[derive(Debug, Clone, Serialize)] -pub struct ClassMethod { +pub struct FunctionDecl { pub export: bool, pub name: String, pub params: ParamList, diff --git a/parser/src/lib.rs b/parser/src/lib.rs index 0a669e8..b39ea1d 100644 --- a/parser/src/lib.rs +++ b/parser/src/lib.rs @@ -242,42 +242,12 @@ impl From> for ClassConstructor { impl From> for TopLevelKind { fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { let rule = pair.as_rule(); - let mut inner = pair.into_inner(); + let mut inner = pair.clone().into_inner(); match rule { Rule::import => TopLevelKind::Include(Path::from(inner.next().unwrap())), - Rule::function_decl => { - let export = if let Some(first) = inner.peek() { - if first.as_rule() == Rule::export { - inner.next(); - true - } else { - false - } - } else { - false - }; - - let return_type = TypeExpr::from(inner.next().unwrap()); - - let name = inner.next().unwrap().as_str().to_string(); - let params = if inner.peek().unwrap().as_rule() == Rule::param_list { - ParamList::from(inner.next().unwrap()) - } else { - ParamList(Vec::new()) - }; - - let body = Block::from(inner.next().unwrap()); - - TopLevelKind::FunctionDecl { - export, - name, - params, - return_type, - body, - } - } + Rule::function_decl => TopLevelKind::FunctionDecl(FunctionDecl::from(pair)), Rule::struct_decl => { let export = if let Some(first) = inner.peek() { @@ -320,7 +290,7 @@ impl From> for TopLevelKind { .map(VarDeclStmt::from) .collect(), constructor: ClassConstructor::from(inner.next().unwrap()), - methods: inner.into_iter().map(ClassMethod::from).collect(), + methods: inner.into_iter().map(FunctionDecl::from).collect(), }, _ => unimplemented!("{rule:#?}"), @@ -596,7 +566,7 @@ impl From> for VarDecl { } } -impl From> for ClassMethod { +impl From> for FunctionDecl { fn from(pair: pest::iterators::Pair<'_, Rule>) -> Self { let mut inner = pair.into_inner(); diff --git a/src/codegen.rs b/src/codegen.rs index 1525f28..cbbd3a8 100644 --- a/src/codegen.rs +++ b/src/codegen.rs @@ -1,7 +1,7 @@ use parser::ast::{ - Attribute, BinaryOp, Block, ClassMethod, Expression, Literal, Path, Postfix, Prefix, Statement, - StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, VarAssignStmt, - VarDecl, VarDeclStmt, + Attribute, BinaryOp, Block, Expression, FunctionDecl, Literal, Path, Postfix, Prefix, + Statement, StatementBranch, TopLevel, TopLevelKind, TypeExpr, TypeExprKind, TypePostfix, + VarAssignStmt, VarDecl, VarDeclStmt, }; // --------------------------------------------------------------------------- @@ -310,7 +310,7 @@ impl ToRust for TopLevelKind { Self::Include(path) => { cg.addln(&format!("use {};", path.get_rust())); } - + Self::FunctionDecl(decl) => decl.to_rust(cg), Self::StructDecl { export, name, @@ -328,35 +328,6 @@ impl ToRust for TopLevelKind { cg.indent -= 1; cg.addln("}\n"); } - - Self::FunctionDecl { - export, - name, - params, - return_type, - body, - } => { - let vis = if *export { "pub " } else { "" }; - - let params_str = params - .0 - .iter() - .map(VarDecl::get_rust) - .collect::>() - .join(", "); - - cg.addln(&format!( - "{}fn {}({}) -> {} {{", - vis, - name, - params_str, - return_type.get_rust() - )); - cg.indent += 1; - body.to_rust(cg); - cg.indent -= 1; - cg.addln("}\n"); - } Self::ClassDecl { export, name, @@ -547,7 +518,7 @@ impl ToRust for Statement { } } -impl ToRust for ClassMethod { +impl ToRust for FunctionDecl { fn to_rust(&self, cg: &mut RustCodegen) { let vis = if self.export { "pub " } else { "" };