feat: error handling, better prelude, double linked lists usage etc

This commit is contained in:
2026-01-02 22:06:56 +01:00
parent a453603b9b
commit 6176d549c1
10 changed files with 148 additions and 65 deletions

View File

@@ -1,35 +1,34 @@
#pragma once
#include "ir/op.hpp"
#include "prelude/linkedlist.hpp"
namespace IR
{
using OpView = View<Op*>;
using BlockID = unsigned int;
class Block
{
public:
Block(BlockID id, OpView&& ops)
: m_id(id), m_ops(ops) {}
Block(BlockID id, const OpView& ops)
: m_id(id), m_ops(DoubleLinkedList<Op*>::FromView(ops)) {}
public:
const OpView& ops() const { return m_ops; }
DoubleLinkedList<Op*>& ops() { return m_ops; }
public:
StringView Format(int indent) const
{
StringBuilder sb;
sb.AppendIndent(indent);
sb.AppendFormat("b%d:\n", m_id);
for (size_t i = 0; i < m_ops.size; ++i)
for (ListNode<Op*>* cur = m_ops.Begin(); cur != nullptr; cur = cur->next)
{
sb << m_ops.data[i]->Format(indent + 2) << '\n';
sb << cur->value->Format(indent + 2) << '\n';
}
return sb.view();
}
private:
BlockID m_id;
OpView m_ops;
DoubleLinkedList<Op*> m_ops;
};
} // namespace IR

View File

@@ -1,6 +1,7 @@
#pragma once
#include <unordered_map>
#include "parser/nodes.hpp"
#include "prelude/error.hpp"
#include "prelude/string.hpp"
#include "ir/value.hpp"
#include "ir/ops.hpp"
@@ -11,8 +12,8 @@ namespace IR
class IRBuilder
{
public:
IRBuilder(const Node *root)
: m_root(root), m_ops(new OpBuilder()) {}
IRBuilder(const StringView &filename, const Node *root)
: m_root(root), m_ops(new OpBuilder()), m_filename(filename) {}
public:
// TODO: support other literals
@@ -36,8 +37,9 @@ namespace IR
{
if (m_locals.find(var->name()) == m_locals.end())
{
// TODO: throw proper error
assert(0 && "ERROR: variable does not exist");
// TODO: pass line:offset when Node will have them
ErrorLogger::Raise(Error::CompileError(m_filename, StringView::FromFormat("use of undefined variable '%s'", var->name().c_str())));
assert(false);
}
auto dst = AllocateNamed<Instruction>(m_locals[var->name()]->GetValueType());
m_ops->Push(new LoadOp(dst, m_locals[var->name()]));
@@ -189,6 +191,7 @@ namespace IR
private:
const Node *m_root = nullptr;
StringView m_filename;
OpBuilder *m_ops = nullptr;
unsigned int m_value_counter = 0;

View File

@@ -52,7 +52,7 @@ namespace IR
public:
const StringView &name() const { return m_name; }
const Block &body() const { return m_body; }
Block &body() { return m_body; }
const View<StringView> &params() const { return m_params; }
private: