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 isFunction () const noexcept
 Check if the node is a function.
 
const std::optional< std::string > & getUnqualifiedName () const noexcept
 Get the unqualified name, if it has been set.
 
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 setUnqualifiedName (const std::string &name) noexcept
 Set the unqualified name (used by Capture nodes)
 
void setString (const std::string &value) noexcept
 Set the String object.
 
void setPositionFrom (const Node &source) noexcept
 Position the current node at a given span in a file.
 
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.
 
FileSpan position () const noexcept
 Get the span of the node (start and end)
 
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::optional< std::string > m_unqualified_name { std::nullopt }
 Used by Capture nodes, to have the FQN in the value, and the captured name here.
 
FileSpan m_pos
 
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

class Parser
 
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 31 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 34 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::import_().

◆ 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 169 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 174 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()

◆ getUnqualifiedName()

const std::optional< std::string > & Ark::internal::Node::getUnqualifiedName ( ) const
nodiscardnoexcept

Get the unqualified name, if it has been set.

Returns
const std::optional<std::string>&

Definition at line 96 of file Node.cpp.

References m_unqualified_name.

◆ 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 88 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

◆ 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().

◆ 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().

◆ position()

◆ 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::function(), Ark::internal::Parser::import_(), Ark::internal::Parser::letMutSet(), 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.

◆ 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

◆ setPositionFrom()

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

Position the current node at a given span in a file.

Parameters
sourcenode to copy filename and position from

Definition at line 122 of file Node.cpp.

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

◆ setString()

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

◆ setUnqualifiedName()

void Ark::internal::Node::setUnqualifiedName ( const std::string & name)
noexcept

Set the unqualified name (used by Capture nodes)

Parameters
name

Definition at line 112 of file Node.cpp.

◆ 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 388 of file Node.cpp.

◆ operator==

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

Definition at line 378 of file Node.cpp.

◆ Parser

friend class Parser
friend

Definition at line 239 of file Node.hpp.

Member Data Documentation

◆ m_after_comment

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

Comment after node.

Definition at line 249 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 250 of file Node.hpp.

Referenced by repr(), and setAltSyntax().

◆ m_comment

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

Definition at line 248 of file Node.hpp.

Referenced by attachNearestCommentBefore(), and comment().

◆ m_filename

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

Definition at line 247 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 251 of file Node.hpp.

Referenced by isAnonymousFunction(), and setFunctionKind().

◆ m_pos

FileSpan Ark::internal::Node::m_pos
private

Definition at line 246 of file Node.hpp.

Referenced by position().

◆ m_type

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

Definition at line 242 of file Node.hpp.

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

◆ m_unqualified_name

std::optional<std::string> Ark::internal::Node::m_unqualified_name { std::nullopt }
private

Used by Capture nodes, to have the FQN in the value, and the captured name here.

Definition at line 244 of file Node.hpp.

Referenced by getUnqualifiedName().

◆ m_value

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

Definition at line 243 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: