Added parentheses support to eval and some formatting
This commit is contained in:
@ -112,6 +112,7 @@ class Chord(Event):
|
|||||||
"""Set notes to the class"""
|
"""Set notes to the class"""
|
||||||
self.notes = notes
|
self.notes = notes
|
||||||
|
|
||||||
|
|
||||||
@dataclass
|
@dataclass
|
||||||
class RomanNumeral(Event):
|
class RomanNumeral(Event):
|
||||||
"""Class for roman numbers"""
|
"""Class for roman numbers"""
|
||||||
@ -228,17 +229,25 @@ class Ziffers(Sequence):
|
|||||||
key = self.options["key"]
|
key = self.options["key"]
|
||||||
scale = self.options["scale"]
|
scale = self.options["scale"]
|
||||||
if isinstance(self.current, (Pitch, RandomPitch)):
|
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)
|
self.current.set_note(note)
|
||||||
elif isinstance(self.current, Chord):
|
elif isinstance(self.current, Chord):
|
||||||
pcs = self.current.pitch_classes
|
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)
|
self.current.set_notes(notes)
|
||||||
elif isinstance(self.current, RomanNumeral):
|
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.current.set_pitch_classes(pitch_classes)
|
||||||
|
|
||||||
|
|
||||||
self.loop_i += 1
|
self.loop_i += 1
|
||||||
return self.current
|
return self.current
|
||||||
|
|
||||||
|
|||||||
@ -121,8 +121,14 @@ class ZiffersTransformer(Transformer):
|
|||||||
name = items[1]
|
name = items[1]
|
||||||
chord_notes = chord_from_roman_numeral(numeral, name)
|
chord_notes = chord_from_roman_numeral(numeral, name)
|
||||||
parsed_number = parse_roman(numeral)
|
parsed_number = parse_roman(numeral)
|
||||||
return RomanNumeral(text=numeral, value=parsed_number, chord_type=name, notes=chord_notes)
|
return RomanNumeral(
|
||||||
return RomanNumeral(value=parse_roman(numeral), text=numeral, notes=chord_from_roman_numeral(numeral))
|
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):
|
def chord_name(self, item):
|
||||||
"""Return name for chord"""
|
"""Return name for chord"""
|
||||||
@ -222,9 +228,13 @@ class ZiffersTransformer(Transformer):
|
|||||||
val = items[0]
|
val = items[0]
|
||||||
return Eval(values=val)
|
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):
|
def operation(self, items):
|
||||||
"""Return partial eval operations"""
|
"""Return partial eval operations"""
|
||||||
return items
|
return flatten(items)
|
||||||
|
|
||||||
def atom(self, token):
|
def atom(self, token):
|
||||||
"""Return partial eval item"""
|
"""Return partial eval item"""
|
||||||
|
|||||||
@ -18,7 +18,7 @@
|
|||||||
?roman_number: /iv|v|v?i{1,3}/
|
?roman_number: /iv|v|v?i{1,3}/
|
||||||
|
|
||||||
// Valid as integer
|
// Valid as integer
|
||||||
?number: SIGNED_NUMBER | random_integer | cyclic_number
|
?number: NUMBER | random_integer | cyclic_number
|
||||||
cyclic_number: "<" number (WS number)* ">"
|
cyclic_number: "<" number (WS number)* ">"
|
||||||
|
|
||||||
// Repeats
|
// Repeats
|
||||||
@ -69,11 +69,12 @@
|
|||||||
|
|
||||||
// Rules for evaluating clauses inside {}
|
// Rules for evaluating clauses inside {}
|
||||||
// TODO: Support for parenthesis?
|
// TODO: Support for parenthesis?
|
||||||
eval: "{" operation "}"
|
eval: "{" operation+ "}"
|
||||||
operation: atom (operator atom)+
|
operation: atom (operator (sub_operations | operation))*
|
||||||
atom: (number | DECIMAL)
|
sub_operations: "(" operation ")"
|
||||||
|
atom: (NUMBER | DECIMAL)
|
||||||
|
|
||||||
%import common.NUMBER
|
%import common.NUMBER
|
||||||
%import common.SIGNED_NUMBER
|
//%import common.SIGNED_NUMBER
|
||||||
%import common.DECIMAL
|
%import common.DECIMAL
|
||||||
%import common.WS
|
%import common.WS
|
||||||
Reference in New Issue
Block a user