AST - Refactored class member data structures
This commit is contained in:
parent
f2637fb319
commit
c3bdc23081
2 changed files with 23 additions and 55 deletions
20
src/ast.rs
20
src/ast.rs
|
@ -16,22 +16,20 @@ pub struct FunDecl {
|
||||||
pub body: Vec<StmtNode>,
|
pub body: Vec<StmtNode>,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A getter method for a property
|
/// The declaration of a class member.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct PropertyAccessor {
|
pub enum ClassMemberKind {
|
||||||
pub name: Token,
|
Method,
|
||||||
pub body: Vec<StmtNode>,
|
Getter,
|
||||||
|
Setter,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// The declaration of a class member.
|
/// The declaration of a class member.
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum ClassMemberDecl {
|
pub struct ClassMemberDecl {
|
||||||
Method(FunDecl),
|
pub kind: ClassMemberKind,
|
||||||
StaticMethod(FunDecl),
|
pub is_static: bool,
|
||||||
PropertyGetter(PropertyAccessor),
|
pub fun_decl: FunDecl,
|
||||||
StaticPropertyGetter(PropertyAccessor),
|
|
||||||
PropertySetter(PropertyAccessor),
|
|
||||||
StaticPropertySetter(PropertyAccessor),
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A class declaration.
|
/// A class declaration.
|
||||||
|
|
|
@ -1,11 +1,6 @@
|
||||||
use std::fmt::Write;
|
use std::fmt::Write;
|
||||||
|
|
||||||
use crate::{
|
use crate::{ast::{BinaryExpr, ClassMemberDecl, ClassMemberKind, ExprNode, FunDecl, ProgramNode, PropertyAccessor, StmtNode}, tokens::Token};
|
||||||
ast::{
|
|
||||||
BinaryExpr, ClassMemberDecl, ExprNode, FunDecl, ProgramNode, PropertyAccessor, StmtNode,
|
|
||||||
},
|
|
||||||
tokens::Token,
|
|
||||||
};
|
|
||||||
|
|
||||||
/* -------------------------------- *
|
/* -------------------------------- *
|
||||||
* Dumper trait and implementations *
|
* Dumper trait and implementations *
|
||||||
|
@ -47,33 +42,23 @@ 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) {
|
fn member_header(member: &ClassMemberDecl) -> String {
|
||||||
dumper.add_line(format!(
|
match member.kind {
|
||||||
"{}{} ({}) {{",
|
ClassMemberKind::Method => format!("({})", fun_decl_params(&member.fun_decl.params)),
|
||||||
if is_static { "static " } else { "" },
|
ClassMemberKind::Getter => ">".to_owned(),
|
||||||
method.name.lexeme,
|
ClassMemberKind::Setter => "<".to_owned(),
|
||||||
fun_decl_params(&method.params)
|
}
|
||||||
));
|
|
||||||
dumper.depth += 1;
|
|
||||||
dump_statement_list(dumper, &method.body);
|
|
||||||
dumper.depth -= 1;
|
|
||||||
dumper.add_line("}".to_owned());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn dump_accessor(
|
fn dump_member(dumper: &mut Dumper, member: &ClassMemberDecl) {
|
||||||
dumper: &mut Dumper,
|
|
||||||
accessor: &PropertyAccessor,
|
|
||||||
is_setter: bool,
|
|
||||||
is_static: bool,
|
|
||||||
) {
|
|
||||||
dumper.add_line(format!(
|
dumper.add_line(format!(
|
||||||
"{}{} {} {{",
|
"{}{} ({}) {{",
|
||||||
if is_static { "static " } else { "" },
|
if member.is_static { "static " } else { "" },
|
||||||
accessor.name.lexeme,
|
member.fun_decl.name.lexeme,
|
||||||
if is_setter { "<" } else { ">" },
|
member_header(member),
|
||||||
));
|
));
|
||||||
dumper.depth += 1;
|
dumper.depth += 1;
|
||||||
dump_statement_list(dumper, &accessor.body);
|
dump_statement_list(dumper, &member.fun_decl.body);
|
||||||
dumper.depth -= 1;
|
dumper.depth -= 1;
|
||||||
dumper.add_line("}".to_owned());
|
dumper.add_line("}".to_owned());
|
||||||
}
|
}
|
||||||
|
@ -111,22 +96,7 @@ fn dump_statement(dumper: &mut Dumper, stmt: &StmtNode) {
|
||||||
if !decl.members.is_empty() {
|
if !decl.members.is_empty() {
|
||||||
dumper.depth += 1;
|
dumper.depth += 1;
|
||||||
for member in decl.members.iter() {
|
for member in decl.members.iter() {
|
||||||
match &member {
|
dump_member(dumper, &member);
|
||||||
ClassMemberDecl::Method(method) => dump_method(dumper, method, false),
|
|
||||||
ClassMemberDecl::StaticMethod(method) => dump_method(dumper, method, true),
|
|
||||||
ClassMemberDecl::PropertyGetter(accessor) => {
|
|
||||||
dump_accessor(dumper, accessor, false, false)
|
|
||||||
}
|
|
||||||
ClassMemberDecl::PropertySetter(accessor) => {
|
|
||||||
dump_accessor(dumper, accessor, true, false)
|
|
||||||
}
|
|
||||||
ClassMemberDecl::StaticPropertyGetter(accessor) => {
|
|
||||||
dump_accessor(dumper, accessor, false, true)
|
|
||||||
}
|
|
||||||
ClassMemberDecl::StaticPropertySetter(accessor) => {
|
|
||||||
dump_accessor(dumper, accessor, true, true)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
dumper.depth -= 1;
|
dumper.depth -= 1;
|
||||||
dumper.add_line(String::default());
|
dumper.add_line(String::default());
|
||||||
|
|
Loading…
Reference in a new issue