Symbol quoting and user defined incoming MIDI message processing

This commit is contained in:
Robert Bendun 2022-06-19 15:45:14 +02:00
parent 387835a155
commit ed3b2006e9
2 changed files with 28 additions and 2 deletions

View File

@ -609,6 +609,23 @@ error:
unreachable();
});
global.force_define("incoming", +[](Interpreter &i, std::vector<Value> args) -> Result<Value> {
assert(args.size() == 2, "Wrong arity of 'incoming' function");
assert(args[0].type == Value::Type::Symbol, "Expected symbol for first argument of 'incoming'");
assert(is_callable(args[1].type), "Expected function for second argument of 'incoming'");
std::string const& symbol = args[0].s;
if (symbol == "note_on" || symbol == "noteon") {
i.callbacks->note_on = std::move(args[1]);
} else if (symbol == "note_off" || symbol == "noteoff") {
i.callbacks->note_off = std::move(args[1]);
} else {
assert(false, "symbol not recognized for 'incoming'");
}
return Value{};
});
{
constexpr auto pgmchange = +[](Interpreter &i, std::vector<Value> args) -> Result<Value> {
using Program = Shape<Value::Type::Number>;
@ -668,6 +685,10 @@ Result<Value> Interpreter::eval(Ast &&ast)
switch (ast.token.type) {
case Token::Type::Symbol:
{
if (ast.token.source.starts_with('\'')) {
return Value::from(std::move(ast.token.source).substr(1));
}
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;

View File

@ -100,6 +100,11 @@ bool unicode::is_space(u32 space)
bool unicode::is_identifier(u32 letter, unicode::First_Character is_first_character)
{
return (unicode::is_letter(letter) || letter == '_' || letter == '#' || letter == '$' || letter == '@')
|| (!bool(is_first_character) && (letter == '\'' || unicode::is_digit(letter)));
return (unicode::is_letter(letter)
|| letter == '\''
|| letter == '_'
|| letter == '#'
|| letter == '$'
|| letter == '@')
|| (!bool(is_first_character) && unicode::is_digit(letter));
}