Added euclid parsing support

This commit is contained in:
2023-02-04 17:17:14 +02:00
parent 63b292a7bd
commit 6597ca3f17
4 changed files with 42 additions and 7 deletions

View File

@ -24,7 +24,7 @@ if __name__ == "__main__":
'Loop cycles (for zloop or z0-z9)': "<0 <1 <2 <3 <4 5>>>>>", 'Loop cycles (for zloop or z0-z9)': "<0 <1 <2 <3 <4 5>>>>>",
'Basic operations': "(1 2 (3 4)+2)*2 ((1 2 3)+(0 9 13))-2 ((3 4 {10})*(2 9 3))%7", 'Basic operations': "(1 2 (3 4)+2)*2 ((1 2 3)+(0 9 13))-2 ((3 4 {10})*(2 9 3))%7",
'Product operations': "(0 1 2 3)+(1 4 2 3) (0 1 2)-(0 2 1)+2", 'Product operations': "(0 1 2 3)+(1 4 2 3) (0 1 2)-(0 2 1)+2",
'Euclid cycles': "(q1)<6,7>(q4 (e3 e4) q2) or (q1)<6,7<(q4 q3 q2)", 'Euclid cycles': "(q1)<6,7>(q4 (e3 e4) q2) (q1)<6,7>(q4 q3 q2)",
'Transformations': "(0 1 2)<r> (0 1 2)<i>(-2 1)", 'Transformations': "(0 1 2)<r> (0 1 2)<i>(-2 1)",
'List assignation': "A=(0 (1,6) 3) B=(3 ? 2) B A B B A", 'List assignation': "A=(0 (1,6) 3) B=(3 ? 2) B A B B A",
'Random repeat': "(: 1 (2,6) 3 :4)", 'Random repeat': "(: 1 (2,6) 3 :4)",

View File

@ -202,3 +202,12 @@ class Atom(Item):
class Integer(Item): class Integer(Item):
''' Class for integers ''' ''' Class for integers '''
value: int value: int
@dataclass
class Euclid(Item):
''' Class for euclidean cycles '''
pulses: int
length: int
onset: list
offset: list = None
rotate: int = None

View File

@ -11,7 +11,7 @@ class ZiffersTransformer(Transformer):
def random_integer(self,s): def random_integer(self,s):
val = s[0][1:-1].split(",") val = s[0][1:-1].split(",")
return RandomInteger(min=val[0],max=val[1],text=s[0]) return RandomInteger(min=val[0],max=val[1],text=s[0].value)
def range(self,s): def range(self,s):
val = s[0].split("..") val = s[0].split("..")
@ -136,6 +136,9 @@ class ZiffersTransformer(Transformer):
val = s.value val = s.value
return Integer(text=val,value=int(val)) return Integer(text=val,value=int(val))
def number(self,s):
return s
def lisp_operation(self,s): def lisp_operation(self,s):
op = s[0] op = s[0]
values = s[1:] values = s[1:]
@ -150,3 +153,18 @@ class ZiffersTransformer(Transformer):
def list_op(self,s): def list_op(self,s):
return ListOperation(values=s) return ListOperation(values=s)
def euclid(self,s):
params = s[1][1:-1].split(",")
init = {"onset":s[0],"pulses":params[0],"length":params[1]}
text = s[0].text+s[1]
if len(params)>2:
init["rotate"] = params[2]
if len(s)>2:
init["offset"] = s[2]
text = text+s[2].text
init["text"] = text
return Euclid(**init)
def euclid_operator(self,s):
return s.value

View File

@ -1,6 +1,6 @@
// Root for the rules // Root for the rules
?root: sequence ?root: sequence
sequence: (pc | dur_change | oct_mod | oct_change | WS | chord | cycle | random_integer | random_pitch | random_percent | range | list | lisp_operation | list_op | subdivision | eval)* sequence: (pc | dur_change | oct_mod | oct_change | WS | chord | cycle | random_integer | random_pitch | random_percent | range | list | lisp_operation | list_op | subdivision | eval | euclid)*
// Pitch classes // Pitch classes
pc: prefix* pitch pc: prefix* pitch
@ -13,13 +13,21 @@
// Chords // Chords
chord: pc pc+ chord: pc pc+
// Valid as integer
?number: SIGNED_NUMBER | random_integer
// List // List
list: prefix* "(" sequence ")" list: prefix* "(" sequence ")"
// Right recursive list operation // Right recursive list operation
list_op: list (operator (list | number))+ list_op: list (operator (list | number))+
operator: /([\+\-\*\/%]|<<|>>)/ operator: /([\+\-\*\/%]|<<|>>)/
?number: SIGNED_NUMBER
euclid: list euclid_operator list?
?euclid_operator: /<[0-9]+,[0-9]+(,[0-9])?>/
// TODO: Support randomization etc.
//euclid_operator: (">" | "<") number "," number ["," number] (">" | "<")
// Lisp like list operation // Lisp like list operation
lisp_operation: "(" operator WS sequence ")" lisp_operation: "(" operator WS sequence ")"
@ -51,7 +59,7 @@
// TODO: Support for parenthesis? // TODO: Support for parenthesis?
eval: "{" operation "}" eval: "{" operation "}"
operation: atom (operator atom)+ operation: atom (operator atom)+
atom: (SIGNED_NUMBER | DECIMAL | random_integer) atom: (number | DECIMAL)
%import common.NUMBER %import common.NUMBER
%import common.SIGNED_NUMBER %import common.SIGNED_NUMBER