ArkScript
A small, fast, functional and scripting language for video games
|
The ArkScript bytecode compiler. More...
#include <Compiler.hpp>
Classes | |
struct | Page |
Public Member Functions | |
Compiler (unsigned debug) | |
Construct a new Compiler object. | |
void | process (const 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 (const Node &x, Page p, bool is_result_unused, bool is_terminal, const std::string &var_name="") |
Compile an expression (a node) recursively. | |
void | compileSymbol (const Node &x, Page p, bool is_result_unused) |
void | compileListInstruction (const Node &c0, const Node &x, Page p, bool is_result_unused) |
void | compileIf (const Node &x, Page p, bool is_result_unused, bool is_terminal, const std::string &var_name) |
void | compileFunction (const Node &x, Page p, bool is_result_unused, const std::string &var_name) |
void | compileLetMutSet (Keyword n, const Node &x, Page p) |
void | compileWhile (const Node &x, Page p) |
void | compilePluginImport (const Node &x, Page p) |
void | handleCalls (const Node &x, Page p, bool is_result_unused, bool is_terminal, const std::string &var_name) |
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 | compilerWarning (const std::string &message, const Node &node) |
Display a warning message. | |
static void | throwCompilerError (const std::string &message, const Node &node) |
Throw a nice error message. | |
Private Attributes | |
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 |
unsigned | m_debug |
the debug level of the compiler | |
Logger | m_logger |
The ArkScript bytecode compiler.
Definition at line 36 of file Compiler.hpp.
|
explicit |
Construct a new Compiler object.
debug | the debug level |
Definition at line 21 of file Compiler.cpp.
|
private |
Register a given node in the symbol table.
Can throw if the table is full
sym |
Definition at line 625 of file Compiler.cpp.
References m_symbols, Ark::internal::Node::string(), and throwCompilerError().
Referenced by compileExpression(), compileFunction(), compileLetMutSet(), and compileSymbol().
|
private |
Register a given node in the value table.
Can throw if the table is full
x |
Definition at line 641 of file Compiler.cpp.
References m_values, and throwCompilerError().
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 657 of file Compiler.cpp.
References m_values, and throwCompilerError().
|
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 | |
var_name |
Definition at line 132 of file Compiler.cpp.
References addSymbol(), addValue(), Ark::internal::Namespace::ast, Ark::internal::Begin, 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::Fun, Ark::internal::GET_FIELD, getBuiltin(), getListInstruction(), handleCalls(), Ark::internal::If, Ark::internal::Import, Ark::internal::Keyword, Ark::internal::Let, Ark::internal::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::internal::Symbol, throwCompilerError(), Ark::internal::typeToString(), Ark::internal::Unused, and Ark::internal::While.
Referenced by compileExpression(), compileFunction(), compileIf(), compileLetMutSet(), compileListInstruction(), compileWhile(), handleCalls(), and process().
|
private |
Definition at line 338 of file Compiler.cpp.
References addSymbol(), addValue(), Ark::internal::CAPTURE, Ark::internal::Capture, compileExpression(), compilerWarning(), Ark::internal::Node::constList(), Ark::internal::List, Ark::internal::LOAD_CONST, m_code_pages, Ark::internal::MAKE_CLOSURE, page(), Ark::internal::POP, Ark::internal::RET, Ark::internal::STORE, Ark::internal::Symbol, throwCompilerError(), and Ark::internal::typeToString().
Referenced by compileExpression().
|
private |
Definition at line 313 of file Compiler.cpp.
References compileExpression(), Ark::internal::Node::constList(), Ark::internal::IR::Entity::Goto(), Ark::internal::IR::Entity::GotoIf(), Ark::internal::IR::Entity::Label(), m_current_label, and page().
Referenced by compileExpression().
Definition at line 383 of file Compiler.cpp.
References addSymbol(), compileExpression(), Ark::internal::Node::constList(), Ark::internal::Let, Ark::internal::Mut, page(), Ark::internal::SET_VAL, Ark::internal::STORE, Ark::internal::Symbol, throwCompilerError(), and Ark::internal::typeToString().
Referenced by compileExpression().
|
private |
Definition at line 254 of file Compiler.cpp.
References Ark::internal::APPEND, Ark::internal::APPEND_IN_PLACE, compileExpression(), compilerWarning(), Ark::internal::CONCAT, Ark::internal::CONCAT_IN_PLACE, Ark::internal::Node::constList(), getListInstruction(), Ark::internal::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, Ark::internal::Node::string(), and throwCompilerError().
Referenced by compileExpression().
Definition at line 431 of file Compiler.cpp.
References addValue(), Ark::internal::Node::constList(), page(), Ark::internal::PLUGIN, and Ark::internal::String.
Referenced by compileExpression().
|
staticprivate |
Display a warning message.
message | |
node |
Definition at line 122 of file Compiler.cpp.
References Ark::Diagnostics::makeContextWithNode().
Referenced by compileFunction(), compileListInstruction(), and compileSymbol().
|
private |
Definition at line 236 of file Compiler.cpp.
References addSymbol(), Ark::internal::BUILTIN, compilerWarning(), getBuiltin(), getOperator(), Ark::internal::LOAD_SYMBOL, page(), Ark::internal::POP, Ark::internal::Node::string(), and throwCompilerError().
Referenced by compileExpression().
Definition at line 404 of file Compiler.cpp.
References compileExpression(), Ark::internal::Node::constList(), Ark::internal::CREATE_SCOPE, Ark::internal::IR::Entity::Goto(), Ark::internal::IR::Entity::GotoIf(), Ark::internal::IR::Entity::Label(), m_current_label, page(), Ark::internal::POP_SCOPE, and throwCompilerError().
Referenced by compileExpression().
|
staticprivatenoexcept |
Checking if a symbol is a builtin.
name | symbol name |
Definition at line 62 of file Compiler.cpp.
References Ark::internal::Builtins::builtins.
Referenced by compileExpression(), and compileSymbol().
|
staticprivatenoexcept |
Checking if a symbol is a list instruction.
name |
Definition at line 73 of file Compiler.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 54 of file Compiler.cpp.
References Ark::internal::FIRST_OPERATOR, and Ark::internal::Language::operators.
Referenced by compileSymbol(), and handleCalls().
|
private |
Definition at line 449 of file Compiler.cpp.
References Ark::internal::ADD, Ark::internal::Language::And, Ark::internal::ASSERT, Ark::internal::AT_AT, Ark::internal::CALL, Ark::internal::Capture, compileExpression(), Ark::internal::Node::constList(), Ark::internal::DIV, Ark::internal::DUP, Ark::internal::FIRST_OPERATOR, getOperator(), Ark::internal::IR::Entity::GotoIf(), isTernaryInst(), isUnaryInst(), Ark::internal::JUMP, Ark::internal::IR::Entity::Label(), m_current_label, m_temp_pages, Ark::internal::MOD, Ark::internal::MUL, nodeProducesOutput(), Ark::internal::Language::operators, Ark::internal::Language::Or, page(), Ark::internal::POP, Ark::internal::SUB, Ark::internal::Symbol, and throwCompilerError().
Referenced by compileExpression().
|
nodiscardnoexcept |
Return the IR blocks (one per scope)
Definition at line 39 of file Compiler.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 110 of file Compiler.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 90 of file Compiler.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 81 of file Compiler.cpp.
References Ark::internal::Begin, Ark::internal::Node::constList(), Ark::internal::Fun, Ark::internal::If, Ark::internal::Keyword, Ark::internal::List, and Ark::internal::Node::nodeType().
Referenced by compileListInstruction(), and handleCalls().
helper functions to get a temp or finalized code page
page | page descriptor |
Definition at line 97 of file Compiler.hpp.
Referenced by compileExpression(), compileFunction(), compileIf(), compileLetMutSet(), compileListInstruction(), compilePluginImport(), compileSymbol(), compileWhile(), and handleCalls().
void Ark::internal::Compiler::process | ( | const Node & | ast | ) |
Start the compilation.
ast |
Definition at line 25 of file Compiler.cpp.
References compileExpression(), m_code_pages, m_logger, Ark::internal::Logger::traceEnd(), and Ark::internal::Logger::traceStart().
Referenced by Ark::Welder::generateBytecode().
|
nodiscardnoexcept |
Return the symbol table pre-computed.
Definition at line 44 of file Compiler.cpp.
References m_symbols.
Referenced by Ark::Welder::generateBytecode().
|
staticprivate |
Throw a nice error message.
message | |
node |
Definition at line 127 of file Compiler.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(), and handleCalls().
|
nodiscardnoexcept |
Return the value table pre-computed.
Definition at line 49 of file Compiler.cpp.
References m_values.
Referenced by Ark::Welder::generateBytecode().
|
private |
Definition at line 84 of file Compiler.hpp.
Referenced by compileFunction(), intermediateRepresentation(), and process().
|
private |
Definition at line 86 of file Compiler.hpp.
Referenced by compileIf(), compileWhile(), and handleCalls().
|
private |
the debug level of the compiler
Definition at line 88 of file Compiler.hpp.
|
private |
Definition at line 89 of file Compiler.hpp.
Referenced by process().
|
private |
Definition at line 82 of file Compiler.hpp.
Referenced by addSymbol(), and symbols().
|
private |
we need temporary code pages for some compilations passes
Definition at line 85 of file Compiler.hpp.
Referenced by handleCalls().
|
private |
Definition at line 83 of file Compiler.hpp.
Referenced by addValue(), addValue(), and values().