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

The ArkScript AST to IR compiler. More...

#include <ASTLowerer.hpp>

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

Classes

struct  Page
 

Public Member Functions

 ASTLowerer (unsigned debug)
 Construct a new ASTLowerer 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 warning (const std::string &message, const Node &node)
 Display a warning message.
 
static void buildAndThrowError (const std::string &message, const Node &node)
 Throw a nice error message.
 

Private Attributes

LocalsLocator m_locals_locator
 
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
 
Logger m_logger
 

Detailed Description

The ArkScript AST to IR compiler.

Definition at line 36 of file ASTLowerer.hpp.

Constructor & Destructor Documentation

◆ ASTLowerer()

Ark::internal::ASTLowerer::ASTLowerer ( unsigned debug)
explicit

Construct a new ASTLowerer object.

Parameters
debugthe debug level

Definition at line 16 of file ASTLowerer.cpp.

Member Function Documentation

◆ addSymbol()

uint16_t Ark::internal::ASTLowerer::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 665 of file ASTLowerer.cpp.

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

Referenced by compileExpression(), compileFunction(), compileLetMutSet(), and compileSymbol().

◆ addValue() [1/2]

uint16_t Ark::internal::ASTLowerer::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 681 of file ASTLowerer.cpp.

References buildAndThrowError(), and m_values.

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

◆ addValue() [2/2]

uint16_t Ark::internal::ASTLowerer::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 697 of file ASTLowerer.cpp.

References buildAndThrowError(), and m_values.

◆ buildAndThrowError()

void Ark::internal::ASTLowerer::buildAndThrowError ( const std::string & message,
const Node & node )
staticprivate

◆ compileExpression()

◆ compileFunction()

◆ compileIf()

◆ compileLetMutSet()

◆ compileListInstruction()

◆ compilePluginImport()

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

◆ compileSymbol()

◆ compileWhile()

◆ getBuiltin()

std::optional< uint16_t > Ark::internal::ASTLowerer::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 57 of file ASTLowerer.cpp.

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

Referenced by compileExpression(), and compileSymbol().

◆ getListInstruction()

std::optional< Instruction > Ark::internal::ASTLowerer::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 68 of file ASTLowerer.cpp.

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

Referenced by compileExpression(), and compileListInstruction().

◆ getOperator()

std::optional< Instruction > Ark::internal::ASTLowerer::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 49 of file ASTLowerer.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::ASTLowerer::intermediateRepresentation ( ) const
nodiscardnoexcept

Return the IR blocks (one per scope)

Returns
const std::vector<Block>&

Definition at line 34 of file ASTLowerer.cpp.

References m_code_pages.

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

◆ isTernaryInst()

bool Ark::internal::ASTLowerer::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 105 of file ASTLowerer.cpp.

References Ark::internal::AT_AT.

Referenced by handleCalls().

◆ isUnaryInst()

bool Ark::internal::ASTLowerer::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 85 of file ASTLowerer.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::ASTLowerer::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 76 of file ASTLowerer.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::ASTLowerer::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 98 of file ASTLowerer.hpp.

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

◆ process()

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

Start the compilation.

Parameters
ast

Definition at line 20 of file ASTLowerer.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::ASTLowerer::symbols ( ) const
nodiscardnoexcept

Return the symbol table pre-computed.

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

Definition at line 39 of file ASTLowerer.cpp.

References m_symbols.

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

◆ values()

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

Return the value table pre-computed.

Returns
const std::vector<ValTableElem>&

Definition at line 44 of file ASTLowerer.cpp.

References m_values.

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

◆ warning()

void Ark::internal::ASTLowerer::warning ( const std::string & message,
const Node & node )
staticprivate

Display a warning message.

Parameters
message
node

Definition at line 117 of file ASTLowerer.cpp.

References Ark::Diagnostics::makeContextWithNode().

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

Member Data Documentation

◆ m_code_pages

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

Definition at line 86 of file ASTLowerer.hpp.

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

◆ m_current_label

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

Definition at line 88 of file ASTLowerer.hpp.

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

◆ m_locals_locator

LocalsLocator Ark::internal::ASTLowerer::m_locals_locator
private

◆ m_logger

Logger Ark::internal::ASTLowerer::m_logger
private

Definition at line 90 of file ASTLowerer.hpp.

Referenced by process().

◆ m_symbols

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

Definition at line 84 of file ASTLowerer.hpp.

Referenced by addSymbol(), and symbols().

◆ m_temp_pages

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

we need temporary code pages for some compilations passes

Definition at line 87 of file ASTLowerer.hpp.

Referenced by handleCalls().

◆ m_values

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

Definition at line 85 of file ASTLowerer.hpp.

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


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