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, const std::vector< std::string > &libenv, uint16_t options=DefaultFeatures) | |
Construct a new Compiler object. | |
void | feed (const std::string &code, const std::string &filename=ARK_NO_NAME_FILE) |
Feed the differents variables with information taken from the given source code file. | |
void | compile () |
Start the compilation. | |
void | saveTo (const std::string &file) |
Save generated bytecode to a file. | |
const bytecode_t & | bytecode () 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< uint8_t > & | page (int i) noexcept |
helper functions to get a temp or finalized code page | |
std::vector< uint8_t > * | page_ptr (int i) noexcept |
helper functions to get a temp or finalized code page | |
void | setNumberAt (int p, std::size_t at_inst, std::size_t number) |
std::size_t | countArkObjects (const std::vector< internal::Node > &lst) noexcept |
Count the number of "valid" ark objects in a node. | |
std::optional< std::size_t > | isOperator (const std::string &name) noexcept |
Checking if a symbol is an operator. | |
std::optional< std::size_t > | isBuiltin (const std::string &name) noexcept |
Checking if a symbol is a builtin. | |
std::optional< internal::Instruction > | isSpecific (const std::string &name) noexcept |
Check if a symbol needs to be compiled to a specific instruction. | |
bool | isUnaryInst (internal::Instruction inst) noexcept |
Check if a given instruction is unary (takes only one argument) | |
void | pushSpecificInstArgc (internal::Instruction inst, uint16_t previous, int p) noexcept |
Compute specific instruction argument count. | |
bool | mayBeFromPlugin (const std::string &name) noexcept |
Checking if a symbol may be coming from a plugin. | |
void | throwCompilerError (const std::string &message, const internal::Node &node) |
Throw a nice error message. | |
void | compilerWarning (const std::string &message, const internal::Node &node) |
Display a warning message. | |
void | _compile (const internal::Node &x, int p, bool produces_result, bool is_terminal, const std::string &var_name="") |
Compile a single node recursively. | |
void | compileSymbol (const internal::Node &x, int p, bool produces_result) |
void | compileSpecific (const internal::Node &c0, const internal::Node &x, int p, bool produces_result) |
void | compileIf (const internal::Node &x, int p, bool produces_result, bool is_terminal, const std::string &var_name) |
void | compileFunction (const internal::Node &x, int p, bool produces_result, 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 | compileQuote (const internal::Node &x, int p, bool produces_result, bool is_terminal, const std::string &var_name) |
void | compilePluginImport (const internal::Node &x, int p) |
void | compileDel (const internal::Node &x, int p) |
void | handleCalls (const internal::Node &x, int p, bool produces_result, bool is_terminal, const std::string &var_name) |
void | putValue (const internal::Node &x, int p, bool produces_result) |
Put a value in the bytecode, handling the closures chains. | |
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. | |
void | pushNumber (uint16_t n, std::vector< uint8_t > *page=nullptr) noexcept |
Push a number on stack (need 2 bytes) | |
std::string | offerSuggestion (const std::string &str) |
Suggest a symbol of what the user may have meant to input. | |
Private Attributes | |
internal::Parser | m_parser |
internal::Optimizer | m_optimizer |
uint16_t | m_options |
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< uint8_t > > | m_code_pages |
std::vector< std::vector< uint8_t > > | 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 | Ark::State |
The ArkScript bytecode compiler.
Definition at line 35 of file Compiler.hpp.
Ark::Compiler::Compiler | ( | unsigned | debug, |
const std::vector< std::string > & | libenv, | ||
uint16_t | options = DefaultFeatures |
||
) |
Construct a new Compiler object.
debug | the debug level |
options | the compilers options |
Definition at line 21 of file Compiler.cpp.
|
private |
Compile a single node recursively.
x | the internal::Node to compile |
p | the current page number we're on |
produces_result | |
is_terminal | |
var_name |
Definition at line 262 of file Compiler.cpp.
References _compile(), addSymbol(), addValue(), compileDel(), compileFunction(), compileIf(), compileLetMutSet(), compilePluginImport(), compileQuote(), compileSpecific(), compileSymbol(), compileWhile(), Ark::internal::Node::constList(), handleCalls(), isBuiltin(), isSpecific(), Ark::internal::Node::nodeType(), page(), page_ptr(), pushNumber(), and Ark::internal::Node::string().
Referenced by _compile(), compile(), compileFunction(), compileIf(), compileQuote(), compileSpecific(), compileWhile(), handleCalls(), and putValue().
|
private |
Register a symbol as defined, so that later we can throw errors on undefined symbols.
sym |
Definition at line 775 of file Compiler.cpp.
References m_defined_symbols.
Referenced by compileFunction(), and compileLetMutSet().
|
private |
Register a given node in the symbol table.
Can throw if the table is full
sym |
Definition at line 722 of file Compiler.cpp.
References m_symbols, and throwCompilerError().
Referenced by _compile(), compileDel(), compileFunction(), compileLetMutSet(), and compileSymbol().
|
private |
Register a given node in the value table.
Can throw if the table is full
x |
Definition at line 741 of file Compiler.cpp.
References m_values, and throwCompilerError().
Referenced by _compile(), compileFunction(), compilePluginImport(), and compileQuote().
|
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 758 of file Compiler.cpp.
References m_values, and throwCompilerError().
|
noexcept |
Return the constructed bytecode object.
Definition at line 88 of file Compiler.cpp.
References m_bytecode.
Referenced by Ark::State::doString().
|
private |
Checks for undefined symbols, not present in the defined symbols table.
Definition at line 783 of file Compiler.cpp.
References m_defined_symbols, m_symbols, mayBeFromPlugin(), offerSuggestion(), and throwCompilerError().
Referenced by compile().
void Ark::Compiler::compile | ( | ) |
Start the compilation.
Definition at line 35 of file Compiler.cpp.
References _compile(), Ark::internal::Optimizer::ast(), checkForUndefinedSymbol(), m_bytecode, m_code_pages, m_optimizer, page(), pushFileHeader(), pushNumber(), and pushSymAndValTables().
Referenced by Ark::State::compile(), and Ark::State::doString().
|
private |
Definition at line 577 of file Compiler.cpp.
References addSymbol(), Ark::internal::Node::constList(), page(), page_ptr(), and pushNumber().
Referenced by _compile().
|
private |
Definition at line 454 of file Compiler.cpp.
References _compile(), addDefinedSymbol(), addSymbol(), addValue(), compilerWarning(), Ark::internal::Node::constList(), m_code_pages, m_defined_symbols, page(), page_ptr(), pushNumber(), and throwCompilerError().
Referenced by _compile().
|
private |
Definition at line 426 of file Compiler.cpp.
References _compile(), Ark::internal::Node::constList(), page(), page_ptr(), pushNumber(), and setNumberAt().
Referenced by _compile().
|
private |
Definition at line 507 of file Compiler.cpp.
References addDefinedSymbol(), addSymbol(), Ark::internal::Node::constList(), page(), page_ptr(), pushNumber(), and putValue().
Referenced by _compile().
|
private |
Definition at line 566 of file Compiler.cpp.
References addValue(), Ark::internal::Node::constList(), m_plugins, page(), page_ptr(), and pushNumber().
Referenced by _compile().
|
private |
Definition at line 546 of file Compiler.cpp.
References _compile(), addValue(), compilerWarning(), Ark::internal::Node::constList(), m_code_pages, page(), page_ptr(), and pushNumber().
Referenced by _compile().
|
private |
Display a warning message.
message | |
node |
Definition at line 256 of file Compiler.cpp.
References Ark::FeatureShowWarnings, m_options, and Ark::internal::makeNodeBasedErrorCtx().
Referenced by compileFunction(), compileQuote(), compileSpecific(), and compileSymbol().
|
private |
Definition at line 388 of file Compiler.cpp.
References _compile(), compilerWarning(), Ark::internal::Node::constList(), countArkObjects(), isSpecific(), page(), pushSpecificInstArgc(), and Ark::internal::Node::string().
Referenced by _compile().
|
private |
Definition at line 362 of file Compiler.cpp.
References addSymbol(), compilerWarning(), isBuiltin(), isOperator(), page(), page_ptr(), pushNumber(), and Ark::internal::Node::string().
Referenced by _compile().
|
private |
Definition at line 525 of file Compiler.cpp.
References _compile(), Ark::internal::Node::constList(), page(), page_ptr(), pushNumber(), and setNumberAt().
Referenced by _compile().
|
privatenoexcept |
Count the number of "valid" ark objects in a node.
Isn't considered valid a GetField, because we use this function to count the number of arguments of function calls.
lst |
Definition at line 181 of file Compiler.cpp.
Referenced by compileSpecific().
void Ark::Compiler::feed | ( | const std::string & | code, |
const std::string & | filename = ARK_NO_NAME_FILE |
||
) |
Feed the differents variables with information taken from the given source code file.
code | the code of the file |
filename | the name of the file |
Definition at line 26 of file Compiler.cpp.
References Ark::internal::Parser::ast(), Ark::internal::MacroProcessor::ast(), Ark::internal::Optimizer::feed(), Ark::internal::MacroProcessor::feed(), Ark::internal::Parser::feed(), m_debug, m_optimizer, m_options, and m_parser.
Referenced by Ark::State::compile(), and Ark::State::doString().
|
private |
Definition at line 586 of file Compiler.cpp.
References _compile(), Ark::internal::Node::constList(), isUnaryInst(), m_temp_pages, Ark::internal::operators, page(), page_ptr(), pushNumber(), and throwCompilerError().
Referenced by _compile().
|
privatenoexcept |
Checking if a symbol is a builtin.
name | symbol name |
Definition at line 200 of file Compiler.cpp.
References Ark::internal::Builtins::builtins.
Referenced by _compile(), and compileSymbol().
|
privatenoexcept |
Checking if a symbol is an operator.
name | symbol name |
Definition at line 192 of file Compiler.cpp.
References Ark::internal::operators.
Referenced by compileSymbol().
|
inlineprivatenoexcept |
Check if a symbol needs to be compiled to a specific instruction.
name |
Definition at line 167 of file Compiler.hpp.
Referenced by _compile(), and compileSpecific().
|
privatenoexcept |
Check if a given instruction is unary (takes only one argument)
inst |
Definition at line 211 of file Compiler.cpp.
Referenced by handleCalls().
|
privatenoexcept |
Checking if a symbol may be coming from a plugin.
name | symbol name |
Definition at line 241 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 802 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 109 of file Compiler.hpp.
Referenced by _compile(), compile(), compileDel(), compileFunction(), compileIf(), compileLetMutSet(), compilePluginImport(), compileQuote(), compileSpecific(), compileSymbol(), compileWhile(), and handleCalls().
|
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 122 of file Compiler.hpp.
Referenced by _compile(), compileDel(), compileFunction(), compileIf(), compileLetMutSet(), compilePluginImport(), compileQuote(), compileSymbol(), compileWhile(), and handleCalls().
|
privatenoexcept |
Push the file headers (magic, version used, timestamp)
Definition at line 93 of file Compiler.cpp.
References ARK_VERSION_MAJOR, ARK_VERSION_MINOR, ARK_VERSION_PATCH, m_bytecode, and pushNumber().
Referenced by compile().
|
privatenoexcept |
Push a number on stack (need 2 bytes)
n | the number to push |
page | the page where it should land, nullptr for current page |
Definition at line 821 of file Compiler.cpp.
Referenced by _compile(), compile(), compileDel(), compileFunction(), compileIf(), compileLetMutSet(), compilePluginImport(), compileQuote(), compileSymbol(), compileWhile(), handleCalls(), pushFileHeader(), and pushSymAndValTables().
|
privatenoexcept |
Compute specific instruction argument count.
inst | |
previous | |
p |
Definition at line 232 of file Compiler.cpp.
Referenced by compileSpecific().
|
private |
Push the symbols and values tables.
Definition at line 125 of file Compiler.cpp.
References m_bytecode, m_symbols, m_values, and pushNumber().
Referenced by compile().
|
private |
Put a value in the bytecode, handling the closures chains.
x | value node |
p | current page index |
produces_result |
Definition at line 713 of file Compiler.cpp.
References _compile(), and Ark::internal::Node::constList().
Referenced by compileLetMutSet().
void Ark::Compiler::saveTo | ( | const std::string & | file | ) |
Save generated bytecode to a file.
file | the name of the file where the bytecode will be saved |
Definition at line 78 of file Compiler.cpp.
References m_bytecode, and m_debug.
Referenced by Ark::State::compile().
|
inlineprivate |
Definition at line 129 of file Compiler.hpp.
Referenced by compileIf(), and compileWhile().
|
private |
Throw a nice error message.
message | |
node |
Definition at line 251 of file Compiler.cpp.
References Ark::internal::makeNodeBasedErrorCtx().
Referenced by addSymbol(), addValue(), checkForUndefinedSymbol(), compileFunction(), and handleCalls().
|
friend |
Definition at line 74 of file Compiler.hpp.
|
private |
Definition at line 88 of file Compiler.hpp.
Referenced by bytecode(), compile(), pushFileHeader(), pushSymAndValTables(), and saveTo().
|
private |
Definition at line 85 of file Compiler.hpp.
Referenced by compile(), compileFunction(), and compileQuote().
|
private |
the debug level of the compiler
Definition at line 89 of file Compiler.hpp.
|
private |
Definition at line 82 of file Compiler.hpp.
Referenced by addDefinedSymbol(), checkForUndefinedSymbol(), Ark::State::compile(), compileFunction(), Ark::State::doString(), and offerSuggestion().
|
private |
Definition at line 78 of file Compiler.hpp.
|
private |
Definition at line 79 of file Compiler.hpp.
Referenced by compilerWarning(), and feed().
|
private |
Definition at line 77 of file Compiler.hpp.
Referenced by feed().
|
private |
Definition at line 83 of file Compiler.hpp.
Referenced by compilePluginImport().
|
private |
Definition at line 81 of file Compiler.hpp.
Referenced by addSymbol(), checkForUndefinedSymbol(), and pushSymAndValTables().
|
private |
we need temporary code pages for some compilations passes
Definition at line 86 of file Compiler.hpp.
Referenced by handleCalls().
|
private |
Definition at line 84 of file Compiler.hpp.
Referenced by addValue(), and pushSymAndValTables().