From d542bbe69697518b873afaec358339fb2c003eb3 Mon Sep 17 00:00:00 2001 From: Robert Bendun Date: Sat, 21 May 2022 23:12:50 +0200 Subject: [PATCH] Small refactoring --- src/environment.cc | 4 +--- src/musique.hh | 10 +++++++--- src/value.cc | 5 +++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/environment.cc b/src/environment.cc index a91454e..0e74f2b 100644 --- a/src/environment.cc +++ b/src/environment.cc @@ -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); diff --git a/src/musique.hh b/src/musique.hh index d96270f..e54a79b 100644 --- a/src/musique.hh +++ b/src/musique.hh @@ -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) { - 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::value()` + // also does not work, so this is probably the best way to express this: + return std::move(*static_cast(this)).value(); } } @@ -464,7 +468,7 @@ struct Value Value& operator=(Value &&) = default; template - requires (!std::is_same_v, Value>) + requires (!std::is_same_v, Value>) && std::invocable> && is_one_of>, Value, Result> 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; diff --git a/src/value.cc b/src/value.cc index 75eea17..6503f35 100644 --- a/src/value.cc +++ b/src/value.cc @@ -4,7 +4,7 @@ Result 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 Lambda::operator()(Interpreter &i, std::vector 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;