From 22deb90c3e56d1a4c36316932b5afa316602b8ac Mon Sep 17 00:00:00 2001 From: Jesse Brault Date: Mon, 26 May 2025 08:30:15 -0500 Subject: [PATCH] Move ast nodes to new ast/node module. --- src/ast/build.rs | 39 +- src/ast/children.rs | 30 +- src/ast/mod.rs | 2374 +----------------------------- src/ast/named.rs | 88 -- src/ast/node/call_expression.rs | 106 ++ src/ast/node/class.rs | 184 +++ src/ast/node/closure.rs | 204 +++ src/ast/node/compilation_unit.rs | 63 + src/ast/node/d_string.rs | 55 + src/ast/node/expression.rs | 155 ++ src/ast/node/function.rs | 543 +++++++ src/ast/node/generics.rs | 56 + src/ast/node/implements_list.rs | 28 + src/ast/node/interface.rs | 67 + src/ast/node/level.rs | 37 + src/ast/node/literal.rs | 13 + src/ast/node/mod.rs | 21 + src/ast/node/module.rs | 43 + src/ast/node/named.rs | 16 + src/ast/node/names.rs | 146 ++ src/ast/node/object_access.rs | 56 + src/ast/node/operators.rs | 44 + src/ast/node/statement.rs | 384 +++++ src/ast/node/tuple_arguments.rs | 22 + src/ast/node/type_use.rs | 167 +++ src/ast/node/use_statement.rs | 87 ++ src/ast/pretty_print.rs | 328 +++-- src/ast/unparse.rs | 182 ++- src/name_analysis/gather.rs | 33 +- src/name_analysis/mod.rs | 14 +- src/name_analysis/resolve.rs | 61 +- src/name_analysis/symbol.rs | 11 +- 32 files changed, 2915 insertions(+), 2742 deletions(-) delete mode 100644 src/ast/named.rs create mode 100644 src/ast/node/call_expression.rs create mode 100644 src/ast/node/class.rs create mode 100644 src/ast/node/closure.rs create mode 100644 src/ast/node/compilation_unit.rs create mode 100644 src/ast/node/d_string.rs create mode 100644 src/ast/node/expression.rs create mode 100644 src/ast/node/function.rs create mode 100644 src/ast/node/generics.rs create mode 100644 src/ast/node/implements_list.rs create mode 100644 src/ast/node/interface.rs create mode 100644 src/ast/node/level.rs create mode 100644 src/ast/node/literal.rs create mode 100644 src/ast/node/mod.rs create mode 100644 src/ast/node/module.rs create mode 100644 src/ast/node/named.rs create mode 100644 src/ast/node/names.rs create mode 100644 src/ast/node/object_access.rs create mode 100644 src/ast/node/operators.rs create mode 100644 src/ast/node/statement.rs create mode 100644 src/ast/node/tuple_arguments.rs create mode 100644 src/ast/node/type_use.rs create mode 100644 src/ast/node/use_statement.rs diff --git a/src/ast/build.rs b/src/ast/build.rs index a037a95..1f1dea4 100644 --- a/src/ast/build.rs +++ b/src/ast/build.rs @@ -1,6 +1,26 @@ -use crate::ast::*; +use crate::ast::node::call_expression::*; +use crate::ast::node::class::*; +use crate::ast::node::closure::*; +use crate::ast::node::compilation_unit::*; +use crate::ast::node::d_string::*; +use crate::ast::node::expression::*; +use crate::ast::node::function::*; +use crate::ast::node::generics::*; +use crate::ast::node::implements_list::*; +use crate::ast::node::interface::*; +use crate::ast::node::level::*; +use crate::ast::node::literal::*; +use crate::ast::node::module::*; +use crate::ast::node::names::*; +use crate::ast::node::object_access::*; +use crate::ast::node::operators::*; +use crate::ast::node::statement::*; +use crate::ast::node::tuple_arguments::*; +use crate::ast::node::type_use::*; +use crate::ast::node::use_statement::*; use crate::parser::Rule; use pest::iterators::{Pair, Pairs}; +use std::range::Range; use std::str::FromStr; fn expect_and_use( @@ -897,7 +917,7 @@ fn build_interface_operator_function_declaration( } fn build_class_constructor(file_id: usize, class_constructor_pair: Pair) -> ClassConstructor { - ClassConstructor( + ClassConstructor::new( class_constructor_pair .into_inner() .map(|data_member_pair| { @@ -1188,7 +1208,7 @@ fn build_if_statement(file_id: usize, if_statement_pair: Pair) -> IfStatem fn build_if_else_statement(file_id: usize, if_else_statement_pair: Pair) -> IfElseStatement { let mut if_statement = None; - let mut else_ifs = ElseIfs::default(); + let mut else_ifs = vec![]; let mut else_block = None; for inner_pair in if_else_statement_pair.into_inner() { @@ -1199,7 +1219,7 @@ fn build_if_else_statement(file_id: usize, if_else_statement_pair: Pair) - Rule::ElseIf => { let mut else_if_inner = inner_pair.into_inner(); else_if_inner.next().unwrap(); // else - else_ifs.0.push(Box::new(expect_and_use( + else_ifs.push(Box::new(expect_and_use( file_id, else_if_inner.next().unwrap(), Rule::IfStatement, @@ -1220,7 +1240,11 @@ fn build_if_else_statement(file_id: usize, if_else_statement_pair: Pair) - } } - IfElseStatement::new(if_statement.unwrap(), Box::new(else_ifs), else_block) + IfElseStatement::new( + if_statement.unwrap(), + Box::new(ElseIfs::new(else_ifs)), + else_block, + ) } fn build_while_statement(file_id: usize, while_statement_pair: Pair) -> WhileStatement { @@ -1791,7 +1815,7 @@ fn build_double_quote_string(file_id: usize, pair: Pair) -> Literal { if parts.len() == 1 && parts[0].is_string() { Literal::String(parts.pop().unwrap().unwrap_string()) } else { - Literal::DString(Box::new(DString(parts))) + Literal::DString(Box::new(DString::new(parts))) } } @@ -1801,7 +1825,7 @@ fn build_backtick_string(file_id: usize, pair: Pair) -> Literal { if parts.len() == 1 && parts[0].is_string() { Literal::String(parts.pop().unwrap().unwrap_string()) } else { - Literal::BacktickString(Box::new(DString(parts))) + Literal::BacktickString(Box::new(DString::new(parts))) } } @@ -1837,6 +1861,7 @@ mod tests { use crate::ast::pretty_print::PrettyPrint; use crate::parser::DeimosParser; use indoc::indoc; + use pest::Parser; fn assert_builds(src: &str) { let parse_result = DeimosParser::parse(Rule::CompilationUnit, src); diff --git a/src/ast/children.rs b/src/ast/children.rs index cb815d1..6a7117f 100644 --- a/src/ast/children.rs +++ b/src/ast/children.rs @@ -1,4 +1,22 @@ -use crate::ast::*; +use crate::ast::node::call_expression::*; +use crate::ast::node::class::*; +use crate::ast::node::closure::*; +use crate::ast::node::compilation_unit::*; +use crate::ast::node::d_string::*; +use crate::ast::node::expression::*; +use crate::ast::node::function::*; +use crate::ast::node::generics::*; +use crate::ast::node::implements_list::*; +use crate::ast::node::interface::*; +use crate::ast::node::level::*; +use crate::ast::node::literal::*; +use crate::ast::node::module::*; +use crate::ast::node::names::*; +use crate::ast::node::object_access::*; +use crate::ast::node::statement::*; +use crate::ast::node::tuple_arguments::*; +use crate::ast::node::type_use::*; +use crate::ast::node::use_statement::*; pub enum ChildRef<'a> { Identifier(&'a Identifier), @@ -216,7 +234,7 @@ impl HasChildren for UseStatement { impl HasChildren for ModuleLevelDeclaration { fn children(&self) -> Vec { - use ModuleLevelDeclaration::*; + use crate::ast::node::level::ModuleLevelDeclaration::*; match self { Module(module_declaration) => module_declaration.children(), Interface(interface_declaration) => interface_declaration.children(), @@ -231,7 +249,7 @@ impl HasChildren for ModuleLevelDeclaration { impl HasChildren for InterfaceLevelDeclaration { fn children(&self) -> Vec { - use InterfaceLevelDeclaration::*; + use crate::ast::node::level::InterfaceLevelDeclaration::*; match self { Module(module_declaration) => module_declaration.children(), Interface(interface_declaration) => interface_declaration.children(), @@ -246,7 +264,7 @@ impl HasChildren for InterfaceLevelDeclaration { impl HasChildren for ClassLevelDeclaration { fn children(&self) -> Vec { - use ClassLevelDeclaration::*; + use crate::ast::node::level::ClassLevelDeclaration::*; match self { Module(module_declaration) => module_declaration.children(), Interface(interface_declaration) => interface_declaration.children(), @@ -436,7 +454,7 @@ impl HasChildren for BlockStatement { impl HasChildren for Statement { fn children(&self) -> Vec { - use Statement::*; + use crate::ast::node::statement::Statement::*; match self { BlockStatement(block_statement) => block_statement.children(), VariableDeclarationStatement(variable_declaration_statement) => { @@ -549,7 +567,7 @@ impl HasChildren for ForStatement { impl HasChildren for Expression { fn children(&self) -> Vec { - use Expression::*; + use crate::ast::node::expression::Expression::*; match self { Ternary(ternary) => ternary.children(), Binary(binary) => binary.children(), diff --git a/src/ast/mod.rs b/src/ast/mod.rs index d1ba881..3303ace 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -1,2377 +1,5 @@ -use crate::ast::named::Named; -use crate::name_analysis::symbol::Symbol; -use pest::Parser; -use std::borrow::Cow; -use std::range::Range; - pub mod build; pub mod children; -pub mod named; +pub mod node; pub mod pretty_print; pub mod unparse; - -/* Operators */ - -#[derive(Debug)] -pub enum Operator { - Binary(BinaryOperator), - PrefixUnary(PrefixUnaryOperator), - SuffixUnary(SuffixUnaryOperator), -} - -#[derive(Debug)] -pub enum BinaryOperator { - Or, - And, - EqualTo, - NotEqualTo, - Greater, - Less, - GreaterEqual, - LessEqual, - Add, - Subtract, - Multiply, - Divide, - Modulo, - LeftShift, - RightShift, -} - -#[derive(Debug)] -pub enum PrefixUnaryOperator { - Spread, - BorrowMut, - Borrow, - Star, - Mut, - Not, - Negative, -} - -#[derive(Debug)] -pub enum SuffixUnaryOperator { - PlusPlus, - MinusMinus, - Call, - Index, -} - -/* Names */ - -#[derive(Debug)] -pub struct Identifier { - name: String, - file_id: usize, - range: Range, - scope_id: Option, - saved_symbol: Option, -} - -impl Identifier { - pub fn new(name: &str, file_id: usize, range: Range) -> Self { - Self { - name: name.to_string(), - file_id, - range, - scope_id: None, - saved_symbol: None, - } - } -} - -#[derive(Debug)] -pub struct FullyQualifiedName { - identifiers: Vec>, - file_id: usize, - range: Range, - scope_id: Option, - saved_symbol: Option, -} - -impl FullyQualifiedName { - pub fn new(identifiers: Vec>, file_id: usize, range: Range) -> Self { - Self { - identifiers, - range, - file_id, - scope_id: None, - saved_symbol: None, - } - } - - pub fn identifiers(&self) -> Vec<&Identifier> { - self.identifiers.iter().map(Box::as_ref).collect() - } - - pub fn identifiers_mut(&mut self) -> Vec<&mut Identifier> { - self.identifiers.iter_mut().map(Box::as_mut).collect() - } - - #[deprecated(note = "Grammar will instead distinguish between fqns and identifiers")] - pub fn last(&self) -> &Identifier { - &self.identifiers[self.identifiers.len() - 1] - } - - #[deprecated(note = "Grammar will instead distinguish between fqns and identifiers")] - pub fn last_mut(&mut self) -> &mut Identifier { - let last_index = self.identifiers.len() - 1; - &mut self.identifiers[last_index] - } - - #[deprecated(note = "Grammar will instead distinguish between fqns and identifiers")] - pub fn len(&self) -> usize { - self.identifiers.len() - } - - #[deprecated(note = "Grammar will instead distinguish between fqns and identifiers")] - pub fn is_single_identifier(&self) -> bool { - self.identifiers.len() == 1 - } -} - -/* Type Use */ - -#[derive(Debug)] -pub enum TypeUse { - Primitive(Box), - InterfaceOrClass(Box), - Tuple(Box), - Function(Box), -} - -#[derive(Debug)] -pub enum PrimitiveTypeUse { - Byte, - Short, - Char, - Int, - Long, - Double, - Bool, - String, - Array(Option>), - Any, - Void, -} - -#[derive(Debug)] -pub struct InterfaceOrClassTypeUse { - borrow_count: usize, - is_mutable: bool, - fqn: Box, - generics: Box, -} - -impl InterfaceOrClassTypeUse { - pub fn new( - borrow_count: usize, - is_mutable: bool, - fqn: Box, - generics: Box, - ) -> Self { - Self { - borrow_count, - is_mutable, - fqn, - generics, - } - } - - pub fn borrow_count(&self) -> usize { - self.borrow_count - } - - pub fn is_mutable(&self) -> bool { - self.is_mutable - } - - pub fn fqn(&self) -> &FullyQualifiedName { - &self.fqn - } - - pub fn fqn_mut(&mut self) -> &mut FullyQualifiedName { - &mut self.fqn - } - - pub fn generics(&self) -> &GenericArguments { - &self.generics - } - - pub fn generics_mut(&mut self) -> &mut GenericArguments { - &mut self.generics - } -} - -#[derive(Debug)] -pub struct TupleTypeUse { - borrow_count: usize, - is_mutable: bool, - arguments: Box, -} - -impl TupleTypeUse { - pub fn new(borrow_count: usize, is_mutable: bool, arguments: Box) -> Self { - Self { - borrow_count, - is_mutable, - arguments, - } - } - - pub fn borrow_count(&self) -> usize { - self.borrow_count - } - - pub fn is_mutable(&self) -> bool { - self.is_mutable - } - - pub fn arguments(&self) -> &TupleArguments { - &self.arguments - } - - pub fn arguments_mut(&mut self) -> &mut TupleArguments { - &mut self.arguments - } -} - -#[derive(Debug)] -pub struct FunctionTypeUse { - borrow_count: usize, - function_modifier: Option, - generics: Box, - parameters: Box, - return_type: Box, -} - -impl FunctionTypeUse { - pub fn new( - borrow_count: usize, - function_modifier: Option, - generics: Box, - parameters: Box, - return_type: Box, - ) -> Self { - Self { - borrow_count, - function_modifier, - generics, - parameters, - return_type, - } - } - - pub fn borrow_count(&self) -> usize { - self.borrow_count - } - - pub fn function_modifier(&self) -> Option<&FunctionTypeModifier> { - self.function_modifier.as_ref() - } - - pub fn generics(&self) -> &GenericParameters { - &self.generics - } - - pub fn generics_mut(&mut self) -> &mut GenericParameters { - &mut self.generics - } - - pub fn parameters(&self) -> &Box { - &self.parameters - } - - pub fn parameters_mut(&mut self) -> &mut Box { - &mut self.parameters - } - - pub fn return_type(&self) -> &ReturnType { - &self.return_type - } - - pub fn return_type_mut(&mut self) -> &mut ReturnType { - &mut self.return_type - } -} - -/* Generic arguments */ - -#[derive(Debug)] -pub struct GenericArguments(Vec>); - -impl GenericArguments { - pub fn new(arguments: Vec>) -> Self { - Self(arguments) - } - - pub fn is_empty(&self) -> bool { - self.0.is_empty() - } - - pub fn arguments(&self) -> Vec<&TypeUse> { - self.0.iter().map(Box::as_ref).collect() - } - - pub fn arguments_mut(&mut self) -> Vec<&mut TypeUse> { - self.0.iter_mut().map(Box::as_mut).collect() - } -} - -impl Default for GenericArguments { - fn default() -> Self { - GenericArguments(Vec::new()) - } -} - -/* Generic parameters */ - -#[derive(Debug)] -pub struct GenericParameters(Vec>); - -impl GenericParameters { - pub fn new(identifiers: Vec>) -> Self { - Self(identifiers) - } - - pub fn is_empty(&self) -> bool { - self.0.is_empty() - } - - pub fn identifiers(&self) -> Vec<&Identifier> { - self.0.iter().map(Box::as_ref).collect() - } - - pub fn identifiers_mut(&mut self) -> Vec<&mut Identifier> { - self.0.iter_mut().map(Box::as_mut).collect() - } -} - -impl Default for GenericParameters { - fn default() -> Self { - GenericParameters(Vec::new()) - } -} - -/* Tuple Arguments */ - -#[derive(Debug)] -pub struct TupleArguments(Vec>); - -impl TupleArguments { - pub fn new(arguments: Vec>) -> Self { - Self(arguments) - } - - pub fn is_empty(&self) -> bool { - self.0.is_empty() - } - - pub fn type_uses(&self) -> Vec<&TypeUse> { - self.0.iter().map(Box::as_ref).collect() - } - - pub fn type_uses_mut(&mut self) -> Vec<&mut TypeUse> { - self.0.iter_mut().map(Box::as_mut).collect() - } -} - -/* Implements List */ - -#[derive(Debug)] -pub struct ImplementsList(Vec>); - -impl ImplementsList { - pub fn new(type_uses: Vec>) -> Self { - Self(type_uses) - } - - pub fn is_empty(&self) -> bool { - self.0.is_empty() - } - - pub fn type_uses(&self) -> Vec<&TypeUse> { - self.0.iter().map(Box::as_ref).collect() - } - - pub fn type_uses_mut(&mut self) -> Vec<&mut TypeUse> { - self.0.iter_mut().map(Box::as_mut).collect() - } -} - -impl Default for ImplementsList { - fn default() -> Self { - ImplementsList(Vec::new()) - } -} - -/* Function Type Modifier */ - -#[derive(Debug)] -pub enum FunctionTypeModifier { - Cons, - MutRef, - Mut, - Ref, -} - -/* Function Parameters */ - -#[derive(Debug)] -pub struct Parameters(Vec>); - -impl Parameters { - pub fn new(parameters: Vec>) -> Self { - Self(parameters) - } - - pub fn is_empty(&self) -> bool { - self.0.is_empty() - } - - pub fn parameters(&self) -> Vec<&Parameter> { - self.0.iter().map(Box::as_ref).collect() - } - - pub fn parameters_mut(&mut self) -> Vec<&mut Parameter> { - self.0.iter_mut().map(Box::as_mut).collect() - } -} - -impl Default for Parameters { - fn default() -> Self { - Parameters(Vec::new()) - } -} - -#[derive(Debug)] -pub struct Parameter { - identifier: Box, - type_use: Box, -} - -impl Parameter { - pub fn new(identifier: Box, type_use: Box) -> Self { - Self { - identifier, - type_use, - } - } - - pub fn identifier(&self) -> &Identifier { - &self.identifier - } - - pub fn identifier_mut(&mut self) -> &mut Identifier { - &mut self.identifier - } - - pub fn type_use(&self) -> &TypeUse { - &self.type_use - } - - pub fn type_use_mut(&mut self) -> &mut TypeUse { - &mut self.type_use - } -} - -/* Return Type */ - -#[derive(Debug)] -pub struct ReturnType { - declared_type: Box, - references: Box, -} - -impl ReturnType { - pub fn new(declared_type: Box, references: Box) -> Self { - Self { - declared_type, - references, - } - } - - pub fn void() -> Self { - ReturnType { - declared_type: Box::new(TypeUse::Primitive(Box::new(PrimitiveTypeUse::Void))), - references: Box::new(References::default()), - } - } - - pub fn declared_type(&self) -> &TypeUse { - &self.declared_type - } - - pub fn declared_type_mut(&mut self) -> &mut TypeUse { - &mut self.declared_type - } - - pub fn references(&self) -> &References { - &self.references - } - - pub fn references_mut(&mut self) -> &mut References { - &mut self.references - } -} - -/* References */ - -#[derive(Debug)] -pub struct References(Vec>); - -impl References { - pub fn new(identifiers: Vec>) -> Self { - Self(identifiers) - } - - pub fn is_empty(&self) -> bool { - self.0.is_empty() - } - - pub fn identifiers(&self) -> Vec<&Identifier> { - self.0.iter().map(Box::as_ref).collect() - } - - pub fn identifiers_mut(&mut self) -> Vec<&mut Identifier> { - self.0.iter_mut().map(Box::as_mut).collect() - } -} - -impl Default for References { - fn default() -> Self { - References(Vec::new()) - } -} - -/* Top-level construct */ - -#[derive(Debug)] -pub struct CompilationUnit { - file_name: String, - file_id: usize, - namespace: Option>, - use_statements: Vec>, - declarations: Vec>, -} - -impl CompilationUnit { - pub fn new( - file_name: String, - file_id: usize, - namespace: Option>, - use_statements: Vec>, - declarations: Vec>, - ) -> Self { - Self { - file_name, - file_id, - namespace, - use_statements, - declarations, - } - } - - pub fn file_name(&self) -> &str { - &self.file_name - } - - pub fn file_id(&self) -> usize { - self.file_id - } - - pub fn namespace(&self) -> Option<&FullyQualifiedName> { - // We have to use this non-map syntax because we need a reference like `&self.namespace` - if let Some(namespace) = &self.namespace { - Some(namespace.as_ref()) - } else { - None - } - } - - pub fn use_statements(&self) -> Vec<&UseStatement> { - self.use_statements.iter().map(Box::as_ref).collect() - } - - pub fn use_statements_mut(&mut self) -> Vec<&mut UseStatement> { - self.use_statements.iter_mut().map(Box::as_mut).collect() - } - - pub fn declarations(&self) -> Vec<&ModuleLevelDeclaration> { - self.declarations.iter().map(Box::as_ref).collect() - } - - pub fn declarations_mut(&mut self) -> Vec<&mut ModuleLevelDeclaration> { - self.declarations.iter_mut().map(Box::as_mut).collect() - } -} - -/* Use Statement */ - -#[derive(Debug)] -pub struct UseStatement { - identifiers: Vec>, - last: Box, - file_id: usize, - range: Range, -} - -impl UseStatement { - pub fn new( - identifiers: Vec>, - last: Box, - file_id: usize, - range: Range, - ) -> Self { - UseStatement { - identifiers, - last, - file_id, - range, - } - } - - pub fn base_name(&self) -> Cow<'_, str> { - use UseStatementLast::*; - if self.identifiers.is_empty() { - match self.last.as_ref() { - Identifier(_) => Cow::from(""), - Star | Identifiers(_) => panic!(), // should never get here because of grammar - } - } else if self.identifiers.len() == 1 { - self.identifiers[0].name() - } else { - let mut acc = String::new(); - for (i, identifier) in self.identifiers.iter().enumerate() { - acc.push_str(&identifier.name()); - if i != self.identifiers.len() - 1 { - acc.push_str("::"); - } - } - Cow::from(acc) - } - } - - pub fn is_star(&self) -> bool { - match self.last.as_ref() { - UseStatementLast::Star => true, - _ => false, - } - } - - pub fn identifiers(&self) -> Vec<&Identifier> { - self.identifiers.iter().map(Box::as_ref).collect() - } - - pub fn identifiers_mut(&mut self) -> Vec<&mut Identifier> { - self.identifiers.iter_mut().map(Box::as_mut).collect() - } - - pub fn last(&self) -> &UseStatementLast { - &self.last - } - - pub fn last_mut(&mut self) -> &mut UseStatementLast { - &mut self.last - } - - pub fn file_id(&self) -> usize { - self.file_id - } - - pub fn range(&self) -> Range { - self.range - } -} - -#[derive(Debug)] -pub enum UseStatementLast { - Identifier(Box), - Identifiers(Vec>), - Star, -} - -/* Declarations allowed in each level */ - -#[derive(Debug)] -pub enum ModuleLevelDeclaration { - Module(Box), - Interface(Box), - Class(Box), - Function(Box), - PlatformFunction(Box), -} - -#[derive(Debug)] -pub enum InterfaceLevelDeclaration { - Module(Box), - Interface(Box), - Class(Box), - Function(Box), - OperatorFunction(Box), -} - -#[derive(Debug)] -pub enum ClassLevelDeclaration { - Module(Box), - Interface(Box), - Class(Box), - Function(Box), - OperatorFunction(Box), - PlatformFunction(Box), - Property(Box), - Field(Box), -} - -/* Main Declarations */ - -#[derive(Debug)] -pub struct ModuleDeclaration { - is_public: bool, - identifier: Box, - declarations: Vec>, -} - -impl ModuleDeclaration { - pub fn new( - is_public: bool, - identifier: Box, - declarations: Vec>, - ) -> Self { - Self { - is_public, - identifier, - declarations, - } - } - - pub fn is_public(&self) -> bool { - self.is_public - } - - pub fn identifier(&self) -> &Identifier { - &self.identifier - } - - pub fn identifier_mut(&mut self) -> &mut Identifier { - &mut self.identifier - } - - pub fn declarations(&self) -> Vec<&ModuleLevelDeclaration> { - self.declarations.iter().map(Box::as_ref).collect() - } - - pub fn declarations_mut(&mut self) -> Vec<&mut ModuleLevelDeclaration> { - self.declarations.iter_mut().map(Box::as_mut).collect() - } -} - -#[derive(Debug)] -pub struct InterfaceDeclaration { - is_public: bool, - identifier: Box, - generics: Box, - implements: Box, - declarations: Vec>, -} - -impl InterfaceDeclaration { - pub fn new( - is_public: bool, - identifier: Box, - generics: Box, - implements: Box, - declarations: Vec>, - ) -> Self { - Self { - is_public, - identifier, - generics, - implements, - declarations, - } - } - - pub fn is_public(&self) -> bool { - self.is_public - } - - pub fn identifier(&self) -> &Identifier { - &self.identifier - } - - pub fn identifier_mut(&mut self) -> &mut Identifier { - &mut self.identifier - } - - pub fn generics(&self) -> &GenericParameters { - &self.generics - } - - pub fn generics_mut(&mut self) -> &mut GenericParameters { - &mut self.generics - } - - pub fn implements(&self) -> &ImplementsList { - &self.implements - } - - pub fn implements_mut(&mut self) -> &mut ImplementsList { - &mut self.implements - } - - pub fn declarations(&self) -> Vec<&InterfaceLevelDeclaration> { - self.declarations.iter().map(Box::as_ref).collect() - } - - pub fn declarations_mut(&mut self) -> Vec<&mut InterfaceLevelDeclaration> { - self.declarations.iter_mut().map(Box::as_mut).collect() - } -} - -#[derive(Debug)] -pub struct ClassDeclaration { - is_public: bool, - identifier: Box, - generics: Box, - class_constructor: Option>, - implements: Box, - declarations: Vec>, -} - -impl ClassDeclaration { - pub fn new( - is_public: bool, - identifier: Box, - generics: Box, - class_constructor: Option>, - implements: Box, - declarations: Vec>, - ) -> Self { - Self { - is_public, - identifier, - generics, - class_constructor, - implements, - declarations, - } - } - - pub fn is_public(&self) -> bool { - self.is_public - } - - pub fn identifier(&self) -> &Identifier { - &self.identifier - } - - pub fn identifier_mut(&mut self) -> &mut Identifier { - &mut self.identifier - } - - pub fn generics(&self) -> &GenericParameters { - &self.generics - } - - pub fn generics_mut(&mut self) -> &mut GenericParameters { - &mut self.generics - } - - pub fn class_constructor(&self) -> Option<&ClassConstructor> { - if let Some(class_constructor) = &self.class_constructor { - Some(class_constructor.as_ref()) - } else { - None - } - } - - pub fn class_constructor_mut(&mut self) -> Option<&mut ClassConstructor> { - if let Some(class_constructor) = &mut self.class_constructor { - Some(class_constructor.as_mut()) - } else { - None - } - } - - pub fn implements(&self) -> &ImplementsList { - &self.implements - } - - pub fn implements_mut(&mut self) -> &mut ImplementsList { - &mut self.implements - } - - pub fn declarations(&self) -> Vec<&ClassLevelDeclaration> { - self.declarations.iter().map(Box::as_ref).collect() - } - - pub fn declarations_mut(&mut self) -> Vec<&mut ClassLevelDeclaration> { - self.declarations.iter_mut().map(Box::as_mut).collect() - } -} - -/* Function declarations and components */ - -#[derive(Debug)] -pub struct FunctionDefinition { - is_public: bool, - modifier: Option, - generics: Box, - identifier: Box, - parameters: Box, - return_type: Box, - body: Box, -} - -impl FunctionDefinition { - pub fn new( - is_public: bool, - modifier: Option, - generics: Box, - identifier: Box, - parameters: Box, - return_type: Box, - body: Box, - ) -> Self { - Self { - is_public, - modifier, - generics, - identifier, - parameters, - return_type, - body, - } - } - - pub fn is_public(&self) -> bool { - self.is_public - } - - pub fn modifier(&self) -> Option<&FunctionModifier> { - self.modifier.as_ref() - } - - pub fn generics(&self) -> &GenericParameters { - &self.generics - } - - pub fn generics_mut(&mut self) -> &mut GenericParameters { - &mut self.generics - } - - pub fn identifier(&self) -> &Identifier { - &self.identifier - } - - pub fn identifier_mut(&mut self) -> &mut Identifier { - &mut self.identifier - } - - pub fn parameters(&self) -> &Parameters { - &self.parameters - } - - pub fn parameters_mut(&mut self) -> &mut Parameters { - &mut self.parameters - } - - pub fn return_type(&self) -> &ReturnType { - &self.return_type - } - - pub fn return_type_mut(&mut self) -> &mut ReturnType { - &mut self.return_type - } - - pub fn body(&self) -> &FunctionBody { - &self.body - } - - pub fn body_mut(&mut self) -> &mut FunctionBody { - &mut self.body - } -} - -#[derive(Debug)] -pub struct OperatorFunctionDefinition { - is_public: bool, - modifier: Option, - generics: Box, - operator: Operator, - parameters: Box, - return_type: Box, - body: Box, -} - -impl OperatorFunctionDefinition { - pub fn new( - is_public: bool, - modifier: Option, - generics: Box, - operator: Operator, - parameters: Box, - return_type: Box, - body: Box, - ) -> Self { - Self { - is_public, - modifier, - generics, - operator, - parameters, - return_type, - body, - } - } - - pub fn is_public(&self) -> bool { - self.is_public - } - - pub fn modifier(&self) -> Option<&FunctionModifier> { - self.modifier.as_ref() - } - - pub fn generics(&self) -> &GenericParameters { - &self.generics - } - - pub fn generics_mut(&mut self) -> &mut GenericParameters { - &mut self.generics - } - - pub fn operator(&self) -> &Operator { - &self.operator - } - - pub fn parameters(&self) -> &Parameters { - &self.parameters - } - - pub fn parameters_mut(&mut self) -> &mut Parameters { - &mut self.parameters - } - - pub fn return_type(&self) -> &ReturnType { - &self.return_type - } - - pub fn return_type_mut(&mut self) -> &mut ReturnType { - &mut self.return_type - } - - pub fn body(&self) -> &FunctionBody { - &self.body - } - - pub fn body_mut(&mut self) -> &mut FunctionBody { - &mut self.body - } -} - -#[derive(Debug)] -pub struct PlatformFunctionDeclaration { - is_public: bool, - modifier: Option, - generics: Box, - identifier: Box, - parameters: Box, - return_type: Box, -} - -impl PlatformFunctionDeclaration { - pub fn new( - is_public: bool, - modifier: Option, - generics: Box, - identifier: Box, - parameters: Box, - return_type: Box, - ) -> Self { - Self { - is_public, - modifier, - generics, - identifier, - parameters, - return_type, - } - } - - pub fn is_public(&self) -> bool { - self.is_public - } - - pub fn modifier(&self) -> Option<&FunctionModifier> { - self.modifier.as_ref() - } - - pub fn generics(&self) -> &GenericParameters { - &self.generics - } - - pub fn generics_mut(&mut self) -> &mut GenericParameters { - &mut self.generics - } - - pub fn identifier(&self) -> &Identifier { - &self.identifier - } - - pub fn identifier_mut(&mut self) -> &mut Identifier { - &mut self.identifier - } - - pub fn parameters(&self) -> &Parameters { - &self.parameters - } - - pub fn parameters_mut(&mut self) -> &mut Parameters { - &mut self.parameters - } - - pub fn return_type(&self) -> &ReturnType { - &self.return_type - } - - pub fn return_type_mut(&mut self) -> &mut ReturnType { - &mut self.return_type - } -} - -#[derive(Debug)] -pub struct InterfaceFunctionDeclaration { - modifier: Option, - generics: Box, - identifier: Box, - parameters: Box, - return_type: Box, - body: Option>, -} - -impl InterfaceFunctionDeclaration { - pub fn new( - modifier: Option, - generics: Box, - identifier: Box, - parameters: Box, - return_type: Box, - body: Option>, - ) -> Self { - Self { - modifier, - generics, - identifier, - parameters, - return_type, - body, - } - } - - pub fn modifier(&self) -> Option<&FunctionModifier> { - self.modifier.as_ref() - } - - pub fn generics(&self) -> &GenericParameters { - &self.generics - } - - pub fn generics_mut(&mut self) -> &mut GenericParameters { - &mut self.generics - } - - pub fn identifier(&self) -> &Identifier { - &self.identifier - } - - pub fn identifier_mut(&mut self) -> &mut Identifier { - &mut self.identifier - } - - pub fn parameters(&self) -> &Parameters { - &self.parameters - } - - pub fn parameters_mut(&mut self) -> &mut Parameters { - &mut self.parameters - } - - pub fn return_type(&self) -> &ReturnType { - &self.return_type - } - - pub fn return_type_mut(&mut self) -> &mut ReturnType { - &mut self.return_type - } - - pub fn body(&self) -> Option<&FunctionBody> { - FunctionBody::unwrap_option(&self.body) - } - - pub fn body_mut(&mut self) -> Option<&mut FunctionBody> { - FunctionBody::unwrap_option_mut(&mut self.body) - } -} - -#[derive(Debug)] -pub struct InterfaceOperatorFunctionDeclaration { - modifier: Option, - generics: Box, - operator: Operator, - parameters: Box, - return_type: Box, - body: Option>, -} - -impl InterfaceOperatorFunctionDeclaration { - pub fn new( - modifier: Option, - generics: Box, - operator: Operator, - parameters: Box, - return_type: Box, - body: Option>, - ) -> Self { - Self { - modifier, - generics, - operator, - parameters, - return_type, - body, - } - } - - pub fn modifier(&self) -> Option<&FunctionModifier> { - self.modifier.as_ref() - } - - pub fn generics(&self) -> &GenericParameters { - &self.generics - } - - pub fn generics_mut(&mut self) -> &mut GenericParameters { - &mut self.generics - } - - pub fn operator(&self) -> &Operator { - &self.operator - } - - pub fn parameters(&self) -> &Box { - &self.parameters - } - - pub fn parameters_mut(&mut self) -> &mut Box { - &mut self.parameters - } - - pub fn return_type(&self) -> &ReturnType { - &self.return_type - } - - pub fn return_type_mut(&mut self) -> &mut ReturnType { - &mut self.return_type - } - - pub fn body(&self) -> Option<&FunctionBody> { - FunctionBody::unwrap_option(&self.body) - } - - pub fn body_mut(&mut self) -> Option<&mut FunctionBody> { - FunctionBody::unwrap_option_mut(&mut self.body) - } -} - -#[derive(Debug)] -pub enum FunctionModifier { - Static, - Cons, - Mut, - Ref, - MutRef, -} - -#[derive(Debug)] -pub enum FunctionBody { - Equals(Box), - Block(Box), - Alias(Box), -} - -impl FunctionBody { - fn unwrap_option(opt: &Option>) -> Option<&FunctionBody> { - if let Some(body) = opt { - Some(body.as_ref()) - } else { - None - } - } - - fn unwrap_option_mut(opt: &mut Option>) -> Option<&mut FunctionBody> { - if let Some(body) = opt { - Some(body.as_mut()) - } else { - None - } - } -} - -/* Class components */ - -#[derive(Debug)] -pub struct ClassConstructor(Vec>); - -impl ClassConstructor { - pub fn new(parameters: Vec>) -> Self { - Self(parameters) - } - - pub fn parameters(&self) -> Vec<&ClassConstructorParameter> { - self.0.iter().map(|p| p.as_ref()).collect() - } - - pub fn parameters_mut(&mut self) -> Vec<&mut ClassConstructorParameter> { - self.0.iter_mut().map(|p| p.as_mut()).collect() - } -} - -#[derive(Debug)] -pub enum ClassConstructorParameter { - Property(Box), - Field(Box), -} - -#[derive(Debug)] -pub struct PropertyDeclaration { - is_mutable: bool, - identifier: Box, - declared_type: Box, -} - -impl PropertyDeclaration { - pub fn new(is_mutable: bool, identifier: Box, declared_type: Box) -> Self { - Self { - is_mutable, - identifier, - declared_type, - } - } - - pub fn is_mutable(&self) -> bool { - self.is_mutable - } - - pub fn identifier(&self) -> &Identifier { - &self.identifier - } - - pub fn identifier_mut(&mut self) -> &mut Identifier { - &mut self.identifier - } - - pub fn declared_type(&self) -> &TypeUse { - &self.declared_type - } - - pub fn declared_type_mut(&mut self) -> &mut TypeUse { - &mut self.declared_type - } -} - -#[derive(Debug)] -pub struct FieldDeclaration { - is_mutable: bool, - identifier: Box, - declared_type: Box, -} - -impl FieldDeclaration { - pub fn new(is_mutable: bool, identifier: Box, declared_type: Box) -> Self { - Self { - is_mutable, - identifier, - declared_type, - } - } - - pub fn is_mutable(&self) -> bool { - self.is_mutable - } - - pub fn identifier(&self) -> &Identifier { - &self.identifier - } - - pub fn identifier_mut(&mut self) -> &mut Identifier { - &mut self.identifier - } - - pub fn declared_type(&self) -> &TypeUse { - &self.declared_type - } - - pub fn declared_type_mut(&mut self) -> &mut TypeUse { - &mut self.declared_type - } -} - -/* Statements */ - -#[derive(Debug)] -pub struct BlockStatement { - statements: Vec>, - expression: Option>, -} - -impl BlockStatement { - pub fn new(statements: Vec>, expression: Option>) -> Self { - Self { - statements, - expression, - } - } - - pub fn statements(&self) -> Vec<&Statement> { - self.statements.iter().map(AsRef::as_ref).collect() - } - - pub fn statements_mut(&mut self) -> Vec<&mut Statement> { - self.statements.iter_mut().map(AsMut::as_mut).collect() - } - - pub fn expression(&self) -> Option<&Expression> { - if let Some(ref expression) = self.expression { - Some(expression.as_ref()) - } else { - None - } - } - - pub fn expression_mut(&mut self) -> Option<&mut Expression> { - if let Some(ref mut expression) = self.expression { - Some(expression.as_mut()) - } else { - None - } - } -} - -#[derive(Debug)] -pub enum Statement { - BlockStatement(Box), - VariableDeclarationStatement(Box), - AssignStatement(Box), - CallStatement(Box), - ReturnStatement(Box), - IfStatement(Box), - IfElseStatement(Box), - WhileStatement(Box), - ForStatement(Box), -} - -#[derive(Debug)] -pub struct VariableDeclarationStatement { - is_mutable: bool, - identifier: Box, - declared_type: Option>, - initializer: Option>, -} - -impl VariableDeclarationStatement { - pub fn new( - is_mutable: bool, - identifier: Box, - declared_type: Option>, - initializer: Option>, - ) -> Self { - Self { - is_mutable, - identifier, - declared_type, - initializer, - } - } - - pub fn is_mutable(&self) -> bool { - self.is_mutable - } - - pub fn identifier(&self) -> &Identifier { - &self.identifier - } - - pub fn identifier_mut(&mut self) -> &mut Identifier { - &mut self.identifier - } - - pub fn declared_type(&self) -> Option<&TypeUse> { - if let Some(type_use) = &self.declared_type { - Some(type_use.as_ref()) - } else { - None - } - } - - pub fn declared_type_mut(&mut self) -> Option<&mut TypeUse> { - if let Some(type_use) = &mut self.declared_type { - Some(type_use.as_mut()) - } else { - None - } - } - - pub fn initializer(&self) -> Option<&Expression> { - if let Some(initializer) = &self.initializer { - Some(initializer.as_ref()) - } else { - None - } - } - - pub fn initializer_mut(&mut self) -> Option<&mut Expression> { - if let Some(initializer) = &mut self.initializer { - Some(initializer.as_mut()) - } else { - None - } - } -} - -#[derive(Debug)] -pub struct AssignStatement { - lhs: Box, - rhs: Box, -} - -impl AssignStatement { - pub fn new(lhs: Box, rhs: Box) -> Self { - Self { lhs, rhs } - } - - pub fn lhs(&self) -> &Expression { - &self.lhs - } - - pub fn lhs_mut(&mut self) -> &mut Expression { - &mut self.lhs - } - - pub fn rhs(&self) -> &Expression { - &self.rhs - } - - pub fn rhs_mut(&mut self) -> &mut Expression { - &mut self.rhs - } -} - -#[derive(Debug)] -pub struct CallStatement(Box); - -impl CallStatement { - pub fn new(call_expression: Box) -> Self { - Self(call_expression) - } - - pub fn expression(&self) -> &Expression { - &self.0 - } - - pub fn expression_mut(&mut self) -> &mut Expression { - &mut self.0 - } -} - -#[derive(Debug)] -pub struct ReturnStatement(Option>); - -impl ReturnStatement { - pub fn new(expression: Option>) -> Self { - Self(expression) - } - - pub fn expression(&self) -> Option<&Expression> { - if let Some(expression) = &self.0 { - Some(expression.as_ref()) - } else { - None - } - } - - pub fn expression_mut(&mut self) -> Option<&mut Expression> { - if let Some(expression) = &mut self.0 { - Some(expression.as_mut()) - } else { - None - } - } -} - -#[derive(Debug)] -pub struct IfStatement { - condition: Box, - then_block: Box, -} - -impl IfStatement { - pub fn new(condition: Box, then_block: Box) -> Self { - Self { - condition, - then_block, - } - } - - pub fn condition(&self) -> &Expression { - &self.condition - } - - pub fn condition_mut(&mut self) -> &mut Expression { - &mut self.condition - } - - pub fn then_block(&self) -> &BlockStatement { - &self.then_block - } - - pub fn then_block_mut(&mut self) -> &mut BlockStatement { - &mut self.then_block - } -} - -#[derive(Debug)] -pub struct IfElseStatement { - if_statement: Box, - else_ifs: Box, - else_block: Option>, -} - -impl IfElseStatement { - pub fn new( - if_statement: Box, - else_ifs: Box, - else_block: Option>, - ) -> Self { - Self { - if_statement, - else_ifs, - else_block, - } - } - - pub fn if_statement(&self) -> &IfStatement { - &self.if_statement - } - - pub fn if_statement_mut(&mut self) -> &mut IfStatement { - &mut self.if_statement - } - - pub fn else_ifs(&self) -> &ElseIfs { - &self.else_ifs - } - - pub fn else_ifs_mut(&mut self) -> &mut ElseIfs { - &mut self.else_ifs - } - - pub fn else_block(&self) -> Option<&ElseBlock> { - if let Some(else_block) = &self.else_block { - Some(else_block.as_ref()) - } else { - None - } - } - - pub fn else_block_mut(&mut self) -> Option<&mut ElseBlock> { - if let Some(else_block) = &mut self.else_block { - Some(else_block.as_mut()) - } else { - None - } - } -} - -#[derive(Debug, Default)] -pub struct ElseIfs(Vec>); - -impl ElseIfs { - pub fn new(if_statements: Vec>) -> Self { - Self(if_statements) - } - - pub fn if_statements(&self) -> Vec<&IfStatement> { - self.0.iter().map(Box::as_ref).collect() - } - - pub fn if_statements_mut(&mut self) -> Vec<&mut IfStatement> { - self.0.iter_mut().map(Box::as_mut).collect() - } -} - -#[derive(Debug)] -pub struct ElseBlock(Box); - -impl ElseBlock { - pub fn new(block: Box) -> Self { - Self(block) - } - - pub fn block_statement(&self) -> &BlockStatement { - &self.0 - } - - pub fn block_statement_mut(&mut self) -> &mut BlockStatement { - &mut self.0 - } -} - -#[derive(Debug)] -pub struct WhileStatement { - condition: Box, - body: Box, -} - -impl WhileStatement { - pub fn new(condition: Box, body: Box) -> Self { - Self { condition, body } - } - - pub fn condition(&self) -> &Expression { - &self.condition - } - - pub fn condition_mut(&mut self) -> &mut Expression { - &mut self.condition - } - - pub fn body(&self) -> &BlockStatement { - &self.body - } - - pub fn body_mut(&mut self) -> &mut BlockStatement { - &mut self.body - } -} - -#[derive(Debug)] -pub struct ForStatement { - variable: Box, - iterator: Box, - body: Box, -} - -impl ForStatement { - pub fn new( - variable: Box, - iterator: Box, - body: Box, - ) -> Self { - Self { - variable, - iterator, - body, - } - } - - pub fn variable(&self) -> &Identifier { - &self.variable - } - - pub fn variable_mut(&mut self) -> &mut Identifier { - &mut self.variable - } - - pub fn iterator(&self) -> &Expression { - &self.iterator - } - - pub fn iterator_mut(&mut self) -> &mut Expression { - &mut self.iterator - } - - pub fn body(&self) -> &BlockStatement { - &self.body - } - - pub fn body_mut(&mut self) -> &mut BlockStatement { - &mut self.body - } -} - -/* Expressions */ - -#[derive(Debug)] -pub enum Expression { - Ternary(Box), - Binary(Box), - UnaryPrefix(Box), - UnarySuffix(Box), - Call(Box), - ObjectAccess(Box), - Literal(Box), - FullyQualifiedName(Box), - Closure(Box), -} - -#[derive(Debug)] -pub struct TernaryExpression { - condition: Box, - true_expression: Box, - false_expression: Box, -} - -impl TernaryExpression { - pub fn new( - condition: Box, - true_expression: Box, - false_expression: Box, - ) -> Self { - Self { - condition, - true_expression, - false_expression, - } - } - - pub fn condition(&self) -> &Expression { - &self.condition - } - - pub fn condition_mut(&mut self) -> &mut Expression { - &mut self.condition - } - - pub fn true_expression(&self) -> &Expression { - &self.true_expression - } - - pub fn true_expression_mut(&mut self) -> &mut Expression { - &mut self.true_expression - } - - pub fn false_expression(&self) -> &Expression { - &self.false_expression - } - - pub fn false_expression_mut(&mut self) -> &mut Expression { - &mut self.false_expression - } -} - -#[derive(Debug)] -pub struct BinaryExpression { - left: Box, - operator: BinaryOperator, - right: Box, -} - -impl BinaryExpression { - pub fn new(left: Box, operator: BinaryOperator, right: Box) -> Self { - Self { - left, - operator, - right, - } - } - - pub fn left(&self) -> &Expression { - &self.left - } - - pub fn left_mut(&mut self) -> &mut Expression { - &mut self.left - } - - pub fn operator(&self) -> &BinaryOperator { - &self.operator - } - - pub fn right(&self) -> &Expression { - &self.right - } - - pub fn right_mut(&mut self) -> &mut Expression { - &mut self.right - } -} - -#[derive(Debug)] -pub struct PrefixExpression { - operator: PrefixUnaryOperator, - expression: Box, -} - -impl PrefixExpression { - pub fn new(operator: PrefixUnaryOperator, expression: Box) -> Self { - Self { - operator, - expression, - } - } - - pub fn operator(&self) -> &PrefixUnaryOperator { - &self.operator - } - - pub fn expression(&self) -> &Expression { - &self.expression - } - - pub fn expression_mut(&mut self) -> &mut Expression { - &mut self.expression - } -} - -#[derive(Debug)] -pub struct SuffixExpression { - expression: Box, - operator: SuffixUnaryOperator, -} - -impl SuffixExpression { - pub fn new(expression: Box, operator: SuffixUnaryOperator) -> Self { - Self { - expression, - operator, - } - } - - pub fn expression(&self) -> &Expression { - &self.expression - } - - pub fn expression_mut(&mut self) -> &mut Expression { - &mut self.expression - } - - pub fn operator(&self) -> &SuffixUnaryOperator { - &self.operator - } -} - -#[derive(Debug)] -pub struct CallExpression { - callee: Box, - turbo_fish: Option>, - arguments: Box, -} - -impl CallExpression { - pub fn new( - callee: Box, - turbo_fish: Option>, - arguments: Box, - ) -> Self { - Self { - callee, - turbo_fish, - arguments, - } - } - - pub fn callee(&self) -> &Expression { - &self.callee - } - - pub fn callee_mut(&mut self) -> &mut Expression { - &mut self.callee - } - - pub fn turbo_fish(&self) -> Option<&TurboFish> { - if let Some(turbo_fish) = &self.turbo_fish { - Some(turbo_fish.as_ref()) - } else { - None - } - } - - pub fn turbo_fish_mut(&mut self) -> Option<&mut TurboFish> { - if let Some(turbo_fish) = &mut self.turbo_fish { - Some(turbo_fish.as_mut()) - } else { - None - } - } - - pub fn arguments(&self) -> &CallArguments { - &self.arguments - } - - pub fn arguments_mut(&mut self) -> &mut CallArguments { - &mut self.arguments - } -} - -#[derive(Debug)] -pub struct TurboFish(Box); - -impl TurboFish { - pub fn new(generics: Box) -> Self { - Self(generics) - } - - pub fn generics(&self) -> &GenericArguments { - &self.0 - } - - pub fn generics_mut(&mut self) -> &mut GenericArguments { - &mut self.0 - } -} - -#[derive(Debug)] -pub struct CallArguments(Vec>); - -impl CallArguments { - pub fn new(arguments: Vec>) -> Self { - Self(arguments) - } - - pub fn arguments(&self) -> Vec<&CallArgument> { - self.0.iter().map(Box::as_ref).collect() - } - - pub fn arguments_mut(&mut self) -> Vec<&mut CallArgument> { - self.0.iter_mut().map(Box::as_mut).collect() - } -} - -#[derive(Debug)] -pub struct CallArgument(Box); - -impl CallArgument { - pub fn new(expression: Box) -> Self { - Self(expression) - } - - pub fn expression(&self) -> &Expression { - &self.0 - } - - pub fn expression_mut(&mut self) -> &mut Expression { - &mut self.0 - } -} - -#[derive(Debug)] -pub struct Closure { - modifier: Option, - is_move: bool, - captures: Box, - parameters: Box, - statements: Vec>, - expression: Option>, -} - -impl Closure { - pub fn new( - modifier: Option, - is_move: bool, - captures: Box, - parameters: Box, - statements: Vec>, - expression: Option>, - ) -> Self { - Self { - modifier, - is_move, - captures, - parameters, - statements, - expression, - } - } - - pub fn modifier(&self) -> Option<&ClosureModifier> { - self.modifier.as_ref() - } - - pub fn is_move(&self) -> bool { - self.is_move - } - - pub fn captures(&self) -> &ClosureCaptures { - &self.captures - } - - pub fn captures_mut(&mut self) -> &mut ClosureCaptures { - &mut self.captures - } - - pub fn parameters(&self) -> &ClosureParameters { - &self.parameters - } - - pub fn parameters_mut(&mut self) -> &mut ClosureParameters { - &mut self.parameters - } - - pub fn statements(&self) -> Vec<&Statement> { - self.statements.iter().map(Box::as_ref).collect() - } - - pub fn statements_mut(&mut self) -> Vec<&mut Statement> { - self.statements.iter_mut().map(Box::as_mut).collect() - } - - pub fn expression(&self) -> Option<&Expression> { - if let Some(expression) = &self.expression { - Some(expression) - } else { - None - } - } - - pub fn expression_mut(&mut self) -> Option<&mut Expression> { - if let Some(expression) = &mut self.expression { - Some(expression) - } else { - None - } - } -} - -#[derive(Debug)] -pub enum ClosureModifier { - Cons, - Mut, -} - -#[derive(Debug, Default)] -pub struct ClosureCaptures(Vec>); - -impl ClosureCaptures { - pub fn new(captures: Vec>) -> Self { - Self(captures) - } - - pub fn captures(&self) -> Vec<&ClosureCapture> { - self.0.iter().map(Box::as_ref).collect() - } - - pub fn captures_mut(&mut self) -> Vec<&mut ClosureCapture> { - self.0.iter_mut().map(Box::as_mut).collect() - } -} - -impl ClosureCaptures { - pub fn is_empty(&self) -> bool { - self.0.is_empty() - } -} - -#[derive(Debug)] -pub struct ClosureCapture { - borrow_count: usize, - is_mutable: bool, - identifier: Box, -} - -impl ClosureCapture { - pub fn new(borrow_count: usize, is_mutable: bool, identifier: Box) -> Self { - Self { - borrow_count, - is_mutable, - identifier, - } - } - - pub fn borrow_count(&self) -> usize { - self.borrow_count - } - - pub fn is_mutable(&self) -> bool { - self.is_mutable - } - - pub fn identifier(&self) -> &Identifier { - &self.identifier - } - - pub fn identifier_mut(&mut self) -> &mut Identifier { - &mut self.identifier - } -} - -#[derive(Debug, Default)] -pub struct ClosureParameters(Vec>); - -impl ClosureParameters { - pub fn new(parameters: Vec>) -> Self { - Self(parameters) - } - - pub fn parameters(&self) -> Vec<&ClosureParameter> { - self.0.iter().map(Box::as_ref).collect() - } - - pub fn parameters_mut(&mut self) -> Vec<&mut ClosureParameter> { - self.0.iter_mut().map(Box::as_mut).collect() - } - - pub fn is_empty(&self) -> bool { - self.0.is_empty() - } -} - -#[derive(Debug)] -pub struct ClosureParameter { - identifier: Box, - type_use: Option>, -} - -impl ClosureParameter { - pub fn new(identifier: Box, type_use: Option>) -> Self { - Self { - identifier, - type_use, - } - } - - pub fn identifier(&self) -> &Identifier { - &self.identifier - } - - pub fn identifier_mut(&mut self) -> &mut Identifier { - &mut self.identifier - } - - pub fn type_use(&self) -> Option<&TypeUse> { - if let Some(type_use) = &self.type_use { - Some(type_use) - } else { - None - } - } - - pub fn type_use_mut(&mut self) -> Option<&mut TypeUse> { - if let Some(type_use) = &mut self.type_use { - Some(type_use.as_mut()) - } else { - None - } - } -} - -#[derive(Debug)] -pub struct ObjectAccess { - receiver: Box, - navigations: Box, -} - -impl ObjectAccess { - pub fn new(receiver: Box, navigations: Box) -> Self { - Self { - receiver, - navigations, - } - } - - pub fn receiver(&self) -> &Expression { - &self.receiver - } - - pub fn receiver_mut(&mut self) -> &mut Expression { - &mut self.receiver - } - - pub fn navigations(&self) -> &ObjectNavigations { - &self.navigations - } - - pub fn navigations_mut(&mut self) -> &mut ObjectNavigations { - &mut self.navigations - } -} - -#[derive(Debug)] -pub struct ObjectNavigations(Vec>); - -impl ObjectNavigations { - pub fn new(navigations: Vec>) -> Self { - Self(navigations) - } - - pub fn navigations(&self) -> Vec<&ObjectNavigation> { - self.0.iter().map(Box::as_ref).collect() - } - - pub fn navigations_mut(&mut self) -> Vec<&mut ObjectNavigation> { - self.0.iter_mut().map(Box::as_mut).collect() - } -} - -#[derive(Debug)] -pub enum ObjectNavigation { - Index(Box), - Identifier(Box), -} - -#[derive(Debug)] -pub enum Literal { - Integer(i32), - Long(i64), - Double(f64), - USize(usize), - String(String), - DString(Box), - BacktickString(Box), - Boolean(bool), -} - -#[derive(Debug)] -pub struct DString(Vec>); - -impl DString { - pub fn new(parts: Vec>) -> Self { - Self(parts) - } - - pub fn parts(&self) -> Vec<&DStringPart> { - self.0.iter().map(Box::as_ref).collect() - } - - pub fn parts_mut(&mut self) -> Vec<&mut DStringPart> { - self.0.iter_mut().map(Box::as_mut).collect() - } -} - -#[derive(Debug)] -pub enum DStringPart { - String(String), - Expression(Box), -} - -impl DStringPart { - pub fn from_string(s: &str) -> DStringPart { - DStringPart::String(String::from(s)) - } - - pub fn from_expression(e: Expression) -> DStringPart { - DStringPart::Expression(Box::new(e)) - } - - pub fn is_string(&self) -> bool { - match self { - DStringPart::String(_) => true, - _ => false, - } - } - - pub fn unwrap_string(self) -> String { - match self { - DStringPart::String(s) => s, - _ => panic!(), - } - } - - pub fn is_expression(&self) -> bool { - match self { - DStringPart::Expression(_) => true, - _ => false, - } - } -} diff --git a/src/ast/named.rs b/src/ast/named.rs deleted file mode 100644 index 4c31873..0000000 --- a/src/ast/named.rs +++ /dev/null @@ -1,88 +0,0 @@ -use crate::ast::{FullyQualifiedName, Identifier}; -use crate::name_analysis::symbol::Symbol; -use std::borrow::Cow; -use std::range::Range; - -pub trait Named { - fn name(&self) -> Cow<'_, str>; - - fn file_id(&self) -> usize; - fn range(&self) -> Range; - - fn set_scope_id(&mut self, scope_id: usize); - fn scope_id(&self) -> Option; - - fn set_saved_symbol(&mut self, symbol: Symbol); - fn saved_symbol(&self) -> Option; -} - -impl Named for Identifier { - fn name(&self) -> Cow<'_, str> { - Cow::Borrowed(&self.name) - } - - fn file_id(&self) -> usize { - self.file_id - } - - fn range(&self) -> Range { - self.range - } - - fn set_scope_id(&mut self, id: usize) { - self.scope_id = Some(id); - } - - fn scope_id(&self) -> Option { - self.scope_id - } - - fn set_saved_symbol(&mut self, saved_symbol: Symbol) { - self.saved_symbol = Some(saved_symbol); - } - - fn saved_symbol(&self) -> Option { - self.saved_symbol.clone() - } -} - -impl Named for FullyQualifiedName { - fn name(&self) -> Cow<'_, str> { - if self.identifiers.len() == 1 { - self.identifiers[0].name() - } else { - let mut acc = String::new(); - for (i, identifier) in self.identifiers.iter().enumerate() { - acc += &identifier.name(); - if i < self.identifiers.len() - 1 { - acc += "::"; - } - } - Cow::Owned(acc) - } - } - - fn file_id(&self) -> usize { - self.file_id - } - - fn range(&self) -> Range { - self.range - } - - fn set_scope_id(&mut self, id: usize) { - self.scope_id = Some(id); - } - - fn scope_id(&self) -> Option { - self.scope_id - } - - fn set_saved_symbol(&mut self, symbol: Symbol) { - self.saved_symbol = Some(symbol); - } - - fn saved_symbol(&self) -> Option { - self.saved_symbol.clone() - } -} diff --git a/src/ast/node/call_expression.rs b/src/ast/node/call_expression.rs new file mode 100644 index 0000000..f0cae6a --- /dev/null +++ b/src/ast/node/call_expression.rs @@ -0,0 +1,106 @@ +use crate::ast::node::expression::Expression; +use crate::ast::node::generics::GenericArguments; + +#[derive(Debug)] +pub struct CallExpression { + callee: Box, + turbo_fish: Option>, + arguments: Box, +} + +impl CallExpression { + pub fn new( + callee: Box, + turbo_fish: Option>, + arguments: Box, + ) -> Self { + Self { + callee, + turbo_fish, + arguments, + } + } + + pub fn callee(&self) -> &Expression { + &self.callee + } + + pub fn callee_mut(&mut self) -> &mut Expression { + &mut self.callee + } + + pub fn turbo_fish(&self) -> Option<&TurboFish> { + if let Some(turbo_fish) = &self.turbo_fish { + Some(turbo_fish.as_ref()) + } else { + None + } + } + + pub fn turbo_fish_mut(&mut self) -> Option<&mut TurboFish> { + if let Some(turbo_fish) = &mut self.turbo_fish { + Some(turbo_fish.as_mut()) + } else { + None + } + } + + pub fn arguments(&self) -> &CallArguments { + &self.arguments + } + + pub fn arguments_mut(&mut self) -> &mut CallArguments { + &mut self.arguments + } +} + +#[derive(Debug)] +pub struct TurboFish(Box); + +impl TurboFish { + pub fn new(generics: Box) -> Self { + Self(generics) + } + + pub fn generics(&self) -> &GenericArguments { + &self.0 + } + + pub fn generics_mut(&mut self) -> &mut GenericArguments { + &mut self.0 + } +} + +#[derive(Debug)] +pub struct CallArguments(Vec>); + +impl CallArguments { + pub fn new(arguments: Vec>) -> Self { + Self(arguments) + } + + pub fn arguments(&self) -> Vec<&CallArgument> { + self.0.iter().map(Box::as_ref).collect() + } + + pub fn arguments_mut(&mut self) -> Vec<&mut CallArgument> { + self.0.iter_mut().map(Box::as_mut).collect() + } +} + +#[derive(Debug)] +pub struct CallArgument(Box); + +impl CallArgument { + pub fn new(expression: Box) -> Self { + Self(expression) + } + + pub fn expression(&self) -> &Expression { + &self.0 + } + + pub fn expression_mut(&mut self) -> &mut Expression { + &mut self.0 + } +} diff --git a/src/ast/node/class.rs b/src/ast/node/class.rs new file mode 100644 index 0000000..a3eda98 --- /dev/null +++ b/src/ast/node/class.rs @@ -0,0 +1,184 @@ +use crate::ast::node::generics::GenericParameters; +use crate::ast::node::implements_list::ImplementsList; +use crate::ast::node::level::ClassLevelDeclaration; +use crate::ast::node::names::Identifier; +use crate::ast::node::type_use::TypeUse; + +#[derive(Debug)] +pub struct ClassDeclaration { + is_public: bool, + identifier: Box, + generics: Box, + class_constructor: Option>, + implements: Box, + declarations: Vec>, +} + +impl ClassDeclaration { + pub fn new( + is_public: bool, + identifier: Box, + generics: Box, + class_constructor: Option>, + implements: Box, + declarations: Vec>, + ) -> Self { + Self { + is_public, + identifier, + generics, + class_constructor, + implements, + declarations, + } + } + + pub fn is_public(&self) -> bool { + self.is_public + } + + pub fn identifier(&self) -> &Identifier { + &self.identifier + } + + pub fn identifier_mut(&mut self) -> &mut Identifier { + &mut self.identifier + } + + pub fn generics(&self) -> &GenericParameters { + &self.generics + } + + pub fn generics_mut(&mut self) -> &mut GenericParameters { + &mut self.generics + } + + pub fn class_constructor(&self) -> Option<&ClassConstructor> { + if let Some(class_constructor) = &self.class_constructor { + Some(class_constructor.as_ref()) + } else { + None + } + } + + pub fn class_constructor_mut(&mut self) -> Option<&mut ClassConstructor> { + if let Some(class_constructor) = &mut self.class_constructor { + Some(class_constructor.as_mut()) + } else { + None + } + } + + pub fn implements(&self) -> &ImplementsList { + &self.implements + } + + pub fn implements_mut(&mut self) -> &mut ImplementsList { + &mut self.implements + } + + pub fn declarations(&self) -> Vec<&ClassLevelDeclaration> { + self.declarations.iter().map(Box::as_ref).collect() + } + + pub fn declarations_mut(&mut self) -> Vec<&mut ClassLevelDeclaration> { + self.declarations.iter_mut().map(Box::as_mut).collect() + } +} + +#[derive(Debug)] +pub struct ClassConstructor(Vec>); + +impl ClassConstructor { + pub fn new(parameters: Vec>) -> Self { + Self(parameters) + } + + pub fn parameters(&self) -> Vec<&ClassConstructorParameter> { + self.0.iter().map(|p| p.as_ref()).collect() + } + + pub fn parameters_mut(&mut self) -> Vec<&mut ClassConstructorParameter> { + self.0.iter_mut().map(|p| p.as_mut()).collect() + } +} + +#[derive(Debug)] +pub enum ClassConstructorParameter { + Property(Box), + Field(Box), +} + +#[derive(Debug)] +pub struct PropertyDeclaration { + is_mutable: bool, + identifier: Box, + declared_type: Box, +} + +impl PropertyDeclaration { + pub fn new(is_mutable: bool, identifier: Box, declared_type: Box) -> Self { + Self { + is_mutable, + identifier, + declared_type, + } + } + + pub fn is_mutable(&self) -> bool { + self.is_mutable + } + + pub fn identifier(&self) -> &Identifier { + &self.identifier + } + + pub fn identifier_mut(&mut self) -> &mut Identifier { + &mut self.identifier + } + + pub fn declared_type(&self) -> &TypeUse { + &self.declared_type + } + + pub fn declared_type_mut(&mut self) -> &mut TypeUse { + &mut self.declared_type + } +} + +#[derive(Debug)] +pub struct FieldDeclaration { + is_mutable: bool, + identifier: Box, + declared_type: Box, +} + +impl FieldDeclaration { + pub fn new(is_mutable: bool, identifier: Box, declared_type: Box) -> Self { + Self { + is_mutable, + identifier, + declared_type, + } + } + + pub fn is_mutable(&self) -> bool { + self.is_mutable + } + + pub fn identifier(&self) -> &Identifier { + &self.identifier + } + + pub fn identifier_mut(&mut self) -> &mut Identifier { + &mut self.identifier + } + + pub fn declared_type(&self) -> &TypeUse { + &self.declared_type + } + + pub fn declared_type_mut(&mut self) -> &mut TypeUse { + &mut self.declared_type + } +} diff --git a/src/ast/node/closure.rs b/src/ast/node/closure.rs new file mode 100644 index 0000000..3acfe25 --- /dev/null +++ b/src/ast/node/closure.rs @@ -0,0 +1,204 @@ +use crate::ast::node::expression::Expression; +use crate::ast::node::names::Identifier; +use crate::ast::node::statement::Statement; +use crate::ast::node::type_use::TypeUse; + +#[derive(Debug)] +pub struct Closure { + modifier: Option, + is_move: bool, + captures: Box, + parameters: Box, + statements: Vec>, + expression: Option>, +} + +impl Closure { + pub fn new( + modifier: Option, + is_move: bool, + captures: Box, + parameters: Box, + statements: Vec>, + expression: Option>, + ) -> Self { + Self { + modifier, + is_move, + captures, + parameters, + statements, + expression, + } + } + + pub fn modifier(&self) -> Option<&ClosureModifier> { + self.modifier.as_ref() + } + + pub fn is_move(&self) -> bool { + self.is_move + } + + pub fn captures(&self) -> &ClosureCaptures { + &self.captures + } + + pub fn captures_mut(&mut self) -> &mut ClosureCaptures { + &mut self.captures + } + + pub fn parameters(&self) -> &ClosureParameters { + &self.parameters + } + + pub fn parameters_mut(&mut self) -> &mut ClosureParameters { + &mut self.parameters + } + + pub fn statements(&self) -> Vec<&Statement> { + self.statements.iter().map(Box::as_ref).collect() + } + + pub fn statements_mut(&mut self) -> Vec<&mut Statement> { + self.statements.iter_mut().map(Box::as_mut).collect() + } + + pub fn expression(&self) -> Option<&Expression> { + if let Some(expression) = &self.expression { + Some(expression) + } else { + None + } + } + + pub fn expression_mut(&mut self) -> Option<&mut Expression> { + if let Some(expression) = &mut self.expression { + Some(expression) + } else { + None + } + } +} + +#[derive(Debug)] +pub enum ClosureModifier { + Cons, + Mut, +} + +#[derive(Debug, Default)] +pub struct ClosureCaptures(Vec>); + +impl ClosureCaptures { + pub fn new(captures: Vec>) -> Self { + Self(captures) + } + + pub fn captures(&self) -> Vec<&ClosureCapture> { + self.0.iter().map(Box::as_ref).collect() + } + + pub fn captures_mut(&mut self) -> Vec<&mut ClosureCapture> { + self.0.iter_mut().map(Box::as_mut).collect() + } +} + +impl ClosureCaptures { + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } +} + +#[derive(Debug)] +pub struct ClosureCapture { + borrow_count: usize, + is_mutable: bool, + identifier: Box, +} + +impl ClosureCapture { + pub fn new(borrow_count: usize, is_mutable: bool, identifier: Box) -> Self { + Self { + borrow_count, + is_mutable, + identifier, + } + } + + pub fn borrow_count(&self) -> usize { + self.borrow_count + } + + pub fn is_mutable(&self) -> bool { + self.is_mutable + } + + pub fn identifier(&self) -> &Identifier { + &self.identifier + } + + pub fn identifier_mut(&mut self) -> &mut Identifier { + &mut self.identifier + } +} + +#[derive(Debug, Default)] +pub struct ClosureParameters(Vec>); + +impl ClosureParameters { + pub fn new(parameters: Vec>) -> Self { + Self(parameters) + } + + pub fn parameters(&self) -> Vec<&ClosureParameter> { + self.0.iter().map(Box::as_ref).collect() + } + + pub fn parameters_mut(&mut self) -> Vec<&mut ClosureParameter> { + self.0.iter_mut().map(Box::as_mut).collect() + } + + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } +} + +#[derive(Debug)] +pub struct ClosureParameter { + identifier: Box, + type_use: Option>, +} + +impl ClosureParameter { + pub fn new(identifier: Box, type_use: Option>) -> Self { + Self { + identifier, + type_use, + } + } + + pub fn identifier(&self) -> &Identifier { + &self.identifier + } + + pub fn identifier_mut(&mut self) -> &mut Identifier { + &mut self.identifier + } + + pub fn type_use(&self) -> Option<&TypeUse> { + if let Some(type_use) = &self.type_use { + Some(type_use) + } else { + None + } + } + + pub fn type_use_mut(&mut self) -> Option<&mut TypeUse> { + if let Some(type_use) = &mut self.type_use { + Some(type_use.as_mut()) + } else { + None + } + } +} diff --git a/src/ast/node/compilation_unit.rs b/src/ast/node/compilation_unit.rs new file mode 100644 index 0000000..51c7cea --- /dev/null +++ b/src/ast/node/compilation_unit.rs @@ -0,0 +1,63 @@ +use crate::ast::node::level::ModuleLevelDeclaration; +use crate::ast::node::names::FullyQualifiedName; +use crate::ast::node::use_statement::UseStatement; + +#[derive(Debug)] +pub struct CompilationUnit { + file_name: String, + file_id: usize, + namespace: Option>, + use_statements: Vec>, + declarations: Vec>, +} + +impl CompilationUnit { + pub fn new( + file_name: String, + file_id: usize, + namespace: Option>, + use_statements: Vec>, + declarations: Vec>, + ) -> Self { + Self { + file_name, + file_id, + namespace, + use_statements, + declarations, + } + } + + pub fn file_name(&self) -> &str { + &self.file_name + } + + pub fn file_id(&self) -> usize { + self.file_id + } + + pub fn namespace(&self) -> Option<&FullyQualifiedName> { + // We have to use this non-map syntax because we need a reference like `&self.namespace` + if let Some(namespace) = &self.namespace { + Some(namespace.as_ref()) + } else { + None + } + } + + pub fn use_statements(&self) -> Vec<&UseStatement> { + self.use_statements.iter().map(Box::as_ref).collect() + } + + pub fn use_statements_mut(&mut self) -> Vec<&mut UseStatement> { + self.use_statements.iter_mut().map(Box::as_mut).collect() + } + + pub fn declarations(&self) -> Vec<&ModuleLevelDeclaration> { + self.declarations.iter().map(Box::as_ref).collect() + } + + pub fn declarations_mut(&mut self) -> Vec<&mut ModuleLevelDeclaration> { + self.declarations.iter_mut().map(Box::as_mut).collect() + } +} diff --git a/src/ast/node/d_string.rs b/src/ast/node/d_string.rs new file mode 100644 index 0000000..ba54d0c --- /dev/null +++ b/src/ast/node/d_string.rs @@ -0,0 +1,55 @@ +use crate::ast::node::expression::Expression; + +#[derive(Debug)] +pub struct DString(Vec>); + +impl DString { + pub fn new(parts: Vec>) -> Self { + Self(parts) + } + + pub fn parts(&self) -> Vec<&DStringPart> { + self.0.iter().map(Box::as_ref).collect() + } + + pub fn parts_mut(&mut self) -> Vec<&mut DStringPart> { + self.0.iter_mut().map(Box::as_mut).collect() + } +} + +#[derive(Debug)] +pub enum DStringPart { + String(String), + Expression(Box), +} + +impl DStringPart { + pub fn from_string(s: &str) -> DStringPart { + DStringPart::String(String::from(s)) + } + + pub fn from_expression(e: Expression) -> DStringPart { + DStringPart::Expression(Box::new(e)) + } + + pub fn is_string(&self) -> bool { + match self { + DStringPart::String(_) => true, + _ => false, + } + } + + pub fn unwrap_string(self) -> String { + match self { + DStringPart::String(s) => s, + _ => panic!(), + } + } + + pub fn is_expression(&self) -> bool { + match self { + DStringPart::Expression(_) => true, + _ => false, + } + } +} diff --git a/src/ast/node/expression.rs b/src/ast/node/expression.rs new file mode 100644 index 0000000..9d13d73 --- /dev/null +++ b/src/ast/node/expression.rs @@ -0,0 +1,155 @@ +use crate::ast::node::call_expression::CallExpression; +use crate::ast::node::closure::Closure; +use crate::ast::node::literal::Literal; +use crate::ast::node::names::FullyQualifiedName; +use crate::ast::node::object_access::ObjectAccess; +use crate::ast::node::operators::{BinaryOperator, PrefixUnaryOperator, SuffixUnaryOperator}; + +#[derive(Debug)] +pub enum Expression { + Ternary(Box), + Binary(Box), + UnaryPrefix(Box), + UnarySuffix(Box), + Call(Box), + ObjectAccess(Box), + Literal(Box), + FullyQualifiedName(Box), + Closure(Box), +} + +#[derive(Debug)] +pub struct TernaryExpression { + condition: Box, + true_expression: Box, + false_expression: Box, +} + +impl TernaryExpression { + pub fn new( + condition: Box, + true_expression: Box, + false_expression: Box, + ) -> Self { + Self { + condition, + true_expression, + false_expression, + } + } + + pub fn condition(&self) -> &Expression { + &self.condition + } + + pub fn condition_mut(&mut self) -> &mut Expression { + &mut self.condition + } + + pub fn true_expression(&self) -> &Expression { + &self.true_expression + } + + pub fn true_expression_mut(&mut self) -> &mut Expression { + &mut self.true_expression + } + + pub fn false_expression(&self) -> &Expression { + &self.false_expression + } + + pub fn false_expression_mut(&mut self) -> &mut Expression { + &mut self.false_expression + } +} + +#[derive(Debug)] +pub struct BinaryExpression { + left: Box, + operator: BinaryOperator, + right: Box, +} + +impl BinaryExpression { + pub fn new(left: Box, operator: BinaryOperator, right: Box) -> Self { + Self { + left, + operator, + right, + } + } + + pub fn left(&self) -> &Expression { + &self.left + } + + pub fn left_mut(&mut self) -> &mut Expression { + &mut self.left + } + + pub fn operator(&self) -> &BinaryOperator { + &self.operator + } + + pub fn right(&self) -> &Expression { + &self.right + } + + pub fn right_mut(&mut self) -> &mut Expression { + &mut self.right + } +} + +#[derive(Debug)] +pub struct PrefixExpression { + operator: PrefixUnaryOperator, + expression: Box, +} + +impl PrefixExpression { + pub fn new(operator: PrefixUnaryOperator, expression: Box) -> Self { + Self { + operator, + expression, + } + } + + pub fn operator(&self) -> &PrefixUnaryOperator { + &self.operator + } + + pub fn expression(&self) -> &Expression { + &self.expression + } + + pub fn expression_mut(&mut self) -> &mut Expression { + &mut self.expression + } +} + +#[derive(Debug)] +pub struct SuffixExpression { + expression: Box, + operator: SuffixUnaryOperator, +} + +impl SuffixExpression { + pub fn new(expression: Box, operator: SuffixUnaryOperator) -> Self { + Self { + expression, + operator, + } + } + + pub fn expression(&self) -> &Expression { + &self.expression + } + + pub fn expression_mut(&mut self) -> &mut Expression { + &mut self.expression + } + + pub fn operator(&self) -> &SuffixUnaryOperator { + &self.operator + } +} diff --git a/src/ast/node/function.rs b/src/ast/node/function.rs new file mode 100644 index 0000000..5070101 --- /dev/null +++ b/src/ast/node/function.rs @@ -0,0 +1,543 @@ +use crate::ast::node::expression::Expression; +use crate::ast::node::generics::GenericParameters; +use crate::ast::node::names::Identifier; +use crate::ast::node::operators::Operator; +use crate::ast::node::statement::BlockStatement; +use crate::ast::node::type_use::{PrimitiveTypeUse, TypeUse}; + +#[derive(Debug)] +pub enum FunctionTypeModifier { + Cons, + MutRef, + Mut, + Ref, +} + +#[derive(Debug)] +pub struct Parameters(Vec>); + +impl Parameters { + pub fn new(parameters: Vec>) -> Self { + Self(parameters) + } + + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } + + pub fn parameters(&self) -> Vec<&Parameter> { + self.0.iter().map(Box::as_ref).collect() + } + + pub fn parameters_mut(&mut self) -> Vec<&mut Parameter> { + self.0.iter_mut().map(Box::as_mut).collect() + } +} + +impl Default for Parameters { + fn default() -> Self { + Parameters(Vec::new()) + } +} + +#[derive(Debug)] +pub struct Parameter { + identifier: Box, + type_use: Box, +} + +impl Parameter { + pub fn new(identifier: Box, type_use: Box) -> Self { + Self { + identifier, + type_use, + } + } + + pub fn identifier(&self) -> &Identifier { + &self.identifier + } + + pub fn identifier_mut(&mut self) -> &mut Identifier { + &mut self.identifier + } + + pub fn type_use(&self) -> &TypeUse { + &self.type_use + } + + pub fn type_use_mut(&mut self) -> &mut TypeUse { + &mut self.type_use + } +} + +#[derive(Debug)] +pub struct ReturnType { + declared_type: Box, + references: Box, +} + +impl ReturnType { + pub fn new(declared_type: Box, references: Box) -> Self { + Self { + declared_type, + references, + } + } + + pub fn void() -> Self { + ReturnType { + declared_type: Box::new(TypeUse::Primitive(Box::new(PrimitiveTypeUse::Void))), + references: Box::new(References::default()), + } + } + + pub fn declared_type(&self) -> &TypeUse { + &self.declared_type + } + + pub fn declared_type_mut(&mut self) -> &mut TypeUse { + &mut self.declared_type + } + + pub fn references(&self) -> &References { + &self.references + } + + pub fn references_mut(&mut self) -> &mut References { + &mut self.references + } +} + +#[derive(Debug)] +pub struct References(Vec>); + +impl References { + pub fn new(identifiers: Vec>) -> Self { + Self(identifiers) + } + + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } + + pub fn identifiers(&self) -> Vec<&Identifier> { + self.0.iter().map(Box::as_ref).collect() + } + + pub fn identifiers_mut(&mut self) -> Vec<&mut Identifier> { + self.0.iter_mut().map(Box::as_mut).collect() + } +} + +impl Default for References { + fn default() -> Self { + References(Vec::new()) + } +} + +#[derive(Debug)] +pub struct FunctionDefinition { + is_public: bool, + modifier: Option, + generics: Box, + identifier: Box, + parameters: Box, + return_type: Box, + body: Box, +} + +impl FunctionDefinition { + pub fn new( + is_public: bool, + modifier: Option, + generics: Box, + identifier: Box, + parameters: Box, + return_type: Box, + body: Box, + ) -> Self { + Self { + is_public, + modifier, + generics, + identifier, + parameters, + return_type, + body, + } + } + + pub fn is_public(&self) -> bool { + self.is_public + } + + pub fn modifier(&self) -> Option<&FunctionModifier> { + self.modifier.as_ref() + } + + pub fn generics(&self) -> &GenericParameters { + &self.generics + } + + pub fn generics_mut(&mut self) -> &mut GenericParameters { + &mut self.generics + } + + pub fn identifier(&self) -> &Identifier { + &self.identifier + } + + pub fn identifier_mut(&mut self) -> &mut Identifier { + &mut self.identifier + } + + pub fn parameters(&self) -> &Parameters { + &self.parameters + } + + pub fn parameters_mut(&mut self) -> &mut Parameters { + &mut self.parameters + } + + pub fn return_type(&self) -> &ReturnType { + &self.return_type + } + + pub fn return_type_mut(&mut self) -> &mut ReturnType { + &mut self.return_type + } + + pub fn body(&self) -> &FunctionBody { + &self.body + } + + pub fn body_mut(&mut self) -> &mut FunctionBody { + &mut self.body + } +} + +#[derive(Debug)] +pub struct OperatorFunctionDefinition { + is_public: bool, + modifier: Option, + generics: Box, + operator: Operator, + parameters: Box, + return_type: Box, + body: Box, +} + +impl OperatorFunctionDefinition { + pub fn new( + is_public: bool, + modifier: Option, + generics: Box, + operator: Operator, + parameters: Box, + return_type: Box, + body: Box, + ) -> Self { + Self { + is_public, + modifier, + generics, + operator, + parameters, + return_type, + body, + } + } + + pub fn is_public(&self) -> bool { + self.is_public + } + + pub fn modifier(&self) -> Option<&FunctionModifier> { + self.modifier.as_ref() + } + + pub fn generics(&self) -> &GenericParameters { + &self.generics + } + + pub fn generics_mut(&mut self) -> &mut GenericParameters { + &mut self.generics + } + + pub fn operator(&self) -> &Operator { + &self.operator + } + + pub fn parameters(&self) -> &Parameters { + &self.parameters + } + + pub fn parameters_mut(&mut self) -> &mut Parameters { + &mut self.parameters + } + + pub fn return_type(&self) -> &ReturnType { + &self.return_type + } + + pub fn return_type_mut(&mut self) -> &mut ReturnType { + &mut self.return_type + } + + pub fn body(&self) -> &FunctionBody { + &self.body + } + + pub fn body_mut(&mut self) -> &mut FunctionBody { + &mut self.body + } +} + +#[derive(Debug)] +pub struct PlatformFunctionDeclaration { + is_public: bool, + modifier: Option, + generics: Box, + identifier: Box, + parameters: Box, + return_type: Box, +} + +impl PlatformFunctionDeclaration { + pub fn new( + is_public: bool, + modifier: Option, + generics: Box, + identifier: Box, + parameters: Box, + return_type: Box, + ) -> Self { + Self { + is_public, + modifier, + generics, + identifier, + parameters, + return_type, + } + } + + pub fn is_public(&self) -> bool { + self.is_public + } + + pub fn modifier(&self) -> Option<&FunctionModifier> { + self.modifier.as_ref() + } + + pub fn generics(&self) -> &GenericParameters { + &self.generics + } + + pub fn generics_mut(&mut self) -> &mut GenericParameters { + &mut self.generics + } + + pub fn identifier(&self) -> &Identifier { + &self.identifier + } + + pub fn identifier_mut(&mut self) -> &mut Identifier { + &mut self.identifier + } + + pub fn parameters(&self) -> &Parameters { + &self.parameters + } + + pub fn parameters_mut(&mut self) -> &mut Parameters { + &mut self.parameters + } + + pub fn return_type(&self) -> &ReturnType { + &self.return_type + } + + pub fn return_type_mut(&mut self) -> &mut ReturnType { + &mut self.return_type + } +} + +#[derive(Debug)] +pub struct InterfaceFunctionDeclaration { + modifier: Option, + generics: Box, + identifier: Box, + parameters: Box, + return_type: Box, + body: Option>, +} + +impl InterfaceFunctionDeclaration { + pub fn new( + modifier: Option, + generics: Box, + identifier: Box, + parameters: Box, + return_type: Box, + body: Option>, + ) -> Self { + Self { + modifier, + generics, + identifier, + parameters, + return_type, + body, + } + } + + pub fn modifier(&self) -> Option<&FunctionModifier> { + self.modifier.as_ref() + } + + pub fn generics(&self) -> &GenericParameters { + &self.generics + } + + pub fn generics_mut(&mut self) -> &mut GenericParameters { + &mut self.generics + } + + pub fn identifier(&self) -> &Identifier { + &self.identifier + } + + pub fn identifier_mut(&mut self) -> &mut Identifier { + &mut self.identifier + } + + pub fn parameters(&self) -> &Parameters { + &self.parameters + } + + pub fn parameters_mut(&mut self) -> &mut Parameters { + &mut self.parameters + } + + pub fn return_type(&self) -> &ReturnType { + &self.return_type + } + + pub fn return_type_mut(&mut self) -> &mut ReturnType { + &mut self.return_type + } + + pub fn body(&self) -> Option<&FunctionBody> { + FunctionBody::unwrap_option(&self.body) + } + + pub fn body_mut(&mut self) -> Option<&mut FunctionBody> { + FunctionBody::unwrap_option_mut(&mut self.body) + } +} + +#[derive(Debug)] +pub struct InterfaceOperatorFunctionDeclaration { + modifier: Option, + generics: Box, + operator: Operator, + parameters: Box, + return_type: Box, + body: Option>, +} + +impl InterfaceOperatorFunctionDeclaration { + pub fn new( + modifier: Option, + generics: Box, + operator: Operator, + parameters: Box, + return_type: Box, + body: Option>, + ) -> Self { + Self { + modifier, + generics, + operator, + parameters, + return_type, + body, + } + } + + pub fn modifier(&self) -> Option<&FunctionModifier> { + self.modifier.as_ref() + } + + pub fn generics(&self) -> &GenericParameters { + &self.generics + } + + pub fn generics_mut(&mut self) -> &mut GenericParameters { + &mut self.generics + } + + pub fn operator(&self) -> &Operator { + &self.operator + } + + pub fn parameters(&self) -> &Box { + &self.parameters + } + + pub fn parameters_mut(&mut self) -> &mut Box { + &mut self.parameters + } + + pub fn return_type(&self) -> &ReturnType { + &self.return_type + } + + pub fn return_type_mut(&mut self) -> &mut ReturnType { + &mut self.return_type + } + + pub fn body(&self) -> Option<&FunctionBody> { + FunctionBody::unwrap_option(&self.body) + } + + pub fn body_mut(&mut self) -> Option<&mut FunctionBody> { + FunctionBody::unwrap_option_mut(&mut self.body) + } +} + +#[derive(Debug)] +pub enum FunctionModifier { + Static, + Cons, + Mut, + Ref, + MutRef, +} + +#[derive(Debug)] +pub enum FunctionBody { + Equals(Box), + Block(Box), + Alias(Box), +} + +impl FunctionBody { + fn unwrap_option(opt: &Option>) -> Option<&FunctionBody> { + if let Some(body) = opt { + Some(body.as_ref()) + } else { + None + } + } + + fn unwrap_option_mut(opt: &mut Option>) -> Option<&mut FunctionBody> { + if let Some(body) = opt { + Some(body.as_mut()) + } else { + None + } + } +} diff --git a/src/ast/node/generics.rs b/src/ast/node/generics.rs new file mode 100644 index 0000000..1816c1c --- /dev/null +++ b/src/ast/node/generics.rs @@ -0,0 +1,56 @@ +use crate::ast::node::names::Identifier; +use crate::ast::node::type_use::TypeUse; + +#[derive(Debug)] +pub struct GenericArguments(Vec>); + +impl GenericArguments { + pub fn new(arguments: Vec>) -> Self { + Self(arguments) + } + + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } + + pub fn arguments(&self) -> Vec<&TypeUse> { + self.0.iter().map(Box::as_ref).collect() + } + + pub fn arguments_mut(&mut self) -> Vec<&mut TypeUse> { + self.0.iter_mut().map(Box::as_mut).collect() + } +} + +impl Default for GenericArguments { + fn default() -> Self { + GenericArguments(Vec::new()) + } +} + +#[derive(Debug)] +pub struct GenericParameters(Vec>); + +impl GenericParameters { + pub fn new(identifiers: Vec>) -> Self { + Self(identifiers) + } + + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } + + pub fn identifiers(&self) -> Vec<&Identifier> { + self.0.iter().map(Box::as_ref).collect() + } + + pub fn identifiers_mut(&mut self) -> Vec<&mut Identifier> { + self.0.iter_mut().map(Box::as_mut).collect() + } +} + +impl Default for GenericParameters { + fn default() -> Self { + GenericParameters(Vec::new()) + } +} diff --git a/src/ast/node/implements_list.rs b/src/ast/node/implements_list.rs new file mode 100644 index 0000000..6838ab1 --- /dev/null +++ b/src/ast/node/implements_list.rs @@ -0,0 +1,28 @@ +use crate::ast::node::type_use::TypeUse; + +#[derive(Debug)] +pub struct ImplementsList(Vec>); + +impl ImplementsList { + pub fn new(type_uses: Vec>) -> Self { + Self(type_uses) + } + + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } + + pub fn type_uses(&self) -> Vec<&TypeUse> { + self.0.iter().map(Box::as_ref).collect() + } + + pub fn type_uses_mut(&mut self) -> Vec<&mut TypeUse> { + self.0.iter_mut().map(Box::as_mut).collect() + } +} + +impl Default for ImplementsList { + fn default() -> Self { + ImplementsList(Vec::new()) + } +} diff --git a/src/ast/node/interface.rs b/src/ast/node/interface.rs new file mode 100644 index 0000000..ff24a26 --- /dev/null +++ b/src/ast/node/interface.rs @@ -0,0 +1,67 @@ +use crate::ast::node::generics::GenericParameters; +use crate::ast::node::implements_list::ImplementsList; +use crate::ast::node::level::InterfaceLevelDeclaration; +use crate::ast::node::names::Identifier; + +#[derive(Debug)] +pub struct InterfaceDeclaration { + is_public: bool, + identifier: Box, + generics: Box, + implements: Box, + declarations: Vec>, +} + +impl InterfaceDeclaration { + pub fn new( + is_public: bool, + identifier: Box, + generics: Box, + implements: Box, + declarations: Vec>, + ) -> Self { + Self { + is_public, + identifier, + generics, + implements, + declarations, + } + } + + pub fn is_public(&self) -> bool { + self.is_public + } + + pub fn identifier(&self) -> &Identifier { + &self.identifier + } + + pub fn identifier_mut(&mut self) -> &mut Identifier { + &mut self.identifier + } + + pub fn generics(&self) -> &GenericParameters { + &self.generics + } + + pub fn generics_mut(&mut self) -> &mut GenericParameters { + &mut self.generics + } + + pub fn implements(&self) -> &ImplementsList { + &self.implements + } + + pub fn implements_mut(&mut self) -> &mut ImplementsList { + &mut self.implements + } + + pub fn declarations(&self) -> Vec<&InterfaceLevelDeclaration> { + self.declarations.iter().map(Box::as_ref).collect() + } + + pub fn declarations_mut(&mut self) -> Vec<&mut InterfaceLevelDeclaration> { + self.declarations.iter_mut().map(Box::as_mut).collect() + } +} diff --git a/src/ast/node/level.rs b/src/ast/node/level.rs new file mode 100644 index 0000000..91d7a55 --- /dev/null +++ b/src/ast/node/level.rs @@ -0,0 +1,37 @@ +use crate::ast::node::class::{ClassDeclaration, FieldDeclaration, PropertyDeclaration}; +use crate::ast::node::function::{ + FunctionDefinition, InterfaceFunctionDeclaration, InterfaceOperatorFunctionDeclaration, + OperatorFunctionDefinition, PlatformFunctionDeclaration, +}; +use crate::ast::node::interface::InterfaceDeclaration; +use crate::ast::node::module::ModuleDeclaration; + +#[derive(Debug)] +pub enum ModuleLevelDeclaration { + Module(Box), + Interface(Box), + Class(Box), + Function(Box), + PlatformFunction(Box), +} + +#[derive(Debug)] +pub enum InterfaceLevelDeclaration { + Module(Box), + Interface(Box), + Class(Box), + Function(Box), + OperatorFunction(Box), +} + +#[derive(Debug)] +pub enum ClassLevelDeclaration { + Module(Box), + Interface(Box), + Class(Box), + Function(Box), + OperatorFunction(Box), + PlatformFunction(Box), + Property(Box), + Field(Box), +} diff --git a/src/ast/node/literal.rs b/src/ast/node/literal.rs new file mode 100644 index 0000000..9b50924 --- /dev/null +++ b/src/ast/node/literal.rs @@ -0,0 +1,13 @@ +use crate::ast::node::d_string::DString; + +#[derive(Debug)] +pub enum Literal { + Integer(i32), + Long(i64), + Double(f64), + USize(usize), + String(String), + DString(Box), + BacktickString(Box), + Boolean(bool), +} diff --git a/src/ast/node/mod.rs b/src/ast/node/mod.rs new file mode 100644 index 0000000..2a56779 --- /dev/null +++ b/src/ast/node/mod.rs @@ -0,0 +1,21 @@ +pub mod call_expression; +pub mod class; +pub mod closure; +pub mod compilation_unit; +pub mod d_string; +pub mod expression; +pub mod function; +pub mod generics; +pub mod implements_list; +pub mod interface; +pub mod level; +pub mod literal; +pub mod module; +pub mod named; +pub mod names; +pub mod object_access; +pub mod operators; +pub mod statement; +pub mod tuple_arguments; +pub mod type_use; +pub mod use_statement; diff --git a/src/ast/node/module.rs b/src/ast/node/module.rs new file mode 100644 index 0000000..9dd707e --- /dev/null +++ b/src/ast/node/module.rs @@ -0,0 +1,43 @@ +use crate::ast::node::level::ModuleLevelDeclaration; +use crate::ast::node::names::Identifier; + +#[derive(Debug)] +pub struct ModuleDeclaration { + is_public: bool, + identifier: Box, + declarations: Vec>, +} + +impl ModuleDeclaration { + pub fn new( + is_public: bool, + identifier: Box, + declarations: Vec>, + ) -> Self { + Self { + is_public, + identifier, + declarations, + } + } + + pub fn is_public(&self) -> bool { + self.is_public + } + + pub fn identifier(&self) -> &Identifier { + &self.identifier + } + + pub fn identifier_mut(&mut self) -> &mut Identifier { + &mut self.identifier + } + + pub fn declarations(&self) -> Vec<&ModuleLevelDeclaration> { + self.declarations.iter().map(Box::as_ref).collect() + } + + pub fn declarations_mut(&mut self) -> Vec<&mut ModuleLevelDeclaration> { + self.declarations.iter_mut().map(Box::as_mut).collect() + } +} diff --git a/src/ast/node/named.rs b/src/ast/node/named.rs new file mode 100644 index 0000000..450682b --- /dev/null +++ b/src/ast/node/named.rs @@ -0,0 +1,16 @@ +use crate::name_analysis::symbol::Symbol; +use std::borrow::Cow; +use std::range::Range; + +pub trait Named { + fn name(&self) -> Cow<'_, str>; + + fn file_id(&self) -> usize; + fn range(&self) -> Range; + + fn set_scope_id(&mut self, scope_id: usize); + fn scope_id(&self) -> Option; + + fn set_saved_symbol(&mut self, symbol: Symbol); + fn saved_symbol(&self) -> Option; +} diff --git a/src/ast/node/names.rs b/src/ast/node/names.rs new file mode 100644 index 0000000..7dc57a0 --- /dev/null +++ b/src/ast/node/names.rs @@ -0,0 +1,146 @@ +use std::borrow::Cow; +use crate::name_analysis::symbol::Symbol; +use std::range::Range; +use crate::ast::node::named::Named; + +#[derive(Debug)] +pub struct Identifier { + name: String, + file_id: usize, + range: Range, + scope_id: Option, + saved_symbol: Option, +} + +impl Identifier { + pub fn new(name: &str, file_id: usize, range: Range) -> Self { + Self { + name: name.to_string(), + file_id, + range, + scope_id: None, + saved_symbol: None, + } + } +} + +impl Named for Identifier { + fn name(&self) -> Cow<'_, str> { + Cow::Borrowed(&self.name) + } + + fn file_id(&self) -> usize { + self.file_id + } + + fn range(&self) -> Range { + self.range + } + + fn set_scope_id(&mut self, id: usize) { + self.scope_id = Some(id); + } + + fn scope_id(&self) -> Option { + self.scope_id + } + + fn set_saved_symbol(&mut self, saved_symbol: Symbol) { + self.saved_symbol = Some(saved_symbol); + } + + fn saved_symbol(&self) -> Option { + self.saved_symbol.clone() + } +} + +#[derive(Debug)] +pub struct FullyQualifiedName { + identifiers: Vec>, + file_id: usize, + range: Range, + scope_id: Option, + saved_symbol: Option, +} + +impl FullyQualifiedName { + pub fn new(identifiers: Vec>, file_id: usize, range: Range) -> Self { + Self { + identifiers, + range, + file_id, + scope_id: None, + saved_symbol: None, + } + } + + pub fn identifiers(&self) -> Vec<&Identifier> { + self.identifiers.iter().map(Box::as_ref).collect() + } + + pub fn identifiers_mut(&mut self) -> Vec<&mut Identifier> { + self.identifiers.iter_mut().map(Box::as_mut).collect() + } + + #[deprecated(note = "Grammar will instead distinguish between fqns and identifiers")] + pub fn last(&self) -> &Identifier { + &self.identifiers[self.identifiers.len() - 1] + } + + #[deprecated(note = "Grammar will instead distinguish between fqns and identifiers")] + pub fn last_mut(&mut self) -> &mut Identifier { + let last_index = self.identifiers.len() - 1; + &mut self.identifiers[last_index] + } + + #[deprecated(note = "Grammar will instead distinguish between fqns and identifiers")] + pub fn len(&self) -> usize { + self.identifiers.len() + } + + #[deprecated(note = "Grammar will instead distinguish between fqns and identifiers")] + pub fn is_single_identifier(&self) -> bool { + self.identifiers.len() == 1 + } +} + +impl Named for FullyQualifiedName { + fn name(&self) -> Cow<'_, str> { + if self.identifiers.len() == 1 { + self.identifiers[0].name() + } else { + let mut acc = String::new(); + for (i, identifier) in self.identifiers.iter().enumerate() { + acc += &identifier.name(); + if i < self.identifiers.len() - 1 { + acc += "::"; + } + } + Cow::Owned(acc) + } + } + + fn file_id(&self) -> usize { + self.file_id + } + + fn range(&self) -> Range { + self.range + } + + fn set_scope_id(&mut self, id: usize) { + self.scope_id = Some(id); + } + + fn scope_id(&self) -> Option { + self.scope_id + } + + fn set_saved_symbol(&mut self, symbol: Symbol) { + self.saved_symbol = Some(symbol); + } + + fn saved_symbol(&self) -> Option { + self.saved_symbol.clone() + } +} diff --git a/src/ast/node/object_access.rs b/src/ast/node/object_access.rs new file mode 100644 index 0000000..4e567da --- /dev/null +++ b/src/ast/node/object_access.rs @@ -0,0 +1,56 @@ +use crate::ast::node::expression::Expression; +use crate::ast::node::names::Identifier; + +#[derive(Debug)] +pub struct ObjectAccess { + receiver: Box, + navigations: Box, +} + +impl ObjectAccess { + pub fn new(receiver: Box, navigations: Box) -> Self { + Self { + receiver, + navigations, + } + } + + pub fn receiver(&self) -> &Expression { + &self.receiver + } + + pub fn receiver_mut(&mut self) -> &mut Expression { + &mut self.receiver + } + + pub fn navigations(&self) -> &ObjectNavigations { + &self.navigations + } + + pub fn navigations_mut(&mut self) -> &mut ObjectNavigations { + &mut self.navigations + } +} + +#[derive(Debug)] +pub struct ObjectNavigations(Vec>); + +impl ObjectNavigations { + pub fn new(navigations: Vec>) -> Self { + Self(navigations) + } + + pub fn navigations(&self) -> Vec<&ObjectNavigation> { + self.0.iter().map(Box::as_ref).collect() + } + + pub fn navigations_mut(&mut self) -> Vec<&mut ObjectNavigation> { + self.0.iter_mut().map(Box::as_mut).collect() + } +} + +#[derive(Debug)] +pub enum ObjectNavigation { + Index(Box), + Identifier(Box), +} diff --git a/src/ast/node/operators.rs b/src/ast/node/operators.rs new file mode 100644 index 0000000..463a621 --- /dev/null +++ b/src/ast/node/operators.rs @@ -0,0 +1,44 @@ +#[derive(Debug)] +pub enum Operator { + Binary(BinaryOperator), + PrefixUnary(PrefixUnaryOperator), + SuffixUnary(SuffixUnaryOperator), +} + +#[derive(Debug)] +pub enum BinaryOperator { + Or, + And, + EqualTo, + NotEqualTo, + Greater, + Less, + GreaterEqual, + LessEqual, + Add, + Subtract, + Multiply, + Divide, + Modulo, + LeftShift, + RightShift, +} + +#[derive(Debug)] +pub enum PrefixUnaryOperator { + Spread, + BorrowMut, + Borrow, + Star, + Mut, + Not, + Negative, +} + +#[derive(Debug)] +pub enum SuffixUnaryOperator { + PlusPlus, + MinusMinus, + Call, + Index, +} diff --git a/src/ast/node/statement.rs b/src/ast/node/statement.rs new file mode 100644 index 0000000..b605c91 --- /dev/null +++ b/src/ast/node/statement.rs @@ -0,0 +1,384 @@ +use crate::ast::node::expression::Expression; +use crate::ast::node::names::Identifier; +use crate::ast::node::type_use::TypeUse; + +#[derive(Debug)] +pub struct BlockStatement { + statements: Vec>, + expression: Option>, +} + +impl BlockStatement { + pub fn new(statements: Vec>, expression: Option>) -> Self { + Self { + statements, + expression, + } + } + + pub fn statements(&self) -> Vec<&Statement> { + self.statements.iter().map(AsRef::as_ref).collect() + } + + pub fn statements_mut(&mut self) -> Vec<&mut Statement> { + self.statements.iter_mut().map(AsMut::as_mut).collect() + } + + pub fn expression(&self) -> Option<&Expression> { + if let Some(ref expression) = self.expression { + Some(expression.as_ref()) + } else { + None + } + } + + pub fn expression_mut(&mut self) -> Option<&mut Expression> { + if let Some(ref mut expression) = self.expression { + Some(expression.as_mut()) + } else { + None + } + } +} + +#[derive(Debug)] +pub enum Statement { + BlockStatement(Box), + VariableDeclarationStatement(Box), + AssignStatement(Box), + CallStatement(Box), + ReturnStatement(Box), + IfStatement(Box), + IfElseStatement(Box), + WhileStatement(Box), + ForStatement(Box), +} + +#[derive(Debug)] +pub struct VariableDeclarationStatement { + is_mutable: bool, + identifier: Box, + declared_type: Option>, + initializer: Option>, +} + +impl VariableDeclarationStatement { + pub fn new( + is_mutable: bool, + identifier: Box, + declared_type: Option>, + initializer: Option>, + ) -> Self { + Self { + is_mutable, + identifier, + declared_type, + initializer, + } + } + + pub fn is_mutable(&self) -> bool { + self.is_mutable + } + + pub fn identifier(&self) -> &Identifier { + &self.identifier + } + + pub fn identifier_mut(&mut self) -> &mut Identifier { + &mut self.identifier + } + + pub fn declared_type(&self) -> Option<&TypeUse> { + if let Some(type_use) = &self.declared_type { + Some(type_use.as_ref()) + } else { + None + } + } + + pub fn declared_type_mut(&mut self) -> Option<&mut TypeUse> { + if let Some(type_use) = &mut self.declared_type { + Some(type_use.as_mut()) + } else { + None + } + } + + pub fn initializer(&self) -> Option<&Expression> { + if let Some(initializer) = &self.initializer { + Some(initializer.as_ref()) + } else { + None + } + } + + pub fn initializer_mut(&mut self) -> Option<&mut Expression> { + if let Some(initializer) = &mut self.initializer { + Some(initializer.as_mut()) + } else { + None + } + } +} + +#[derive(Debug)] +pub struct AssignStatement { + lhs: Box, + rhs: Box, +} + +impl AssignStatement { + pub fn new(lhs: Box, rhs: Box) -> Self { + Self { lhs, rhs } + } + + pub fn lhs(&self) -> &Expression { + &self.lhs + } + + pub fn lhs_mut(&mut self) -> &mut Expression { + &mut self.lhs + } + + pub fn rhs(&self) -> &Expression { + &self.rhs + } + + pub fn rhs_mut(&mut self) -> &mut Expression { + &mut self.rhs + } +} + +#[derive(Debug)] +pub struct CallStatement(Box); + +impl CallStatement { + pub fn new(call_expression: Box) -> Self { + Self(call_expression) + } + + pub fn expression(&self) -> &Expression { + &self.0 + } + + pub fn expression_mut(&mut self) -> &mut Expression { + &mut self.0 + } +} + +#[derive(Debug)] +pub struct ReturnStatement(Option>); + +impl ReturnStatement { + pub fn new(expression: Option>) -> Self { + Self(expression) + } + + pub fn expression(&self) -> Option<&Expression> { + if let Some(expression) = &self.0 { + Some(expression.as_ref()) + } else { + None + } + } + + pub fn expression_mut(&mut self) -> Option<&mut Expression> { + if let Some(expression) = &mut self.0 { + Some(expression.as_mut()) + } else { + None + } + } +} + +#[derive(Debug)] +pub struct IfStatement { + condition: Box, + then_block: Box, +} + +impl IfStatement { + pub fn new(condition: Box, then_block: Box) -> Self { + Self { + condition, + then_block, + } + } + + pub fn condition(&self) -> &Expression { + &self.condition + } + + pub fn condition_mut(&mut self) -> &mut Expression { + &mut self.condition + } + + pub fn then_block(&self) -> &BlockStatement { + &self.then_block + } + + pub fn then_block_mut(&mut self) -> &mut BlockStatement { + &mut self.then_block + } +} + +#[derive(Debug)] +pub struct IfElseStatement { + if_statement: Box, + else_ifs: Box, + else_block: Option>, +} + +impl IfElseStatement { + pub fn new( + if_statement: Box, + else_ifs: Box, + else_block: Option>, + ) -> Self { + Self { + if_statement, + else_ifs, + else_block, + } + } + + pub fn if_statement(&self) -> &IfStatement { + &self.if_statement + } + + pub fn if_statement_mut(&mut self) -> &mut IfStatement { + &mut self.if_statement + } + + pub fn else_ifs(&self) -> &ElseIfs { + &self.else_ifs + } + + pub fn else_ifs_mut(&mut self) -> &mut ElseIfs { + &mut self.else_ifs + } + + pub fn else_block(&self) -> Option<&ElseBlock> { + if let Some(else_block) = &self.else_block { + Some(else_block.as_ref()) + } else { + None + } + } + + pub fn else_block_mut(&mut self) -> Option<&mut ElseBlock> { + if let Some(else_block) = &mut self.else_block { + Some(else_block.as_mut()) + } else { + None + } + } +} + +#[derive(Debug, Default)] +pub struct ElseIfs(Vec>); + +impl ElseIfs { + pub fn new(if_statements: Vec>) -> Self { + Self(if_statements) + } + + pub fn if_statements(&self) -> Vec<&IfStatement> { + self.0.iter().map(Box::as_ref).collect() + } + + pub fn if_statements_mut(&mut self) -> Vec<&mut IfStatement> { + self.0.iter_mut().map(Box::as_mut).collect() + } +} + +#[derive(Debug)] +pub struct ElseBlock(Box); + +impl ElseBlock { + pub fn new(block: Box) -> Self { + Self(block) + } + + pub fn block_statement(&self) -> &BlockStatement { + &self.0 + } + + pub fn block_statement_mut(&mut self) -> &mut BlockStatement { + &mut self.0 + } +} + +#[derive(Debug)] +pub struct WhileStatement { + condition: Box, + body: Box, +} + +impl WhileStatement { + pub fn new(condition: Box, body: Box) -> Self { + Self { condition, body } + } + + pub fn condition(&self) -> &Expression { + &self.condition + } + + pub fn condition_mut(&mut self) -> &mut Expression { + &mut self.condition + } + + pub fn body(&self) -> &BlockStatement { + &self.body + } + + pub fn body_mut(&mut self) -> &mut BlockStatement { + &mut self.body + } +} + +#[derive(Debug)] +pub struct ForStatement { + variable: Box, + iterator: Box, + body: Box, +} + +impl ForStatement { + pub fn new( + variable: Box, + iterator: Box, + body: Box, + ) -> Self { + Self { + variable, + iterator, + body, + } + } + + pub fn variable(&self) -> &Identifier { + &self.variable + } + + pub fn variable_mut(&mut self) -> &mut Identifier { + &mut self.variable + } + + pub fn iterator(&self) -> &Expression { + &self.iterator + } + + pub fn iterator_mut(&mut self) -> &mut Expression { + &mut self.iterator + } + + pub fn body(&self) -> &BlockStatement { + &self.body + } + + pub fn body_mut(&mut self) -> &mut BlockStatement { + &mut self.body + } +} diff --git a/src/ast/node/tuple_arguments.rs b/src/ast/node/tuple_arguments.rs new file mode 100644 index 0000000..66da1f2 --- /dev/null +++ b/src/ast/node/tuple_arguments.rs @@ -0,0 +1,22 @@ +use crate::ast::node::type_use::TypeUse; + +#[derive(Debug)] +pub struct TupleArguments(Vec>); + +impl TupleArguments { + pub fn new(arguments: Vec>) -> Self { + Self(arguments) + } + + pub fn is_empty(&self) -> bool { + self.0.is_empty() + } + + pub fn type_uses(&self) -> Vec<&TypeUse> { + self.0.iter().map(Box::as_ref).collect() + } + + pub fn type_uses_mut(&mut self) -> Vec<&mut TypeUse> { + self.0.iter_mut().map(Box::as_mut).collect() + } +} diff --git a/src/ast/node/type_use.rs b/src/ast/node/type_use.rs new file mode 100644 index 0000000..b27a88d --- /dev/null +++ b/src/ast/node/type_use.rs @@ -0,0 +1,167 @@ +use crate::ast::node::function::{FunctionTypeModifier, Parameters, ReturnType}; +use crate::ast::node::generics::{GenericArguments, GenericParameters}; +use crate::ast::node::names::FullyQualifiedName; +use crate::ast::node::tuple_arguments::TupleArguments; + +#[derive(Debug)] +pub enum TypeUse { + Primitive(Box), + InterfaceOrClass(Box), + Tuple(Box), + Function(Box), +} + +#[derive(Debug)] +pub enum PrimitiveTypeUse { + Byte, + Short, + Char, + Int, + Long, + Double, + Bool, + String, + Array(Option>), + Any, + Void, +} + +#[derive(Debug)] +pub struct InterfaceOrClassTypeUse { + borrow_count: usize, + is_mutable: bool, + fqn: Box, + generics: Box, +} + +impl InterfaceOrClassTypeUse { + pub fn new( + borrow_count: usize, + is_mutable: bool, + fqn: Box, + generics: Box, + ) -> Self { + Self { + borrow_count, + is_mutable, + fqn, + generics, + } + } + + pub fn borrow_count(&self) -> usize { + self.borrow_count + } + + pub fn is_mutable(&self) -> bool { + self.is_mutable + } + + pub fn fqn(&self) -> &FullyQualifiedName { + &self.fqn + } + + pub fn fqn_mut(&mut self) -> &mut FullyQualifiedName { + &mut self.fqn + } + + pub fn generics(&self) -> &GenericArguments { + &self.generics + } + + pub fn generics_mut(&mut self) -> &mut GenericArguments { + &mut self.generics + } +} + +#[derive(Debug)] +pub struct TupleTypeUse { + borrow_count: usize, + is_mutable: bool, + arguments: Box, +} + +impl TupleTypeUse { + pub fn new(borrow_count: usize, is_mutable: bool, arguments: Box) -> Self { + Self { + borrow_count, + is_mutable, + arguments, + } + } + + pub fn borrow_count(&self) -> usize { + self.borrow_count + } + + pub fn is_mutable(&self) -> bool { + self.is_mutable + } + + pub fn arguments(&self) -> &TupleArguments { + &self.arguments + } + + pub fn arguments_mut(&mut self) -> &mut TupleArguments { + &mut self.arguments + } +} + +#[derive(Debug)] +pub struct FunctionTypeUse { + borrow_count: usize, + function_modifier: Option, + generics: Box, + parameters: Box, + return_type: Box, +} + +impl FunctionTypeUse { + pub fn new( + borrow_count: usize, + function_modifier: Option, + generics: Box, + parameters: Box, + return_type: Box, + ) -> Self { + Self { + borrow_count, + function_modifier, + generics, + parameters, + return_type, + } + } + + pub fn borrow_count(&self) -> usize { + self.borrow_count + } + + pub fn function_modifier(&self) -> Option<&FunctionTypeModifier> { + self.function_modifier.as_ref() + } + + pub fn generics(&self) -> &GenericParameters { + &self.generics + } + + pub fn generics_mut(&mut self) -> &mut GenericParameters { + &mut self.generics + } + + pub fn parameters(&self) -> &Box { + &self.parameters + } + + pub fn parameters_mut(&mut self) -> &mut Box { + &mut self.parameters + } + + pub fn return_type(&self) -> &ReturnType { + &self.return_type + } + + pub fn return_type_mut(&mut self) -> &mut ReturnType { + &mut self.return_type + } +} diff --git a/src/ast/node/use_statement.rs b/src/ast/node/use_statement.rs new file mode 100644 index 0000000..b018e62 --- /dev/null +++ b/src/ast/node/use_statement.rs @@ -0,0 +1,87 @@ +use crate::ast::node::named::Named; +use crate::ast::node::names::Identifier; +use std::borrow::Cow; +use std::range::Range; + +#[derive(Debug)] +pub struct UseStatement { + identifiers: Vec>, + last: Box, + file_id: usize, + range: Range, +} + +impl UseStatement { + pub fn new( + identifiers: Vec>, + last: Box, + file_id: usize, + range: Range, + ) -> Self { + UseStatement { + identifiers, + last, + file_id, + range, + } + } + + pub fn base_name(&self) -> Cow<'_, str> { + use UseStatementLast::*; + if self.identifiers.is_empty() { + match self.last.as_ref() { + Identifier(_) => Cow::from(""), + Star | Identifiers(_) => panic!(), // should never get here because of grammar + } + } else if self.identifiers.len() == 1 { + self.identifiers[0].name() + } else { + let mut acc = String::new(); + for (i, identifier) in self.identifiers.iter().enumerate() { + acc.push_str(&identifier.name()); + if i != self.identifiers.len() - 1 { + acc.push_str("::"); + } + } + Cow::from(acc) + } + } + + pub fn is_star(&self) -> bool { + match self.last.as_ref() { + UseStatementLast::Star => true, + _ => false, + } + } + + pub fn identifiers(&self) -> Vec<&Identifier> { + self.identifiers.iter().map(Box::as_ref).collect() + } + + pub fn identifiers_mut(&mut self) -> Vec<&mut Identifier> { + self.identifiers.iter_mut().map(Box::as_mut).collect() + } + + pub fn last(&self) -> &UseStatementLast { + &self.last + } + + pub fn last_mut(&mut self) -> &mut UseStatementLast { + &mut self.last + } + + pub fn file_id(&self) -> usize { + self.file_id + } + + pub fn range(&self) -> Range { + self.range + } +} + +#[derive(Debug)] +pub enum UseStatementLast { + Identifier(Box), + Identifiers(Vec>), + Star, +} diff --git a/src/ast/pretty_print.rs b/src/ast/pretty_print.rs index d2d756f..108e60e 100644 --- a/src/ast/pretty_print.rs +++ b/src/ast/pretty_print.rs @@ -1,7 +1,27 @@ -use crate::ast::*; +use crate::ast::node::call_expression::*; +use crate::ast::node::class::*; +use crate::ast::node::closure::*; +use crate::ast::node::compilation_unit::*; +use crate::ast::node::d_string::*; +use crate::ast::node::expression::*; +use crate::ast::node::function::*; +use crate::ast::node::generics::*; +use crate::ast::node::implements_list::*; +use crate::ast::node::interface::*; +use crate::ast::node::level::*; +use crate::ast::node::literal::*; +use crate::ast::node::module::*; +use crate::ast::node::named::Named; +use crate::ast::node::names::*; +use crate::ast::node::object_access::*; +use crate::ast::node::operators::*; +use crate::ast::node::statement::*; +use crate::ast::node::tuple_arguments::*; +use crate::ast::node::type_use::*; +use crate::ast::node::use_statement::*; +use crate::ast::unparse::Unparse; use crate::util::indent_writer::IndentWriter; use std::fmt::Debug; -use crate::ast::unparse::Unparse; pub trait PrettyPrint { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()>; @@ -9,7 +29,7 @@ pub trait PrettyPrint { impl PrettyPrint for Operator { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use Operator::*; + use crate::ast::node::operators::Operator::*; match self { Binary(o) => o.pretty_print(writer), PrefixUnary(o) => o.pretty_print(writer), @@ -47,7 +67,7 @@ impl PrettyPrint for SuffixUnaryOperator { impl PrettyPrint for Identifier { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - writer.writeln_indented(&format!("Identifier({})", self.name)) + writer.writeln_indented(&format!("Identifier({})", self.name())) } } @@ -55,7 +75,7 @@ impl PrettyPrint for FullyQualifiedName { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("FullyQualifiedName")?; writer.increase_indent(); - for identifier in &self.identifiers { + for identifier in self.identifiers() { identifier.pretty_print(writer)?; } writer.decrease_indent(); @@ -65,7 +85,7 @@ impl PrettyPrint for FullyQualifiedName { impl PrettyPrint for TypeUse { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use TypeUse::*; + use crate::ast::node::type_use::TypeUse::*; match self { Primitive(primitive_type_use) => { writer.writeln_indented("PrimitiveTypeUse")?; @@ -98,7 +118,7 @@ impl PrettyPrint for TypeUse { impl PrettyPrint for PrimitiveTypeUse { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use PrimitiveTypeUse::*; + use crate::ast::node::type_use::PrimitiveTypeUse::*; match self { Byte => writer.writeln_indented("Byte"), Short => writer.writeln_indented("Short"), @@ -127,11 +147,12 @@ impl PrettyPrint for InterfaceOrClassTypeUse { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented(&format!( "InterfaceOrClassTypeUse(borrow_count = {}, is_mutable = {})", - self.borrow_count, self.is_mutable + self.borrow_count(), + self.is_mutable() ))?; writer.increase_indent(); - self.fqn.pretty_print(writer)?; - self.generics.pretty_print(writer)?; + self.fqn().pretty_print(writer)?; + self.generics().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -141,10 +162,11 @@ impl PrettyPrint for TupleTypeUse { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented(&format!( "TupleTypeUse(borrow_count = {}, is_mutable = {})", - self.borrow_count, self.is_mutable + self.borrow_count(), + self.is_mutable() ))?; writer.increase_indent(); - self.arguments.pretty_print(writer)?; + self.arguments().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -154,15 +176,15 @@ impl PrettyPrint for FunctionTypeUse { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented(&format!( "FunctionTypeUse(borrow_count = {})", - self.borrow_count + self.borrow_count() ))?; writer.increase_indent(); - if let Some(function_type_modifier) = &self.function_modifier { + if let Some(function_type_modifier) = self.function_modifier() { function_type_modifier.pretty_print(writer)?; } - self.generics.pretty_print(writer)?; - self.parameters.pretty_print(writer)?; - self.return_type.pretty_print(writer)?; + self.generics().pretty_print(writer)?; + self.parameters().pretty_print(writer)?; + self.return_type().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -172,7 +194,7 @@ impl PrettyPrint for GenericArguments { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("GenericArguments")?; writer.increase_indent(); - for type_use in &self.0 { + for type_use in self.arguments() { type_use.pretty_print(writer)?; } writer.decrease_indent(); @@ -184,7 +206,7 @@ impl PrettyPrint for GenericParameters { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("GenericParameters")?; writer.increase_indent(); - for identifier in &self.0 { + for identifier in self.identifiers() { identifier.pretty_print(writer)?; } writer.decrease_indent(); @@ -196,7 +218,7 @@ impl PrettyPrint for TupleArguments { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("TupleArguments")?; writer.increase_indent(); - for type_use in &self.0 { + for type_use in self.type_uses() { type_use.pretty_print(writer)?; } writer.decrease_indent(); @@ -208,7 +230,7 @@ impl PrettyPrint for ImplementsList { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("ImplementsList")?; writer.increase_indent(); - for type_use in &self.0 { + for type_use in self.type_uses() { type_use.pretty_print(writer)?; } writer.decrease_indent(); @@ -218,7 +240,7 @@ impl PrettyPrint for ImplementsList { impl PrettyPrint for FunctionTypeModifier { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use FunctionTypeModifier::*; + use crate::ast::node::function::FunctionTypeModifier::*; writer.writeln_indented(&format!( "FunctionTypeModifier({})", match self { @@ -235,7 +257,7 @@ impl PrettyPrint for Parameters { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("Parameters")?; writer.increase_indent(); - for parameter in &self.0 { + for parameter in self.parameters() { parameter.pretty_print(writer)?; } writer.decrease_indent(); @@ -247,8 +269,8 @@ impl PrettyPrint for Parameter { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("Parameter")?; writer.increase_indent(); - self.identifier.pretty_print(writer)?; - self.type_use.pretty_print(writer)?; + self.identifier().pretty_print(writer)?; + self.type_use().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -258,8 +280,8 @@ impl PrettyPrint for ReturnType { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("ReturnType")?; writer.increase_indent(); - self.declared_type.pretty_print(writer)?; - self.references.pretty_print(writer)?; + self.declared_type().pretty_print(writer)?; + self.references().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -269,7 +291,7 @@ impl PrettyPrint for References { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("References")?; writer.increase_indent(); - for identifier in &self.0 { + for identifier in self.identifiers() { identifier.pretty_print(writer)?; } writer.decrease_indent(); @@ -281,16 +303,16 @@ impl PrettyPrint for CompilationUnit { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("CompilationUnit")?; writer.increase_indent(); - if let Some(namespace) = &self.namespace { + if let Some(namespace) = self.namespace() { writer.writeln_indented("Namespace")?; writer.increase_indent(); namespace.pretty_print(writer)?; writer.decrease_indent(); } - for use_statement in &self.use_statements { + for use_statement in self.use_statements() { use_statement.pretty_print(writer)?; } - for declaration in &self.declarations { + for declaration in self.declarations() { declaration.pretty_print(writer)?; } writer.decrease_indent(); @@ -300,7 +322,7 @@ impl PrettyPrint for CompilationUnit { impl PrettyPrint for ModuleLevelDeclaration { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use ModuleLevelDeclaration::*; + use crate::ast::node::level::ModuleLevelDeclaration::*; match self { Module(module) => module.pretty_print(writer), Interface(interface) => interface.pretty_print(writer), @@ -313,7 +335,7 @@ impl PrettyPrint for ModuleLevelDeclaration { impl PrettyPrint for InterfaceLevelDeclaration { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use InterfaceLevelDeclaration::*; + use crate::ast::node::level::InterfaceLevelDeclaration::*; match self { Module(module) => module.pretty_print(writer), Interface(interface) => interface.pretty_print(writer), @@ -326,7 +348,7 @@ impl PrettyPrint for InterfaceLevelDeclaration { impl PrettyPrint for ClassLevelDeclaration { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use ClassLevelDeclaration::*; + use crate::ast::node::level::ClassLevelDeclaration::*; match self { Module(module) => module.pretty_print(writer), Interface(interface) => interface.pretty_print(writer), @@ -344,11 +366,11 @@ impl PrettyPrint for ModuleDeclaration { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented(&format!( "ModuleDeclaration(is_public = {})", - self.is_public + self.is_public() ))?; writer.increase_indent(); - self.identifier.pretty_print(writer)?; - for declaration in &self.declarations { + self.identifier().pretty_print(writer)?; + for declaration in self.declarations() { declaration.pretty_print(writer)?; } writer.decrease_indent(); @@ -360,13 +382,13 @@ impl PrettyPrint for InterfaceDeclaration { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented(&format!( "InterfaceDeclaration(is_public = {})", - self.is_public + self.is_public() ))?; writer.increase_indent(); - self.identifier.pretty_print(writer)?; - self.generics.pretty_print(writer)?; - self.implements.pretty_print(writer)?; - for declaration in &self.declarations { + self.identifier().pretty_print(writer)?; + self.generics().pretty_print(writer)?; + self.implements().pretty_print(writer)?; + for declaration in self.declarations() { declaration.pretty_print(writer)?; } writer.decrease_indent(); @@ -376,15 +398,18 @@ impl PrettyPrint for InterfaceDeclaration { impl PrettyPrint for ClassDeclaration { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - writer.writeln_indented(&format!("ClassDeclaration(is_public = {})", self.is_public))?; + writer.writeln_indented(&format!( + "ClassDeclaration(is_public = {})", + self.is_public() + ))?; writer.increase_indent(); - self.identifier.pretty_print(writer)?; - self.generics.pretty_print(writer)?; - if let Some(class_constructor) = &self.class_constructor { + self.identifier().pretty_print(writer)?; + self.generics().pretty_print(writer)?; + if let Some(class_constructor) = self.class_constructor() { class_constructor.pretty_print(writer)?; } - self.implements.pretty_print(writer)?; - for declaration in &self.declarations { + self.implements().pretty_print(writer)?; + for declaration in self.declarations() { declaration.pretty_print(writer)?; } writer.decrease_indent(); @@ -396,17 +421,17 @@ impl PrettyPrint for FunctionDefinition { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented(&format!( "FunctionDefinition(is_public = {})", - self.is_public + self.is_public() ))?; writer.increase_indent(); - if let Some(modifier) = &self.modifier { + if let Some(modifier) = self.modifier() { modifier.pretty_print(writer)?; } - self.generics.pretty_print(writer)?; - self.identifier.pretty_print(writer)?; - self.parameters.pretty_print(writer)?; - self.return_type.pretty_print(writer)?; - self.body.pretty_print(writer)?; + self.generics().pretty_print(writer)?; + self.identifier().pretty_print(writer)?; + self.parameters().pretty_print(writer)?; + self.return_type().pretty_print(writer)?; + self.body().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -416,17 +441,17 @@ impl PrettyPrint for OperatorFunctionDefinition { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented(&format!( "OperatorFunctionDefinition(is_public = {})", - self.is_public + self.is_public() ))?; writer.increase_indent(); - if let Some(modifier) = &self.modifier { + if let Some(modifier) = self.modifier() { modifier.pretty_print(writer)?; } - self.generics.pretty_print(writer)?; - self.operator.pretty_print(writer)?; - self.parameters.pretty_print(writer)?; - self.return_type.pretty_print(writer)?; - self.body.pretty_print(writer)?; + self.generics().pretty_print(writer)?; + self.operator().pretty_print(writer)?; + self.parameters().pretty_print(writer)?; + self.return_type().pretty_print(writer)?; + self.body().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -436,16 +461,16 @@ impl PrettyPrint for PlatformFunctionDeclaration { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented(&format!( "PlatformFunctionDeclaration(is_public = {})", - self.is_public + self.is_public() ))?; writer.increase_indent(); - if let Some(modifier) = &self.modifier { + if let Some(modifier) = self.modifier() { modifier.pretty_print(writer)?; } - self.generics.pretty_print(writer)?; - self.identifier.pretty_print(writer)?; - self.parameters.pretty_print(writer)?; - self.return_type.pretty_print(writer)?; + self.generics().pretty_print(writer)?; + self.identifier().pretty_print(writer)?; + self.parameters().pretty_print(writer)?; + self.return_type().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -455,14 +480,14 @@ impl PrettyPrint for InterfaceFunctionDeclaration { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("InterfaceFunctionDeclaration")?; writer.increase_indent(); - if let Some(modifier) = &self.modifier { + if let Some(modifier) = self.modifier() { modifier.pretty_print(writer)?; } - self.generics.pretty_print(writer)?; - self.identifier.pretty_print(writer)?; - self.parameters.pretty_print(writer)?; - self.return_type.pretty_print(writer)?; - if let Some(body) = &self.body { + self.generics().pretty_print(writer)?; + self.identifier().pretty_print(writer)?; + self.parameters().pretty_print(writer)?; + self.return_type().pretty_print(writer)?; + if let Some(body) = self.body() { body.pretty_print(writer)?; } writer.decrease_indent(); @@ -474,14 +499,14 @@ impl PrettyPrint for InterfaceOperatorFunctionDeclaration { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("InterfaceOperatorFunctionDeclaration")?; writer.increase_indent(); - if let Some(modifier) = &self.modifier { + if let Some(modifier) = self.modifier() { modifier.pretty_print(writer)?; } - self.generics.pretty_print(writer)?; - self.operator.pretty_print(writer)?; - self.parameters.pretty_print(writer)?; - self.return_type.pretty_print(writer)?; - if let Some(body) = &self.body { + self.generics().pretty_print(writer)?; + self.operator().pretty_print(writer)?; + self.parameters().pretty_print(writer)?; + self.return_type().pretty_print(writer)?; + if let Some(body) = self.body() { body.pretty_print(writer)?; } writer.decrease_indent(); @@ -491,7 +516,7 @@ impl PrettyPrint for InterfaceOperatorFunctionDeclaration { impl PrettyPrint for FunctionModifier { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use FunctionModifier::*; + use crate::ast::node::function::FunctionModifier::*; writer.writeln_indented(&format!( "FunctionModifier({})", match self { @@ -507,7 +532,7 @@ impl PrettyPrint for FunctionModifier { impl PrettyPrint for FunctionBody { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use FunctionBody::*; + use crate::ast::node::function::FunctionBody::*; match self { Equals(expression) => { writer.writeln_indented("EqualsFunctionBody")?; @@ -536,7 +561,7 @@ impl PrettyPrint for ClassConstructor { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("ClassConstructor")?; writer.increase_indent(); - for constructor_parameter in &self.0 { + for constructor_parameter in self.parameters() { constructor_parameter.pretty_print(writer)?; } writer.decrease_indent(); @@ -546,7 +571,7 @@ impl PrettyPrint for ClassConstructor { impl PrettyPrint for ClassConstructorParameter { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use ClassConstructorParameter::*; + use crate::ast::node::class::ClassConstructorParameter::*; match self { Property(property) => { writer.writeln_indented("PropertyConstructorParameter")?; @@ -569,11 +594,11 @@ impl PrettyPrint for PropertyDeclaration { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented(&format!( "PropertyDeclaration(is_mutable = {})", - self.is_mutable + self.is_mutable() ))?; writer.increase_indent(); - self.identifier.pretty_print(writer)?; - self.declared_type.pretty_print(writer)?; + self.identifier().pretty_print(writer)?; + self.declared_type().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -583,11 +608,11 @@ impl PrettyPrint for FieldDeclaration { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented(&format!( "FieldDeclaration(is_mutable = {})", - self.is_mutable + self.is_mutable() ))?; writer.increase_indent(); - self.identifier.pretty_print(writer)?; - self.declared_type.pretty_print(writer)?; + self.identifier().pretty_print(writer)?; + self.declared_type().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -597,10 +622,10 @@ impl PrettyPrint for UseStatement { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("UseStatement")?; writer.increase_indent(); - for identifier in &self.identifiers { + for identifier in self.identifiers() { identifier.pretty_print(writer)?; } - self.last.pretty_print(writer)?; + self.last().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -630,10 +655,10 @@ impl PrettyPrint for BlockStatement { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("BlockStatement")?; writer.increase_indent(); - for statement in &self.statements { + for statement in self.statements() { statement.pretty_print(writer)?; } - if let Some(expression) = &self.expression { + if let Some(expression) = self.expression() { expression.pretty_print(writer)?; } Ok(()) @@ -642,7 +667,7 @@ impl PrettyPrint for BlockStatement { impl PrettyPrint for Statement { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use Statement::*; + use crate::ast::node::statement::Statement::*; match self { BlockStatement(block_statement) => block_statement.pretty_print(writer), VariableDeclarationStatement(s) => s.pretty_print(writer), @@ -661,14 +686,14 @@ impl PrettyPrint for VariableDeclarationStatement { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented(&format!( "VariableDeclarationStatement(is_mutable = {})", - self.is_mutable + self.is_mutable() ))?; writer.increase_indent(); - self.identifier.pretty_print(writer)?; - if let Some(declared_type) = &self.declared_type { + self.identifier().pretty_print(writer)?; + if let Some(declared_type) = self.declared_type() { declared_type.pretty_print(writer)?; } - if let Some(initializer) = &self.initializer { + if let Some(initializer) = self.initializer() { initializer.pretty_print(writer)?; } writer.decrease_indent(); @@ -680,8 +705,8 @@ impl PrettyPrint for AssignStatement { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("AssignStatement")?; writer.increase_indent(); - self.lhs.pretty_print(writer)?; - self.rhs.pretty_print(writer)?; + self.lhs().pretty_print(writer)?; + self.rhs().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -691,7 +716,7 @@ impl PrettyPrint for CallStatement { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("CallStatement")?; writer.increase_indent(); - self.0.pretty_print(writer)?; + self.expression().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -700,7 +725,7 @@ impl PrettyPrint for CallStatement { impl PrettyPrint for ReturnStatement { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("ReturnStatement")?; - if let Some(expression) = &self.0 { + if let Some(expression) = self.expression() { writer.increase_indent(); expression.pretty_print(writer)?; writer.decrease_indent(); @@ -714,8 +739,8 @@ impl PrettyPrint for IfStatement { writer.writeln_indented("IfStatement")?; writer.increase_indent(); writer.increase_indent(); - self.condition.pretty_print(writer)?; - self.then_block.pretty_print(writer)?; + self.condition().pretty_print(writer)?; + self.then_block().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -725,9 +750,9 @@ impl PrettyPrint for IfElseStatement { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("IfElseStatement")?; writer.increase_indent(); - self.if_statement.pretty_print(writer)?; - self.else_ifs.pretty_print(writer)?; - if let Some(else_block) = &self.else_block { + self.if_statement().pretty_print(writer)?; + self.else_ifs().pretty_print(writer)?; + if let Some(else_block) = self.else_block() { else_block.pretty_print(writer)?; } writer.decrease_indent(); @@ -739,7 +764,7 @@ impl PrettyPrint for ElseIfs { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("ElseIfs")?; writer.increase_indent(); - for if_statement in &self.0 { + for if_statement in self.if_statements() { if_statement.pretty_print(writer)?; } writer.decrease_indent(); @@ -751,7 +776,7 @@ impl PrettyPrint for ElseBlock { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("ElseBlock")?; writer.increase_indent(); - self.0.pretty_print(writer)?; + self.block_statement().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -761,8 +786,8 @@ impl PrettyPrint for WhileStatement { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("WhileStatement")?; writer.increase_indent(); - self.condition.pretty_print(writer)?; - self.body.pretty_print(writer)?; + self.condition().pretty_print(writer)?; + self.body().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -772,9 +797,9 @@ impl PrettyPrint for ForStatement { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("ForStatement")?; writer.increase_indent(); - self.variable.pretty_print(writer)?; - self.iterator.pretty_print(writer)?; - self.body.pretty_print(writer)?; + self.variable().pretty_print(writer)?; + self.iterator().pretty_print(writer)?; + self.body().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -782,7 +807,7 @@ impl PrettyPrint for ForStatement { impl PrettyPrint for Expression { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use Expression::*; + use crate::ast::node::expression::Expression::*; match self { Ternary(e) => e.pretty_print(writer), Binary(e) => e.pretty_print(writer), @@ -801,9 +826,9 @@ impl PrettyPrint for TernaryExpression { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("TernaryExpression")?; writer.increase_indent(); - self.condition.pretty_print(writer)?; - self.true_expression.pretty_print(writer)?; - self.false_expression.pretty_print(writer)?; + self.condition().pretty_print(writer)?; + self.true_expression().pretty_print(writer)?; + self.false_expression().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -813,9 +838,9 @@ impl PrettyPrint for BinaryExpression { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("BinaryExpression")?; writer.increase_indent(); - self.left.pretty_print(writer)?; - self.operator.pretty_print(writer)?; - self.right.pretty_print(writer)?; + self.left().pretty_print(writer)?; + self.operator().pretty_print(writer)?; + self.right().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -825,8 +850,8 @@ impl PrettyPrint for PrefixExpression { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("PrefixExpression")?; writer.increase_indent(); - self.operator.pretty_print(writer)?; - self.expression.pretty_print(writer)?; + self.operator().pretty_print(writer)?; + self.expression().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -836,8 +861,8 @@ impl PrettyPrint for SuffixExpression { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("SuffixExpression")?; writer.increase_indent(); - self.expression.pretty_print(writer)?; - self.operator.pretty_print(writer)?; + self.expression().pretty_print(writer)?; + self.operator().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -847,11 +872,11 @@ impl PrettyPrint for CallExpression { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("CallExpression")?; writer.increase_indent(); - self.callee.pretty_print(writer)?; - if let Some(turbo_fish) = &self.turbo_fish { + self.callee().pretty_print(writer)?; + if let Some(turbo_fish) = self.turbo_fish() { turbo_fish.pretty_print(writer)?; } - self.arguments.pretty_print(writer)?; + self.arguments().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -861,7 +886,7 @@ impl PrettyPrint for TurboFish { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("TurboFish")?; writer.increase_indent(); - self.0.pretty_print(writer)?; + self.generics().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -871,7 +896,7 @@ impl PrettyPrint for CallArguments { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("CallArguments")?; writer.increase_indent(); - for call_argument in &self.0 { + for call_argument in self.arguments() { call_argument.pretty_print(writer)?; } writer.decrease_indent(); @@ -883,7 +908,7 @@ impl PrettyPrint for CallArgument { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("CallArgument")?; writer.increase_indent(); - self.0.pretty_print(writer)?; + self.expression().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -891,22 +916,22 @@ impl PrettyPrint for CallArgument { impl PrettyPrint for Closure { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - writer.writeln_indented(&format!("Closure(is_move = {})", self.is_move))?; + writer.writeln_indented(&format!("Closure(is_move = {})", self.is_move()))?; writer.increase_indent(); - if let Some(modifier) = &self.modifier { + if let Some(modifier) = self.modifier() { modifier.pretty_print(writer)?; } - self.captures.pretty_print(writer)?; - self.parameters.pretty_print(writer)?; - if !self.statements.is_empty() { + self.captures().pretty_print(writer)?; + self.parameters().pretty_print(writer)?; + if !self.statements().is_empty() { writer.writeln_indented("ClosureStatements")?; writer.increase_indent(); - for statement in &self.statements { + for statement in self.statements() { statement.pretty_print(writer)?; } writer.decrease_indent(); } - if let Some(expression) = &self.expression { + if let Some(expression) = self.expression() { expression.pretty_print(writer)?; } writer.decrease_indent(); @@ -930,7 +955,7 @@ impl PrettyPrint for ClosureCaptures { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("ClosureCaptures")?; writer.increase_indent(); - for capture in &self.0 { + for capture in self.captures() { capture.pretty_print(writer)?; } writer.decrease_indent(); @@ -942,10 +967,11 @@ impl PrettyPrint for ClosureCapture { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented(&format!( "ClosureCapture(borrow_count = {}, is_mutable = {})", - self.borrow_count, self.is_mutable + self.borrow_count(), + self.is_mutable() ))?; writer.increase_indent(); - self.identifier.pretty_print(writer)?; + self.identifier().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -955,7 +981,7 @@ impl PrettyPrint for ClosureParameters { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("ClosureParameters")?; writer.increase_indent(); - for parameter in &self.0 { + for parameter in self.parameters() { parameter.pretty_print(writer)?; } writer.decrease_indent(); @@ -967,8 +993,8 @@ impl PrettyPrint for ClosureParameter { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("ClosureParameter")?; writer.increase_indent(); - self.identifier.pretty_print(writer)?; - if let Some(type_use) = &self.type_use { + self.identifier().pretty_print(writer)?; + if let Some(type_use) = self.type_use() { type_use.pretty_print(writer)?; } writer.decrease_indent(); @@ -980,8 +1006,8 @@ impl PrettyPrint for ObjectAccess { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("ObjectAccess")?; writer.increase_indent(); - self.receiver.pretty_print(writer)?; - self.navigations.pretty_print(writer)?; + self.receiver().pretty_print(writer)?; + self.navigations().pretty_print(writer)?; writer.decrease_indent(); Ok(()) } @@ -991,7 +1017,7 @@ impl PrettyPrint for ObjectNavigations { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("ObjectNavigations")?; writer.increase_indent(); - for object_navigation in &self.0 { + for object_navigation in self.navigations() { object_navigation.pretty_print(writer)?; } writer.decrease_indent(); @@ -1001,7 +1027,7 @@ impl PrettyPrint for ObjectNavigations { impl PrettyPrint for ObjectNavigation { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use ObjectNavigation::*; + use crate::ast::node::object_access::ObjectNavigation::*; match self { Index(e) => { writer.writeln_indented("Index")?; @@ -1022,7 +1048,7 @@ impl PrettyPrint for ObjectNavigation { impl PrettyPrint for Literal { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use Literal::*; + use crate::ast::node::literal::Literal::*; match self { Integer(i) => writer.writeln_indented(&format!("Integer({})", i)), Long(l) => writer.writeln_indented(&format!("Long({})", l)), @@ -1040,7 +1066,7 @@ impl PrettyPrint for DString { fn pretty_print(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.writeln_indented("DString")?; writer.increase_indent(); - for part in &self.0 { + for part in self.parts() { part.pretty_print(writer)?; } Ok(()) diff --git a/src/ast/unparse.rs b/src/ast/unparse.rs index 44ae4bd..58102b8 100644 --- a/src/ast/unparse.rs +++ b/src/ast/unparse.rs @@ -1,4 +1,24 @@ -use crate::ast::*; +use crate::ast::node::call_expression::*; +use crate::ast::node::class::*; +use crate::ast::node::closure::*; +use crate::ast::node::compilation_unit::*; +use crate::ast::node::d_string::*; +use crate::ast::node::expression::*; +use crate::ast::node::function::*; +use crate::ast::node::generics::*; +use crate::ast::node::implements_list::*; +use crate::ast::node::interface::*; +use crate::ast::node::level::*; +use crate::ast::node::literal::*; +use crate::ast::node::module::*; +use crate::ast::node::named::Named; +use crate::ast::node::names::*; +use crate::ast::node::object_access::*; +use crate::ast::node::operators::*; +use crate::ast::node::statement::*; +use crate::ast::node::tuple_arguments::*; +use crate::ast::node::type_use::*; +use crate::ast::node::use_statement::*; use crate::util::indent_writer::IndentWriter; macro_rules! to_unparse_vec { @@ -73,7 +93,7 @@ fn unparse_comma_list(writer: &mut IndentWriter, items: &[&dyn Unparse]) -> std: impl Unparse for Operator { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use Operator::*; + use crate::ast::node::operators::Operator::*; match self { Binary(op) => op.unparse(writer), PrefixUnary(op) => op.unparse(writer), @@ -84,7 +104,7 @@ impl Unparse for Operator { impl Unparse for BinaryOperator { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use BinaryOperator::*; + use crate::ast::node::operators::BinaryOperator::*; match self { Or => writer.write("||"), And => writer.write("&&"), @@ -107,7 +127,7 @@ impl Unparse for BinaryOperator { impl Unparse for PrefixUnaryOperator { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use PrefixUnaryOperator::*; + use crate::ast::node::operators::PrefixUnaryOperator::*; match self { Spread => writer.write("..."), BorrowMut => writer.write("&mut"), @@ -122,7 +142,7 @@ impl Unparse for PrefixUnaryOperator { impl Unparse for SuffixUnaryOperator { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use SuffixUnaryOperator::*; + use crate::ast::node::operators::SuffixUnaryOperator::*; match self { PlusPlus => writer.write("++"), MinusMinus => writer.write("--"), @@ -136,7 +156,7 @@ impl Unparse for SuffixUnaryOperator { impl Unparse for Identifier { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - writer.write(&self.name) + writer.write(&self.name()) } } @@ -154,7 +174,7 @@ impl ListUnparse for FullyQualifiedName { impl Unparse for TypeUse { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use TypeUse::*; + use crate::ast::node::type_use::TypeUse::*; match self { Void => writer.write("Void"), InterfaceOrClass(interface_or_class_type_use) => { @@ -302,7 +322,7 @@ impl ListUnparse for ImplementsList { impl Unparse for FunctionTypeModifier { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use FunctionTypeModifier::*; + use crate::ast::node::function::FunctionTypeModifier::*; match self { Cons => writer.write("cons"), MutRef => writer.write("mut ref"), @@ -426,7 +446,7 @@ impl Unparse for UseStatementLast { impl Unparse for ModuleLevelDeclaration { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use ModuleLevelDeclaration::*; + use crate::ast::node::level::ModuleLevelDeclaration::*; match self { Module(module_declaration) => module_declaration.unparse(writer), Interface(interface_declaration) => interface_declaration.unparse(writer), @@ -441,7 +461,7 @@ impl Unparse for ModuleLevelDeclaration { impl Unparse for InterfaceLevelDeclaration { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use InterfaceLevelDeclaration::*; + use crate::ast::node::level::InterfaceLevelDeclaration::*; match self { Module(module_declaration) => module_declaration.unparse(writer), Interface(interface_declaration) => interface_declaration.unparse(writer), @@ -458,7 +478,7 @@ impl Unparse for InterfaceLevelDeclaration { impl Unparse for ClassLevelDeclaration { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use ClassLevelDeclaration::*; + use crate::ast::node::level::ClassLevelDeclaration::*; match self { Module(module_declaration) => module_declaration.unparse(writer), Interface(interface_declaration) => interface_declaration.unparse(writer), @@ -672,7 +692,7 @@ impl Unparse for InterfaceOperatorFunctionDeclaration { impl Unparse for FunctionModifier { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use FunctionModifier::*; + use crate::ast::node::function::FunctionModifier::*; match self { Static => writer.write("static"), Cons => writer.write("cons"), @@ -687,7 +707,7 @@ impl Unparse for FunctionModifier { impl Unparse for FunctionBody { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use FunctionBody::*; + use crate::ast::node::function::FunctionBody::*; match self { Equals(expression) => { writer.write("= ")?; @@ -730,7 +750,7 @@ impl ListUnparse for ClassConstructor { impl Unparse for ClassConstructorParameter { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use ClassConstructorParameter::*; + use crate::ast::node::class::ClassConstructorParameter::*; match self { Property(property) => property.unparse(writer), Field(field) => field.unparse(writer), @@ -777,10 +797,10 @@ impl Unparse for BlockStatement { impl BlockStatement { fn unparse_inner(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.increase_indent(); - for statement in &self.statements { + for statement in self.statements() { statement.unparse(writer)?; } - if let Some(expression) = &self.expression { + if let Some(expression) = self.expression() { expression.unparse(writer)?; writer.writeln("")?; } @@ -791,7 +811,7 @@ impl BlockStatement { impl Unparse for Statement { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use Statement::*; + use crate::ast::node::statement::Statement::*; match self { BlockStatement(statement) => statement.unparse(writer), CallStatement(call) => call.unparse(writer), @@ -820,15 +840,15 @@ impl Unparse for Statement { impl Unparse for VariableDeclarationStatement { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.write_indented("let ")?; - if self.is_mutable { + if self.is_mutable() { writer.write("mut ")?; } - self.identifier.unparse(writer)?; - if let Some(declared_type) = &self.declared_type { + self.identifier().unparse(writer)?; + if let Some(declared_type) = self.declared_type() { writer.write(": ")?; declared_type.unparse(writer)?; } - if let Some(initializer) = &self.initializer { + if let Some(initializer) = self.initializer() { writer.write(" = ")?; initializer.unparse(writer)?; } @@ -839,9 +859,9 @@ impl Unparse for VariableDeclarationStatement { impl Unparse for AssignStatement { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.write_indented("")?; - self.lhs.unparse(writer)?; + self.lhs().unparse(writer)?; writer.write(" = ")?; - self.rhs.unparse(writer)?; + self.rhs().unparse(writer)?; Ok(()) } } @@ -849,7 +869,7 @@ impl Unparse for AssignStatement { impl Unparse for CallStatement { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.write_indented("")?; - self.0.unparse(writer)?; + self.expression().unparse(writer)?; Ok(()) } } @@ -857,7 +877,7 @@ impl Unparse for CallStatement { impl Unparse for ReturnStatement { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.write_indented("return")?; - if let Some(expression) = &self.0 { + if let Some(expression) = self.expression() { writer.write(" ")?; expression.unparse(writer)?; } @@ -868,9 +888,9 @@ impl Unparse for ReturnStatement { impl Unparse for IfStatement { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.write_indented("if (")?; - self.condition.unparse(writer)?; + self.condition().unparse(writer)?; writer.writeln(") {")?; - self.then_block.unparse_inner(writer)?; + self.then_block().unparse_inner(writer)?; writer.writeln_indented("}")?; Ok(()) } @@ -878,9 +898,9 @@ impl Unparse for IfStatement { impl Unparse for IfElseStatement { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - self.if_statement.unparse(writer)?; - self.else_ifs.unparse(writer)?; - if let Some(else_block) = &self.else_block { + self.if_statement().unparse(writer)?; + self.else_ifs().unparse(writer)?; + if let Some(else_block) = self.else_block() { else_block.unparse(writer)?; } Ok(()) @@ -889,11 +909,11 @@ impl Unparse for IfElseStatement { impl Unparse for ElseIfs { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - for if_statement in &self.0 { + for if_statement in self.if_statements() { writer.write_indented("else if (")?; - if_statement.condition.unparse(writer)?; + if_statement.condition().unparse(writer)?; writer.writeln(") {")?; - if_statement.then_block.unparse_inner(writer)?; + if_statement.then_block().unparse_inner(writer)?; writer.writeln_indented("}")?; } Ok(()) @@ -903,7 +923,7 @@ impl Unparse for ElseIfs { impl Unparse for ElseBlock { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.write_indented("else ")?; - self.0.unparse(writer)?; + self.block_statement().unparse(writer)?; Ok(()) } } @@ -911,9 +931,9 @@ impl Unparse for ElseBlock { impl Unparse for WhileStatement { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.write_indented("while ")?; - self.condition.unparse(writer)?; + self.condition().unparse(writer)?; writer.writeln(" {")?; - self.body.unparse_inner(writer)?; + self.body().unparse_inner(writer)?; writer.writeln_indented("}")?; Ok(()) } @@ -922,11 +942,11 @@ impl Unparse for WhileStatement { impl Unparse for ForStatement { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.write_indented("for ")?; - self.variable.unparse(writer)?; + self.variable().unparse(writer)?; writer.write(" in ")?; - self.iterator.unparse(writer)?; + self.iterator().unparse(writer)?; writer.writeln(" {")?; - self.body.unparse_inner(writer)?; + self.body().unparse_inner(writer)?; writer.writeln_indented("}")?; Ok(()) } @@ -936,7 +956,7 @@ impl Unparse for ForStatement { impl Unparse for Expression { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use Expression::*; + use crate::ast::node::expression::Expression::*; match self { Ternary(ternary) => ternary.unparse(writer), Binary(binary) => binary.unparse(writer), @@ -953,54 +973,54 @@ impl Unparse for Expression { impl Unparse for TernaryExpression { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - self.condition.unparse(writer)?; + self.condition().unparse(writer)?; writer.write(" ? ")?; - self.true_expression.unparse(writer)?; + self.true_expression().unparse(writer)?; writer.write(" : ")?; - self.false_expression.unparse(writer)?; + self.false_expression().unparse(writer)?; Ok(()) } } impl Unparse for BinaryExpression { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - self.left.unparse(writer)?; + self.left().unparse(writer)?; writer.write(" ")?; - self.operator.unparse(writer)?; + self.operator().unparse(writer)?; writer.write(" ")?; - self.right.unparse(writer)?; + self.right().unparse(writer)?; Ok(()) } } impl Unparse for PrefixExpression { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use PrefixUnaryOperator::*; - match &self.operator { + use crate::ast::node::operators::PrefixUnaryOperator::*; + match self.operator() { BorrowMut => writer.write("&mut "), Mut => writer.write("mut "), other => other.unparse(writer), }?; - self.expression.unparse(writer)?; + self.expression().unparse(writer)?; Ok(()) } } impl Unparse for SuffixExpression { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - self.expression.unparse(writer)?; - self.operator.unparse(writer)?; + self.expression().unparse(writer)?; + self.operator().unparse(writer)?; Ok(()) } } impl Unparse for CallExpression { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - self.callee.unparse(writer)?; - if let Some(turbo_fish) = &self.turbo_fish { + self.callee().unparse(writer)?; + if let Some(turbo_fish) = self.turbo_fish() { turbo_fish.unparse(writer)?; } - self.arguments.unparse(writer)?; + self.arguments().unparse(writer)?; Ok(()) } } @@ -1008,7 +1028,7 @@ impl Unparse for CallExpression { impl Unparse for TurboFish { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.write("::")?; - self.0.unparse(writer)?; + self.generics().unparse(writer)?; Ok(()) } } @@ -1033,43 +1053,43 @@ impl ListUnparse for CallArguments { impl Unparse for CallArgument { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - self.0.unparse(writer) + self.expression().unparse(writer) } } impl Unparse for Closure { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - if let Some(modifier) = &self.modifier { + if let Some(modifier) = self.modifier() { match modifier { ClosureModifier::Cons => writer.write("cons "), ClosureModifier::Mut => writer.write("mut "), }?; } - if self.is_move { + if self.is_move() { writer.write("move ")?; } - if !self.captures.is_empty() { - self.captures.unparse(writer)?; + if !self.captures().is_empty() { + self.captures().unparse(writer)?; writer.write(" ")?; } writer.write("{ ")?; - if !self.parameters.is_empty() { - self.parameters.unparse(writer)?; + if !self.parameters().is_empty() { + self.parameters().unparse(writer)?; writer.write(" -> ")?; } - if !self.statements.is_empty() { + if !self.statements().is_empty() { writer.write("\n")?; writer.increase_indent(); - for statement in &self.statements { + for statement in self.statements() { statement.unparse(writer)?; } writer.decrease_indent(); } - if let Some(expression) = &self.expression { - if self.statements.is_empty() { + if let Some(expression) = self.expression() { + if self.statements().is_empty() { expression.unparse(writer)?; writer.write(" }")?; } else { @@ -1080,7 +1100,7 @@ impl Unparse for Closure { writer.decrease_indent(); writer.write_indented("}")?; } - } else if !self.statements.is_empty() { + } else if !self.statements().is_empty() { writer.write_indented(" }")?; } else { writer.write(" }")?; @@ -1092,9 +1112,9 @@ impl Unparse for Closure { impl Unparse for ClosureCaptures { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.write("|")?; - for (i, capture) in self.0.iter().enumerate() { + for (i, capture) in self.captures().iter().enumerate() { capture.unparse(writer)?; - if i != self.0.len() - 1 { + if i != self.captures().len() - 1 { writer.write(", ")?; } } @@ -1105,20 +1125,20 @@ impl Unparse for ClosureCaptures { impl Unparse for ClosureCapture { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - for _ in 0..self.borrow_count { + for _ in 0..self.borrow_count() { writer.write("&")?; } - if self.is_mutable { + if self.is_mutable() { writer.write("mut ")?; } - self.identifier.unparse(writer)?; + self.identifier().unparse(writer)?; Ok(()) } } impl Unparse for ClosureParameters { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - for parameter in &self.0 { + for parameter in self.parameters() { parameter.unparse(writer)?; } Ok(()) @@ -1127,8 +1147,8 @@ impl Unparse for ClosureParameters { impl Unparse for ClosureParameter { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - self.identifier.unparse(writer)?; - if let Some(type_use) = &self.type_use { + self.identifier().unparse(writer)?; + if let Some(type_use) = self.type_use() { writer.write(": ")?; type_use.unparse(writer)?; } @@ -1138,15 +1158,15 @@ impl Unparse for ClosureParameter { impl Unparse for ObjectAccess { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - self.receiver.unparse(writer)?; - self.navigations.unparse(writer)?; + self.receiver().unparse(writer)?; + self.navigations().unparse(writer)?; Ok(()) } } impl Unparse for ObjectNavigations { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - for navigation in &self.0 { + for navigation in self.navigations() { navigation.unparse(writer)?; } Ok(()) @@ -1155,7 +1175,7 @@ impl Unparse for ObjectNavigations { impl Unparse for ObjectNavigation { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use ObjectNavigation::*; + use crate::ast::node::object_access::ObjectNavigation::*; match self { Index(expression) => { writer.write("[")?; @@ -1173,7 +1193,7 @@ impl Unparse for ObjectNavigation { impl Unparse for Literal { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { - use Literal::*; + use crate::ast::node::literal::Literal::*; match self { Integer(i) => writer.write(&format!("{}", i)), Long(l) => writer.write(&format!("{}", l)), @@ -1190,7 +1210,7 @@ impl Unparse for Literal { impl Unparse for DString { fn unparse(&self, writer: &mut IndentWriter) -> std::io::Result<()> { writer.write("\"")?; - for part in &self.0 { + for part in self.parts() { part.unparse(writer)?; } writer.write("\"")?; diff --git a/src/name_analysis/gather.rs b/src/name_analysis/gather.rs index 77f6bc7..48d8f15 100644 --- a/src/name_analysis/gather.rs +++ b/src/name_analysis/gather.rs @@ -1,5 +1,22 @@ -use crate::ast::named::Named; -use crate::ast::*; +use crate::ast::node::call_expression::*; +use crate::ast::node::class::*; +use crate::ast::node::closure::*; +use crate::ast::node::compilation_unit::*; +use crate::ast::node::d_string::*; +use crate::ast::node::expression::*; +use crate::ast::node::function::*; +use crate::ast::node::generics::*; +use crate::ast::node::implements_list::*; +use crate::ast::node::interface::*; +use crate::ast::node::level::*; +use crate::ast::node::literal::*; +use crate::ast::node::module::*; +use crate::ast::node::named::Named; +use crate::ast::node::names::*; +use crate::ast::node::object_access::*; +use crate::ast::node::statement::*; +use crate::ast::node::type_use::*; +use crate::ast::node::use_statement::*; use crate::diagnostic::DmDiagnostic; use crate::name_analysis::fqn_context::FqnContext; use crate::name_analysis::symbol::*; @@ -372,7 +389,7 @@ fn gather_module_level_declaration( fqn_context: &mut FqnContext, diagnostics: &mut Vec, ) { - use ModuleLevelDeclaration::*; + use crate::ast::node::level::ModuleLevelDeclaration::*; match declaration { Module(module_declaration) => { gather_module_declaration(module_declaration, symbol_table, fqn_context, diagnostics); @@ -408,7 +425,7 @@ fn gather_interface_level_declaration( fqn_context: &mut FqnContext, diagnostics: &mut Vec, ) { - use InterfaceLevelDeclaration::*; + use crate::ast::node::level::InterfaceLevelDeclaration::*; match declaration { Module(module_declaration) => { gather_module_declaration(module_declaration, symbol_table, fqn_context, diagnostics); @@ -449,7 +466,7 @@ fn gather_class_level_declaration( fqn_context: &mut FqnContext, diagnostics: &mut Vec, ) { - use ClassLevelDeclaration::*; + use crate::ast::node::level::ClassLevelDeclaration::*; match declaration { Module(module_declaration) => { gather_module_declaration(module_declaration, symbol_table, fqn_context, diagnostics); @@ -880,7 +897,7 @@ fn gather_function_body( fqn_context: &mut FqnContext, diagnostics: &mut Vec, ) { - use crate::ast::FunctionBody::*; + use crate::ast::node::function::FunctionBody::*; match function_body { Equals(expression) => gather_expression(expression, symbol_table, diagnostics), Block(block) => gather_block_statement_inner(block, symbol_table, fqn_context, diagnostics), @@ -994,7 +1011,7 @@ fn gather_statement( fqn_context: &mut FqnContext, diagnostics: &mut Vec, ) { - use crate::ast::Statement::*; + use crate::ast::node::statement::Statement::*; match statement { BlockStatement(block) => { gather_block_statement(block, symbol_table, fqn_context, diagnostics) @@ -1175,7 +1192,7 @@ fn gather_expression( symbol_table: &mut SymbolTable, diagnostics: &mut Vec, ) { - use crate::ast::Expression::*; + use crate::ast::node::expression::Expression::*; match expression { Ternary(ternary_expression) => { gather_ternary_expression(ternary_expression, symbol_table, diagnostics); diff --git a/src/name_analysis/mod.rs b/src/name_analysis/mod.rs index 3072f85..4db7e1d 100644 --- a/src/name_analysis/mod.rs +++ b/src/name_analysis/mod.rs @@ -5,7 +5,7 @@ There are two phases in name analysis. ## 1. Gather -The gather phases has three responsibilities: +The gather phase has three responsibilities: 1. Add all declared symbols to the symbol table. 2. Set the `scope_id` property of all identifiers and fully-qualified-names. @@ -19,8 +19,8 @@ The resolve phase has one main responsibility: resolve all references based on t `scope_id` property. */ -use crate::ast::named::Named; -use crate::ast::CompilationUnit; +use crate::ast::node::compilation_unit::CompilationUnit; +use crate::ast::node::named::Named; use crate::diagnostic::DmDiagnostic; use crate::name_analysis::gather::gather_compilation_unit; use crate::name_analysis::resolve::resolve_compilation_unit; @@ -196,7 +196,7 @@ mod tests { let mut symbol_table = SymbolTable::new(); assert_no_diagnostics(sources, &mut symbol_table); } - + #[test] fn shadow_import() { let sources: HashMap<&str, &str> = HashMap::from([ @@ -206,7 +206,7 @@ mod tests { use greeter::Greeter; class Greeter {} - "} + "}, ), ( "greeter.dm", @@ -214,8 +214,8 @@ mod tests { ns greeter; class Greeter {} - "} - ) + "}, + ), ]); assert_number_of_diagnostics(sources, &mut SymbolTable::new(), 1); } diff --git a/src/name_analysis/resolve.rs b/src/name_analysis/resolve.rs index 4cd672d..f6c0757 100644 --- a/src/name_analysis/resolve.rs +++ b/src/name_analysis/resolve.rs @@ -1,12 +1,29 @@ -use crate::ast::named::Named; -use crate::ast::*; +use crate::ast::node::call_expression::*; +use crate::ast::node::class::*; +use crate::ast::node::closure::*; +use crate::ast::node::compilation_unit::*; +use crate::ast::node::d_string::*; +use crate::ast::node::expression::*; +use crate::ast::node::function::*; +use crate::ast::node::generics::*; +use crate::ast::node::implements_list::*; +use crate::ast::node::interface::*; +use crate::ast::node::level::*; +use crate::ast::node::literal::*; +use crate::ast::node::module::*; +use crate::ast::node::names::*; +use crate::ast::node::object_access::*; +use crate::ast::node::statement::*; +use crate::ast::node::tuple_arguments::*; +use crate::ast::node::type_use::*; +use crate::ast::node::use_statement::*; use crate::diagnostic::DmDiagnostic; use crate::name_analysis::symbol::Symbol; use crate::name_analysis::symbol_table::{SymbolLookupError, SymbolTable}; use codespan_reporting::diagnostic::{Diagnostic, Label}; use std::ops::DerefMut; use std::range::Range; - +use crate::ast::node::named::Named; /* Type Use */ fn resolve_type_use( @@ -282,7 +299,7 @@ fn resolve_module_level_declaration( symbol_table: &mut SymbolTable, diagnostics: &mut Vec, ) { - use crate::ast::ModuleLevelDeclaration::*; + use crate::ast::node::level::ModuleLevelDeclaration::*; match declaration { Module(module_declaration) => { resolve_module_declaration(module_declaration, symbol_table, diagnostics); @@ -309,7 +326,7 @@ fn resolve_interface_level_declaration( symbol_table: &mut SymbolTable, diagnostics: &mut Vec, ) { - use crate::ast::InterfaceLevelDeclaration::*; + use crate::ast::node::level::InterfaceLevelDeclaration::*; match declaration { Module(module_declaration) => { resolve_module_declaration(module_declaration, symbol_table, diagnostics); @@ -342,7 +359,7 @@ fn resolve_class_level_declaration( symbol_table: &mut SymbolTable, diagnostics: &mut Vec, ) { - use crate::ast::ClassLevelDeclaration::*; + use crate::ast::node::level::ClassLevelDeclaration::*; match declaration { Module(module_declaration) => { resolve_module_declaration(module_declaration, symbol_table, diagnostics); @@ -414,7 +431,11 @@ fn resolve_class_declaration( if let Some(class_constructor) = class_declaration.class_constructor_mut() { resolve_class_constructor(class_constructor, symbol_table, diagnostics); } - resolve_implements_list(class_declaration.implements_mut(), symbol_table, diagnostics); + resolve_implements_list( + class_declaration.implements_mut(), + symbol_table, + diagnostics, + ); for declaration in class_declaration.declarations_mut() { resolve_class_level_declaration(declaration, symbol_table, diagnostics); } @@ -498,7 +519,7 @@ fn resolve_function_body( symbol_table: &mut SymbolTable, diagnostics: &mut Vec, ) { - use crate::ast::FunctionBody::*; + use crate::ast::node::function::FunctionBody::*; match function_body { Equals(expression) => resolve_expression(expression, symbol_table, diagnostics), Block(block) => resolve_block_statement(block, symbol_table, diagnostics), @@ -521,7 +542,7 @@ fn resolve_class_constructor( symbol_table: &mut SymbolTable, diagnostics: &mut Vec, ) { - use crate::ast::ClassConstructorParameter::*; + use crate::ast::node::class::ClassConstructorParameter::*; for parameter in class_constructor.parameters_mut() { match parameter { Property(property) => resolve_property_declaration(property, symbol_table, diagnostics), @@ -574,7 +595,7 @@ fn resolve_statement( symbol_table: &mut SymbolTable, diagnostics: &mut Vec, ) { - use crate::ast::Statement::*; + use crate::ast::node::statement::Statement::*; match statement { BlockStatement(block) => resolve_block_statement(block, symbol_table, diagnostics), VariableDeclarationStatement(variable_declaration) => { @@ -658,7 +679,7 @@ fn resolve_expression( symbol_table: &mut SymbolTable, diagnostics: &mut Vec, ) { - use crate::ast::Expression::*; + use crate::ast::node::expression::Expression::*; match expression { Ternary(ternary_expression) => { resolve_ternary_expression(ternary_expression, symbol_table, diagnostics); @@ -741,7 +762,11 @@ fn resolve_ternary_expression( symbol_table: &mut SymbolTable, diagnostics: &mut Vec, ) { - resolve_expression(ternary_expression.condition_mut(), symbol_table, diagnostics); + resolve_expression( + ternary_expression.condition_mut(), + symbol_table, + diagnostics, + ); resolve_expression( ternary_expression.true_expression_mut(), symbol_table, @@ -768,7 +793,11 @@ fn resolve_prefix_expression( symbol_table: &mut SymbolTable, diagnostics: &mut Vec, ) { - resolve_expression(prefix_expression.expression_mut(), symbol_table, diagnostics); + resolve_expression( + prefix_expression.expression_mut(), + symbol_table, + diagnostics, + ); } fn resolve_suffix_expression( @@ -776,7 +805,11 @@ fn resolve_suffix_expression( symbol_table: &mut SymbolTable, diagnostics: &mut Vec, ) { - resolve_expression(suffix_expression.expression_mut(), symbol_table, diagnostics); + resolve_expression( + suffix_expression.expression_mut(), + symbol_table, + diagnostics, + ); } fn resolve_call_expression( diff --git a/src/name_analysis/symbol.rs b/src/name_analysis/symbol.rs index d877cad..dcf3fd0 100644 --- a/src/name_analysis/symbol.rs +++ b/src/name_analysis/symbol.rs @@ -1,5 +1,6 @@ -use crate::ast::named::Named; -use crate::ast::{Identifier, UseStatement}; +use crate::ast::node::named::Named; +use crate::ast::node::names::Identifier; +use crate::ast::node::use_statement::UseStatement; use std::cell::RefCell; use std::fmt::{Debug, Display, Formatter}; use std::ops::Deref; @@ -97,11 +98,7 @@ pub struct UseStatementSymbol { } impl UseStatementSymbol { - pub fn new( - fqn: &str, - declared_name: &str, - identifier: Option<&Identifier>, - ) -> Self { + pub fn new(fqn: &str, declared_name: &str, identifier: Option<&Identifier>) -> Self { UseStatementSymbol { fqn: fqn.to_string(), declared_name: declared_name.to_string(),