Added '&' operator that joins chords together.

For example 'c & g' makes chord 'chord c g'
This commit is contained in:
Robert Bendun 2022-08-18 21:08:30 +02:00
parent 881dc8975a
commit e090778db9
2 changed files with 28 additions and 1 deletions

View File

@ -730,6 +730,33 @@ error:
assert(args.back().type == Value::Type::Number, "Only numbers can be used for indexing"); // TODO(assert) assert(args.back().type == Value::Type::Number, "Only numbers can be used for indexing"); // TODO(assert)
return std::move(args.front()).index(i, std::move(args.back()).n.as_int()); return std::move(args.front()).index(i, std::move(args.back()).n.as_int());
}; };
operators["&"] = +[](Interpreter &i, std::vector<Value> args) -> Result<Value> {
using Chord_Chord = Shape<Value::Type::Music, Value::Type::Music>;
if (Chord_Chord::typecheck(args)) {
auto [lhs, rhs] = Chord_Chord::move_from(args);
auto &l = lhs.notes;
auto &r = rhs.notes;
// Append one set of notes to another to make bigger chord!
l.reserve(l.size() + r.size());
std::move(r.begin(), r.end(), std::back_inserter(l));
return Value::from(lhs);
}
return Error {
.details = errors::Unsupported_Types_For {
.type = errors::Unsupported_Types_For::Operator,
.name = "&",
.possibilities = {
"(music, music) -> music",
}
},
.location = {}
};
};
} }
} }

View File

@ -502,7 +502,7 @@ static usize precedense(std::string_view op)
if (one_of(op, "and")) return 150; if (one_of(op, "and")) return 150;
if (one_of(op, "<", ">", "<=", ">=", "==", "!=")) return 200; if (one_of(op, "<", ">", "<=", ">=", "==", "!=")) return 200;
if (one_of(op, "+", "-")) return 300; if (one_of(op, "+", "-")) return 300;
if (one_of(op, "*", "/")) return 400; if (one_of(op, "*", "/", "&")) return 400;
unreachable(); unreachable();
} }