![]() |
ArkScript
A small, lisp-inspired, functional scripting language
|
The ArkScript AST to IR compiler. More...
#include <ASTLowerer.hpp>
Classes | |
| struct | Page |
| struct | Var |
Public Member Functions | |
| ASTLowerer (unsigned debug) | |
| Construct a new ASTLowerer object. | |
| void | addToTables (const std::vector< std::string > &symbols, const std::vector< ValTableElem > &constants) |
| Pre-fill tables (used by the debugger) | |
| void | offsetPagesBy (std::size_t offset) |
| Start bytecode pages at a given offset (by default, 0) | |
| 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. | |
| IR::label_t | lastLabel () const noexcept |
Public Member Functions inherited from Ark::internal::Pass | |
| Pass (std::string name, unsigned debug_level) | |
| Construct a new Pass object. | |
| virtual | ~Pass ()=default |
| void | configureLogger (std::ostream &os) |
| Set a custom output stream for the logger. | |
Private Types | |
| enum class | ErrorKind { InvalidNodeMacro , InvalidNodeNoReturnValue , InvalidNodeInOperatorNoReturnValue , InvalidNodeInTailCallNoReturnValue } |
Private Member Functions | |
| Page | createNewCodePage (PageCreationData &&args=PageCreationData {}) noexcept |
| IR::Block & | block (const Page page) noexcept |
| IR::Block::vec_t & | page (const Page page) noexcept |
| helper functions to get a temp or finalised code page | |
| bool | isFunctionCallingItself (const std::string &name) noexcept |
| Check if we are in a recursive self call. | |
| void | warning (const std::string &message, const Node &node) |
| Display a warning message. | |
| void | compileExpression (Node &x, Page p, bool is_result_unused, bool is_terminal, bool can_use_ref) |
| Compile an expression (a node) recursively. | |
| void | compileSymbol (const Node &x, Page p, bool is_result_unused, bool can_use_ref) |
| void | compileListInstruction (Node &x, Page p, bool is_result_unused) |
| void | compileApplyInstruction (Node &x, Page p, bool is_result_unused) |
| void | compileIf (Node &x, Page p, bool is_result_unused, bool is_terminal, bool can_use_ref) |
| void | compileFunction (Node &x, Page p, bool is_result_unused) |
| void | setFunctionMetadata (Page p, std::size_t arg_count, bool mutates_args) |
| void | compileLetMutSet (Keyword n, Node &x, Page p, bool is_result_unused) |
| void | compileWhile (Node &x, Page p) |
| void | compilePluginImport (const 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, bool can_use_ref) |
| void | handleShortcircuit (Node &x, Page p, bool can_use_ref) |
| void | handleOperator (Node &x, Page p, Instruction op) |
| bool | handleFunctionCall (Node &x, Page p, 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 | isBreakpoint (const Node &node) |
| 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 bool | isRepeatableOperation (Instruction inst) noexcept |
| Check if an operator can be repeated. | |
| static void | buildAndThrowError (const std::string &message, const Node &node) |
| Throw a nice error message. | |
| static void | makeError (ErrorKind kind, const Node &node, const std::string &additional_ctx) |
| Throw a nice error message, using a message builder. | |
Private Attributes | |
| LocalsLocator | m_locals_locator |
| std::vector< std::string > | m_symbols |
| std::vector< ValTableElem > | m_values |
| std::size_t | m_start_page_at_offset = 0 |
| Used to offset the page numbers when compiling code in the debugger. | |
| 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< Var > | m_opened_vars |
| stack of vars we are currently declaring | |
Additional Inherited Members | |
Protected Attributes inherited from Ark::internal::Pass | |
| Logger | m_logger |
The ArkScript AST to IR compiler.
Definition at line 48 of file ASTLowerer.hpp.
|
strongprivate |
| Enumerator | |
|---|---|
| InvalidNodeMacro | |
| InvalidNodeNoReturnValue | |
| InvalidNodeInOperatorNoReturnValue | |
| InvalidNodeInTailCallNoReturnValue | |
Definition at line 130 of file ASTLowerer.hpp.
|
explicit |
Construct a new ASTLowerer object.
| debug | the debug level |
Definition at line 28 of file ASTLowerer.cpp.
|
private |
Register a given node in the symbol table.
Can throw if the table is full
| sym |
Definition at line 1040 of file ASTLowerer.cpp.
References buildAndThrowError(), m_symbols, Ark::MaxValue16Bits, and Ark::internal::Node::string().
Referenced by compileExpression(), compileFunction(), compileLetMutSet(), compileSymbol(), and handleFunctionCall().
| void Ark::internal::ASTLowerer::addToTables | ( | const std::vector< std::string > & | symbols, |
| const std::vector< ValTableElem > & | constants ) |
Pre-fill tables (used by the debugger)
| symbols | |
| constants |
Definition at line 32 of file ASTLowerer.cpp.
References m_symbols, m_values, and symbols().
Referenced by Ark::Welder::generateBytecodeUsingTables().
|
private |
Register a given node in the value table.
Can throw if the table is full
| x |
Definition at line 1056 of file ASTLowerer.cpp.
References buildAndThrowError(), m_values, and Ark::MaxValue16Bits.
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 1072 of file ASTLowerer.cpp.
References buildAndThrowError(), m_values, and Ark::MaxValue16Bits.
Definition at line 157 of file ASTLowerer.hpp.
Referenced by setFunctionMetadata().
|
staticprivate |
Throw a nice error message.
| message | |
| node |
Definition at line 183 of file ASTLowerer.cpp.
References Ark::internal::Node::filename(), and Ark::internal::Node::position().
Referenced by addSymbol(), addValue(), addValue(), compileApplyInstruction(), compileExpression(), compileFunction(), compileIf(), compileLetMutSet(), compileListInstruction(), compileSymbol(), handleFunctionCall(), handleOperator(), handleShortcircuit(), and makeError().
|
private |
Definition at line 442 of file ASTLowerer.cpp.
References buildAndThrowError(), compileExpression(), Ark::internal::Node::constList(), Ark::internal::IR::Entity::Goto(), InvalidNodeNoReturnValue, Ark::internal::IR::Entity::Label(), Ark::internal::Node::list(), m_current_label, makeError(), nodeProducesOutput(), and page().
Referenced by compileExpression().
|
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 | |
| can_use_ref |
Definition at line 212 of file ASTLowerer.cpp.
References addSymbol(), addValue(), Ark::internal::Language::Apply, Ark::internal::Namespace::ast, Ark::internal::Begin, buildAndThrowError(), compileApplyInstruction(), compileExpression(), compileFunction(), compileIf(), compileLetMutSet(), compileListInstruction(), compilePluginImport(), compileSymbol(), compileWhile(), Ark::internal::Node::constArkNamespace(), Ark::internal::Node::constList(), Ark::internal::Del, Ark::internal::Field, Ark::internal::Node::filename(), Ark::internal::Fun, getBuiltin(), getListInstruction(), handleCalls(), Ark::internal::If, Ark::internal::Import, isBreakpoint(), Ark::internal::Keyword, Ark::internal::Let, Ark::internal::FilePos::line, Ark::internal::List, Ark::internal::Node::list(), Ark::internal::Mut, Ark::internal::Namespace, Ark::internal::Node::nodeType(), Ark::internal::Number, page(), Ark::internal::Node::position(), Ark::internal::Set, Ark::internal::FileSpan::start, Ark::internal::String, Ark::Value::string(), Ark::internal::Symbol, Ark::internal::typeToString(), Ark::internal::Unused, and Ark::internal::While.
Referenced by compileApplyInstruction(), compileExpression(), compileFunction(), compileIf(), compileLetMutSet(), compileListInstruction(), compileWhile(), handleFunctionCall(), handleOperator(), handleShortcircuit(), process(), and pushFunctionCallArguments().
Definition at line 520 of file ASTLowerer.cpp.
References Ark::internal::LocalsLocator::addLocal(), addSymbol(), addValue(), Ark::internal::IR::AnonymousBlockName, buildAndThrowError(), Ark::internal::Capture, Ark::internal::LocalsLocator::Closure, compileExpression(), Ark::internal::Node::constList(), createNewCodePage(), Ark::internal::LocalsLocator::createScope(), Ark::internal::LocalsLocator::deleteScope(), Ark::internal::LocalsLocator::Function, Ark::internal::ASTLowerer::Page::index, InvalidNodeMacro, Ark::internal::Node::isAnonymousFunction(), Ark::internal::List, Ark::internal::Node::list(), m_locals_locator, m_opened_vars, makeError(), Ark::internal::MutArg, page(), Ark::internal::RefArg, setFunctionMetadata(), Ark::internal::Symbol, Ark::internal::typeToString(), and warning().
Referenced by compileExpression().
|
private |
Definition at line 468 of file ASTLowerer.cpp.
References buildAndThrowError(), 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::List, Ark::internal::Node::list(), m_current_label, m_locals_locator, Ark::internal::LocalsLocator::markLastLocalAsUnreachable(), page(), and Ark::internal::LocalsLocator::saveScopeLengthForBranch().
Referenced by compileExpression().
|
private |
Definition at line 635 of file ASTLowerer.cpp.
References Ark::internal::LocalsLocator::addLocal(), addSymbol(), buildAndThrowError(), compileExpression(), Ark::internal::Node::constList(), Ark::internal::Node::filename(), InvalidNodeMacro, Ark::internal::Let, Ark::internal::FilePos::line, Ark::internal::List, Ark::internal::Node::list(), m_locals_locator, m_opened_vars, makeError(), Ark::internal::Mut, Ark::internal::MutArg, page(), Ark::internal::Node::position(), Ark::internal::RefArg, Ark::internal::FileSpan::start, Ark::internal::Symbol, and Ark::internal::typeToString().
Referenced by compileExpression().
|
private |
Definition at line 361 of file ASTLowerer.cpp.
References buildAndThrowError(), compileExpression(), Ark::internal::Node::constList(), getListInstruction(), InvalidNodeNoReturnValue, Ark::internal::Node::list(), makeError(), Ark::MaxValue16Bits, nodeProducesOutput(), page(), Ark::Value::string(), and warning().
Referenced by compileExpression().
Definition at line 718 of file ASTLowerer.cpp.
References addValue(), Ark::internal::Node::constList(), Ark::internal::Node::filename(), Ark::internal::FilePos::line, page(), Ark::internal::Node::position(), Ark::internal::FileSpan::start, and Ark::internal::String.
Referenced by compileExpression().
|
private |
Definition at line 324 of file ASTLowerer.cpp.
References addSymbol(), Ark::internal::Language::And, buildAndThrowError(), Ark::internal::Node::filename(), getBuiltin(), getOperator(), Ark::internal::FilePos::line, Ark::internal::LocalsLocator::lookupLastScopeByName(), m_locals_locator, Ark::internal::Language::Or, page(), Ark::internal::Node::position(), Ark::internal::FileSpan::start, Ark::internal::Node::string(), Ark::internal::Language::UpdateRef, and warning().
Referenced by compileExpression(), and pushFunctionCallArguments().
Definition at line 685 of file ASTLowerer.cpp.
References compileExpression(), Ark::internal::Node::constList(), Ark::internal::LocalsLocator::createScope(), Ark::internal::LocalsLocator::deleteScope(), Ark::internal::Node::filename(), Ark::internal::IR::Entity::Goto(), Ark::internal::IR::Entity::GotoIf(), InvalidNodeMacro, Ark::internal::IR::Entity::Label(), Ark::internal::FilePos::line, Ark::internal::Node::list(), m_current_label, m_locals_locator, makeError(), page(), Ark::internal::Node::position(), and Ark::internal::FileSpan::start.
Referenced by compileExpression().
|
inlineprivatenoexcept |
Definition at line 138 of file ASTLowerer.hpp.
Referenced by compileFunction(), handleFunctionCall(), and process().
|
staticprivatenoexcept |
Checking if a symbol is a builtin.
| name | symbol name |
Definition at line 81 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 92 of file ASTLowerer.cpp.
References Ark::internal::Language::listInstructions.
Referenced by compileExpression(), and compileListInstruction().
|
staticprivatenoexcept |
Checking if a symbol is an operator.
| name | symbol name |
Definition at line 73 of file ASTLowerer.cpp.
References Ark::internal::FirstOperator, and Ark::internal::Language::operators.
Referenced by compileSymbol(), and handleCalls().
|
private |
Definition at line 761 of file ASTLowerer.cpp.
References Ark::internal::Language::And, Ark::internal::Node::constList(), getOperator(), handleFunctionCall(), handleOperator(), handleShortcircuit(), Ark::internal::Node::nodeType(), Ark::internal::Language::Or, page(), Ark::internal::Node::string(), and Ark::internal::Symbol.
Referenced by compileExpression().
Definition at line 890 of file ASTLowerer.cpp.
References addSymbol(), buildAndThrowError(), Ark::internal::Builtin, Ark::internal::Builtins::builtins, Ark::internal::Capture, Ark::internal::Classic, compileExpression(), Ark::internal::Node::constList(), createNewCodePage(), Ark::internal::Node::filename(), Ark::internal::IR::Entity::Goto(), isBreakpoint(), isFunctionCallingItself(), Ark::internal::IR::Entity::Label(), Ark::internal::FilePos::line, Ark::internal::Node::list(), m_current_label, m_opened_vars, m_temp_pages, nodeProducesOutput(), Ark::internal::Node::nodeType(), page(), Ark::internal::Node::position(), pushFunctionCallArguments(), Ark::internal::Node::repr(), Ark::internal::SelfNotRecursive, Ark::internal::FileSpan::start, Ark::internal::Node::string(), Ark::internal::Symbol, and Ark::internal::SymbolByIndex.
Referenced by handleCalls().
|
private |
Definition at line 837 of file ASTLowerer.cpp.
References buildAndThrowError(), compileExpression(), Ark::internal::Node::constList(), Ark::internal::Node::filename(), Ark::internal::FirstOperator, InvalidNodeInOperatorNoReturnValue, isBreakpoint(), isRepeatableOperation(), isTernaryInst(), isUnaryInst(), Ark::internal::FilePos::line, Ark::internal::Node::list(), makeError(), nodeProducesOutput(), Ark::internal::Language::operators, page(), Ark::internal::Node::position(), Ark::internal::Node::repr(), and Ark::internal::FileSpan::start.
Referenced by handleCalls().
Definition at line 794 of file ASTLowerer.cpp.
References Ark::internal::Language::And, buildAndThrowError(), compileExpression(), Ark::internal::Node::constList(), Ark::internal::IR::Entity::Goto(), Ark::internal::IR::Entity::Label(), Ark::internal::Node::list(), m_current_label, nodeProducesOutput(), page(), and Ark::internal::Node::string().
Referenced by handleCalls().
|
nodiscardnoexcept |
Return the IR blocks (one per scope)
Definition at line 58 of file ASTLowerer.cpp.
References m_code_pages.
Referenced by Ark::Welder::generateBytecode().
|
staticprivate |
Checks if a node is a list and is a call to 'breakpoint'
| node | node to check |
Definition at line 100 of file ASTLowerer.cpp.
References Ark::internal::Node::constList(), Ark::internal::List, Ark::internal::Node::nodeType(), and Ark::internal::Symbol.
Referenced by compileExpression(), handleFunctionCall(), handleOperator(), and pushFunctionCallArguments().
|
inlinenodiscardprivatenoexcept |
Check if we are in a recursive self call.
| name | symbol name being compiled |
Definition at line 183 of file ASTLowerer.hpp.
Referenced by handleFunctionCall().
|
staticprivatenoexcept |
Check if an operator can be repeated.
| inst |
Definition at line 163 of file ASTLowerer.cpp.
Referenced by handleOperator().
|
staticprivatenoexcept |
Check if a given instruction is ternary (takes three arguments)
| inst |
Definition at line 151 of file ASTLowerer.cpp.
Referenced by handleOperator().
|
staticprivatenoexcept |
Check if a given instruction is unary (takes only one argument)
| inst |
Definition at line 131 of file ASTLowerer.cpp.
Referenced by handleOperator().
|
inlinenodiscardnoexcept |
Definition at line 101 of file ASTLowerer.hpp.
Referenced by Ark::Welder::generateBytecode().
|
staticprivate |
Throw a nice error message, using a message builder.
| kind | error kind |
| node | erroneous node |
| additional_ctx | optional context for the error, e.g. the macro name |
Definition at line 188 of file ASTLowerer.cpp.
References buildAndThrowError(), InvalidNodeInOperatorNoReturnValue, InvalidNodeInTailCallNoReturnValue, InvalidNodeMacro, and InvalidNodeNoReturnValue.
Referenced by compileApplyInstruction(), compileFunction(), compileLetMutSet(), compileListInstruction(), compileWhile(), handleOperator(), and pushFunctionCallArguments().
|
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 107 of file ASTLowerer.cpp.
References Ark::internal::Begin, Ark::internal::Node::constList(), Ark::internal::Fun, Ark::internal::If, Ark::internal::Keyword, Ark::internal::Let, Ark::internal::List, Ark::internal::Mut, nodeProducesOutput(), Ark::internal::Node::nodeType(), Ark::internal::Set, and Ark::internal::Symbol.
Referenced by compileApplyInstruction(), compileListInstruction(), handleFunctionCall(), handleOperator(), handleShortcircuit(), nodeProducesOutput(), and pushFunctionCallArguments().
| void Ark::internal::ASTLowerer::offsetPagesBy | ( | std::size_t | offset | ) |
Start bytecode pages at a given offset (by default, 0)
| offset |
Definition at line 38 of file ASTLowerer.cpp.
References m_start_page_at_offset.
Referenced by Ark::Welder::generateBytecodeUsingTables().
|
inlineprivatenoexcept |
helper functions to get a temp or finalised code page
| page | page descriptor |
Definition at line 170 of file ASTLowerer.hpp.
Referenced by compileApplyInstruction(), compileExpression(), compileFunction(), compileIf(), compileLetMutSet(), compileListInstruction(), compilePluginImport(), compileSymbol(), compileWhile(), handleCalls(), handleFunctionCall(), handleOperator(), handleShortcircuit(), and setFunctionMetadata().
| void Ark::internal::ASTLowerer::process | ( | Node & | ast | ) |
Start the compilation.
| ast |
Definition at line 43 of file ASTLowerer.cpp.
References compileExpression(), createNewCodePage(), Ark::internal::Pass::m_logger, Ark::internal::Logger::traceEnd(), and Ark::internal::Logger::traceStart().
Referenced by Ark::Welder::generateBytecode().
|
private |
Definition at line 737 of file ASTLowerer.cpp.
References compileExpression(), compileSymbol(), Ark::internal::Node::constList(), InvalidNodeInTailCallNoReturnValue, InvalidNodeNoReturnValue, isBreakpoint(), Ark::internal::Node::list(), makeError(), nodeProducesOutput(), and Ark::internal::Symbol.
Referenced by handleFunctionCall().
|
private |
Definition at line 615 of file ASTLowerer.cpp.
References Ark::internal::IR::Block::Metadata::argument_count, block(), Ark::internal::IR::Block::Metadata::is_mutating_args, Ark::internal::IR::Block::Metadata::is_recursive, Ark::internal::IR::Block::Metadata::is_simple, Ark::internal::IR::Block::metadata, and page().
Referenced by compileFunction().
|
nodiscardnoexcept |
Return the symbol table pre-computed.
Definition at line 63 of file ASTLowerer.cpp.
References m_symbols.
Referenced by addToTables(), and Ark::Welder::generateBytecode().
|
nodiscardnoexcept |
Return the value table pre-computed.
Definition at line 68 of file ASTLowerer.cpp.
References m_values.
Referenced by Ark::Welder::generateBytecode().
|
private |
Display a warning message.
| message | |
| node |
Definition at line 178 of file ASTLowerer.cpp.
References Ark::internal::Logger::colorize(), Ark::internal::Pass::m_logger, Ark::Diagnostics::makeContextWithNode(), and Ark::internal::Logger::warn().
Referenced by compileFunction(), compileListInstruction(), and compileSymbol().
|
private |
Definition at line 125 of file ASTLowerer.hpp.
Referenced by intermediateRepresentation().
|
private |
Definition at line 127 of file ASTLowerer.hpp.
Referenced by compileApplyInstruction(), compileIf(), compileWhile(), handleFunctionCall(), and handleShortcircuit().
|
private |
Definition at line 119 of file ASTLowerer.hpp.
Referenced by compileFunction(), compileIf(), compileLetMutSet(), compileSymbol(), and compileWhile().
|
private |
stack of vars we are currently declaring
Definition at line 128 of file ASTLowerer.hpp.
Referenced by compileFunction(), compileLetMutSet(), and handleFunctionCall().
|
private |
Used to offset the page numbers when compiling code in the debugger.
Definition at line 124 of file ASTLowerer.hpp.
Referenced by offsetPagesBy().
|
private |
Definition at line 122 of file ASTLowerer.hpp.
Referenced by addSymbol(), addToTables(), and symbols().
|
private |
we need temporary code pages for some compilations passes
Definition at line 126 of file ASTLowerer.hpp.
Referenced by handleFunctionCall().
|
private |
Definition at line 123 of file ASTLowerer.hpp.
Referenced by addToTables(), addValue(), addValue(), and values().