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