ArkScript
A small, lisp-inspired, functional scripting language
Ark::internal::Node Class Reference

A node of an Abstract Syntax Tree for ArkScript. More...

#include <Node.hpp>

Collaboration diagram for Ark::internal::Node:
[legend]

Public Types

using Value = std::variant<double, std::string, Keyword, std::vector<Node>, Namespace>
 

Public Member Functions

 Node ()=default
 
 Node (NodeType node_type, const std::string &value)
 
 Node (NodeType node_type)
 
 Node (double value)
 
 Node (long value)
 
 Node (Keyword value)
 
 Node (const Namespace &namespace_)
 
const std::string & string () const noexcept
 Return the string held by the value (if the node type allows it)
 
double number () const noexcept
 Return the number held by the value (if the node type allows it)
 
Keyword keyword () const noexcept
 Return the keyword held by the value (if the node type allows it)
 
NamespacearkNamespace () noexcept
 Return the namespace held by the value (if the node type allows it)
 
const NamespaceconstArkNamespace () const noexcept
 Return the namespace held by the value (if the node type allows it)
 
void push_back (const Node &node) noexcept
 Every node has a list as well as a value so we can push_back on all node no matter their type.
 
std::vector< Node > & list () noexcept
 Return the list of sub-nodes held by the node.
 
const std::vector< Node > & constList () const noexcept
 Return the list of sub-nodes held by the node.
 
NodeType nodeType () const noexcept
 Return the node type.
 
bool isListLike () const noexcept
 Check if the node is a list like node.
 
bool isStringLike () const noexcept
 Check if the node is a string like node.
 
bool isFunction () const noexcept
 Check if the node is a function.
 
void updateValueAndType (const Node &source) noexcept
 Copy a node to the current one, while keeping the filename and position in the file.
 
void setNodeType (NodeType type) noexcept
 Set the Node Type object.
 
void setString (const std::string &value) noexcept
 Set the String object.
 
void setPos (std::size_t line, std::size_t col) noexcept
 Set the Position of the node in the text.
 
void setFilename (const std::string &filename) noexcept
 Set the original Filename where the node was.
 
NodeattachNearestCommentBefore (const std::string &comment)
 Set the comment field with the nearest comment before this node.
 
NodeattachCommentAfter (const std::string &comment)
 Set the comment_after field with the nearest comment after this node.
 
void setAltSyntax (bool toggle)
 Set the m_alt_syntax flag of the node.
 
void setFunctionKind (bool anonymous)
 Set the m_is_anonymous_function flag on the node.
 
bool isAnonymousFunction () const noexcept
 Check if a node is an anonymous function.
 
bool isAltSyntax () const
 Check if a node is alt syntax.
 
std::size_t line () const noexcept
 Get the line at which this node was created.
 
std::size_t col () const noexcept
 Get the column at which this node was created.
 
const std::string & filename () const noexcept
 Return the filename in which this node was created.
 
const std::string & comment () const noexcept
 Return the comment attached to this node, if any.
 
const std::string & commentAfter () const noexcept
 Return the comment attached after this node, if any.
 
std::string repr () const noexcept
 Compute a representation of the node without any comments or additional sugar, colors, types.
 
std::ostream & debugPrint (std::ostream &os) const noexcept
 Print a node to an output stream with added type annotations.
 

Private Attributes

NodeType m_type { NodeType::Unused }
 
Value m_value
 
std::size_t m_line = 0
 
std::size_t m_col = 0
 
std::string m_filename
 
std::string m_comment
 
std::string m_after_comment
 Comment after node.
 
bool m_alt_syntax = false
 Used to tell if a node uses the alternative syntax (if available), eg (begin) / {}, (list) / [].
 
bool m_is_anonymous_function = true
 Function nodes are marked as anonymous/non-anonymous by the ASTLowerer, to enable some optimisations.
 

Friends

bool operator== (const Node &A, const Node &B)
 
bool operator< (const Node &A, const Node &B)
 

Detailed Description

A node of an Abstract Syntax Tree for ArkScript.

Definition at line 29 of file Node.hpp.

Member Typedef Documentation

◆ Value

using Ark::internal::Node::Value = std::variant<double, std::string, Keyword, std::vector<Node>, Namespace>

Definition at line 32 of file Node.hpp.

Constructor & Destructor Documentation

◆ Node() [1/7]

Ark::internal::Node::Node ( )
default

◆ Node() [2/7]

Ark::internal::Node::Node ( NodeType node_type,
const std::string & value )

Definition at line 11 of file Node.cpp.

◆ Node() [3/7]

Ark::internal::Node::Node ( NodeType node_type)
explicit

Definition at line 15 of file Node.cpp.

References Ark::internal::Field, Ark::internal::List, m_type, m_value, and Ark::internal::Macro.

◆ Node() [4/7]

Ark::internal::Node::Node ( double value)
explicit

Definition at line 22 of file Node.cpp.

◆ Node() [5/7]

Ark::internal::Node::Node ( long value)
explicit

Definition at line 26 of file Node.cpp.

◆ Node() [6/7]

Ark::internal::Node::Node ( Keyword value)
explicit

Definition at line 30 of file Node.cpp.

◆ Node() [7/7]

Ark::internal::Node::Node ( const Namespace & namespace_)
explicit

Definition at line 34 of file Node.cpp.

Member Function Documentation

◆ arkNamespace()

Namespace & Ark::internal::Node::arkNamespace ( )
nodiscardnoexcept

Return the namespace held by the value (if the node type allows it)

Returns
Namespace&

Definition at line 53 of file Node.cpp.

References m_value.

Referenced by Ark::internal::Optimizer::countAndPruneDeadCode(), Ark::internal::ImportSolver::findAndReplaceImports(), Ark::internal::MacroProcessor::processNode(), and Ark::internal::NameResolutionPass::visit().

◆ attachCommentAfter()

Node & Ark::internal::Node::attachCommentAfter ( const std::string & comment)

Set the comment_after field with the nearest comment after this node.

Parameters
comment
Returns
Node& reference to this node after updating it

Definition at line 134 of file Node.cpp.

References comment(), and m_after_comment.

◆ attachNearestCommentBefore()

Node & Ark::internal::Node::attachNearestCommentBefore ( const std::string & comment)

Set the comment field with the nearest comment before this node.

Parameters
comment
Returns
Node& reference to this node after updating it

Definition at line 128 of file Node.cpp.

References comment(), and m_comment.

Referenced by Ark::internal::Parser::functionArgs(), Ark::internal::Parser::import_(), and Ark::internal::Parser::nil().

◆ col()

◆ comment()

const std::string & Ark::internal::Node::comment ( ) const
nodiscardnoexcept

Return the comment attached to this node, if any.

Returns
const std::string&

Definition at line 179 of file Node.cpp.

References m_comment.

Referenced by attachCommentAfter(), attachNearestCommentBefore(), Formatter::format(), Formatter::formatFunction(), Formatter::formatImport(), and Formatter::shouldSplitOnNewline().

◆ commentAfter()

const std::string & Ark::internal::Node::commentAfter ( ) const
nodiscardnoexcept

Return the comment attached after this node, if any.

Returns
const std::string&

Definition at line 184 of file Node.cpp.

References m_after_comment.

Referenced by Formatter::format().

◆ constArkNamespace()

const Namespace & Ark::internal::Node::constArkNamespace ( ) const
nodiscardnoexcept

Return the namespace held by the value (if the node type allows it)

Returns
const Namespace&

Definition at line 58 of file Node.cpp.

References m_value.

Referenced by Ark::internal::ASTLowerer::compileExpression(), and repr().

◆ constList()

const std::vector< Node > & Ark::internal::Node::constList ( ) const
nodiscardnoexcept

Return the list of sub-nodes held by the node.

Returns
const std::vector<Node>&

Definition at line 73 of file Node.cpp.

References m_value.

Referenced by JsonCompiler::_compile(), Ark::internal::ConditionalExecutor::applyMacro(), Ark::internal::FunctionExecutor::applyMacro(), Ark::internal::FunctionExecutor::canHandle(), Ark::internal::MacroProcessor::checkMacroArgCountEq(), Ark::internal::MacroProcessor::checkMacroArgCountGe(), Ark::internal::ASTLowerer::compileExpression(), Ark::internal::ASTLowerer::compileFunction(), Ark::internal::ASTLowerer::compileIf(), Ark::internal::ASTLowerer::compileLetMutSet(), Ark::internal::ASTLowerer::compileListInstruction(), Ark::internal::ASTLowerer::compilePluginImport(), Ark::internal::ASTLowerer::compileWhile(), Ark::internal::Optimizer::countAndPruneDeadCode(), Ark::internal::MacroProcessor::evaluate(), Ark::internal::ImportSolver::findAndReplaceImports(), Formatter::format(), Formatter::formatBegin(), Formatter::formatBlock(), Formatter::formatCall(), Formatter::formatCondition(), Formatter::formatDel(), Formatter::formatFunction(), Formatter::formatImport(), Formatter::formatLoop(), Formatter::formatMacro(), Formatter::formatVariable(), Ark::internal::ASTLowerer::handleCalls(), Ark::internal::MacroProcessor::handleMacroNode(), Ark::internal::MacroProcessor::isBeginNode(), Ark::internal::MacroProcessor::isConstEval(), Formatter::isFuncCall(), isFunction(), Formatter::isListStartingWithKeyword(), Formatter::lineOfLastNodeIn(), Ark::internal::FunctionExecutor::macroNode(), Ark::internal::ASTLowerer::nodeProducesOutput(), Formatter::processAst(), Ark::internal::MacroProcessor::processNode(), Ark::internal::ASTLowerer::pushFunctionCallArguments(), Ark::internal::MacroProcessor::registerFuncDef(), Ark::internal::MacroProcessor::removeBegin(), repr(), Formatter::shouldAddNewLineBetweenNodes(), Formatter::shouldSplitOnNewline(), JsonCompiler::toJsonList(), Ark::internal::NameResolutionPass::visit(), and Ark::internal::NameResolutionPass::visitKeyword().

◆ debugPrint()

◆ filename()

◆ isAltSyntax()

bool Ark::internal::Node::isAltSyntax ( ) const
nodiscard

Check if a node is alt syntax.

Returns
bool

Definition at line 159 of file Node.cpp.

References m_alt_syntax.

◆ isAnonymousFunction()

bool Ark::internal::Node::isAnonymousFunction ( ) const
nodiscardnoexcept

Check if a node is an anonymous function.

Returns
true if the node is of an anonymous function
false

Definition at line 154 of file Node.cpp.

References m_is_anonymous_function.

Referenced by Ark::internal::ASTLowerer::compileFunction().

◆ isFunction()

bool Ark::internal::Node::isFunction ( ) const
nodiscardnoexcept

Check if the node is a function.

Returns
true if the node is a function declaration
false

Definition at line 93 of file Node.cpp.

References constList(), Ark::internal::Fun, Ark::internal::Keyword, Ark::internal::List, and m_type.

◆ isListLike()

bool Ark::internal::Node::isListLike ( ) const
nodiscardnoexcept

◆ isStringLike()

bool Ark::internal::Node::isStringLike ( ) const
nodiscardnoexcept

Check if the node is a string like node.

Returns
true if the node is either a symbol, a string or a spread
false

Definition at line 88 of file Node.cpp.

References m_type, Ark::internal::Spread, Ark::internal::String, and Ark::internal::Symbol.

Referenced by Ark::Diagnostics::makeContextWithNode().

◆ keyword()

Keyword Ark::internal::Node::keyword ( ) const
nodiscardnoexcept

Return the keyword held by the value (if the node type allows it)

Returns
Keyword

Definition at line 48 of file Node.cpp.

References m_value.

Referenced by JsonCompiler::_compile(), Formatter::format(), Formatter::formatBlock(), Ark::internal::MacroProcessor::removeBegin(), and repr().

◆ line()

◆ list()

◆ nodeType()

◆ number()

double Ark::internal::Node::number ( ) const
nodiscardnoexcept

Return the number held by the value (if the node type allows it)

Returns
double

Definition at line 43 of file Node.cpp.

References m_value.

Referenced by JsonCompiler::_compile(), Ark::internal::MacroProcessor::evaluate(), Formatter::format(), Ark::internal::MacroProcessor::isTruthy(), and repr().

◆ push_back()

void Ark::internal::Node::push_back ( const Node & node)
noexcept

Every node has a list as well as a value so we can push_back on all node no matter their type.

Parameters
nodea sub-node to push on the list held by the current node

Definition at line 63 of file Node.cpp.

Referenced by Ark::internal::MacroProcessor::evaluate(), Ark::internal::ImportSolver::findAndReplaceImports(), Ark::internal::Parser::import_(), and Ark::internal::Parser::process().

◆ repr()

◆ setAltSyntax()

void Ark::internal::Node::setAltSyntax ( bool toggle)

Set the m_alt_syntax flag of the node.

Parameters
toggle

Definition at line 144 of file Node.cpp.

References m_alt_syntax.

◆ setFilename()

void Ark::internal::Node::setFilename ( const std::string & filename)
noexcept

Set the original Filename where the node was.

Parameters
filename

Definition at line 123 of file Node.cpp.

Referenced by Ark::internal::MacroProcessor::evaluate().

◆ setFunctionKind()

void Ark::internal::Node::setFunctionKind ( bool anonymous)

Set the m_is_anonymous_function flag on the node.

Parameters
anonymoustrue to mark the node as an anonymous function

Definition at line 149 of file Node.cpp.

References m_is_anonymous_function.

◆ setNodeType()

void Ark::internal::Node::setNodeType ( NodeType type)
noexcept

◆ setPos()

void Ark::internal::Node::setPos ( std::size_t line,
std::size_t col )
noexcept

Set the Position of the node in the text.

Parameters
line
col

Definition at line 117 of file Node.cpp.

Referenced by Ark::internal::MacroProcessor::evaluate(), and Ark::internal::Parser::setNodePosAndFilename().

◆ setString()

void Ark::internal::Node::setString ( const std::string & value)
noexcept

Set the String object.

Parameters
value

Definition at line 112 of file Node.cpp.

Referenced by Ark::internal::MacroProcessor::evaluate(), and Ark::internal::NameResolutionPass::updateSymbolWithFullyQualifiedName().

◆ string()

◆ updateValueAndType()

void Ark::internal::Node::updateValueAndType ( const Node & source)
noexcept

Copy a node to the current one, while keeping the filename and position in the file.

Parameters
sourcenode to copy type and value from

Definition at line 101 of file Node.cpp.

Referenced by Ark::internal::ConditionalExecutor::applyMacro(), Ark::internal::FunctionExecutor::applyMacro(), Ark::internal::SymbolExecutor::applyMacro(), and Ark::internal::MacroProcessor::evaluate().

Friends And Related Symbol Documentation

◆ operator<

bool operator< ( const Node & A,
const Node & B )
friend

Definition at line 398 of file Node.cpp.

◆ operator==

bool operator== ( const Node & A,
const Node & B )
friend

Definition at line 388 of file Node.cpp.

Member Data Documentation

◆ m_after_comment

std::string Ark::internal::Node::m_after_comment
private

Comment after node.

Definition at line 259 of file Node.hpp.

Referenced by attachCommentAfter(), and commentAfter().

◆ m_alt_syntax

bool Ark::internal::Node::m_alt_syntax = false
private

Used to tell if a node uses the alternative syntax (if available), eg (begin) / {}, (list) / [].

Definition at line 260 of file Node.hpp.

Referenced by isAltSyntax(), repr(), and setAltSyntax().

◆ m_col

std::size_t Ark::internal::Node::m_col = 0
private

Definition at line 256 of file Node.hpp.

Referenced by col().

◆ m_comment

std::string Ark::internal::Node::m_comment
private

Definition at line 258 of file Node.hpp.

Referenced by attachNearestCommentBefore(), and comment().

◆ m_filename

std::string Ark::internal::Node::m_filename
private

Definition at line 257 of file Node.hpp.

Referenced by filename().

◆ m_is_anonymous_function

bool Ark::internal::Node::m_is_anonymous_function = true
private

Function nodes are marked as anonymous/non-anonymous by the ASTLowerer, to enable some optimisations.

Definition at line 261 of file Node.hpp.

Referenced by isAnonymousFunction(), and setFunctionKind().

◆ m_line

std::size_t Ark::internal::Node::m_line = 0
private

Definition at line 256 of file Node.hpp.

Referenced by line().

◆ m_type

NodeType Ark::internal::Node::m_type { NodeType::Unused }
private

Definition at line 253 of file Node.hpp.

Referenced by isFunction(), isListLike(), isStringLike(), Node(), nodeType(), and repr().

◆ m_value

Value Ark::internal::Node::m_value
private

Definition at line 254 of file Node.hpp.

Referenced by arkNamespace(), constArkNamespace(), constList(), keyword(), list(), Node(), number(), and string().


The documentation for this class was generated from the following files: