Added parentheses support to eval and some formatting

This commit is contained in:
2023-02-09 00:16:26 +02:00
parent f51b230099
commit 55b6940f35
3 changed files with 44 additions and 24 deletions

View File

@ -112,6 +112,7 @@ class Chord(Event):
"""Set notes to the class"""
self.notes = notes
@dataclass
class RomanNumeral(Event):
"""Class for roman numbers"""
@ -228,17 +229,25 @@ class Ziffers(Sequence):
key = self.options["key"]
scale = self.options["scale"]
if isinstance(self.current, (Pitch, RandomPitch)):
note = note_from_pc(root=key,pitch_class=self.current.pitch_class,intervals=scale,modifier=self.current.modifier)
note = note_from_pc(
root=key,
pitch_class=self.current.pitch_class,
intervals=scale,
modifier=self.current.modifier,
)
self.current.set_note(note)
elif isinstance(self.current, Chord):
pcs = self.current.pitch_classes
notes = [pc.set_note(note_from_pc(key, pc.pitch_class, scale)) for pc in pcs]
notes = [
pc.set_note(note_from_pc(key, pc.pitch_class, scale)) for pc in pcs
]
self.current.set_notes(notes)
elif isinstance(self.current, RomanNumeral):
pitch_classes = [midi_to_pitch_class(note, key, scale) for note in self.current.notes]
pitch_classes = [
midi_to_pitch_class(note, key, scale) for note in self.current.notes
]
self.current.set_pitch_classes(pitch_classes)
self.loop_i += 1
return self.current

View File

@ -121,8 +121,14 @@ class ZiffersTransformer(Transformer):
name = items[1]
chord_notes = chord_from_roman_numeral(numeral, name)
parsed_number = parse_roman(numeral)
return RomanNumeral(text=numeral, value=parsed_number, chord_type=name, notes=chord_notes)
return RomanNumeral(value=parse_roman(numeral), text=numeral, notes=chord_from_roman_numeral(numeral))
return RomanNumeral(
text=numeral, value=parsed_number, chord_type=name, notes=chord_notes
)
return RomanNumeral(
value=parse_roman(numeral),
text=numeral,
notes=chord_from_roman_numeral(numeral),
)
def chord_name(self, item):
"""Return name for chord"""
@ -222,9 +228,13 @@ class ZiffersTransformer(Transformer):
val = items[0]
return Eval(values=val)
def sub_operations(self, items):
"""Returns list of operations"""
return Eval(values=items[0], wrap_start="(", wrap_end=")")
def operation(self, items):
"""Return partial eval operations"""
return items
return flatten(items)
def atom(self, token):
"""Return partial eval item"""

View File

@ -18,7 +18,7 @@
?roman_number: /iv|v|v?i{1,3}/
// Valid as integer
?number: SIGNED_NUMBER | random_integer | cyclic_number
?number: NUMBER | random_integer | cyclic_number
cyclic_number: "<" number (WS number)* ">"
// Repeats
@ -69,11 +69,12 @@
// Rules for evaluating clauses inside {}
// TODO: Support for parenthesis?
eval: "{" operation "}"
operation: atom (operator atom)+
atom: (number | DECIMAL)
eval: "{" operation+ "}"
operation: atom (operator (sub_operations | operation))*
sub_operations: "(" operation ")"
atom: (NUMBER | DECIMAL)
%import common.NUMBER
%import common.SIGNED_NUMBER
//%import common.SIGNED_NUMBER
%import common.DECIMAL
%import common.WS