Dumper - Support static methods

This commit is contained in:
Emmanuel BENOîT 2023-01-12 08:09:23 +01:00
parent 52c4b6315e
commit 0a31442d56

View file

@ -1,7 +1,7 @@
use std::fmt::Write; use std::fmt::Write;
use crate::{ use crate::{
ast::{BinaryExpr, ExprNode, ProgramNode, StmtNode}, ast::{BinaryExpr, ClassMemberDecl, ExprNode, FunDecl, ProgramNode, StmtNode},
tokens::Token, tokens::Token,
}; };
@ -45,6 +45,19 @@ fn dump_substatement(dumper: &mut Dumper, statement: &StmtNode) {
dumper.depth -= depth_change; 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) { fn dump_statement(dumper: &mut Dumper, stmt: &StmtNode) {
match stmt { match stmt {
StmtNode::VarDecl(name, Some(expr)) => { StmtNode::VarDecl(name, Some(expr)) => {
@ -75,18 +88,13 @@ fn dump_statement(dumper: &mut Dumper, stmt: &StmtNode) {
StmtNode::ClassDecl(decl) => { StmtNode::ClassDecl(decl) => {
dumper.add_line(format!("class {} {{", decl.name.lexeme)); dumper.add_line(format!("class {} {{", decl.name.lexeme));
if !decl.methods.is_empty() { if !decl.members.is_empty() {
dumper.depth += 1; dumper.depth += 1;
for method in decl.methods.iter() { for member in decl.members.iter() {
dumper.add_line(format!( match &member {
"{} ({}) {{", ClassMemberDecl::Method(method) => dump_method(dumper, method, false),
method.name.lexeme, ClassMemberDecl::StaticMethod(method) => dump_method(dumper, method, true),
fun_decl_params(&method.params) };
));
dumper.depth += 1;
dump_statement_list(dumper, &method.body);
dumper.depth -= 1;
dumper.add_line("}".to_owned());
} }
dumper.depth -= 1; dumper.depth -= 1;
dumper.add_line(String::default()); dumper.add_line(String::default());