![]() |
ArkScript
A small, lisp-inspired, functional scripting language
|
The ArkScript AST to IR compiler. More...
#include <ASTLowerer.hpp>
Classes | |
struct | Page |
Public Member Functions | |
ASTLowerer (unsigned debug) | |
Construct a new ASTLowerer object. | |
void | process (Node &ast) |
Start the compilation. | |
const std::vector< IR::Block > & | intermediateRepresentation () const noexcept |
Return the IR blocks (one per scope) | |
const std::vector< std::string > & | symbols () const noexcept |
Return the symbol table pre-computed. | |
const std::vector< ValTableElem > & | values () const noexcept |
Return the value table pre-computed. | |
Private Member Functions | |
IR::Block & | page (const Page page) noexcept |
helper functions to get a temp or finalized code page | |
void | compileExpression (Node &x, Page p, bool is_result_unused, bool is_terminal) |
Compile an expression (a node) recursively. | |
void | compileSymbol (Node &x, Page p, bool is_result_unused) |
void | compileListInstruction (Node &x, Page p, bool is_result_unused) |
void | compileIf (Node &x, Page p, bool is_result_unused, bool is_terminal) |
void | compileFunction (Node &x, Page p, bool is_result_unused) |
void | compileLetMutSet (Keyword n, Node &x, Page p) |
void | compileWhile (Node &x, Page p) |
void | compilePluginImport (Node &x, Page p) |
void | pushFunctionCallArguments (Node &call, Page p, bool is_tail_call) |
void | handleCalls (Node &x, Page p, bool is_result_unused, bool is_terminal) |
uint16_t | addSymbol (const Node &sym) |
Register a given node in the symbol table. | |
uint16_t | addValue (const Node &x) |
Register a given node in the value table. | |
uint16_t | addValue (std::size_t page_id, const Node ¤t) |
Register a page id (function reference) in the value table. | |
Static Private Member Functions | |
static std::optional< Instruction > | getOperator (const std::string &name) noexcept |
Checking if a symbol is an operator. | |
static std::optional< uint16_t > | getBuiltin (const std::string &name) noexcept |
Checking if a symbol is a builtin. | |
static std::optional< Instruction > | getListInstruction (const std::string &name) noexcept |
Checking if a symbol is a list instruction. | |
static bool | nodeProducesOutput (const Node &node) |
static bool | isUnaryInst (Instruction inst) noexcept |
Check if a given instruction is unary (takes only one argument) | |
static bool | isTernaryInst (Instruction inst) noexcept |
Check if a given instruction is ternary (takes three arguments) | |
static void | warning (const std::string &message, const Node &node) |
Display a warning message. | |
static void | buildAndThrowError (const std::string &message, const Node &node) |
Throw a nice error message. | |
Private Attributes | |
LocalsLocator | m_locals_locator |
std::vector< std::string > | m_symbols |
std::vector< ValTableElem > | m_values |
std::vector< IR::Block > | m_code_pages |
std::vector< IR::Block > | m_temp_pages |
we need temporary code pages for some compilations passes | |
IR::label_t | m_current_label = 0 |
std::stack< std::string > | m_opened_vars |
stack of vars we are currently declaring | |
Logger | m_logger |
The ArkScript AST to IR compiler.
Definition at line 37 of file ASTLowerer.hpp.
|
explicit |
Construct a new ASTLowerer object.
debug | the debug level |
Definition at line 18 of file ASTLowerer.cpp.
|
private |
Register a given node in the symbol table.
Can throw if the table is full
sym |
Definition at line 712 of file ASTLowerer.cpp.
References buildAndThrowError(), m_symbols, and Ark::internal::Node::string().
Referenced by compileExpression(), compileFunction(), compileLetMutSet(), compileSymbol(), and handleCalls().
|
private |
Register a given node in the value table.
Can throw if the table is full
x |
Definition at line 728 of file ASTLowerer.cpp.
References buildAndThrowError(), and m_values.
Referenced by compileExpression(), compileFunction(), and compilePluginImport().
|
private |
Register a page id (function reference) in the value table.
Can throw if the table is full
page_id | |
current | A reference to the current node, for context |
Definition at line 744 of file ASTLowerer.cpp.
References buildAndThrowError(), and m_values.
|
staticprivate |
Throw a nice error message.
message | |
node |
Definition at line 133 of file ASTLowerer.cpp.
References Ark::internal::Node::col(), Ark::internal::Node::filename(), Ark::internal::Node::line(), and Ark::internal::Node::repr().
Referenced by addSymbol(), addValue(), addValue(), compileExpression(), compileFunction(), compileLetMutSet(), compileListInstruction(), compileSymbol(), compileWhile(), handleCalls(), and pushFunctionCallArguments().
|
private |
Compile an expression (a node) recursively.
x | the Node to compile |
p | the current page number we're on |
is_result_unused | |
is_terminal |
Definition at line 138 of file ASTLowerer.cpp.
References addSymbol(), addValue(), Ark::internal::Namespace::ast, Ark::internal::Begin, buildAndThrowError(), Ark::internal::BUILTIN, compileExpression(), compileFunction(), compileIf(), compileLetMutSet(), compileListInstruction(), compilePluginImport(), compileSymbol(), compileWhile(), Ark::internal::Node::constArkNamespace(), Ark::internal::Node::constList(), Ark::internal::DEL, Ark::internal::Del, Ark::internal::Field, Ark::internal::Node::filename(), Ark::internal::Fun, Ark::internal::GET_FIELD, getBuiltin(), getListInstruction(), handleCalls(), Ark::internal::If, Ark::internal::Import, Ark::internal::Keyword, Ark::internal::Let, Ark::internal::Node::line(), Ark::internal::List, Ark::internal::Node::list(), Ark::internal::LOAD_CONST, Ark::internal::Mut, Ark::internal::Namespace, Ark::internal::Node::nodeType(), Ark::internal::Number, page(), Ark::internal::Set, Ark::internal::String, Ark::Value::string(), Ark::internal::Symbol, Ark::internal::typeToString(), Ark::internal::Unused, and Ark::internal::While.
Referenced by compileExpression(), compileFunction(), compileIf(), compileLetMutSet(), compileListInstruction(), compileWhile(), handleCalls(), process(), and pushFunctionCallArguments().
Definition at line 362 of file ASTLowerer.cpp.
References Ark::internal::LocalsLocator::addLocal(), addSymbol(), addValue(), buildAndThrowError(), Ark::internal::CAPTURE, Ark::internal::Capture, Ark::internal::LocalsLocator::Closure, compileExpression(), Ark::internal::Node::constList(), Ark::internal::LocalsLocator::createScope(), Ark::internal::LocalsLocator::deleteScope(), Ark::internal::LocalsLocator::Function, Ark::internal::Node::isAnonymousFunction(), Ark::internal::List, Ark::internal::Node::list(), Ark::internal::LOAD_CONST, m_code_pages, m_locals_locator, m_opened_vars, Ark::internal::MAKE_CLOSURE, page(), Ark::internal::POP, Ark::internal::RET, Ark::internal::STORE, Ark::internal::Symbol, Ark::internal::typeToString(), and warning().
Referenced by compileExpression().
|
private |
Definition at line 328 of file ASTLowerer.cpp.
References compileExpression(), Ark::internal::Node::constList(), Ark::internal::LocalsLocator::dropVarsForBranch(), Ark::internal::IR::Entity::Goto(), Ark::internal::IR::Entity::GotoIf(), Ark::internal::IR::Entity::Label(), Ark::internal::Node::list(), m_current_label, m_locals_locator, page(), and Ark::internal::LocalsLocator::saveScopeLengthForBranch().
Referenced by compileExpression().
Definition at line 425 of file ASTLowerer.cpp.
References Ark::internal::LocalsLocator::addLocal(), addSymbol(), buildAndThrowError(), compileExpression(), Ark::internal::Node::constList(), Ark::internal::Node::filename(), Ark::internal::Let, Ark::internal::Node::line(), Ark::internal::Node::list(), m_locals_locator, m_opened_vars, Ark::internal::Mut, page(), Ark::internal::SET_VAL, Ark::internal::STORE, Ark::internal::Symbol, and Ark::internal::typeToString().
Referenced by compileExpression().
|
private |
Definition at line 267 of file ASTLowerer.cpp.
References Ark::internal::APPEND, Ark::internal::APPEND_IN_PLACE, buildAndThrowError(), compileExpression(), Ark::internal::CONCAT, Ark::internal::CONCAT_IN_PLACE, Ark::internal::Node::constList(), getListInstruction(), Ark::internal::LIST, Ark::internal::Node::list(), nodeProducesOutput(), page(), Ark::internal::POP, Ark::internal::POP_LIST, Ark::internal::POP_LIST_IN_PLACE, Ark::internal::SET_AT_2_INDEX, Ark::internal::SET_AT_INDEX, and warning().
Referenced by compileExpression().
Definition at line 493 of file ASTLowerer.cpp.
References addValue(), Ark::internal::Node::constList(), Ark::internal::Node::filename(), Ark::internal::Node::line(), page(), Ark::internal::PLUGIN, and Ark::internal::String.
Referenced by compileExpression().
Definition at line 243 of file ASTLowerer.cpp.
References addSymbol(), buildAndThrowError(), Ark::internal::BUILTIN, getBuiltin(), getOperator(), Ark::internal::LOAD_SYMBOL, Ark::internal::LOAD_SYMBOL_BY_INDEX, Ark::internal::LocalsLocator::lookupLastScopeByName(), m_locals_locator, page(), Ark::internal::POP, Ark::internal::Node::string(), and warning().
Referenced by compileExpression().
Definition at line 460 of file ASTLowerer.cpp.
References buildAndThrowError(), compileExpression(), Ark::internal::Node::constList(), Ark::internal::CREATE_SCOPE, Ark::internal::LocalsLocator::createScope(), Ark::internal::LocalsLocator::deleteScope(), Ark::internal::Node::filename(), Ark::internal::IR::Entity::Goto(), Ark::internal::IR::Entity::GotoIf(), Ark::internal::IR::Entity::Label(), Ark::internal::Node::line(), Ark::internal::Node::list(), m_current_label, m_locals_locator, page(), Ark::internal::POP_SCOPE, and Ark::internal::RESET_SCOPE_JUMP.
Referenced by compileExpression().
|
staticprivatenoexcept |
Checking if a symbol is a builtin.
name | symbol name |
Definition at line 59 of file ASTLowerer.cpp.
References Ark::internal::Builtins::builtins.
Referenced by compileExpression(), and compileSymbol().
|
staticprivatenoexcept |
Checking if a symbol is a list instruction.
name |
Definition at line 70 of file ASTLowerer.cpp.
References Ark::internal::LIST, and Ark::internal::Language::listInstructions.
Referenced by compileExpression(), and compileListInstruction().
|
staticprivatenoexcept |
Checking if a symbol is an operator.
name | symbol name |
Definition at line 51 of file ASTLowerer.cpp.
References Ark::internal::FIRST_OPERATOR, and Ark::internal::Language::operators.
Referenced by compileSymbol(), and handleCalls().
|
private |
Definition at line 539 of file ASTLowerer.cpp.
References Ark::internal::ADD, addSymbol(), Ark::internal::Language::And, Ark::internal::ASSERT, Ark::internal::AT_AT, buildAndThrowError(), Ark::internal::CALL, Ark::internal::Capture, compileExpression(), Ark::internal::Node::constList(), Ark::internal::DIV, Ark::internal::Node::filename(), Ark::internal::FIRST_OPERATOR, Ark::internal::GET_CURRENT_PAGE_ADDR, getOperator(), Ark::internal::IR::Entity::Goto(), isTernaryInst(), isUnaryInst(), Ark::internal::JUMP, Ark::internal::IR::Entity::Label(), Ark::internal::Node::line(), Ark::internal::Node::list(), m_current_label, m_opened_vars, m_temp_pages, Ark::internal::MOD, Ark::internal::MUL, nodeProducesOutput(), Ark::internal::Node::nodeType(), Ark::internal::Language::operators, Ark::internal::Language::Or, page(), Ark::internal::POP, Ark::internal::PUSH_RETURN_ADDRESS, pushFunctionCallArguments(), Ark::internal::Node::repr(), Ark::internal::SHORTCIRCUIT_AND, Ark::internal::SHORTCIRCUIT_OR, Ark::internal::Node::string(), Ark::internal::SUB, and Ark::internal::Symbol.
Referenced by compileExpression().
|
nodiscardnoexcept |
Return the IR blocks (one per scope)
Definition at line 36 of file ASTLowerer.cpp.
References m_code_pages.
Referenced by Ark::Welder::generateBytecode().
|
staticprivatenoexcept |
Check if a given instruction is ternary (takes three arguments)
inst |
Definition at line 116 of file ASTLowerer.cpp.
References Ark::internal::AT_AT.
Referenced by handleCalls().
|
staticprivatenoexcept |
Check if a given instruction is unary (takes only one argument)
inst |
Definition at line 96 of file ASTLowerer.cpp.
References Ark::internal::EMPTY, Ark::internal::HEAD, Ark::internal::ISNIL, Ark::internal::LEN, Ark::internal::NOT, Ark::internal::TAIL, Ark::internal::TO_NUM, Ark::internal::TO_STR, and Ark::internal::TYPE.
Referenced by handleCalls().
|
staticprivate |
Checks if a node is a list and has a keyboard as its first node, indicating if it's producing a value on the stack or not
node | node to check |
Definition at line 78 of file ASTLowerer.cpp.
References Ark::internal::Begin, Ark::internal::Node::constList(), Ark::internal::Fun, Ark::internal::If, Ark::internal::Keyword, Ark::internal::List, nodeProducesOutput(), Ark::internal::Node::nodeType(), Ark::internal::Symbol, and Ark::internal::Language::UpdateRef.
Referenced by compileListInstruction(), handleCalls(), nodeProducesOutput(), and pushFunctionCallArguments().
helper functions to get a temp or finalized code page
page | page descriptor |
Definition at line 100 of file ASTLowerer.hpp.
Referenced by compileExpression(), compileFunction(), compileIf(), compileLetMutSet(), compileListInstruction(), compilePluginImport(), compileSymbol(), compileWhile(), and handleCalls().
void Ark::internal::ASTLowerer::process | ( | Node & | ast | ) |
Start the compilation.
ast |
Definition at line 22 of file ASTLowerer.cpp.
References compileExpression(), m_code_pages, m_logger, Ark::internal::Logger::traceEnd(), and Ark::internal::Logger::traceStart().
Referenced by Ark::Welder::generateBytecode().
|
private |
Definition at line 512 of file ASTLowerer.cpp.
References buildAndThrowError(), compileExpression(), Ark::internal::Node::constList(), Ark::internal::Node::list(), and nodeProducesOutput().
Referenced by handleCalls().
|
nodiscardnoexcept |
Return the symbol table pre-computed.
Definition at line 41 of file ASTLowerer.cpp.
References m_symbols.
Referenced by Ark::Welder::generateBytecode().
|
nodiscardnoexcept |
Return the value table pre-computed.
Definition at line 46 of file ASTLowerer.cpp.
References m_values.
Referenced by Ark::Welder::generateBytecode().
|
staticprivate |
Display a warning message.
message | |
node |
Definition at line 128 of file ASTLowerer.cpp.
References Ark::Diagnostics::makeContextWithNode().
Referenced by compileFunction(), compileListInstruction(), and compileSymbol().
|
private |
Definition at line 87 of file ASTLowerer.hpp.
Referenced by compileFunction(), intermediateRepresentation(), and process().
|
private |
Definition at line 89 of file ASTLowerer.hpp.
Referenced by compileIf(), compileWhile(), and handleCalls().
|
private |
Definition at line 82 of file ASTLowerer.hpp.
Referenced by compileFunction(), compileIf(), compileLetMutSet(), compileSymbol(), and compileWhile().
|
private |
Definition at line 92 of file ASTLowerer.hpp.
Referenced by process().
|
private |
stack of vars we are currently declaring
Definition at line 90 of file ASTLowerer.hpp.
Referenced by compileFunction(), compileLetMutSet(), and handleCalls().
|
private |
Definition at line 85 of file ASTLowerer.hpp.
Referenced by addSymbol(), and symbols().
|
private |
we need temporary code pages for some compilations passes
Definition at line 88 of file ASTLowerer.hpp.
Referenced by handleCalls().
|
private |
Definition at line 86 of file ASTLowerer.hpp.
Referenced by addValue(), addValue(), and values().