Added range evaluation and more operators
This commit is contained in:
@ -328,6 +328,8 @@ class Sequence(Meta):
|
||||
yield item
|
||||
else:
|
||||
yield from item.evaluate_tree(options)
|
||||
elif isinstance(item, Range):
|
||||
yield from item.evaluate(options)
|
||||
elif isinstance(item, Cyclic):
|
||||
yield from _resolve_item(item.get_value(), options)
|
||||
elif isinstance(item, Euclid):
|
||||
@ -689,6 +691,10 @@ class Range(Item):
|
||||
start: int = field(default=None)
|
||||
end: int = field(default=None)
|
||||
|
||||
def evaluate(self, options):
|
||||
for i in range(self.start,self.end+1):
|
||||
yield Pitch(pitch_class=i, kwargs=options)
|
||||
|
||||
|
||||
@dataclass(kw_only=True)
|
||||
class Operator(Item):
|
||||
@ -714,6 +720,7 @@ class ListOperation(Sequence):
|
||||
def filter_operation(input_list):
|
||||
flattened_list = []
|
||||
|
||||
# TODO: ADD Options here and evaluate events?
|
||||
for item in input_list:
|
||||
if isinstance(item, (list, Sequence)):
|
||||
if isinstance(item, ListOperation):
|
||||
@ -740,6 +747,7 @@ class ListOperation(Sequence):
|
||||
(right.values if isinstance(right, Sequence) else [right]), left
|
||||
)
|
||||
left = [
|
||||
#TODO: Get options from x value?
|
||||
Pitch(
|
||||
pitch_class=operation(x.get_value(options), y.get_value(options)),
|
||||
kwargs=options,
|
||||
@ -849,5 +857,7 @@ class RepeatedSequence(Sequence):
|
||||
self.local_options = self.local_options | item.as_options()
|
||||
elif isinstance(item, Rest):
|
||||
yield item.get_updated_item(self.local_options)
|
||||
elif isinstance(item, Range):
|
||||
yield from item.evaluate(options)
|
||||
elif isinstance(item, (Event, RandomInteger)):
|
||||
yield Pitch(pitch_class=item.get_value(self.local_options), kwargs=self.local_options)
|
||||
|
||||
@ -49,6 +49,10 @@ OPERATORS = {
|
||||
"*": operator.mul,
|
||||
"/": operator.truediv,
|
||||
"%": operator.mod,
|
||||
"|": operator.or_,
|
||||
"&": operator.and_,
|
||||
"<<": operator.ilshift,
|
||||
">>": operator.irshift
|
||||
}
|
||||
|
||||
|
||||
|
||||
@ -71,7 +71,7 @@ class ZiffersTransformer(Transformer):
|
||||
def range(self, item) -> Range:
|
||||
"""Parses range syntax"""
|
||||
val = item[0].split("..")
|
||||
return Range(start=val[0], end=val[1], text=item[0])
|
||||
return Range(start=int(val[0]), end=int(val[1]), text=item[0].value)
|
||||
|
||||
def cycle(self, items) -> Cyclic:
|
||||
"""Parses cycle"""
|
||||
|
||||
@ -43,7 +43,7 @@
|
||||
// Right recursive list operation
|
||||
list_op: list (operator right_op)+
|
||||
right_op: list | number
|
||||
operator: /([\+\-\*\/%]|<<|>>)/
|
||||
operator: /([\+\-\*\/%\|\&]|<<|>>)/
|
||||
|
||||
// Euclidean cycles
|
||||
// TODO: Support randomization etc.
|
||||
@ -56,7 +56,7 @@
|
||||
|
||||
// Subdivision
|
||||
subdivision: "[" subitems "]"
|
||||
subitems: (pitch_class | random_integer | random_pitch | rest | oct_mod | oct_change | WS | chord | named_roman | cycle | subdivision | list | list_op)*
|
||||
subitems: (pitch_class | random_integer | random_pitch | rest | oct_mod | oct_change | WS | chord | named_roman | cycle | subdivision | list | list_op | range)*
|
||||
|
||||
// Control characters modifying future events
|
||||
oct_mod: octave WS
|
||||
@ -67,7 +67,7 @@
|
||||
|
||||
// Generative rules
|
||||
random_integer: /\(-?[0-9]+,-?[0-9]+\)/
|
||||
range: /-?[0-9]\.\.-?[0-9]/
|
||||
range: /-?[0-9]+\.\.-?[0-9]+/
|
||||
cycle: "<" sequence ">"
|
||||
random_pitch: /(\?)(?!\d)/
|
||||
random_percent: /(%)(?!\d)/
|
||||
|
||||
Reference in New Issue
Block a user