ArkScript
A small, fast, functional and scripting language for video games
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 (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.
 
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.
 
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
 
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.
 

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 30 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 33 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 10 of file Node.cpp.

◆ Node() [3/7]

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

Definition at line 14 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 21 of file Node.cpp.

◆ Node() [5/7]

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

Definition at line 25 of file Node.cpp.

◆ Node() [6/7]

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

Definition at line 29 of file Node.cpp.

◆ Node() [7/7]

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

Definition at line 33 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 52 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 125 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 119 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 150 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 155 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 57 of file Node.cpp.

References m_value.

Referenced by Ark::internal::Compiler::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 72 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::Compiler::compileExpression(), Ark::internal::Compiler::compileFunction(), Ark::internal::Compiler::compileIf(), Ark::internal::Compiler::compileLetMutSet(), Ark::internal::Compiler::compileListInstruction(), Ark::internal::Compiler::compilePluginImport(), Ark::internal::Compiler::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::Compiler::handleCalls(), Ark::internal::MacroProcessor::handleMacroNode(), Ark::internal::MacroProcessor::isBeginNode(), Ark::internal::MacroProcessor::isConstEval(), Formatter::isFuncCall(), Formatter::isListStartingWithKeyword(), Formatter::lineOfLastNodeIn(), Ark::internal::Compiler::nodeProducesOutput(), Formatter::processAst(), Ark::internal::MacroProcessor::processNode(), 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()

◆ 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 87 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 47 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 42 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 62 of file Node.cpp.

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

◆ repr()

◆ setFilename()

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

Set the original Filename where the node was.

Parameters
filename

Definition at line 114 of file Node.cpp.

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

◆ 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 108 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 103 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 92 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 342 of file Node.cpp.

◆ operator==

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

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

Referenced by attachCommentAfter(), and commentAfter().

◆ m_col

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

Definition at line 225 of file Node.hpp.

Referenced by col().

◆ m_comment

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

Definition at line 227 of file Node.hpp.

Referenced by attachNearestCommentBefore(), and comment().

◆ m_filename

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

Definition at line 226 of file Node.hpp.

Referenced by filename().

◆ m_line

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

Definition at line 225 of file Node.hpp.

Referenced by line().

◆ m_type

NodeType Ark::internal::Node::m_type
private

Definition at line 222 of file Node.hpp.

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

◆ m_value

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

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