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)
 Construct a new Compiler object.
 
void process (const internal::Node &ast)
 Start the compilation.
 
const bytecode_tbytecode () 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 &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.
 
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::InstructiongetSpecific (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::Nodem_symbols
 
std::vector< std::string > m_defined_symbols
 
std::vector< std::string > m_plugins
 
std::vector< internal::ValTableElemm_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
 

Detailed Description

The ArkScript bytecode compiler.

Definition at line 35 of file Compiler.hpp.

Constructor & Destructor Documentation

◆ Compiler()

Ark::Compiler::Compiler ( unsigned  debug)
explicit

Construct a new Compiler object.

Parameters
debugthe debug level

Definition at line 21 of file Compiler.cpp.

Member Function Documentation

◆ 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 684 of file Compiler.cpp.

References m_defined_symbols.

Referenced by compileFunction(), compileLetMutSet(), and Ark::Welder::registerSymbol().

◆ 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 634 of file Compiler.cpp.

References m_symbols, Ark::internal::Node::string(), and throwCompilerError().

Referenced by compileExpression(), 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 652 of file Compiler.cpp.

References m_values, and throwCompilerError().

Referenced by compileExpression(), compileFunction(), and compilePluginImport().

◆ 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 668 of file Compiler.cpp.

References m_values, and throwCompilerError().

◆ bytecode()

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

Return the constructed bytecode object.

Returns
const bytecode_t&

Definition at line 85 of file Compiler.cpp.

References m_bytecode.

Referenced by Ark::Welder::generateBytecode().

◆ checkForUndefinedSymbol()

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

◆ compileExpression()

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

◆ compileFunction()

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

◆ compileIf()

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

Definition at line 394 of file Compiler.cpp.

References compileExpression(), Ark::internal::Node::constList(), and page().

Referenced by compileExpression().

◆ 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

◆ compilerWarning()

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

Display a warning message.

Parameters
message
node

Definition at line 250 of file Compiler.cpp.

References Ark::Diagnostics::makeContextWithNode().

Referenced by compileFunction(), compileSpecific(), and compileSymbol().

◆ compileSpecific()

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

◆ compileSymbol()

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

◆ compileWhile()

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

◆ computeSpecificInstArgc()

uint16_t Ark::Compiler::computeSpecificInstArgc ( internal::Instruction  inst,
uint16_t  previous 
)
staticprivatenoexcept

Compute specific instruction argument count.

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

◆ getBuiltin()

std::optional< std::size_t > Ark::Compiler::getBuiltin ( const std::string &  name)
staticprivatenoexcept

Checking if a symbol is a builtin.

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

Definition at line 190 of file Compiler.cpp.

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

Referenced by compileExpression(), and compileSymbol().

◆ getOperator()

std::optional< std::size_t > Ark::Compiler::getOperator ( const std::string &  name)
staticprivatenoexcept

Checking if a symbol is an operator.

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

Definition at line 182 of file Compiler.cpp.

References Ark::internal::operators.

Referenced by compileSymbol().

◆ getSpecific()

static std::optional< internal::Instruction > Ark::Compiler::getSpecific ( const std::string &  name)
inlinestaticprivatenoexcept

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 134 of file Compiler.hpp.

Referenced by compileExpression(), and compileSpecific().

◆ handleCalls()

◆ isUnaryInst()

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

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

Parameters
inst
Returns
true the instruction is unary
false

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

◆ 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 240 of file Compiler.cpp.

References Ark::Utils::splitString().

Referenced by checkForUndefinedSymbol().

◆ offerSuggestion()

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

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

Parameters
strthe string
Returns
std::string

Definition at line 709 of file Compiler.cpp.

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

Referenced by checkForUndefinedSymbol().

◆ page()

std::vector< internal::Word > & Ark::Compiler::page ( const 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<internal::Word>&

Definition at line 92 of file Compiler.hpp.

Referenced by compileExpression(), compileFunction(), compileIf(), compileLetMutSet(), compilePluginImport(), compileSpecific(), compileSymbol(), compileWhile(), handleCalls(), and process().

◆ page_ptr()

std::vector< internal::Word > * Ark::Compiler::page_ptr ( const 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<internal::Word>*

Definition at line 105 of file Compiler.hpp.

◆ process()

void Ark::Compiler::process ( const internal::Node ast)

Start the compilation.

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

◆ pushFileHeader()

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

◆ pushSymAndValTables()

void Ark::Compiler::pushSymAndValTables ( )
private

◆ throwCompilerError()

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

Friends And Related Function Documentation

◆ State

friend class State
friend

Definition at line 59 of file Compiler.hpp.

◆ Welder

friend class Welder
friend

Definition at line 60 of file Compiler.hpp.

Member Data Documentation

◆ m_bytecode

bytecode_t Ark::Compiler::m_bytecode
private

Definition at line 71 of file Compiler.hpp.

Referenced by bytecode(), process(), pushFileHeader(), and pushSymAndValTables().

◆ m_code_pages

std::vector<std::vector<internal::Word> > Ark::Compiler::m_code_pages
private

Definition at line 68 of file Compiler.hpp.

Referenced by compileFunction(), and process().

◆ m_debug

unsigned Ark::Compiler::m_debug
private

the debug level of the compiler

Definition at line 72 of file Compiler.hpp.

◆ m_defined_symbols

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

◆ m_plugins

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

Definition at line 66 of file Compiler.hpp.

Referenced by compilePluginImport().

◆ m_symbols

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

Definition at line 64 of file Compiler.hpp.

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

◆ m_temp_pages

std::vector<std::vector<internal::Word> > Ark::Compiler::m_temp_pages
private

we need temporary code pages for some compilations passes

Definition at line 69 of file Compiler.hpp.

Referenced by handleCalls().

◆ m_values

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

Definition at line 67 of file Compiler.hpp.

Referenced by addValue(), and pushSymAndValTables().


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