ArkScript
A small, lisp-inspired, functional scripting language
Ark::internal::ASTLowerer Class Referencefinal

The ArkScript AST to IR compiler. More...

#include <ASTLowerer.hpp>

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

Classes

struct  Page
 

Public Member Functions

 ASTLowerer (unsigned debug)
 Construct a new ASTLowerer object.
 
void addToTables (const std::vector< std::string > &symbols, const std::vector< ValTableElem > &constants)
 Pre-fill tables (used by the debugger)
 
void offsetPagesBy (std::size_t offset)
 Start bytecode pages at a given offset (by default, 0)
 
void process (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.
 
- Public Member Functions inherited from Ark::internal::Pass
 Pass (std::string name, unsigned debug_level)
 Construct a new Pass object.
 
virtual ~Pass ()=default
 
void configureLogger (std::ostream &os)
 Set a custom output stream for the logger.
 

Private Types

enum class  ErrorKind { InvalidNodeMacro , InvalidNodeNoReturnValue , InvalidNodeInOperatorNoReturnValue , InvalidNodeInTailCallNoReturnValue }
 

Private Member Functions

Page createNewCodePage (const bool temp=false) noexcept
 
IR::Blockpage (const Page page) noexcept
 helper functions to get a temp or finalized code page
 
bool isFunctionCallingItself (const std::string &name) noexcept
 Check if we are in a recursive self call.
 
void warning (const std::string &message, const Node &node)
 Display a warning message.
 
void compileExpression (Node &x, Page p, bool is_result_unused, bool is_terminal)
 Compile an expression (a node) recursively.
 
void compileSymbol (const Node &x, Page p, bool is_result_unused, bool can_use_ref)
 
void compileListInstruction (Node &x, Page p, bool is_result_unused)
 
void compileApplyInstruction (Node &x, Page p, bool is_result_unused)
 
void compileIf (Node &x, Page p, bool is_result_unused, bool is_terminal)
 
void compileFunction (Node &x, Page p, bool is_result_unused)
 
void compileLetMutSet (Keyword n, Node &x, Page p, bool is_result_unused)
 
void compileWhile (Node &x, Page p)
 
void compilePluginImport (const Node &x, Page p)
 
void pushFunctionCallArguments (Node &call, Page p, bool is_tail_call)
 
void handleCalls (Node &x, Page p, bool is_result_unused, bool is_terminal)
 
void handleShortcircuit (Node &x, Page p)
 
void handleOperator (Node &x, Page p, Instruction op)
 
bool handleFunctionCall (Node &x, Page p, bool is_terminal)
 
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 isBreakpoint (const Node &node)
 
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 bool isRepeatableOperation (Instruction inst) noexcept
 Check if an operator can be repeated.
 
static void buildAndThrowError (const std::string &message, const Node &node)
 Throw a nice error message.
 
static void makeError (ErrorKind kind, const Node &node, const std::string &additional_ctx)
 Throw a nice error message, using a message builder.
 

Private Attributes

LocalsLocator m_locals_locator
 
std::vector< std::string > m_symbols
 
std::vector< ValTableElemm_values
 
std::size_t m_start_page_at_offset = 0
 Used to offset the page numbers when compiling code in the debugger.
 
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
 
std::stack< std::string > m_opened_vars
 stack of vars we are currently declaring
 

Additional Inherited Members

- Protected Attributes inherited from Ark::internal::Pass
Logger m_logger
 

Detailed Description

The ArkScript AST to IR compiler.

Definition at line 41 of file ASTLowerer.hpp.

Member Enumeration Documentation

◆ ErrorKind

enum class Ark::internal::ASTLowerer::ErrorKind
strongprivate
Enumerator
InvalidNodeMacro 
InvalidNodeNoReturnValue 
InvalidNodeInOperatorNoReturnValue 
InvalidNodeInTailCallNoReturnValue 

Definition at line 112 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 27 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 938 of file ASTLowerer.cpp.

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

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

◆ addToTables()

void Ark::internal::ASTLowerer::addToTables ( const std::vector< std::string > & symbols,
const std::vector< ValTableElem > & constants )

Pre-fill tables (used by the debugger)

Parameters
symbols
constants

Definition at line 31 of file ASTLowerer.cpp.

References m_symbols, m_values, and symbols().

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

◆ 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 954 of file ASTLowerer.cpp.

References buildAndThrowError(), m_values, and Ark::MaxValue16Bits.

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 970 of file ASTLowerer.cpp.

References buildAndThrowError(), m_values, and Ark::MaxValue16Bits.

◆ buildAndThrowError()

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

◆ compileApplyInstruction()

◆ compileExpression()

void Ark::internal::ASTLowerer::compileExpression ( Node & x,
Page p,
bool is_result_unused,
bool is_terminal )
private

Compile an expression (a node) recursively.

Parameters
xthe Node to compile
pthe current page number we're on
is_result_unused
is_terminal

Definition at line 210 of file ASTLowerer.cpp.

References addSymbol(), addValue(), Ark::internal::Language::Apply, Ark::internal::Namespace::ast, Ark::internal::Begin, buildAndThrowError(), Ark::internal::BUILTIN, compileApplyInstruction(), compileExpression(), compileFunction(), compileIf(), compileLetMutSet(), compileListInstruction(), compilePluginImport(), compileSymbol(), compileWhile(), Ark::internal::Node::constArkNamespace(), Ark::internal::Node::constList(), Ark::internal::DEL, Ark::internal::Del, Ark::internal::Field, Ark::internal::Node::filename(), Ark::internal::Fun, Ark::internal::GET_FIELD, getBuiltin(), getListInstruction(), handleCalls(), Ark::internal::If, Ark::internal::Import, Ark::internal::Keyword, Ark::internal::Let, Ark::internal::FilePos::line, Ark::internal::List, Ark::internal::Node::list(), Ark::internal::LOAD_CONST, Ark::internal::Mut, Ark::internal::Namespace, Ark::internal::Node::nodeType(), Ark::internal::Number, page(), Ark::internal::Node::position(), Ark::internal::Set, Ark::internal::FileSpan::start, Ark::internal::String, Ark::Value::string(), Ark::internal::Symbol, Ark::internal::typeToString(), Ark::internal::Unused, and Ark::internal::While.

Referenced by compileApplyInstruction(), compileExpression(), compileFunction(), compileIf(), compileLetMutSet(), compileListInstruction(), compileWhile(), handleFunctionCall(), handleOperator(), handleShortcircuit(), process(), and pushFunctionCallArguments().

◆ compileFunction()

◆ compileIf()

◆ compileLetMutSet()

◆ compileListInstruction()

◆ compilePluginImport()

◆ compileSymbol()

◆ compileWhile()

◆ createNewCodePage()

Page Ark::internal::ASTLowerer::createNewCodePage ( const bool temp = false)
inlineprivatenoexcept

Definition at line 120 of file ASTLowerer.hpp.

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

◆ 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 79 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 90 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 71 of file ASTLowerer.cpp.

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

Referenced by compileSymbol(), and handleCalls().

◆ handleCalls()

◆ handleFunctionCall()

◆ handleOperator()

◆ handleShortcircuit()

◆ 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 56 of file ASTLowerer.cpp.

References m_code_pages.

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

◆ isBreakpoint()

bool Ark::internal::ASTLowerer::isBreakpoint ( const Node & node)
staticprivate

Checks if a node is a list and is a call to 'breakpoint'

Parameters
nodenode to check
Returns
true if the node is a 'breakpoint' call: (breakpoint <cond>)
false otherwise

Definition at line 98 of file ASTLowerer.cpp.

References Ark::internal::Node::constList(), Ark::internal::List, Ark::internal::Node::nodeType(), and Ark::internal::Symbol.

Referenced by handleFunctionCall(), handleOperator(), and pushFunctionCallArguments().

◆ isFunctionCallingItself()

bool Ark::internal::ASTLowerer::isFunctionCallingItself ( const std::string & name)
inlinenodiscardprivatenoexcept

Check if we are in a recursive self call.

Parameters
namesymbol name being compiled
Returns
true if the name passed is the name of the last function we entered

Definition at line 151 of file ASTLowerer.hpp.

Referenced by handleFunctionCall().

◆ isRepeatableOperation()

bool Ark::internal::ASTLowerer::isRepeatableOperation ( Instruction inst)
staticprivatenoexcept

Check if an operator can be repeated.

Parameters
inst
Returns
true the instruction can be repeated, eg (+ 1 2 3) compiles to (+ (+ 1 2) 3), false otherwise

Definition at line 161 of file ASTLowerer.cpp.

References Ark::internal::ADD, Ark::internal::DIV, Ark::internal::MUL, and Ark::internal::SUB.

Referenced by handleOperator().

◆ 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 otherwise

Definition at line 149 of file ASTLowerer.cpp.

References Ark::internal::AT_AT.

Referenced by handleOperator().

◆ 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 otherwise

Definition at line 129 of file ASTLowerer.cpp.

References Ark::internal::HEAD, Ark::internal::IS_EMPTY, Ark::internal::IS_NIL, Ark::internal::LEN, Ark::internal::NOT, Ark::internal::TAIL, Ark::internal::TO_NUM, Ark::internal::TO_STR, and Ark::internal::TYPE.

Referenced by handleOperator().

◆ makeError()

void Ark::internal::ASTLowerer::makeError ( ErrorKind kind,
const Node & node,
const std::string & additional_ctx )
staticprivate

Throw a nice error message, using a message builder.

Parameters
kinderror kind
nodeerroneous node
additional_ctxoptional context for the error, e.g. the macro name

Definition at line 186 of file ASTLowerer.cpp.

References buildAndThrowError(), InvalidNodeInOperatorNoReturnValue, InvalidNodeInTailCallNoReturnValue, InvalidNodeMacro, and InvalidNodeNoReturnValue.

Referenced by compileApplyInstruction(), compileFunction(), compileLetMutSet(), compileListInstruction(), compileWhile(), handleOperator(), and pushFunctionCallArguments().

◆ 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 105 of file ASTLowerer.cpp.

References Ark::internal::Begin, Ark::internal::Node::constList(), Ark::internal::Fun, Ark::internal::If, Ark::internal::Keyword, Ark::internal::Let, Ark::internal::List, Ark::internal::Mut, nodeProducesOutput(), Ark::internal::Node::nodeType(), Ark::internal::Set, and Ark::internal::Symbol.

Referenced by compileApplyInstruction(), compileListInstruction(), handleFunctionCall(), handleOperator(), handleShortcircuit(), nodeProducesOutput(), and pushFunctionCallArguments().

◆ offsetPagesBy()

void Ark::internal::ASTLowerer::offsetPagesBy ( std::size_t offset)

Start bytecode pages at a given offset (by default, 0)

Parameters
offset

Definition at line 37 of file ASTLowerer.cpp.

References m_start_page_at_offset.

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

◆ 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 138 of file ASTLowerer.hpp.

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

◆ process()

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

◆ pushFunctionCallArguments()

void Ark::internal::ASTLowerer::pushFunctionCallArguments ( Node & call,
Page p,
bool is_tail_call )
private

◆ 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 61 of file ASTLowerer.cpp.

References m_symbols.

Referenced by addToTables(), and 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 66 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 )
private

Member Data Documentation

◆ m_code_pages

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

Definition at line 107 of file ASTLowerer.hpp.

Referenced by intermediateRepresentation().

◆ m_current_label

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

◆ m_locals_locator

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

◆ m_opened_vars

std::stack<std::string> Ark::internal::ASTLowerer::m_opened_vars
private

stack of vars we are currently declaring

Definition at line 110 of file ASTLowerer.hpp.

Referenced by compileFunction(), and compileLetMutSet().

◆ m_start_page_at_offset

std::size_t Ark::internal::ASTLowerer::m_start_page_at_offset = 0
private

Used to offset the page numbers when compiling code in the debugger.

Definition at line 106 of file ASTLowerer.hpp.

Referenced by offsetPagesBy().

◆ m_symbols

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

Definition at line 104 of file ASTLowerer.hpp.

Referenced by addSymbol(), addToTables(), 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 108 of file ASTLowerer.hpp.

Referenced by handleFunctionCall().

◆ m_values

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

Definition at line 105 of file ASTLowerer.hpp.

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


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