ArkScript
A small, fast, functional and scripting language for video games
Ark::Compiler Class Reference

The ArkScript bytecode compiler. More...

#include <Compiler.hpp>

+ Collaboration diagram for Ark::Compiler:

Public Member Functions

 Compiler (unsigned debug, const std::vector< std::string > &libenv, uint16_t options=DefaultFeatures)
 Construct a new Compiler object. More...
 
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. More...
 
void compile ()
 Start the compilation. More...
 
void saveTo (const std::string &file)
 Save generated bytecode to a file. More...
 
const bytecode_tbytecode () noexcept
 Return the constructed bytecode object. More...
 

Private Member Functions

void pushFileHeader () noexcept
 Push the file headers (magic, version used, timestamp) More...
 
void pushSymAndValTables ()
 Push the symbols and values tables. More...
 
std::vector< uint8_t > & page (int i) noexcept
 helper functions to get a temp or finalized code page More...
 
std::vector< uint8_t > * page_ptr (int i) noexcept
 helper functions to get a temp or finalized code page More...
 
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. More...
 
std::optional< std::size_t > isOperator (const std::string &name) noexcept
 Checking if a symbol is an operator. More...
 
std::optional< std::size_t > isBuiltin (const std::string &name) noexcept
 Checking if a symbol is a builtin. More...
 
std::optional< internal::InstructionisSpecific (const std::string &name) noexcept
 Check if a symbol needs to be compiled to a specific instruction. More...
 
void pushSpecificInstArgc (internal::Instruction inst, uint16_t previous, int p) noexcept
 Compute specific instruction argument count. More...
 
bool mayBeFromPlugin (const std::string &name) noexcept
 Checking if a symbol may be coming from a plugin. More...
 
void throwCompilerError (const std::string &message, const internal::Node &node)
 Throw a nice error message. More...
 
void _compile (const internal::Node &x, int p, bool produces_result, bool is_terminal, const std::string &var_name="")
 Compile a single node recursively. More...
 
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. More...
 
uint16_t addSymbol (const internal::Node &sym)
 Register a given node in the symbol table. More...
 
uint16_t addValue (const internal::Node &x)
 Register a given node in the value table. More...
 
uint16_t addValue (std::size_t page_id, const internal::Node &current)
 Register a page id (function reference) in the value table. More...
 
void addDefinedSymbol (const std::string &sym)
 Register a symbol as defined, so that later we can throw errors on undefined symbols. More...
 
void checkForUndefinedSymbol ()
 Checks for undefined symbols, not present in the defined symbols table. More...
 
void pushNumber (uint16_t n, std::vector< uint8_t > *page=nullptr) noexcept
 Push a number on stack (need 2 bytes) More...
 
std::string offerSuggestion (const std::string &str)
 Suggest a symbol of what the user may have meant to input. More...
 

Private Attributes

internal::Parser m_parser
 
internal::Optimizer m_optimizer
 
uint16_t m_options
 
std::vector< internal::Nodem_symbols
 
std::vector< std::string > m_defined_symbols
 
std::vector< std::string > m_plugins
 
std::vector< internal::ValTableElemm_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 More...
 
bytecode_t m_bytecode
 
unsigned m_debug
 the debug level of the compiler More...
 

Friends

class Ark::State
 

Detailed Description

The ArkScript bytecode compiler.

Definition at line 35 of file Compiler.hpp.

Constructor & Destructor Documentation

◆ Compiler()

Ark::Compiler::Compiler ( unsigned  debug,
const std::vector< std::string > &  libenv,
uint16_t  options = DefaultFeatures 
)

Construct a new Compiler object.

Parameters
debugthe debug level
optionsthe compilers options

Definition at line 19 of file Compiler.cpp.

Member Function Documentation

◆ _compile()

void Ark::Compiler::_compile ( const internal::Node x,
int  p,
bool  produces_result,
bool  is_terminal,
const std::string &  var_name = "" 
)
private

◆ addDefinedSymbol()

void Ark::Compiler::addDefinedSymbol ( const std::string &  sym)
private

Register a symbol as defined, so that later we can throw errors on undefined symbols.

Parameters
sym

Definition at line 729 of file Compiler.cpp.

References m_defined_symbols.

Referenced by compileFunction(), and compileLetMutSet().

◆ addSymbol()

uint16_t Ark::Compiler::addSymbol ( const internal::Node sym)
private

Register a given node in the symbol table.

Can throw if the table is full

Parameters
sym
Returns
uint16_t

Definition at line 676 of file Compiler.cpp.

References m_symbols, and throwCompilerError().

Referenced by _compile(), compileDel(), compileFunction(), compileLetMutSet(), and compileSymbol().

◆ addValue() [1/2]

uint16_t Ark::Compiler::addValue ( const internal::Node x)
private

Register a given node in the value table.

Can throw if the table is full

Parameters
x
Returns
uint16_t

Definition at line 695 of file Compiler.cpp.

References m_values, and throwCompilerError().

Referenced by _compile(), compileFunction(), compilePluginImport(), and compileQuote().

◆ addValue() [2/2]

uint16_t Ark::Compiler::addValue ( std::size_t  page_id,
const internal::Node current 
)
private

Register a page id (function reference) in the value table.

Can throw if the table is full

Parameters
page_id
currentA reference to the current node, for context
Returns
std::size_t

Definition at line 712 of file Compiler.cpp.

References m_values, and throwCompilerError().

◆ bytecode()

const bytecode_t & Ark::Compiler::bytecode ( )
noexcept

Return the constructed bytecode object.

Returns
const bytecode_t&

Definition at line 86 of file Compiler.cpp.

References m_bytecode.

Referenced by Ark::State::doString().

◆ checkForUndefinedSymbol()

void Ark::Compiler::checkForUndefinedSymbol ( )
private

Checks for undefined symbols, not present in the defined symbols table.

Definition at line 737 of file Compiler.cpp.

References m_defined_symbols, m_symbols, mayBeFromPlugin(), offerSuggestion(), and throwCompilerError().

Referenced by compile().

◆ compile()

◆ compileDel()

void Ark::Compiler::compileDel ( const internal::Node x,
int  p 
)
private

◆ compileFunction()

void Ark::Compiler::compileFunction ( const internal::Node x,
int  p,
bool  produces_result,
const std::string &  var_name 
)
private

◆ compileIf()

void Ark::Compiler::compileIf ( const internal::Node x,
int  p,
bool  produces_result,
bool  is_terminal,
const std::string &  var_name 
)
private

◆ compileLetMutSet()

void Ark::Compiler::compileLetMutSet ( internal::Keyword  n,
const internal::Node x,
int  p 
)
private

◆ compilePluginImport()

void Ark::Compiler::compilePluginImport ( const internal::Node x,
int  p 
)
private

◆ compileQuote()

void Ark::Compiler::compileQuote ( const internal::Node x,
int  p,
bool  produces_result,
bool  is_terminal,
const std::string &  var_name 
)
private

◆ compileSpecific()

void Ark::Compiler::compileSpecific ( const internal::Node c0,
const internal::Node x,
int  p,
bool  produces_result 
)
private

◆ compileSymbol()

void Ark::Compiler::compileSymbol ( const internal::Node x,
int  p,
bool  produces_result 
)
private

◆ compileWhile()

void Ark::Compiler::compileWhile ( const internal::Node x,
int  p 
)
private

◆ countArkObjects()

std::size_t Ark::Compiler::countArkObjects ( const std::vector< internal::Node > &  lst)
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.

Parameters
lst
Returns
std::size_t

Definition at line 179 of file Compiler.cpp.

Referenced by compileSpecific().

◆ feed()

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.

Parameters
codethe code of the file
filenamethe name of the file

Definition at line 24 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().

◆ handleCalls()

◆ isBuiltin()

std::optional< std::size_t > Ark::Compiler::isBuiltin ( const std::string &  name)
privatenoexcept

Checking if a symbol is a builtin.

Parameters
namesymbol name
Returns
std::optional<std::size_t> position in the builtins' list

Definition at line 198 of file Compiler.cpp.

References Ark::internal::Builtins::builtins.

Referenced by _compile(), and compileSymbol().

◆ isOperator()

std::optional< std::size_t > Ark::Compiler::isOperator ( const std::string &  name)
privatenoexcept

Checking if a symbol is an operator.

Parameters
namesymbol name
Returns
std::optional<std::size_t> position in the operators' list

Definition at line 190 of file Compiler.cpp.

References Ark::internal::operators.

Referenced by compileSymbol().

◆ isSpecific()

std::optional<internal::Instruction> Ark::Compiler::isSpecific ( const std::string &  name)
inlineprivatenoexcept

Check if a symbol needs to be compiled to a specific instruction.

Parameters
name
Returns
std::optional<internal::Instruction> corresponding instruction if it exists

Definition at line 167 of file Compiler.hpp.

References Ark::internal::APPEND, Ark::internal::APPEND_IN_PLACE, Ark::internal::CONCAT, Ark::internal::CONCAT_IN_PLACE, Ark::internal::LIST, Ark::internal::POP_LIST, and Ark::internal::POP_LIST_IN_PLACE.

Referenced by _compile(), and compileSpecific().

◆ mayBeFromPlugin()

bool Ark::Compiler::mayBeFromPlugin ( const std::string &  name)
privatenoexcept

Checking if a symbol may be coming from a plugin.

Parameters
namesymbol name
Returns
true the symbol may be from a plugin, loaded at runtime
false

Definition at line 218 of file Compiler.cpp.

References Ark::Utils::splitString().

Referenced by checkForUndefinedSymbol().

◆ offerSuggestion()

std::string Ark::Compiler::offerSuggestion ( const std::string &  str)
private

Suggest a symbol of what the user may have meant to input.

Parameters
strthe string
Returns
std::string

Definition at line 756 of file Compiler.cpp.

References Ark::Utils::levenshteinDistance(), and m_defined_symbols.

Referenced by checkForUndefinedSymbol().

◆ page()

std::vector<uint8_t>& Ark::Compiler::page ( int  i)
inlineprivatenoexcept

helper functions to get a temp or finalized code page

Parameters
ipage index, if negative, refers to a temporary code page
Returns
std::vector<uint8_t>&

Definition at line 109 of file Compiler.hpp.

Referenced by _compile(), compile(), compileDel(), compileFunction(), compileIf(), compileLetMutSet(), compilePluginImport(), compileQuote(), compileSpecific(), compileSymbol(), compileWhile(), and handleCalls().

◆ page_ptr()

std::vector<uint8_t>* Ark::Compiler::page_ptr ( int  i)
inlineprivatenoexcept

helper functions to get a temp or finalized code page

Parameters
ipage index, if negative, refers to a temporary code page
Returns
std::vector<uint8_t>*

Definition at line 122 of file Compiler.hpp.

Referenced by _compile(), compileDel(), compileFunction(), compileIf(), compileLetMutSet(), compilePluginImport(), compileQuote(), compileSymbol(), compileWhile(), and handleCalls().

◆ pushFileHeader()

void Ark::Compiler::pushFileHeader ( )
privatenoexcept

Push the file headers (magic, version used, timestamp)

Definition at line 91 of file Compiler.cpp.

References ARK_VERSION_MAJOR, ARK_VERSION_MINOR, ARK_VERSION_PATCH, m_bytecode, and pushNumber().

Referenced by compile().

◆ pushNumber()

void Ark::Compiler::pushNumber ( uint16_t  n,
std::vector< uint8_t > *  page = nullptr 
)
privatenoexcept

Push a number on stack (need 2 bytes)

Parameters
nthe number to push
pagethe page where it should land, nullptr for current page

Definition at line 775 of file Compiler.cpp.

Referenced by _compile(), compile(), compileDel(), compileFunction(), compileIf(), compileLetMutSet(), compilePluginImport(), compileQuote(), compileSymbol(), compileWhile(), handleCalls(), pushFileHeader(), and pushSymAndValTables().

◆ pushSpecificInstArgc()

void Ark::Compiler::pushSpecificInstArgc ( internal::Instruction  inst,
uint16_t  previous,
int  p 
)
privatenoexcept

Compute specific instruction argument count.

Parameters
inst
previous
p

Definition at line 209 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().

◆ pushSymAndValTables()

void Ark::Compiler::pushSymAndValTables ( )
private

◆ putValue()

void Ark::Compiler::putValue ( const internal::Node x,
int  p,
bool  produces_result 
)
private

Put a value in the bytecode, handling the closures chains.

Parameters
xvalue node
pcurrent page index
produces_result

Definition at line 667 of file Compiler.cpp.

References _compile(), and Ark::internal::Node::constList().

Referenced by compileLetMutSet().

◆ saveTo()

void Ark::Compiler::saveTo ( const std::string &  file)

Save generated bytecode to a file.

Parameters
filethe name of the file where the bytecode will be saved

Definition at line 76 of file Compiler.cpp.

References m_bytecode, and m_debug.

Referenced by Ark::State::compile().

◆ setNumberAt()

void Ark::Compiler::setNumberAt ( int  p,
std::size_t  at_inst,
std::size_t  number 
)
inlineprivate

Definition at line 129 of file Compiler.hpp.

Referenced by compileIf(), and compileWhile().

◆ throwCompilerError()

void Ark::Compiler::throwCompilerError ( const std::string &  message,
const internal::Node node 
)
private

Throw a nice error message.

Parameters
message
node

Definition at line 228 of file Compiler.cpp.

References Ark::internal::makeNodeBasedErrorCtx().

Referenced by addSymbol(), addValue(), checkForUndefinedSymbol(), compileFunction(), and handleCalls().

Friends And Related Function Documentation

◆ Ark::State

friend class Ark::State
friend

Definition at line 74 of file Compiler.hpp.

Member Data Documentation

◆ m_bytecode

bytecode_t Ark::Compiler::m_bytecode
private

Definition at line 88 of file Compiler.hpp.

Referenced by bytecode(), compile(), pushFileHeader(), pushSymAndValTables(), and saveTo().

◆ m_code_pages

std::vector<std::vector<uint8_t> > Ark::Compiler::m_code_pages
private

Definition at line 85 of file Compiler.hpp.

Referenced by compile(), compileFunction(), and compileQuote().

◆ m_debug

unsigned Ark::Compiler::m_debug
private

the debug level of the compiler

Definition at line 89 of file Compiler.hpp.

Referenced by feed(), and saveTo().

◆ m_defined_symbols

std::vector<std::string> Ark::Compiler::m_defined_symbols
private

◆ m_optimizer

internal::Optimizer Ark::Compiler::m_optimizer
private

Definition at line 78 of file Compiler.hpp.

Referenced by compile(), and feed().

◆ m_options

uint16_t Ark::Compiler::m_options
private

Definition at line 79 of file Compiler.hpp.

Referenced by feed().

◆ m_parser

internal::Parser Ark::Compiler::m_parser
private

Definition at line 77 of file Compiler.hpp.

Referenced by feed().

◆ m_plugins

std::vector<std::string> Ark::Compiler::m_plugins
private

Definition at line 83 of file Compiler.hpp.

Referenced by compilePluginImport().

◆ m_symbols

std::vector<internal::Node> Ark::Compiler::m_symbols
private

Definition at line 81 of file Compiler.hpp.

Referenced by addSymbol(), checkForUndefinedSymbol(), and pushSymAndValTables().

◆ m_temp_pages

std::vector<std::vector<uint8_t> > Ark::Compiler::m_temp_pages
private

we need temporary code pages for some compilations passes

Definition at line 86 of file Compiler.hpp.

Referenced by handleCalls().

◆ m_values

std::vector<internal::ValTableElem> Ark::Compiler::m_values
private

Definition at line 84 of file Compiler.hpp.

Referenced by addValue(), and pushSymAndValTables().


The documentation for this class was generated from the following files: