From fb148657857a2050375dbc261f61d49d926c182c Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Thu, 29 Dec 2022 14:02:50 +0100 Subject: [PATCH] fix: can now multiply floats in mini notation - fixes #314 --- packages/mini/krill-parser.js | 6 ++-- packages/mini/krill.pegjs | 4 +-- packages/mini/mini.mjs | 56 +++++++++++++++++++---------------- 3 files changed, 35 insertions(+), 31 deletions(-) diff --git a/packages/mini/krill-parser.js b/packages/mini/krill-parser.js index c4a5d09f..2bc565f7 100644 --- a/packages/mini/krill-parser.js +++ b/packages/mini/krill-parser.js @@ -32,7 +32,7 @@ function peg$padEnd(str, targetLength, padString) { } peg$SyntaxError.prototype.format = function(sources) { - var str = "peg error: " + this.message; + var str = "Error: " + this.message; if (this.location) { var src = null; var k; @@ -271,8 +271,8 @@ function peg$parse(input, options) { var peg$f4 = function(a) { return { weight: a} }; var peg$f5 = function(a) { return { replicate: a } }; var peg$f6 = function(p, s, r) { return { operator : { type_: "bjorklund", arguments_ :{ pulse: p, step:s, rotation:r || 0 } } } }; - var peg$f7 = function(a) { return { operator : { type_: "stretch", arguments_ :{ amount:a } } } }; - var peg$f8 = function(a) { return { operator : { type_: "stretch", arguments_ :{ amount:"1/"+a } } } }; + var peg$f7 = function(a) { return { operator : { type_: "stretch", arguments_ :{ amount:a, type: 'slow' } } } }; + var peg$f8 = function(a) { return { operator : { type_: "stretch", arguments_ :{ amount:a, type: 'fast' } } } }; var peg$f9 = function(a) { return { operator : { type_: "fixed-step", arguments_ :{ amount:a } } } }; var peg$f10 = function(a) { return { operator : { type_: "degradeBy", arguments_ :{ amount:(a? a : 0.5) } } } }; var peg$f11 = function(s, o) { return new ElementStub(s, o);}; diff --git a/packages/mini/krill.pegjs b/packages/mini/krill.pegjs index 6302614d..87e9df3f 100644 --- a/packages/mini/krill.pegjs +++ b/packages/mini/krill.pegjs @@ -116,10 +116,10 @@ slice_bjorklund = "(" ws p:number ws comma ws s:number ws comma? ws r:number? ws { return { operator : { type_: "bjorklund", arguments_ :{ pulse: p, step:s, rotation:r || 0 } } } } slice_slow = "/"a:number - { return { operator : { type_: "stretch", arguments_ :{ amount:a } } } } + { return { operator : { type_: "stretch", arguments_ :{ amount:a, type: 'slow' } } } } slice_fast = "*"a:number - { return { operator : { type_: "stretch", arguments_ :{ amount:"1/"+a } } } } + { return { operator : { type_: "stretch", arguments_ :{ amount:a, type: 'fast' } } } } slice_fixed_step = "%"a:number { return { operator : { type_: "fixed-step", arguments_ :{ amount:a } } } } diff --git a/packages/mini/mini.mjs b/packages/mini/mini.mjs index 00ecb24a..745f0f85 100644 --- a/packages/mini/mini.mjs +++ b/packages/mini/mini.mjs @@ -23,8 +23,12 @@ const applyOptions = (parent) => (pat, i) => { if (operator) { switch (operator.type_) { case 'stretch': { - const speed = Fraction(operator.arguments_.amount).inverse(); - return reify(pat).fast(speed); + const legalTypes = ['fast', 'slow']; + const { type, amount } = operator.arguments_; + if (!legalTypes.includes(type)) { + throw new Error(`mini: stretch: type must be one of ${legalTypes.join('|')} but got ${type}`); + } + return reify(pat)[type](amount); } case 'bjorklund': return pat.euclid(operator.arguments_.pulse, operator.arguments_.step, operator.arguments_.rotation); @@ -74,32 +78,32 @@ function resolveReplications(ast) { // could this be made easier?! ast.source_ = ast.source_.map((child) => { const { replicate, ...options } = child.options_ || {}; - if (replicate) { - return { - ...child, - options_: { ...options, weight: replicate }, - source_: { - type_: 'pattern', - arguments_: { - alignment: 'h', - }, - source_: [ - { - type_: 'element', - source_: child.source_, - location_: child.location_, - options_: { - operator: { - type_: 'stretch', - arguments_: { amount: Fraction(replicate).inverse().toString() }, - }, + if (!replicate) { + return child; + } + return { + ...child, + options_: { ...options, weight: replicate }, + source_: { + type_: 'pattern', + arguments_: { + alignment: 'h', + }, + source_: [ + { + type_: 'element', + source_: child.source_, + location_: child.location_, + options_: { + operator: { + type_: 'stretch', + arguments_: { amount: replicate, type: 'fast' }, }, }, - ], - }, - }; - } - return child; + }, + ], + }, + }; }); }