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.
 
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_tbytecode () 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::InstructionisSpecific (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 &current)
 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::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
 
bytecode_t m_bytecode
 
unsigned m_debug
 the debug level of the compiler
 

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 21 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 775 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 722 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 741 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 758 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 88 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 783 of file Compiler.cpp.

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

Referenced by compile().

◆ compile()

void Ark::Compiler::compile ( )

◆ compileDel()

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

Definition at line 577 of file Compiler.cpp.

References addSymbol(), Ark::internal::Node::constList(), page(), page_ptr(), and pushNumber().

Referenced by _compile().

◆ 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

Definition at line 426 of file Compiler.cpp.

References _compile(), Ark::internal::Node::constList(), page(), page_ptr(), pushNumber(), and setNumberAt().

Referenced by _compile().

◆ 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

Definition at line 566 of file Compiler.cpp.

References addValue(), Ark::internal::Node::constList(), m_plugins, page(), page_ptr(), and pushNumber().

Referenced by _compile().

◆ compileQuote()

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

◆ compilerWarning()

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

Display a warning message.

Parameters
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().

◆ 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

Definition at line 525 of file Compiler.cpp.

References _compile(), Ark::internal::Node::constList(), page(), page_ptr(), pushNumber(), and setNumberAt().

Referenced by _compile().

◆ 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 181 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 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().

◆ handleCalls()

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

◆ 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 200 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 192 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.

Referenced by _compile(), and compileSpecific().

◆ isUnaryInst()

bool Ark::Compiler::isUnaryInst ( internal::Instruction  inst)
privatenoexcept

Check if a given instruction is unary (takes only one argument)

Parameters
inst
Returns
true the instruction is unary
false

Definition at line 211 of file Compiler.cpp.

Referenced by handleCalls().

◆ 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 241 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 802 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 93 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 821 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 232 of file Compiler.cpp.

Referenced by compileSpecific().

◆ pushSymAndValTables()

void Ark::Compiler::pushSymAndValTables ( )
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().

◆ 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 713 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 78 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 251 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 compilerWarning(), and 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: