From 0a31442d56b67355fe3bfba1b851193c12f78ace Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emmanuel=20Beno=C3=AEt?= Date: Thu, 12 Jan 2023 08:09:23 +0100 Subject: [PATCH] Dumper - Support static methods --- src/dumper.rs | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/dumper.rs b/src/dumper.rs index f7431fc..b6b0812 100644 --- a/src/dumper.rs +++ b/src/dumper.rs @@ -1,7 +1,7 @@ use std::fmt::Write; use crate::{ - ast::{BinaryExpr, ExprNode, ProgramNode, StmtNode}, + ast::{BinaryExpr, ClassMemberDecl, ExprNode, FunDecl, ProgramNode, StmtNode}, tokens::Token, }; @@ -45,6 +45,19 @@ fn dump_substatement(dumper: &mut Dumper, statement: &StmtNode) { dumper.depth -= depth_change; } +fn dump_method(dumper: &mut Dumper, method: &FunDecl, is_static: bool) { + dumper.add_line(format!( + "{}{} ({}) {{", + if is_static { "static " } else { "" }, + method.name.lexeme, + fun_decl_params(&method.params) + )); + dumper.depth += 1; + dump_statement_list(dumper, &method.body); + dumper.depth -= 1; + dumper.add_line("}".to_owned()); +} + fn dump_statement(dumper: &mut Dumper, stmt: &StmtNode) { match stmt { StmtNode::VarDecl(name, Some(expr)) => { @@ -75,18 +88,13 @@ fn dump_statement(dumper: &mut Dumper, stmt: &StmtNode) { StmtNode::ClassDecl(decl) => { dumper.add_line(format!("class {} {{", decl.name.lexeme)); - if !decl.methods.is_empty() { + if !decl.members.is_empty() { dumper.depth += 1; - for method in decl.methods.iter() { - dumper.add_line(format!( - "{} ({}) {{", - method.name.lexeme, - fun_decl_params(&method.params) - )); - dumper.depth += 1; - dump_statement_list(dumper, &method.body); - dumper.depth -= 1; - dumper.add_line("}".to_owned()); + for member in decl.members.iter() { + match &member { + ClassMemberDecl::Method(method) => dump_method(dumper, method, false), + ClassMemberDecl::StaticMethod(method) => dump_method(dumper, method, true), + }; } dumper.depth -= 1; dumper.add_line(String::default());