Small refactoring

This commit is contained in:
Robert Bendun 2022-05-21 23:12:50 +02:00
parent c51722a607
commit d542bbe696
3 changed files with 11 additions and 8 deletions

View File

@ -27,7 +27,6 @@ Value* Env::find(std::string const& name)
usize Env::operator++() const
{
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()
@ -40,8 +39,7 @@ usize Env::operator++() const
usize Env::operator--()
{
std::cerr << "LEAVE SCOPE" << std::endl;
if (this == pool->data())
if (this == &Env::global())
return 0;
variables.clear();
return std::exchange(parent_enviroment_id, Unused);

View File

@ -159,7 +159,11 @@ struct [[nodiscard("This value may contain critical error, so it should NOT be i
inline auto value() &&
{
if constexpr (not std::is_void_v<T>) {
return Storage::value();
// NOTE This line in ideal world should be `return Storage::value()`
// but C++ does not infer that this is rvalue context.
// `std::add_rvalue_reference_t<Storage>::value()`
// also does not work, so this is probably the best way to express this:
return std::move(*static_cast<Storage*>(this)).value();
}
}
@ -464,7 +468,7 @@ struct Value
Value& operator=(Value &&) = default;
template<typename Callable>
requires (!std::is_same_v<std::remove_cvref_t<Callable>, Value>)
requires (!std::is_same_v<std::decay_t<Callable>, Value>)
&& std::invocable<Callable, Interpreter&, std::vector<Value>>
&& is_one_of<std::invoke_result_t<Callable, Interpreter&, std::vector<Value>>, Value, Result<Value>>
inline Value(Callable &&callable)
@ -535,7 +539,7 @@ struct Interpreter
Interpreter();
~Interpreter();
Interpreter(std::ostream& out);
explicit Interpreter(std::ostream& out);
Interpreter(Interpreter const&) = delete;
Interpreter(Interpreter &&) = default;

View File

@ -4,7 +4,7 @@ Result<Value> Value::from(Token t)
{
switch (t.type) {
case Token::Type::Numeric:
return Number::from(std::move(t)).map(Value::number);
return Value::number(Try(Number::from(std::move(t))));
case Token::Type::Symbol:
if (t.source == "true")
@ -137,7 +137,8 @@ Result<Value> Lambda::operator()(Interpreter &i, std::vector<Value> arguments)
env.force_define(parameters[j], std::move(arguments[j]));
}
auto result = i.eval((Ast)body);
Ast body_copy = body;
auto result = i.eval(std::move(body_copy));
i.current_env = --i.env();
return result;