Compare commits

...

6 Commits

Author SHA1 Message Date
CHatingPython 3a2fa32ce1 fix!: remove references to context::collect
I forgor to do that earlier lol

Refs: #12
2026-06-20 12:56:43 +02:00
CHatingPython f2294079a0 docs(furvm): document the context
Refs: #12
2026-06-20 12:42:42 +02:00
CHatingPython 39f873a984 docs(furvm): document detail folder
Refs: #12
2026-06-20 12:26:53 +02:00
CHatingPython d46aa45c1f feat(furvm): add on release callback to refcount handles
Refs: #12
2026-06-20 12:19:53 +02:00
CHatingPython 190aa8f985 refactor(furc): move arena allocator out of the parser
Refs: #12
2026-06-20 11:56:15 +02:00
CHatingPython f80abd68ad refactor(furvm): introduce a furvm.hpp header
Refs: #12
2026-06-20 11:32:01 +02:00
10 changed files with 195 additions and 56 deletions
+5 -5
View File
@@ -23,7 +23,7 @@ public:
* @param filename Filename for debugging.
* @param content Content.
*/
parser(std::string_view filename, std::string_view content);
parser(furlang::arena& arena, std::string_view filename, std::string_view content);
/**
* @brief Construct a new parser from file.
@@ -32,7 +32,8 @@ public:
*
* @param filename Name of the file.
*/
parser(std::string_view filename);
parser(furlang::arena& arena, std::string_view filename);
~parser() = default;
/**
@@ -40,13 +41,12 @@ public:
*/
parser(parser&&) = default;
parser(const parser&) = delete;
/**
* @brief Move constructor.
*/
parser& operator=(parser&&) = default;
parser(const parser&) = delete;
parser& operator=(const parser&) = delete;
public:
/**
@@ -73,7 +73,7 @@ private:
std::string m_filename;
std::string m_content;
lexer m_lexer;
furlang::arena m_arena;
furlang::arena* m_arena;
std::vector<token_r> m_peekBuffer;
};
+20 -20
View File
@@ -16,11 +16,11 @@ namespace furc::front {
using namespace std::string_literals;
parser::parser(std::string_view filename, std::string_view content)
: m_filename(filename), m_content(content), m_lexer(m_filename, m_content) {}
parser::parser(furlang::arena& arena, std::string_view filename, std::string_view content)
: m_filename(filename), m_content(content), m_lexer(m_filename, m_content), m_arena(&arena) {}
parser::parser(std::string_view filename)
: m_filename(filename) {
parser::parser(furlang::arena& arena, std::string_view filename)
: m_filename(filename), m_arena(&arena) {
std::ifstream file(m_filename, std::ios_base::binary | std::ios_base::ate);
if (!file.is_open()) throw std::runtime_error("failed to open file "s.append(m_filename));
std::streampos size = file.tellg();
@@ -32,7 +32,7 @@ parser::parser(std::string_view filename)
}
ast::program_node_r parser::parse() & {
auto program = m_arena.allocate_shared<ast::program_node>(location{ m_filename });
auto program = m_arena->allocate_shared<ast::program_node>(location{ m_filename });
while (peek_token().has_value()) {
auto decl = parse_declaration();
@@ -67,13 +67,13 @@ ast::declaration_node_r parser::parse_declaration() {
case token_t::LBrace: {
ast::body_r body = parse_body();
if (body.has_error()) return ast::declaration_node_r(ast::error{ body.error().location });
return m_arena.allocate_shared<ast::function_definition_node>(first->location,
return m_arena->allocate_shared<ast::function_definition_node>(first->location,
name->value.string,
std::move(body.value()));
}
case token_t::Semicolon: {
m_peekBuffer.clear();
return m_arena.allocate_shared<ast::function_declaration_node>(first->location, name->value.string);
return m_arena->allocate_shared<ast::function_declaration_node>(first->location, name->value.string);
}
default: return ast::declaration_node_r(ast::error{ tok->location });
}
@@ -109,13 +109,13 @@ ast::statement_node_r parser::parse_statement() {
auto tok = next_token();
if (peek_token()->type == token_t::Semicolon) {
next_token();
return m_arena.allocate_shared<ast::return_statement_node>(location);
return m_arena->allocate_shared<ast::return_statement_node>(location);
}
auto value = parse_expression();
auto err = eat_token(token_t::Semicolon);
if (err.has_error()) return ast::statement_node_r(ast::error{ err.error().location });
return m_arena.allocate_shared<ast::return_statement_node>(location, std::move(value.value()));
return m_arena->allocate_shared<ast::return_statement_node>(location, std::move(value.value()));
}
case keyword_token::If: {
auto tok = next_token();
@@ -136,13 +136,13 @@ ast::statement_node_r parser::parse_statement() {
auto elseBody = parse_statement();
if (elseBody.has_error()) return ast::statement_node_r(ast::error{ elseBody.error().location });
return m_arena.allocate_shared<ast::if_statement_node>(location,
return m_arena->allocate_shared<ast::if_statement_node>(location,
std::move(cond.value()),
std::move(then.value()),
std::move(elseBody.value()));
}
return m_arena.allocate_shared<ast::if_statement_node>(location,
return m_arena->allocate_shared<ast::if_statement_node>(location,
std::move(cond.value()),
std::move(then.value()));
}
@@ -160,7 +160,7 @@ ast::statement_node_r parser::parse_statement() {
auto body = parse_statement();
if (body.has_error()) return ast::statement_node_r(ast::error{ body.error().location });
return m_arena.allocate_shared<ast::while_statement_node>(location,
return m_arena->allocate_shared<ast::while_statement_node>(location,
std::move(cond.value()),
std::move(body.value()));
}
@@ -172,7 +172,7 @@ ast::statement_node_r parser::parse_statement() {
case token_t::LBrace: {
auto body = parse_body();
if (body.has_error()) return ast::statement_node_r(ast::error{ body.error().location });
return m_arena.allocate_shared<ast::compound_statement_node>(location, std::move(body.value()));
return m_arena->allocate_shared<ast::compound_statement_node>(location, std::move(body.value()));
}
default: break;
}
@@ -204,7 +204,7 @@ ast::expression_node_r parser::parse_expression_primary() {
case token_t::Identifier: {
auto tok = next_token();
if (tok.has_error()) return ast::expression_node_r(ast::error{ tok.error().location });
return m_arena.allocate_shared<ast::var_read_expression_node>(tok->location, (*tok)->string);
return m_arena->allocate_shared<ast::var_read_expression_node>(tok->location, (*tok)->string);
}
case token_t::LParen: {
auto tok = next_token();
@@ -216,12 +216,12 @@ ast::expression_node_r parser::parse_expression_primary() {
case token_t::String: {
auto tok = next_token();
if (tok.has_error()) return ast::expression_node_r(ast::error{ tok.error().location });
return m_arena.allocate_shared<ast::string_literal_node>(tok->location, (*tok)->string);
return m_arena->allocate_shared<ast::string_literal_node>(tok->location, (*tok)->string);
}
case token_t::Integer: {
auto tok = next_token();
if (tok.has_error()) return ast::expression_node_r(ast::error{ tok.error().location });
return m_arena.allocate_shared<ast::integer_literal_node>(tok->location, (*tok)->integer);
return m_arena->allocate_shared<ast::integer_literal_node>(tok->location, (*tok)->integer);
}
default: {
return ast::expression_node_r(ast::error{ tok->location });
@@ -262,7 +262,7 @@ ast::expression_node_r parser::parse_expression_unary(std::uint32_t precedence)
expression = std::move(std::move(expr.value()));
}
result = m_arena.allocate_shared<ast::unary_op_expression_node>(token->location,
result = m_arena->allocate_shared<ast::unary_op_expression_node>(token->location,
current.type,
std::move(expression));
}
@@ -389,18 +389,18 @@ ast::expression_node_r parser::parse_expression_rhs(ast::expression_node_p&& ini
switch (current.type) {
case rhsop_info_t::Unaryop:
lhs = m_arena.allocate_shared<ast::unary_op_expression_node>(opToken->location,
lhs = m_arena->allocate_shared<ast::unary_op_expression_node>(opToken->location,
current.unary,
std::move(lhs));
break;
case rhsop_info_t::Binop:
lhs = m_arena.allocate_shared<ast::binary_op_expression_node>(opToken->location,
lhs = m_arena->allocate_shared<ast::binary_op_expression_node>(opToken->location,
current.binary,
std::move(lhs),
std::move(rhs));
break;
case rhsop_info_t::Assignment:
lhs = m_arena.allocate_shared<ast::var_assign_expression_node>(opToken->location,
lhs = m_arena->allocate_shared<ast::var_assign_expression_node>(opToken->location,
current.assignment,
std::move(lhs),
std::move(rhs));
+3 -1
View File
@@ -4,6 +4,7 @@
#include "furc/front/ir_generator.hpp"
#include "furc/front/parser.hpp"
#include "furc/front/ssa.hpp"
#include "furlang/arena.hpp"
#include <iostream>
@@ -19,7 +20,8 @@ int main(void) {
}
}
)";
furc::front::parser parser("<TEMP>", programStr);
furlang::arena arena{};
furc::front::parser parser(arena, "<TEMP>", programStr);
furc::front::ir_generator generator;
auto programResult = parser.parse();
+77 -10
View File
@@ -17,7 +17,11 @@ class context {
public:
friend class executor;
public:
/**
* @brief Constructs a context.
*/
context();
~context() = default;
/**
@@ -33,44 +37,90 @@ public:
context(const context&) = delete;
context& operator=(const context&) = delete;
public:
/**
* @brief Emplaces a module in the context.
*
* @param args Arguments forwarded to the module constructor.
* @return The emplaced module.
*/
template <typename... Args>
auto emplace_module(Args&&... args) {
return m_modules.emplace(std::forward<Args>(args)...);
}
/**
* @brief Returns a module from the context.
*
* @param args Module's id.
* @return A handle to the module.
*/
template <typename... Args>
auto module_at(Args&&... args) {
return m_modules.at(std::forward<Args>(args)...);
}
/**
* @brief Returns a module from the context.
*
* @param args Module's id.
* @return A handle to the module.
*/
template <typename... Args>
auto module_at(Args&&... args) const {
return m_modules.at(std::forward<Args>(args)...);
}
/**
* @brief Erases a module from the context.
*
* @param args Module's id.
*/
template <typename... Args>
auto erase_module(Args&&... args) {
return m_modules.erase(std::forward<Args>(args)...);
void erase_module(Args&&... args) {
m_modules.erase(std::forward<Args>(args)...);
}
public:
/**
* @brief Emplaces an executor in the context.
*
* @param args Arguments forwarded to executor's constructor.
* @return A handle to the emplaced executor.
*/
template <typename... Args>
auto emplace_executor(Args&&... args) {
return m_executors.emplace_back(std::forward<Args>(args)...);
}
/**
* @brief Returns an executor from the context.
*
* @param args Id of the executor.
* @return A handle to the executor.
*/
template <typename... Args>
auto executor_at(Args&&... args) {
return m_executors.at(std::forward<Args>(args)...);
}
/**
* @brief Returns an executor from the context.
*
* @param args Id of the executor.
* @return A handle to the executor.
*/
template <typename... Args>
auto executor_at(Args&&... args) const {
return m_executors.at(std::forward<Args>(args)...);
}
/**
* @brief Erases an executor from the context.
*
* @param args Id of the executor.
*/
template <typename... Args>
auto erase_executor(Args&&... args) {
return m_executors.erase(std::forward<Args>(args)...);
void erase_executor(Args&&... args) {
m_executors.erase(std::forward<Args>(args)...);
}
public:
template <typename... Args>
@@ -78,27 +128,44 @@ public:
return m_things.emplace_back(std::forward<Args>(args)...);
}
/**
* @brief Returns a thing from the context.
*
* @param args Id of the thing.
* @return A handle to the thing.
*/
template <typename... Args>
auto thing_at(Args&&... args) {
return m_things.at(std::forward<Args>(args)...);
}
/**
* @brief Returns a thing from the context.
*
* @param args Id of the thing.
* @return A handle to the thing.
*/
template <typename... Args>
auto thing_at(Args&&... args) const {
return m_things.at(std::forward<Args>(args)...);
}
/**
* @brief Erases a thing from the context.
*
* @param args Id of the thing.
*/
template <typename... Args>
auto erase_thing(Args&&... args) {
return m_things.erase(std::forward<Args>(args)...);
void erase_thing(Args&&... args) {
m_things.erase(std::forward<Args>(args)...);
}
thing_allocator<std::byte> thing_alloc() const { return m_thingAllocator; }
public:
/**
* @brief Removes unreferenced things from the thing list.
* @brief Returns context's thing allocator.
*
* @return The thing allocator.
*/
void collect();
thing_allocator<std::byte> thing_alloc() const { return m_thingAllocator; }
private:
handle_container<mod_h> m_modules;
handle_container<thing_h> m_things;
+9
View File
@@ -6,15 +6,24 @@
namespace furvm {
namespace detail {
/**
* @brief Default specialization for header_has_refcount type trait.
*/
template <typename Header, typename = void>
struct header_has_refcount : std::false_type {};
/**
* @brief Specialization for header_has_refcount type trait.
*/
template <typename Header>
struct header_has_refcount<Header,
std::void_t<decltype(std::declval<Header&>().acquire()),
decltype(std::declval<Header&>().release()),
decltype(std::declval<Header&>().reference_count())>> : std::true_type {};
/**
* @brief An alias for header_has_refcount's value.
*/
template <typename Header>
static constexpr auto header_has_refcount_v = header_has_refcount<Header>::value;
@@ -8,15 +8,69 @@
namespace furvm {
namespace detail {
/**
* @brief Serializes an integer.
*
* @param os Output stream.
* @param value Integer.
* @return The output stream.
*/
std::ostream& serialize(std::ostream& os, std::int8_t value);
std::ostream& serialize(std::ostream& os, std::int16_t value);
/**
* @brief Serializes an integer.
*
* @param os Output stream.
* @param value Integer.
* @return The output stream.
*/
std::ostream& serialize(std::ostream& os, std::int32_t value);
std::ostream& serialize(std::ostream& os, std::int64_t value);
/**
* @brief Serializes an integer.
*
* @param os Output stream.
* @param value Integer.
* @return The output stream.
*/
std::ostream& serialize(std::ostream& os, std::uint8_t value);
/**
* @brief Serializes an integer.
*
* @param os Output stream.
* @param value Integer.
* @return The output stream.
*/
std::ostream& serialize(std::ostream& os, std::uint16_t value);
/**
* @brief Serializes an integer.
*
* @param os Output stream.
* @param value Integer.
* @return The output stream.
*/
std::ostream& serialize(std::ostream& os, std::uint32_t value);
/**
* @brief Serializes an integer.
*
* @param os Output stream.
* @param value Integer.
* @return The output stream.
*/
std::ostream& serialize(std::ostream& os, std::uint64_t value);
/**
* @brief Serializes a string.
*
* @param os Output stream.
* @param value String.
* @return The output stream.
*/
std::ostream& serialize(std::ostream& os, const std::string& value);
} // namespace detail
+12
View File
@@ -0,0 +1,12 @@
#ifndef FURVM_HPP
#define FURVM_HPP
#include "furvm/context.hpp" // IWYU pragma: export
#include "furvm/executor.hpp" // IWYU pragma: export
#include "furvm/function.hpp" // IWYU pragma: export
#include "furvm/fwd.hpp" // IWYU pragma: export
#include "furvm/handle.hpp" // IWYU pragma: export
#include "furvm/instruction.hpp" // IWYU pragma: export
#include "furvm/thing.hpp" // IWYU pragma: export
#endif // FURVM_HPP
+13 -8
View File
@@ -5,6 +5,7 @@
#include "furvm/fwd.hpp"
#include <atomic>
#include <functional>
#include <tuple>
#include <type_traits>
#include <unordered_map>
@@ -24,20 +25,24 @@ public:
using id_type = Id;
using refcount_type = std::uint32_t;
public:
template <typename IdFwd>
refcount_header(IdFwd&& id, refcount_type refCount)
: m_id(std::forward<IdFwd>(id)), m_refCount(refCount) {}
template <typename IdFwd, typename Func>
refcount_header(IdFwd&& id, refcount_type refCount, Func&& onRelease)
: m_id(std::forward<IdFwd>(id)), m_refCount(refCount), m_onRelease(std::forward<Func>(onRelease)) {}
public:
refcount_type reference_count() const { return m_refCount; }
void acquire() { ++m_refCount; }
void release() { --m_refCount; }
void release() {
--m_refCount;
if (m_refCount == 0) m_onRelease(m_id);
}
public:
id_type id() const { return m_id; }
private:
id_type m_id;
std::atomic<refcount_type> m_refCount;
id_type m_id;
std::atomic<refcount_type> m_refCount;
std::function<void(const id_type&)> m_onRelease;
};
template <typename Id>
@@ -153,7 +158,7 @@ public:
id_type idFwd = std::forward<IdFwd>(id);
if (auto it = m_pairs.find(idFwd); it != m_pairs.end()) delete it->second;
auto pair = new pair_type(std::piecewise_construct,
std::forward_as_tuple(idFwd, 0),
std::forward_as_tuple(idFwd, 0, [&](const id_type& id) { erase(id); }),
std::forward_as_tuple(std::forward<Args>(args)...));
m_pairs.emplace(std::move(idFwd), pair);
return { pair };
@@ -209,7 +214,7 @@ public:
}
pair_type* newPair = new pair_type(std::piecewise_construct,
std::forward_as_tuple(id, 0),
std::forward_as_tuple(id, 0, [&](const id_type& id) { erase(id); }),
std::forward_as_tuple(std::forward<Args>(args)...));
m_pairs[id] = newPair;
-2
View File
@@ -7,6 +7,4 @@ namespace furvm {
context::context()
: m_thingAllocator(m_thingArena) {}
void context::collect() {}
} // namespace furvm
+1 -9
View File
@@ -1,12 +1,6 @@
#ifndef LIBFURVM
#include "furvm/context.hpp"
#include "furvm/executor.hpp"
#include "furvm/function.hpp"
#include "furvm/fwd.hpp"
#include "furvm/instruction.hpp"
#include "furvm/module.hpp"
#include "furvm/thing.hpp"
#include "furvm/furvm.hpp"
#include <array>
#include <cstddef>
@@ -35,10 +29,8 @@ int main(void) {
static constexpr std::size_t FPC = 3; // Frames per collection
std::size_t count = 0;
while ((executor->flags() & furvm::executor_flags::Done) != furvm::executor_flags::Done) {
executor->step();
if ((++count % FPC) == 0) context->collect();
}
return 0;