11#ifndef ARK_COMPILER_LOWERER_ASTLOWERER_HPP
12#define ARK_COMPILER_LOWERER_ASTLOWERER_HPP
41 std::optional<std::string>
name { std::nullopt };
64 void addToTables(
const std::vector<std::string>& symbols,
const std::vector<ValTableElem>& constants);
71 void offsetPagesBy(std::size_t offset);
78 void process(
Node& ast);
85 [[nodiscard]]
const std::vector<IR::Block>& intermediateRepresentation()
const noexcept;
92 [[nodiscard]]
const std::vector<std::string>& symbols()
const noexcept;
99 [[nodiscard]]
const std::vector<ValTableElem>& values()
const noexcept;
103 return m_current_label;
124 std::size_t m_start_page_at_offset = 0;
133 InvalidNodeNoReturnValue,
134 InvalidNodeInOperatorNoReturnValue,
135 InvalidNodeInTailCallNoReturnValue
142 const std::size_t new_page_addr = m_start_page_at_offset + m_code_pages.size();
143 m_code_pages.emplace_back(
147 .addr = new_page_addr,
148 .is_closure = args.closure },
150 return Page { .index = new_page_addr, .is_temp =
false };
153 m_temp_pages.emplace_back();
154 return Page { .index = m_temp_pages.size() - 1u, .is_temp =
true };
160 return m_code_pages[page.index - m_start_page_at_offset];
161 return m_temp_pages[page.index];
173 return m_code_pages[page.index - m_start_page_at_offset].data;
174 return m_temp_pages[page.index].data;
185 return !m_opened_vars.empty() && m_opened_vars.top().name == name;
194 static std::optional<Instruction> getOperator(
const std::string& name)
noexcept;
202 static std::optional<uint16_t> getBuiltin(
const std::string& name)
noexcept;
210 static std::optional<Instruction> getListInstruction(
const std::string& name)
noexcept;
218 static bool isBreakpoint(
const Node& node);
226 static bool nodeProducesOutput(
const Node& node);
234 static bool isUnaryInst(
Instruction inst)
noexcept;
242 static bool isTernaryInst(
Instruction inst)
noexcept;
250 static bool isRepeatableOperation(
Instruction inst)
noexcept;
258 void warning(
const std::string& message,
const Node& node);
266 [[noreturn]]
static void buildAndThrowError(
const std::string& message,
const Node& node);
275 static void makeError(
ErrorKind kind,
const Node& node,
const std::string& additional_ctx);
286 void compileExpression(
Node& x, Page p,
bool is_result_unused,
bool is_terminal,
bool can_use_ref);
288 void compileSymbol(
const Node& x, Page p,
bool is_result_unused,
bool can_use_ref);
289 void compileListInstruction(
Node& x, Page p,
bool is_result_unused);
290 void compileApplyInstruction(
Node& x, Page p,
bool is_result_unused);
291 void compileIf(
Node& x, Page p,
bool is_result_unused,
bool is_terminal,
bool can_use_ref);
292 void compileFunction(
Node& x, Page p,
bool is_result_unused);
293 void setFunctionMetadata(Page p, std::size_t arg_count,
bool mutates_args);
294 void compileLetMutSet(
Keyword n,
Node& x, Page p,
bool is_result_unused);
295 void compileWhile(
Node& x, Page p);
296 void compilePluginImport(
const Node& x, Page p);
297 void pushFunctionCallArguments(
Node& call, Page p,
bool is_tail_call);
298 void handleCalls(
Node& x, Page p,
bool is_result_unused,
bool is_terminal,
bool can_use_ref);
299 void handleShortcircuit(
Node& x, Page p,
bool can_use_ref);
301 bool handleFunctionCall(
Node& x, Page p,
bool is_terminal);
310 uint16_t addSymbol(
const Node& sym);
319 uint16_t addValue(
const Node& x);
329 uint16_t addValue(std::size_t page_id,
const Node& current);
An entity in the IR is a bundle of information.
The different instructions used by the compiler and virtual machine.
AST node used by the parser, optimizer and compiler.
Interface for a compiler pass.
The basic value type handled by the compiler.
The ArkScript AST to IR compiler.
IR::Block::vec_t & page(const Page page) noexcept
helper functions to get a temp or finalised code page
IR::Block & block(const Page page) noexcept
std::vector< ValTableElem > m_values
std::stack< Var > m_opened_vars
stack of vars we are currently declaring
std::vector< IR::Block > m_temp_pages
we need temporary code pages for some compilations passes
IR::label_t lastLabel() const noexcept
Page createNewCodePage(PageCreationData &&args=PageCreationData {}) noexcept
std::vector< IR::Block > m_code_pages
std::vector< std::string > m_symbols
LocalsLocator m_locals_locator
bool isFunctionCallingItself(const std::string &name) noexcept
Check if we are in a recursive self call.
A node of an Abstract Syntax Tree for ArkScript.
An interface to describe compiler passes.
Keyword
The different keywords available.
Instruction
The different bytecodes are stored here.
std::size_t argument_count
Block of IR entities, with attached metadata.
std::vector< Entity > vec_t
std::optional< std::string > name