![]() |
ArkScript
A small, fast, functional and scripting language for video games
|
The ArkScript bytecode compiler. More...
#include <Compiler.hpp>
Public Member Functions | |
Compiler (unsigned debug) | |
Construct a new Compiler object. | |
void | process (const internal::Node &ast) |
Start the compilation. | |
const bytecode_t & | bytecode () const noexcept |
Return the constructed bytecode object. | |
Private Member Functions | |
void | pushFileHeader () noexcept |
Push the file headers (magic, version used, timestamp) | |
void | pushSymAndValTables () |
Push the symbols and values tables. | |
std::vector< internal::Word > & | page (const int i) noexcept |
helper functions to get a temp or finalized code page | |
std::vector< internal::Word > * | page_ptr (const int i) noexcept |
helper functions to get a temp or finalized code page | |
bool | mayBeFromPlugin (const std::string &name) noexcept |
Checking if a symbol may be coming from a plugin. | |
void | compileExpression (const internal::Node &x, int p, bool is_result_unused, bool is_terminal, const std::string &var_name="") |
Compile an expression (a node) recursively. | |
void | compileSymbol (const internal::Node &x, int p, bool is_result_unused) |
void | compileSpecific (const internal::Node &c0, const internal::Node &x, int p, bool is_result_unused) |
void | compileIf (const internal::Node &x, int p, bool is_result_unused, bool is_terminal, const std::string &var_name) |
void | compileFunction (const internal::Node &x, int p, bool is_result_unused, const std::string &var_name) |
void | compileLetMutSet (internal::Keyword n, const internal::Node &x, int p) |
void | compileWhile (const internal::Node &x, int p) |
void | compilePluginImport (const internal::Node &x, int p) |
void | handleCalls (const internal::Node &x, int p, bool is_result_unused, bool is_terminal, const std::string &var_name) |
uint16_t | addSymbol (const internal::Node &sym) |
Register a given node in the symbol table. | |
uint16_t | addValue (const internal::Node &x) |
Register a given node in the value table. | |
uint16_t | addValue (std::size_t page_id, const internal::Node ¤t) |
Register a page id (function reference) in the value table. | |
void | addDefinedSymbol (const std::string &sym) |
Register a symbol as defined, so that later we can throw errors on undefined symbols. | |
void | checkForUndefinedSymbol () |
Checks for undefined symbols, not present in the defined symbols table. | |
std::string | offerSuggestion (const std::string &str) const |
Suggest a symbol of what the user may have meant to input. | |
Static Private Member Functions | |
static std::optional< std::size_t > | getOperator (const std::string &name) noexcept |
Checking if a symbol is an operator. | |
static std::optional< std::size_t > | getBuiltin (const std::string &name) noexcept |
Checking if a symbol is a builtin. | |
static std::optional< internal::Instruction > | getSpecific (const std::string &name) noexcept |
Check if a symbol needs to be compiled to a specific instruction. | |
static bool | isUnaryInst (internal::Instruction inst) noexcept |
Check if a given instruction is unary (takes only one argument) | |
static uint16_t | computeSpecificInstArgc (internal::Instruction inst, uint16_t previous) noexcept |
Compute specific instruction argument count. | |
static void | compilerWarning (const std::string &message, const internal::Node &node) |
Display a warning message. | |
static void | throwCompilerError (const std::string &message, const internal::Node &node) |
Throw a nice error message. | |
Private Attributes | |
std::vector< internal::Node > | m_symbols |
std::vector< std::string > | m_defined_symbols |
std::vector< std::string > | m_plugins |
std::vector< internal::ValTableElem > | m_values |
std::vector< std::vector< internal::Word > > | m_code_pages |
std::vector< std::vector< internal::Word > > | m_temp_pages |
we need temporary code pages for some compilations passes | |
bytecode_t | m_bytecode |
unsigned | m_debug |
the debug level of the compiler | |
Friends | |
class | State |
class | Welder |
The ArkScript bytecode compiler.
Definition at line 35 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 symbol as defined, so that later we can throw errors on undefined symbols.
sym |
Definition at line 684 of file Compiler.cpp.
References m_defined_symbols.
Referenced by compileFunction(), compileLetMutSet(), and Ark::Welder::registerSymbol().
|
private |
Register a given node in the symbol table.
Can throw if the table is full
sym |
Definition at line 634 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 652 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 668 of file Compiler.cpp.
References m_values, and throwCompilerError().
|
noexcept |
Return the constructed bytecode object.
Definition at line 85 of file Compiler.cpp.
References m_bytecode.
Referenced by Ark::Welder::generateBytecode().
|
private |
Checks for undefined symbols, not present in the defined symbols table.
Definition at line 691 of file Compiler.cpp.
References m_defined_symbols, m_symbols, mayBeFromPlugin(), offerSuggestion(), and throwCompilerError().
Referenced by process().
|
private |
Compile an expression (a node) recursively.
x | the internal::Node to compile |
p | the current page number we're on |
is_result_unused | |
is_terminal | |
var_name |
Definition at line 260 of file Compiler.cpp.
References addSymbol(), addValue(), Ark::internal::BUILTIN, compileExpression(), compileFunction(), compileIf(), compileLetMutSet(), compilePluginImport(), compileSpecific(), compileSymbol(), compileWhile(), Ark::internal::Node::constList(), Ark::internal::DEL, Ark::internal::GET_FIELD, getBuiltin(), getSpecific(), handleCalls(), Ark::internal::LOAD_CONST, Ark::internal::Node::nodeType(), and page().
Referenced by compileExpression(), compileFunction(), compileIf(), compileLetMutSet(), compileSpecific(), compileWhile(), handleCalls(), and process().
|
private |
Definition at line 419 of file Compiler.cpp.
References addDefinedSymbol(), addSymbol(), addValue(), Ark::internal::CAPTURE, compileExpression(), compilerWarning(), Ark::internal::Node::constList(), Ark::internal::LOAD_CONST, m_code_pages, m_defined_symbols, Ark::internal::MUT, page(), Ark::internal::POP, Ark::internal::RET, and throwCompilerError().
Referenced by compileExpression().
|
private |
Definition at line 394 of file Compiler.cpp.
References compileExpression(), Ark::internal::Node::constList(), and page().
Referenced by compileExpression().
|
private |
Definition at line 468 of file Compiler.cpp.
References addDefinedSymbol(), addSymbol(), compileExpression(), Ark::internal::Node::constList(), Ark::internal::LET, Ark::internal::MUT, page(), and Ark::internal::STORE.
Referenced by compileExpression().
|
private |
Definition at line 507 of file Compiler.cpp.
References addValue(), Ark::internal::Node::constList(), m_plugins, page(), and Ark::internal::PLUGIN.
Referenced by compileExpression().
|
staticprivate |
Display a warning message.
message | |
node |
Definition at line 250 of file Compiler.cpp.
References Ark::Diagnostics::makeContextWithNode().
Referenced by compileFunction(), compileSpecific(), and compileSymbol().
|
private |
Definition at line 369 of file Compiler.cpp.
References compileExpression(), compilerWarning(), computeSpecificInstArgc(), Ark::internal::Node::constList(), getSpecific(), Ark::internal::LIST, page(), Ark::internal::POP, Ark::internal::Node::string(), and throwCompilerError().
Referenced by compileExpression().
|
private |
Definition at line 351 of file Compiler.cpp.
References addSymbol(), compilerWarning(), Ark::internal::FIRST_OPERATOR, getBuiltin(), getOperator(), Ark::internal::LOAD_SYMBOL, page(), Ark::internal::POP, and Ark::internal::Node::string().
Referenced by compileExpression().
|
private |
Definition at line 488 of file Compiler.cpp.
References compileExpression(), Ark::internal::Node::constList(), Ark::internal::JUMP, page(), and Ark::internal::POP_JUMP_IF_FALSE.
Referenced by compileExpression().
|
staticprivatenoexcept |
Compute specific instruction argument count.
inst | |
previous |
Definition at line 222 of file Compiler.cpp.
References Ark::internal::APPEND, Ark::internal::APPEND_IN_PLACE, Ark::internal::CONCAT, Ark::internal::CONCAT_IN_PLACE, and Ark::internal::LIST.
Referenced by compileSpecific().
|
staticprivatenoexcept |
Checking if a symbol is a builtin.
name | symbol name |
Definition at line 190 of file Compiler.cpp.
References Ark::internal::Builtins::builtins.
Referenced by compileExpression(), and compileSymbol().
|
staticprivatenoexcept |
Checking if a symbol is an operator.
name | symbol name |
Definition at line 182 of file Compiler.cpp.
References Ark::internal::operators.
Referenced by compileSymbol().
|
inlinestaticprivatenoexcept |
Check if a symbol needs to be compiled to a specific instruction.
name |
Definition at line 134 of file Compiler.hpp.
Referenced by compileExpression(), and compileSpecific().
|
private |
Definition at line 527 of file Compiler.cpp.
References Ark::internal::ADD, Ark::internal::AND_, Ark::internal::ASSERT, Ark::internal::CALL, compileExpression(), Ark::internal::Node::constList(), Ark::internal::DIV, Ark::internal::FIRST_OPERATOR, isUnaryInst(), Ark::internal::JUMP, m_temp_pages, Ark::internal::MOD, Ark::internal::MUL, Ark::internal::operators, Ark::internal::OR_, page(), Ark::internal::POP, Ark::internal::SUB, and throwCompilerError().
Referenced by compileExpression().
|
staticprivatenoexcept |
Check if a given instruction is unary (takes only one argument)
inst |
Definition at line 201 of file Compiler.cpp.
References Ark::internal::EMPTY, Ark::internal::HASFIELD, 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().
|
privatenoexcept |
Checking if a symbol may be coming from a plugin.
name | symbol name |
Definition at line 240 of file Compiler.cpp.
References Ark::Utils::splitString().
Referenced by checkForUndefinedSymbol().
|
private |
Suggest a symbol of what the user may have meant to input.
str | the string |
Definition at line 709 of file Compiler.cpp.
References Ark::Utils::levenshteinDistance(), and m_defined_symbols.
Referenced by checkForUndefinedSymbol().
|
inlineprivatenoexcept |
helper functions to get a temp or finalized code page
i | page index, if negative, refers to a temporary code page |
Definition at line 92 of file Compiler.hpp.
Referenced by compileExpression(), compileFunction(), compileIf(), compileLetMutSet(), compilePluginImport(), compileSpecific(), compileSymbol(), compileWhile(), handleCalls(), and process().
|
inlineprivatenoexcept |
helper functions to get a temp or finalized code page
i | page index, if negative, refers to a temporary code page |
Definition at line 105 of file Compiler.hpp.
void Ark::Compiler::process | ( | const internal::Node & | ast | ) |
Start the compilation.
ast |
Definition at line 25 of file Compiler.cpp.
References checkForUndefinedSymbol(), compileExpression(), m_bytecode, m_code_pages, page(), pushFileHeader(), and pushSymAndValTables().
Referenced by Ark::Welder::generateBytecode().
|
privatenoexcept |
Push the file headers (magic, version used, timestamp)
Definition at line 90 of file Compiler.cpp.
References ARK_VERSION_MAJOR, ARK_VERSION_MINOR, ARK_VERSION_PATCH, and m_bytecode.
Referenced by process().
|
private |
Push the symbols and values tables.
Definition at line 124 of file Compiler.cpp.
References Ark::internal::FUNC_TYPE, m_bytecode, m_symbols, m_values, Ark::internal::NUMBER_TYPE, Ark::internal::STRING_TYPE, Ark::internal::SYM_TABLE_START, and Ark::internal::VAL_TABLE_START.
Referenced by process().
|
staticprivate |
Throw a nice error message.
message | |
node |
Definition at line 255 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(), checkForUndefinedSymbol(), compileFunction(), compileSpecific(), and handleCalls().
|
friend |
Definition at line 59 of file Compiler.hpp.
|
friend |
Definition at line 60 of file Compiler.hpp.
|
private |
Definition at line 71 of file Compiler.hpp.
Referenced by bytecode(), process(), pushFileHeader(), and pushSymAndValTables().
|
private |
Definition at line 68 of file Compiler.hpp.
Referenced by compileFunction(), and process().
|
private |
the debug level of the compiler
Definition at line 72 of file Compiler.hpp.
|
private |
Definition at line 65 of file Compiler.hpp.
Referenced by addDefinedSymbol(), checkForUndefinedSymbol(), compileFunction(), and offerSuggestion().
|
private |
Definition at line 66 of file Compiler.hpp.
Referenced by compilePluginImport().
|
private |
Definition at line 64 of file Compiler.hpp.
Referenced by addSymbol(), checkForUndefinedSymbol(), and pushSymAndValTables().
|
private |
we need temporary code pages for some compilations passes
Definition at line 69 of file Compiler.hpp.
Referenced by handleCalls().
|
private |
Definition at line 67 of file Compiler.hpp.
Referenced by addValue(), and pushSymAndValTables().