Skip to content

Commit

Permalink
feat: Use custom allocator instead of smart pointers (#100)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrzlgnm authored Dec 13, 2024
1 parent 17f73f7 commit 580522c
Show file tree
Hide file tree
Showing 45 changed files with 882 additions and 824 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
matrix:
toolchain:
- {
runs_on: macos-12,
runs_on: macos-13,
name: "macOS Clang",
CC: clang,
CXX: clang++,
Expand Down
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ target_sources(monkey_lib
source/ast/statements.cpp
source/ast/string_literal.cpp
source/ast/unary_expression.cpp
source/chungus.hpp
source/code/code.cpp
source/compiler/ast_compile.cpp
source/compiler/compiler.cpp
Expand Down
4 changes: 2 additions & 2 deletions source/ast/array_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
struct array_expression : expression
{
[[nodiscard]] auto string() const -> std::string override;
[[nodiscard]] auto eval(environment_ptr env) const -> object_ptr override;
[[nodiscard]] auto eval(environment* env) const -> const object* override;
auto compile(compiler& comp) const -> void override;

std::vector<expression_ptr> elements;
std::vector<const expression*> elements;
};
4 changes: 2 additions & 2 deletions source/ast/binary_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
struct binary_expression : unary_expression
{
[[nodiscard]] auto string() const -> std::string override;
[[nodiscard]] auto eval(environment_ptr env) const -> object_ptr override;
[[nodiscard]] auto eval(environment* env) const -> const object* override;
auto compile(compiler& comp) const -> void override;

expression_ptr left {};
expression* left {};
};
2 changes: 1 addition & 1 deletion source/ast/boolean.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ struct boolean : expression
{
explicit boolean(bool val);
[[nodiscard]] auto string() const -> std::string override;
[[nodiscard]] auto eval(environment_ptr env) const -> object_ptr override;
[[nodiscard]] auto eval(environment* env) const -> const object* override;
auto compile(compiler& comp) const -> void override;

bool value {};
Expand Down
6 changes: 3 additions & 3 deletions source/ast/builtin_function_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include "eval/object.hpp"

builtin_function_expression::builtin_function_expression(
std::string&& name,
std::vector<std::string>&& params,
std::function<object_ptr(array_object::array&& arguments)>&& bod)
std::string name,
std::vector<std::string> params,
std::function<const object*(array_object::array&& arguments)> bod)
: callable_expression {std::move(params)}
, name {std::move(name)}
, body {std::move(bod)}
Expand Down
16 changes: 8 additions & 8 deletions source/ast/builtin_function_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@

struct builtin_function_expression : callable_expression
{
builtin_function_expression(std::string&& name,
std::vector<std::string>&& params,
std::function<object_ptr(std::vector<object_ptr>&& arguments)>&& bod);
builtin_function_expression(std::string name,
std::vector<std::string> params,
std::function<const object*(std::vector<const object*>&& arguments)> bod);

[[nodiscard]] auto call(environment_ptr closure_env,
environment_ptr caller_env,
const std::vector<expression_ptr>& arguments) const -> object_ptr override;
[[nodiscard]] auto call(environment* closure_env,
environment* caller_env,
const std::vector<const expression*>& arguments) const -> const object* override;
[[nodiscard]] auto string() const -> std::string override;
auto compile(compiler& comp) const -> void override;

static const std::vector<builtin_function_expression> builtins;
static const std::vector<const builtin_function_expression*> builtins;

std::string name;
std::function<object_ptr(array_object::array&& arguments)> body;
std::function<const object*(array_object::array&& arguments)> body;
};
6 changes: 3 additions & 3 deletions source/ast/call_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
struct call_expression : expression
{
[[nodiscard]] auto string() const -> std::string override;
[[nodiscard]] auto eval(environment_ptr env) const -> object_ptr override;
[[nodiscard]] auto eval(environment* env) const -> const object* override;
auto compile(compiler& comp) const -> void override;

expression_ptr function {};
std::vector<expression_ptr> arguments;
expression* function {};
std::vector<const expression*> arguments;
};
2 changes: 1 addition & 1 deletion source/ast/callable_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <fmt/format.h>

callable_expression::callable_expression(std::vector<std::string>&& params)
callable_expression::callable_expression(std::vector<std::string> params)
: parameters {std::move(params)}
{
}
Expand Down
15 changes: 5 additions & 10 deletions source/ast/callable_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,13 @@

struct callable_expression : expression
{
callable_expression(callable_expression&&) = delete;
auto operator=(callable_expression&&) -> callable_expression& = default;
callable_expression(const callable_expression&) = default;
auto operator=(const callable_expression&) -> callable_expression& = default;
explicit callable_expression(std::vector<std::string>&& params);
~callable_expression() override = default;
explicit callable_expression(std::vector<std::string> params);

[[nodiscard]] auto eval(environment_ptr env) const -> object_ptr override;
[[nodiscard]] auto eval(environment* env) const -> const object* override;
[[nodiscard]] auto string() const -> std::string override;
[[nodiscard]] virtual auto call(environment_ptr closure_env,
environment_ptr caller_env,
const std::vector<expression_ptr>& arguments) const -> object_ptr = 0;
[[nodiscard]] virtual auto call(environment* closure_env,
environment* caller_env,
const std::vector<const expression*>& arguments) const -> const object* = 0;

std::vector<std::string> parameters;
};
15 changes: 5 additions & 10 deletions source/ast/expression.hpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
#pragma once

#include <memory>

#include <eval/environment_fwd.hpp>
#include <eval/object.hpp>

struct compiler;

struct expression
{
expression() = default;
expression(const expression&) = default;
expression(expression&&) = default;
auto operator=(const expression&) -> expression& = default;
auto operator=(expression&&) -> expression& = default;
virtual ~expression() = default;
expression(const expression&) = delete;
expression(expression&&) = delete;
auto operator=(const expression&) -> expression& = delete;
auto operator=(expression&&) -> expression& = delete;

[[nodiscard]] virtual auto string() const -> std::string = 0;
[[nodiscard]] virtual auto eval(environment_ptr) const -> object_ptr = 0;
[[nodiscard]] virtual auto eval(environment*) const -> const object* = 0;
virtual inline auto compile(compiler& /*comp*/) const -> void = 0;
};

using expression_ptr = std::unique_ptr<expression>;
4 changes: 2 additions & 2 deletions source/ast/function_expression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

#include <fmt/format.h>

function_expression::function_expression(std::vector<std::string>&& parameters, statement_ptr&& body)
function_expression::function_expression(std::vector<std::string>&& parameters, statement* body)
: callable_expression(std::move(parameters))
, body(std::move(body))
, body {body}
{
}

Expand Down
12 changes: 6 additions & 6 deletions source/ast/function_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@

struct function_expression : callable_expression
{
function_expression(std::vector<std::string>&& parameters, statement_ptr&& body);
function_expression(std::vector<std::string>&& parameters, statement* body);

[[nodiscard]] auto string() const -> std::string override;
[[nodiscard]] auto call(environment_ptr closure_env,
environment_ptr caller_env,
const std::vector<expression_ptr>& arguments) const -> object_ptr override;
[[nodiscard]] auto call(environment* closure_env,
environment* caller_env,
const std::vector<const expression*>& arguments) const -> const object* override;
auto compile(compiler& comp) const -> void override;

statement_ptr body;
std::string name;
const statement* body {};
mutable std::string name;
};
4 changes: 2 additions & 2 deletions source/ast/hash_literal_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
struct hash_literal_expression : expression
{
[[nodiscard]] auto string() const -> std::string override;
[[nodiscard]] auto eval(environment_ptr env) const -> object_ptr override;
[[nodiscard]] auto eval(environment* env) const -> const object* override;
auto compile(compiler& comp) const -> void override;

std::vector<std::pair<expression_ptr, expression_ptr>> pairs;
std::vector<std::pair<expression*, expression*>> pairs;
};
4 changes: 1 addition & 3 deletions source/ast/identifier.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ struct identifier : expression
{
explicit identifier(std::string val);
[[nodiscard]] auto string() const -> std::string override;
[[nodiscard]] auto eval(environment_ptr env) const -> object_ptr override;
[[nodiscard]] auto eval(environment* env) const -> const object* override;
auto compile(compiler& comp) const -> void override;

std::string value;
};

using identifier_ptr = std::unique_ptr<identifier>;
8 changes: 4 additions & 4 deletions source/ast/if_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
struct if_expression : expression
{
[[nodiscard]] auto string() const -> std::string override;
[[nodiscard]] auto eval(environment_ptr env) const -> object_ptr override;
[[nodiscard]] auto eval(environment* env) const -> const object* override;
auto compile(compiler& comp) const -> void override;

expression_ptr condition {};
block_statement_ptr consequence {};
block_statement_ptr alternative {};
expression* condition {};
block_statement* consequence {};
block_statement* alternative {};
};
6 changes: 3 additions & 3 deletions source/ast/index_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
struct index_expression : expression
{
[[nodiscard]] auto string() const -> std::string override;
[[nodiscard]] auto eval(environment_ptr env) const -> object_ptr override;
[[nodiscard]] auto eval(environment* env) const -> const object* override;
auto compile(compiler& comp) const -> void override;

expression_ptr left;
expression_ptr index;
expression* left {};
expression* index {};
};
2 changes: 1 addition & 1 deletion source/ast/integer_literal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
struct integer_literal : expression
{
[[nodiscard]] auto string() const -> std::string override;
[[nodiscard]] auto eval(environment_ptr env) const -> object_ptr override;
[[nodiscard]] auto eval(environment* env) const -> object* override;
auto compile(compiler& comp) const -> void override;

int64_t value {};
Expand Down
6 changes: 2 additions & 4 deletions source/ast/program.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@
struct program : expression
{
[[nodiscard]] auto string() const -> std::string override;
[[nodiscard]] auto eval(environment_ptr env) const -> object_ptr override;
[[nodiscard]] auto eval(environment* env) const -> const object* override;
auto compile(compiler& comp) const -> void override;

std::vector<statement_ptr> statements {};
std::vector<const statement*> statements;
};

using program_ptr = std::unique_ptr<program>;
10 changes: 3 additions & 7 deletions source/ast/statements.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
#include <algorithm>
#include <iterator>
#include <vector>

#include "statements.hpp"

#include <fmt/format.h>
Expand All @@ -10,17 +6,17 @@

auto let_statement::string() const -> std::string
{
return fmt::format("let {} = {};", name->string(), value ? value->string() : std::string());
return fmt::format("let {} = {};", name->string(), (value != nullptr) ? value->string() : std::string());
}

auto return_statement::string() const -> std::string
{
return fmt::format("return {};", value ? value->string() : std::string());
return fmt::format("return {};", (value != nullptr) ? value->string() : std::string());
}

auto expression_statement::string() const -> std::string
{
if (expr) {
if (expr != nullptr) {
return expr->string();
}
return {};
Expand Down
21 changes: 9 additions & 12 deletions source/ast/statements.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,40 @@
#include "identifier.hpp"

using statement = expression;
using statement_ptr = expression_ptr;

struct let_statement : statement
{
[[nodiscard]] auto string() const -> std::string override;
[[nodiscard]] auto eval(environment_ptr env) const -> object_ptr override;
[[nodiscard]] auto eval(environment* env) const -> const object* override;
auto compile(compiler& comp) const -> void override;

identifier_ptr name {};
expression_ptr value {};
const identifier* name {};
const expression* value {};
};

struct return_statement : statement
{
[[nodiscard]] auto string() const -> std::string override;
[[nodiscard]] auto eval(environment_ptr env) const -> object_ptr override;
[[nodiscard]] auto eval(environment* env) const -> const object* override;
auto compile(compiler& comp) const -> void override;

expression_ptr value {};
const expression* value {};
};

struct expression_statement : statement
{
[[nodiscard]] auto string() const -> std::string override;
[[nodiscard]] auto eval(environment_ptr env) const -> object_ptr override;
[[nodiscard]] auto eval(environment* env) const -> const object* override;
auto compile(compiler& comp) const -> void override;

expression_ptr expr {};
const expression* expr {};
};

struct block_statement : statement
{
[[nodiscard]] auto string() const -> std::string override;
[[nodiscard]] auto eval(environment_ptr env) const -> object_ptr override;
[[nodiscard]] auto eval(environment* env) const -> const object* override;
auto compile(compiler& comp) const -> void override;

std::vector<statement_ptr> statements {};
std::vector<const statement*> statements;
};

using block_statement_ptr = std::unique_ptr<block_statement>;
2 changes: 1 addition & 1 deletion source/ast/string_literal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ struct string_literal : identifier
{
using identifier::identifier;
[[nodiscard]] auto string() const -> std::string override;
[[nodiscard]] auto eval(environment_ptr env) const -> object_ptr override;
[[nodiscard]] auto eval(environment* env) const -> const object* override;
auto compile(compiler& comp) const -> void override;
};
4 changes: 2 additions & 2 deletions source/ast/unary_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
struct unary_expression : expression
{
[[nodiscard]] auto string() const -> std::string override;
[[nodiscard]] auto eval(environment_ptr env) const -> object_ptr override;
[[nodiscard]] auto eval(environment* env) const -> const object* override;
auto compile(compiler& comp) const -> void override;

token_type op {};
expression_ptr right {};
expression* right {};
};
2 changes: 1 addition & 1 deletion source/ast/util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#include <fmt/format.h>

template<typename Expression>
auto join(const std::vector<std::unique_ptr<Expression>>& nodes, std::string_view sep = {}) -> std::string
auto join(const std::vector<Expression*>& nodes, std::string_view sep = {}) -> std::string
{
auto strs = std::vector<std::string>();
std::transform(
Expand Down
Loading

0 comments on commit 580522c

Please sign in to comment.