In-language comparison operators made holistic

This commit is contained in:
Robert Bendun 2022-06-06 00:20:25 +02:00
parent 7c5f3e2170
commit e528fd9e83
2 changed files with 20 additions and 14 deletions

View File

@ -161,20 +161,8 @@ static Result<Value> equality_operator(Interpreter&, std::vector<Value> args)
template<typename Binary_Predicate> template<typename Binary_Predicate>
static Result<Value> comparison_operator(Interpreter&, std::vector<Value> args) static Result<Value> comparison_operator(Interpreter&, std::vector<Value> args)
{ {
using NN = Shape<Value::Type::Number, Value::Type::Number>; assert(args.size() == 2, "Operator handler cannot accept any shape different then 2 arguments");
using BB = Shape<Value::Type::Bool, Value::Type::Bool>; return Value::from(Binary_Predicate{}(args.front(), args.back()));
if (NN::typecheck(args)) {
auto [a, b] = NN::move_from(args);
return Value::from(Binary_Predicate{}(std::move(a), std::move(b)));
}
if (BB::typecheck(args)) {
auto [a, b] = BB::move_from(args);
return Value::from(Binary_Predicate{}(a, b));
}
unreachable();
} }
/// Registers constants like `fn = full note = 1/1` /// Registers constants like `fn = full note = 1/1`

View File

@ -102,6 +102,24 @@ suite intepreter_test = [] {
evaluates_to(Value::from(true), "0 <= 0"); evaluates_to(Value::from(true), "0 <= 0");
evaluates_to(Value::from(true), "1 < 2"); evaluates_to(Value::from(true), "1 < 2");
evaluates_to(Value::from(false), "1 > 2"); evaluates_to(Value::from(false), "1 > 2");
evaluates_to(Value::from(true), "c == c");
evaluates_to(Value::from(false), "c != c");
evaluates_to(Value::from(true), "c < d");
evaluates_to(Value::from(true), "c != (c 4)");
evaluates_to(Value::from(true), "(c 4) == (c 4)");
evaluates_to(Value::from(true), "(c 3) != (c 4)");
evaluates_to(Value::from(true), "(c 3) < (c 4)");
evaluates_to(Value::from(true), "((c+12) 3) == (c 4)");
// Value is partially ordered, ensure that different types
// are always not equal and not ordered
evaluates_to(Value::from(false), "0 < c");
evaluates_to(Value::from(false), "0 > c");
evaluates_to(Value::from(false), "0 <= c");
evaluates_to(Value::from(false), "0 >= c");
evaluates_to(Value::from(true), "0 != c");
}; };
// Added to explicitly test against bug that was in old implementation of enviroments. // Added to explicitly test against bug that was in old implementation of enviroments.