negative numbers; fixed duration builtin

This commit is contained in:
Robert Bendun 2023-01-10 06:41:08 +01:00
parent 0b8fd86680
commit 87cea02b45
12 changed files with 52 additions and 27 deletions

View File

@ -9,14 +9,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Printing version number on non-quiet launch, or when provided `--version` or `:version`
- Builtin function documentation generation from C++ Musique implementation source code
- New builtins: digits
- Negative numbers!
### Removed
- Release builder, since it's separate part of the project
### Fixed
- `ceil`, `round`, `floor` didn't behave well with negative numbers
- `duration` wasn't filling note length from context and summed all notes inside chord, when it should take max
- `try` evaluated arguments too quickly
## [0.3.1]
### Fixed

View File

@ -74,7 +74,7 @@ r add 1, 3
n Wywołanie funkcji
n nie przymujących argumentów
m constant := (say 42, 10);
m constant := (say 42, 10),
m say (call foo)
p def constant():
p print(42)
@ -199,7 +199,7 @@ c w SonicPi nie ma domyślnej długości nuty;
c za każdym razem trzeba ją definiować
n Zmiana oktawy do 4tej
m oct 4;
m oct 4,
c w SonicPi domyślna oktawa
c jest ustalona jako 4

View File

@ -741,7 +741,7 @@ static Result<Value> builtin_map(Interpreter &interpreter, std::vector<Value> ar
//:
//: # Przykład
//: ```
//:
//:
//: ```
/// Scan computes inclusive prefix sum
static Result<Value> builtin_scan(Interpreter &interpreter, std::vector<Value> args)
@ -1071,9 +1071,11 @@ static Result<Value> builtin_duration(Interpreter &interpreter, std::vector<Valu
auto total = Number{};
for (auto &arg : args) {
Try(traverse(interpreter, std::move(arg), [&](Chord &c) {
auto chord_length = Number();
for (Note &note : c.notes) {
total += note.length ? *note.length : interpreter.current_context->length;
chord_length = std::max(chord_length, note.length ? *note.length : interpreter.current_context->length);
}
total += chord_length;
}));
}
return total;
@ -1149,7 +1151,7 @@ static Result<Value> builtin_permute(Interpreter &i, std::vector<Value> args)
return array;
}
//: Funkcja `sortuje` elementy od najmniejszego do największego (w tym nuty).
//: Funkcja `sortuje` elementy od najmniejszego do największego (w tym nuty).
//:
//: # Sortowanie liczb
//: ```

View File

@ -105,7 +105,7 @@ auto Lexer::next_token() -> Result<std::variant<Token, End_Of_File>>
// Lex numeric literals
// They may have following forms: 0, 0.1
if (consume_if(unicode::is_digit)) {
if (consume_if(unicode::is_digit) || consume_if('-', unicode::is_digit)) {
while (consume_if(unicode::is_digit)) {}
if (peek() == '.') {
consume();

View File

@ -242,7 +242,7 @@ parse_fractional:
.location = std::move(token.location)
};
}
result += Number{ frac, pow10(frac_end - num_end) };
result += Number{ (result.num < 0 ? -1 : 1) * frac, pow10(frac_end - num_end) };
}
return result.simplify();

View File

@ -1,8 +1,8 @@
say (ceil (0 - 4)),
say (ceil (0 - 4.2)),
say (ceil (0 - 4.5)),
say (ceil (0 - 4.8)),
say (ceil (0 - 5)),
say (ceil -4),
say (ceil -4.2),
say (ceil -4.5),
say (ceil -4.8),
say (ceil -5),
say (ceil 4),
say (ceil 4.2),

View File

@ -0,0 +1,5 @@
len (1/4),
say (duration (chord c e g)),
say (duration c),
say (duration (c hn e hn)),
say (duration (c 0.3)),

View File

@ -1,8 +1,8 @@
say (floor (0 - 4)),
say (floor (0 - 4.2)),
say (floor (0 - 4.5)),
say (floor (0 - 4.8)),
say (floor (0 - 5)),
say (floor -4),
say (floor -4.2),
say (floor -4.5),
say (floor -4.8),
say (floor -5),
say (floor 4),
say (floor 4.2),

View File

@ -1,17 +1,17 @@
say (range 0),
say (range (0 - 1)),
say (range -1),
say (range 10),
say (range 1 10),
say (range 1 10 2),
say (up 0),
say (up (0 - 1)),
say (up -1),
say (up 10),
say (up 1 10),
say (up 1 10 2),
say (down 0),
say (down (0 - 1)),
say (down -1),
say (down 10),
say (down 1 10),
say (down 1 10 2),

View File

@ -1,8 +1,8 @@
say (round (0 - 4)),
say (round (0 - 4.2)),
say (round (0 - 4.5)),
say (round (0 - 4.8)),
say (round (0 - 5)),
say (round -4),
say (round -4.2),
say (round -4.5),
say (round -4.8),
say (round -5),
say (round 4),
say (round 4.2),

View File

@ -270,6 +270,18 @@
"5"
],
"stderr_lines": []
},
{
"name": "duration.mq",
"exit_code": 0,
"stdin_lines": [],
"stdout_lines": [
"3/4",
"1/4",
"1",
"3/10"
],
"stderr_lines": []
}
]
}

View File

@ -1,3 +1,3 @@
test: bin/debug/musique
test: bin/$(os)/debug/musique
python3 scripts/test.py