Compare commits

...

3 Commits

Author SHA1 Message Date
db932b94a9 Context aware clojures
Moved from dynamic scoping to static scoping.
2022-05-22 00:34:10 +02:00
da6d57a280 Explicit test for bug that caused all this trouble 2022-05-22 00:05:05 +02:00
c55650e12b 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.
2022-05-21 23:56:13 +02:00
8 changed files with 110 additions and 70 deletions

View File

@ -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

38
examples/church.mq Normal file
View File

@ -0,0 +1,38 @@
-- PAIR definition
var pair = [x y | [z | z x y]];
var car = [p | p [x y | x]];
var cdr = [p | p [x y | y]];
var x = pair 100 200;
say (car x);
say (cdr x);
-- This two values should be builtin, but currently is not
var true = (1 == 1);
var false = (1 != 1);
-- LIST definition
var null = pair true true;
var is_empty = car;
var head = [list | car (cdr list)];
var tail = [list | cdr (cdr list)];
var cons = [head tail | pair false (pair head tail)];
var for_each = [list iterator |
if (is_empty list) [| nil ] [|
iterator (head list);
for_each (tail list) iterator ]];
var map = [list iterator |
if (is_empty list) [| null ] [|
cons (iterator (head list)) (map (tail list) iterator) ]];
var foldr = [list init folder |
if (is_empty list)
[| init ]
[| foldr (tail list) (folder (head list) init) folder ]];
var range = [start stop | if (start >= stop) [|cons start null] [|cons start (range (start+1) stop)]];
var xs = range 1 5;
say (foldr xs 1 [x y|x*y]);

View File

@ -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;
}

View File

@ -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{};
}
@ -162,6 +151,8 @@ Result<Value> Interpreter::eval(Ast &&ast)
assert(param.type == Ast::Type::Literal && param.token.type == Token::Type::Symbol, "Not a name in parameter section of Ast::lambda");
lambda.parameters.push_back(std::string(std::move(param).token.source));
}
lambda.context = env;
lambda.body = std::move(ast.arguments.back());
return Value::lambda(std::move(lambda));
}
@ -170,3 +161,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();
}

View File

@ -3,6 +3,7 @@
#include <concepts>
#include <cstdint>
#include <cstring>
#include <memory>
#include <optional>
#include <ostream>
#include <span>
@ -422,8 +423,9 @@ struct Number
std::ostream& operator<<(std::ostream& os, Number const& num);
struct Value;
struct Env;
struct Interpreter;
struct Value;
using Function = std::function<Result<Value>(Interpreter &i, std::vector<Value>)>;
@ -432,6 +434,7 @@ struct Lambda
Location location;
std::vector<std::string> parameters;
Ast body;
std::shared_ptr<Env> context;
Result<Value> operator()(Interpreter &i, std::vector<Value> params);
};
@ -501,37 +504,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 +543,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

View File

@ -1,3 +1,4 @@
#if 0
#include <boost/ut.hpp>
#include <musique.hh>
@ -78,3 +79,4 @@ suite environment_test = [] {
};
};
};
#endif

View File

@ -68,11 +68,17 @@ 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));
}
};
// Added to explicitly test against bug that was in old implementation of enviroments.
// Previously this test would segfault
should("allow assigning result of function calls to a variable") = [] {
evaluates_to(Value::number(Number(42)), "var x = [i|i] 42; x");
};
};
};

View File

@ -129,16 +129,17 @@ std::string_view type_name(Value::Type t)
Result<Value> Lambda::operator()(Interpreter &i, std::vector<Value> arguments)
{
i.current_env = ++i.env();
auto old_scope = std::exchange(i.env, context);
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.env = old_scope;
return result;
}