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

The ArkScript bytecode compiler. More...

#include <Compiler.hpp>

Collaboration diagram for Ark::internal::Compiler:
[legend]

Classes

struct  Page
 

Public Member Functions

 Compiler (unsigned debug)
 Construct a new Compiler object.
 
void process (const Node &ast)
 Start the compilation.
 
const std::vector< IR::Block > & intermediateRepresentation () const noexcept
 Return the IR blocks (one per scope)
 
const std::vector< std::string > & symbols () const noexcept
 Return the symbol table pre-computed.
 
const std::vector< ValTableElem > & values () const noexcept
 Return the value table pre-computed.
 

Private Member Functions

IR::Blockpage (const Page page) noexcept
 helper functions to get a temp or finalized code page
 
void compileExpression (const Node &x, Page p, bool is_result_unused, bool is_terminal, const std::string &var_name="")
 Compile an expression (a node) recursively.
 
void compileSymbol (const Node &x, Page p, bool is_result_unused)
 
void compileListInstruction (const Node &c0, const Node &x, Page p, bool is_result_unused)
 
void compileIf (const Node &x, Page p, bool is_result_unused, bool is_terminal, const std::string &var_name)
 
void compileFunction (const Node &x, Page p, bool is_result_unused, const std::string &var_name)
 
void compileLetMutSet (Keyword n, const Node &x, Page p)
 
void compileWhile (const Node &x, Page p)
 
void compilePluginImport (const Node &x, Page p)
 
void handleCalls (const Node &x, Page p, bool is_result_unused, bool is_terminal, const std::string &var_name)
 
uint16_t addSymbol (const Node &sym)
 Register a given node in the symbol table.
 
uint16_t addValue (const Node &x)
 Register a given node in the value table.
 
uint16_t addValue (std::size_t page_id, const Node &current)
 Register a page id (function reference) in the value table.
 

Static Private Member Functions

static std::optional< InstructiongetOperator (const std::string &name) noexcept
 Checking if a symbol is an operator.
 
static std::optional< uint16_t > getBuiltin (const std::string &name) noexcept
 Checking if a symbol is a builtin.
 
static std::optional< InstructiongetListInstruction (const std::string &name) noexcept
 Checking if a symbol is a list instruction.
 
static bool nodeProducesOutput (const Node &node)
 
static bool isUnaryInst (Instruction inst) noexcept
 Check if a given instruction is unary (takes only one argument)
 
static bool isTernaryInst (Instruction inst) noexcept
 Check if a given instruction is ternary (takes three arguments)
 
static void compilerWarning (const std::string &message, const Node &node)
 Display a warning message.
 
static void throwCompilerError (const std::string &message, const Node &node)
 Throw a nice error message.
 

Private Attributes

std::vector< std::string > m_symbols
 
std::vector< ValTableElemm_values
 
std::vector< IR::Blockm_code_pages
 
std::vector< IR::Blockm_temp_pages
 we need temporary code pages for some compilations passes
 
IR::label_t m_current_label = 0
 
unsigned m_debug
 the debug level of the compiler
 
Logger m_logger
 

Detailed Description

The ArkScript bytecode compiler.

Definition at line 36 of file Compiler.hpp.

Constructor & Destructor Documentation

◆ Compiler()

Ark::internal::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

◆ addSymbol()

uint16_t Ark::internal::Compiler::addSymbol ( const 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 625 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::internal::Compiler::addValue ( const 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 641 of file Compiler.cpp.

References m_values, and throwCompilerError().

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

◆ addValue() [2/2]

uint16_t Ark::internal::Compiler::addValue ( std::size_t page_id,
const 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 657 of file Compiler.cpp.

References m_values, and throwCompilerError().

◆ compileExpression()

◆ compileFunction()

◆ compileIf()

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

◆ compileLetMutSet()

◆ compileListInstruction()

◆ compilePluginImport()

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

◆ compilerWarning()

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

Display a warning message.

Parameters
message
node

Definition at line 122 of file Compiler.cpp.

References Ark::Diagnostics::makeContextWithNode().

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

◆ compileSymbol()

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

◆ compileWhile()

◆ getBuiltin()

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

Checking if a symbol is a builtin.

Parameters
namesymbol name
Returns
std::optional<uint16_t> builtin number

Definition at line 62 of file Compiler.cpp.

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

Referenced by compileExpression(), and compileSymbol().

◆ getListInstruction()

std::optional< Instruction > Ark::internal::Compiler::getListInstruction ( const std::string & name)
staticprivatenoexcept

Checking if a symbol is a list instruction.

Parameters
name
Returns
std::optional<Instruction> list instruction

Definition at line 73 of file Compiler.cpp.

References Ark::internal::LIST, and Ark::internal::Language::listInstructions.

Referenced by compileExpression(), and compileListInstruction().

◆ getOperator()

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

Checking if a symbol is an operator.

Parameters
namesymbol name
Returns
std::optional<Instruction> operator instruction

Definition at line 54 of file Compiler.cpp.

References Ark::internal::FIRST_OPERATOR, and Ark::internal::Language::operators.

Referenced by compileSymbol(), and handleCalls().

◆ handleCalls()

◆ intermediateRepresentation()

const std::vector< IR::Block > & Ark::internal::Compiler::intermediateRepresentation ( ) const
nodiscardnoexcept

Return the IR blocks (one per scope)

Returns
const std::vector<Block>&

Definition at line 39 of file Compiler.cpp.

References m_code_pages.

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

◆ isTernaryInst()

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

Check if a given instruction is ternary (takes three arguments)

Parameters
inst
Returns
true the instruction is ternary
false

Definition at line 110 of file Compiler.cpp.

References Ark::internal::AT_AT.

Referenced by handleCalls().

◆ isUnaryInst()

bool Ark::internal::Compiler::isUnaryInst ( 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 90 of file Compiler.cpp.

References Ark::internal::EMPTY, 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().

◆ nodeProducesOutput()

bool Ark::internal::Compiler::nodeProducesOutput ( const Node & node)
staticprivate

Checks if a node is a list and has a keyboard as its first node, indicating if it's producing a value on the stack or not

Parameters
nodenode to check
Returns
true if the node produces an output on the stack (fun, if, begin)
false otherwise (let, mut, set, while, import, del)

Definition at line 81 of file Compiler.cpp.

References Ark::internal::Begin, Ark::internal::Node::constList(), Ark::internal::Fun, Ark::internal::If, Ark::internal::Keyword, Ark::internal::List, and Ark::internal::Node::nodeType().

Referenced by compileListInstruction(), and handleCalls().

◆ page()

IR::Block & Ark::internal::Compiler::page ( const Page page)
inlineprivatenoexcept

helper functions to get a temp or finalized code page

Parameters
pagepage descriptor
Returns
std::vector<IR::Block>&

Definition at line 97 of file Compiler.hpp.

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

◆ process()

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

Start the compilation.

Parameters
ast

Definition at line 25 of file Compiler.cpp.

References compileExpression(), m_code_pages, m_logger, Ark::internal::Logger::traceEnd(), and Ark::internal::Logger::traceStart().

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

◆ symbols()

const std::vector< std::string > & Ark::internal::Compiler::symbols ( ) const
nodiscardnoexcept

Return the symbol table pre-computed.

Returns
const std::vector<std::string>&

Definition at line 44 of file Compiler.cpp.

References m_symbols.

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

◆ throwCompilerError()

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

◆ values()

const std::vector< ValTableElem > & Ark::internal::Compiler::values ( ) const
nodiscardnoexcept

Return the value table pre-computed.

Returns
const std::vector<ValTableElem>&

Definition at line 49 of file Compiler.cpp.

References m_values.

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

Member Data Documentation

◆ m_code_pages

std::vector<IR::Block> Ark::internal::Compiler::m_code_pages
private

Definition at line 84 of file Compiler.hpp.

Referenced by compileFunction(), intermediateRepresentation(), and process().

◆ m_current_label

IR::label_t Ark::internal::Compiler::m_current_label = 0
private

Definition at line 86 of file Compiler.hpp.

Referenced by compileIf(), compileWhile(), and handleCalls().

◆ m_debug

unsigned Ark::internal::Compiler::m_debug
private

the debug level of the compiler

Definition at line 88 of file Compiler.hpp.

◆ m_logger

Logger Ark::internal::Compiler::m_logger
private

Definition at line 89 of file Compiler.hpp.

Referenced by process().

◆ m_symbols

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

Definition at line 82 of file Compiler.hpp.

Referenced by addSymbol(), and symbols().

◆ m_temp_pages

std::vector<IR::Block> Ark::internal::Compiler::m_temp_pages
private

we need temporary code pages for some compilations passes

Definition at line 85 of file Compiler.hpp.

Referenced by handleCalls().

◆ m_values

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

Definition at line 83 of file Compiler.hpp.

Referenced by addValue(), addValue(), and values().


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