New environment implementation
It's a failure of locating precise source of the bug that would cause `var x = [i|i] 0` to segfault. New implementation DOES NOT have this bug.
This commit is contained in:
parent
c51722a607
commit
c55650e12b
6
Makefile
6
Makefile
@ -19,7 +19,7 @@ Obj= \
|
||||
Release_Obj=$(addprefix bin/,$(Obj))
|
||||
Debug_Obj=$(addprefix bin/debug/,$(Obj))
|
||||
|
||||
all: bin/musique bin/unit-tests
|
||||
all: bin/musique
|
||||
|
||||
debug: bin/debug/musique
|
||||
|
||||
@ -60,8 +60,8 @@ doc: Doxyfile src/*.cc src/*.hh
|
||||
doc-open: doc
|
||||
xdg-open ./doc/build/html/index.html
|
||||
|
||||
bin/unit-tests: src/tests/*.cc $(Release_Obj)
|
||||
g++ $(CXXFLAGS) $(CPPFLAGS) -o $@ $^
|
||||
bin/unit-tests: src/tests/*.cc $(Debug_Obj)
|
||||
g++ $(CXXFLAGS) $(CPPFLAGS) $(DEBUG_FLAGS) -o $@ $^
|
||||
|
||||
clean:
|
||||
rm -rf bin coverage
|
||||
|
@ -2,7 +2,14 @@
|
||||
|
||||
#include <iostream>
|
||||
|
||||
std::vector<Env> *Env::pool = nullptr;
|
||||
std::shared_ptr<Env> Env::global = nullptr;
|
||||
|
||||
std::shared_ptr<Env> Env::make()
|
||||
{
|
||||
auto new_env = new Env();
|
||||
assert(new_env, "Cannot construct new env");
|
||||
return std::shared_ptr<Env>(new_env);
|
||||
}
|
||||
|
||||
Env& Env::force_define(std::string name, Value new_value)
|
||||
{
|
||||
@ -10,14 +17,9 @@ Env& Env::force_define(std::string name, Value new_value)
|
||||
return *this;
|
||||
}
|
||||
|
||||
Env& Env::parent()
|
||||
{
|
||||
return (*pool)[parent_enviroment_id];
|
||||
}
|
||||
|
||||
Value* Env::find(std::string const& name)
|
||||
{
|
||||
for (Env *prev = nullptr, *env = this; env != prev; prev = std::exchange(env, &env->parent())) {
|
||||
for (Env *env = this; env; env = env->parent.get()) {
|
||||
if (auto it = env->variables.find(name); it != env->variables.end()) {
|
||||
return &it->second;
|
||||
}
|
||||
@ -25,29 +27,14 @@ Value* Env::find(std::string const& name)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
usize Env::operator++() const
|
||||
std::shared_ptr<Env> Env::enter()
|
||||
{
|
||||
std::cerr << "ENTER SCOPE" << std::endl;
|
||||
auto const parent_id = this - pool->data();
|
||||
auto const free = std::find_if(pool->begin(), pool->end(), [](Env const& env) { return env.parent_enviroment_id == Env::Unused; });
|
||||
Env* next = free == pool->end()
|
||||
? &pool->emplace_back()
|
||||
: &*free;
|
||||
|
||||
next->parent_enviroment_id = parent_id;
|
||||
return next - pool->data();
|
||||
auto next = make();
|
||||
next->parent = shared_from_this();
|
||||
return next;
|
||||
}
|
||||
|
||||
usize Env::operator--()
|
||||
std::shared_ptr<Env> Env::leave()
|
||||
{
|
||||
std::cerr << "LEAVE SCOPE" << std::endl;
|
||||
if (this == pool->data())
|
||||
return 0;
|
||||
variables.clear();
|
||||
return std::exchange(parent_enviroment_id, Unused);
|
||||
}
|
||||
|
||||
Env& Env::global()
|
||||
{
|
||||
return (*pool)[0];
|
||||
return parent;
|
||||
}
|
||||
|
@ -27,17 +27,16 @@ Interpreter::Interpreter()
|
||||
|
||||
Interpreter::~Interpreter()
|
||||
{
|
||||
Env::pool = nullptr;
|
||||
Env::global.reset();
|
||||
}
|
||||
|
||||
Interpreter::Interpreter(std::ostream& out)
|
||||
: out(out)
|
||||
{
|
||||
assert(Env::pool == nullptr, "Only one instance of interpreter can be at one time");
|
||||
Env::pool = &env_pool;
|
||||
assert(!bool(Env::global), "Only one instance of interpreter can be at one time");
|
||||
|
||||
auto &global = env_pool.emplace_back();
|
||||
global.parent_enviroment_id = 0;
|
||||
env = Env::global = Env::make();
|
||||
auto &global = *Env::global;
|
||||
|
||||
global.force_define("typeof", [](Interpreter&, std::vector<Value> args) -> Value {
|
||||
assert(args.size() == 1, "typeof expects only one argument");
|
||||
@ -78,16 +77,6 @@ Interpreter::Interpreter(std::ostream& out)
|
||||
operators["!="] = binary_operator(std::not_equal_to<>{});
|
||||
}
|
||||
|
||||
Env& Interpreter::env()
|
||||
{
|
||||
return env_pool[current_env];
|
||||
}
|
||||
|
||||
Env const& Interpreter::env() const
|
||||
{
|
||||
return env_pool[current_env];
|
||||
}
|
||||
|
||||
Result<Value> Interpreter::eval(Ast &&ast)
|
||||
{
|
||||
switch (ast.type) {
|
||||
@ -95,7 +84,7 @@ Result<Value> Interpreter::eval(Ast &&ast)
|
||||
switch (ast.token.type) {
|
||||
case Token::Type::Symbol:
|
||||
if (ast.token.source != "nil") {
|
||||
auto const value = env().find(std::string(ast.token.source));
|
||||
auto const value = env->find(std::string(ast.token.source));
|
||||
assert(value, "Missing variable error is not implemented yet: variable: "s + std::string(ast.token.source));
|
||||
return *value;
|
||||
}
|
||||
@ -149,7 +138,7 @@ Result<Value> Interpreter::eval(Ast &&ast)
|
||||
assert(ast.arguments.size() == 2, "Only simple assigments are supported now");
|
||||
assert(ast.arguments.front().type == Ast::Type::Literal, "Only names are supported as LHS arguments now");
|
||||
assert(ast.arguments.front().token.type == Token::Type::Symbol, "Only names are supported as LHS arguments now");
|
||||
env().force_define(std::string(ast.arguments.front().token.source), Try(eval(std::move(ast.arguments.back()))));
|
||||
env->force_define(std::string(ast.arguments.front().token.source), Try(eval(std::move(ast.arguments.back()))));
|
||||
return Value{};
|
||||
}
|
||||
|
||||
@ -170,3 +159,13 @@ Result<Value> Interpreter::eval(Ast &&ast)
|
||||
unimplemented();
|
||||
}
|
||||
}
|
||||
void Interpreter::enter_scope()
|
||||
{
|
||||
env = env->enter();
|
||||
}
|
||||
|
||||
void Interpreter::leave_scope()
|
||||
{
|
||||
assert(env != Env::global, "Cannot leave global scope");
|
||||
env = env->leave();
|
||||
}
|
||||
|
@ -3,6 +3,7 @@
|
||||
#include <concepts>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
#include <span>
|
||||
@ -501,37 +502,38 @@ std::string_view type_name(Value::Type t);
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, Value const& v);
|
||||
|
||||
struct Env
|
||||
struct Env : std::enable_shared_from_this<Env>
|
||||
{
|
||||
static std::vector<Env> *pool;
|
||||
std::unordered_map<std::string, Value> variables;
|
||||
usize parent_enviroment_id;
|
||||
// Constructor of Env class
|
||||
static std::shared_ptr<Env> make();
|
||||
|
||||
static std::shared_ptr<Env> global;
|
||||
std::unordered_map<std::string, Value> variables;
|
||||
std::shared_ptr<Env> parent;
|
||||
|
||||
Env() = default;
|
||||
Env(Env const&) = delete;
|
||||
Env(Env &&) = default;
|
||||
Env& operator=(Env const&) = delete;
|
||||
Env& operator=(Env &&) = default;
|
||||
|
||||
static Env& global();
|
||||
|
||||
/// Defines new variable regardless of it's current existance
|
||||
Env& force_define(std::string name, Value new_value);
|
||||
Env& parent();
|
||||
Value* find(std::string const& name);
|
||||
|
||||
usize operator++() const;
|
||||
usize operator--();
|
||||
// Scope menagment
|
||||
std::shared_ptr<Env> enter();
|
||||
std::shared_ptr<Env> leave();
|
||||
|
||||
static constexpr decltype(Env::parent_enviroment_id) Unused = -1;
|
||||
private:
|
||||
// Ensure that all values of this class are behind shared_ptr
|
||||
Env() = default;
|
||||
};
|
||||
|
||||
struct Interpreter
|
||||
{
|
||||
std::ostream &out;
|
||||
std::unordered_map<std::string, Function> operators;
|
||||
std::vector<Env> env_pool;
|
||||
usize current_env = 0;
|
||||
std::shared_ptr<Env> env;
|
||||
|
||||
Interpreter();
|
||||
~Interpreter();
|
||||
@ -539,10 +541,11 @@ struct Interpreter
|
||||
Interpreter(Interpreter const&) = delete;
|
||||
Interpreter(Interpreter &&) = default;
|
||||
|
||||
Env& env();
|
||||
Env const& env() const;
|
||||
|
||||
Result<Value> eval(Ast &&ast);
|
||||
|
||||
// Scope managment
|
||||
void enter_scope();
|
||||
void leave_scope();
|
||||
};
|
||||
|
||||
namespace errors
|
||||
|
@ -1,3 +1,4 @@
|
||||
#if 0
|
||||
#include <boost/ut.hpp>
|
||||
#include <musique.hh>
|
||||
|
||||
@ -78,3 +79,4 @@ suite environment_test = [] {
|
||||
};
|
||||
};
|
||||
};
|
||||
#endif
|
||||
|
@ -68,7 +68,7 @@ suite intepreter_test = [] {
|
||||
expect(eq(result.error().type, errors::Not_Callable));
|
||||
}
|
||||
{
|
||||
i.env().force_define("call_me", Value::number(Number(10)));
|
||||
i.env->force_define("call_me", Value::number(Number(10)));
|
||||
auto result = Parser::parse("call_me 20", "test").and_then([&](Ast &&ast) { return i.eval(std::move(ast)); });
|
||||
expect(!result.has_value()) << "Expected code to have failed";
|
||||
expect(eq(result.error().type, errors::Not_Callable));
|
||||
|
@ -129,16 +129,16 @@ std::string_view type_name(Value::Type t)
|
||||
|
||||
Result<Value> Lambda::operator()(Interpreter &i, std::vector<Value> arguments)
|
||||
{
|
||||
i.current_env = ++i.env();
|
||||
// TODO Add some kind of scope guard
|
||||
i.enter_scope();
|
||||
assert(parameters.size() == arguments.size(), "wrong number of arguments");
|
||||
|
||||
auto &env = i.env();
|
||||
for (usize j = 0; j < parameters.size(); ++j) {
|
||||
env.force_define(parameters[j], std::move(arguments[j]));
|
||||
i.env->force_define(parameters[j], std::move(arguments[j]));
|
||||
}
|
||||
|
||||
auto result = i.eval((Ast)body);
|
||||
|
||||
i.current_env = --i.env();
|
||||
i.leave_scope();
|
||||
return result;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user