From 28aed391f2429fa387eaed1d34e6e36f28988e93 Mon Sep 17 00:00:00 2001 From: Edwin Jakobs Date: Sat, 30 Mar 2024 16:42:36 +0100 Subject: [PATCH] [orx-fcurve] Replace |repeat-block| with (repeat-block) --- orx-fcurve/README.md | 12 ++++++------ orx-fcurve/src/commonMain/kotlin/EFCurve.kt | 9 +++++---- orx-fcurve/src/commonTest/kotlin/TestEFCurve.kt | 10 +++++----- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/orx-fcurve/README.md b/orx-fcurve/README.md index 35e695be..f3c3c026 100644 --- a/orx-fcurve/README.md +++ b/orx-fcurve/README.md @@ -178,24 +178,24 @@ For example: `M0 L{3 * 4},4` evaluates to `M0 L12,4`. ## Repetitions EFCurves add support for repetitions. Repetitions are expanded by replacing -occurrences of `||[]` with `number-of-repetitions` copies +occurrences of `()[]` with `number-of-repetitions` copies of `text-to-repeat`. For example: - * `M0 |h1 m1|[3]` expands to `M0 h1 m1 h1 m1 h1 m1` - * `M0 |h1 m1|[0]` expands to `M0` + * `M0 (h1 m1)[3]` expands to `M0 h1 m1 h1 m1 h1 m1` + * `M0 (h1 m1)[0]` expands to `M0` ### Nested repetitions Repetitions can be nested. -For example `|M0 |h1 m1|[3]|[2]` expands to `M0 h1 m1 h1 m1 h1 m1 M0 h1 m1 h1 m1 h1 m1`. +For example `(M0 (h1 m1)[3])[2]` expands to `M0 h1 m1 h1 m1 h1 m1 M0 h1 m1 h1 m1 h1 m1`. ### Interaction between repetitions and expressions -`M0 |H{it + 1} m1][3]` expands to `M0 H1 m1 H2 m1 H3 m1` +`M0 (H{it + 1} m1)[3]` expands to `M0 H1 m1 H2 m1 H3 m1` -`M0 |H{index + 1} m{it}]{1.2, 1.3, 1.4}` expands to `M0 H1 m1.2 H2 m1.3 H3 m1.4` +`M0 (H{index + 1} m{it}){1.2, 1.3, 1.4}` expands to `M0 H1 m1.2 H2 m1.3 H3 m1.4` # References diff --git a/orx-fcurve/src/commonMain/kotlin/EFCurve.kt b/orx-fcurve/src/commonMain/kotlin/EFCurve.kt index 20fed533..f89bf5dc 100644 --- a/orx-fcurve/src/commonMain/kotlin/EFCurve.kt +++ b/orx-fcurve/src/commonMain/kotlin/EFCurve.kt @@ -8,17 +8,18 @@ import org.openrndr.extra.expressions.evaluateExpression * @param constants a map of constants that is passed to [evaluateExpression] */ fun efcurve(ef: String, constants: Map = emptyMap()): String { - val expression = Regex("\\{([^_]+)\\}") - // IntelliJ falsely reports a redundant escape character. the escape character is required when running the regular // expression on a javascript target. Removing the escape character will result in a `Lone quantifier brackets` // syntax error. @Suppress("RegExpRedundantEscape") - val repetition = Regex("""\|([^|]+)\|\[([^\[\]]+)\]""") + val expression = Regex("\\{([^{}]+)\\}") @Suppress("RegExpRedundantEscape") - val list = Regex("\\|([^|]+)\\|\\{([^\\[\\]]+)\\}") + val repetition = Regex("""\(([^()]+)\)\[([^\[\]]+)\]""") + + @Suppress("RegExpRedundantEscape") + val list = Regex("\\(([^()]+)\\)\\{([^\\[\\]]+)\\}") /** * perform comment substitution diff --git a/orx-fcurve/src/commonTest/kotlin/TestEFCurve.kt b/orx-fcurve/src/commonTest/kotlin/TestEFCurve.kt index 8c245c73..ce46b794 100644 --- a/orx-fcurve/src/commonTest/kotlin/TestEFCurve.kt +++ b/orx-fcurve/src/commonTest/kotlin/TestEFCurve.kt @@ -5,7 +5,7 @@ import kotlin.test.assertEquals class TestEFCurve { @Test fun comments() { - val text = """M1 |h5 m3|{ + val text = """M1 (h5 m3){ |10.3 # toch wel handig zo'n comment |11.2 |14.5 @@ -21,13 +21,13 @@ class TestEFCurve { @Test fun listExpansion() { - assertEquals("M0 L1.0, ${3.0} L1.0, ${6.0}", efcurve("M0 |L1.0, {it}|{3, 6}")) + assertEquals("M0 L1.0, ${3.0} L1.0, ${6.0}", efcurve("M0 (L1.0, {it}){3, 6}")) } @Test fun repetition() { - assertEquals("M0 L1.0, 3.0 L1.0, 3.0", efcurve("M0 |L1.0, 3.0|[2]")) - assertEquals("M0 L1.0, ${0.0} L1.0, ${1.0}", efcurve("M0 |L1.0, {it}|[2]")) - assertEquals("M0 L1.0, ${0.0} L1.0, ${1.0} L1.0, ${0.0} L1.0, ${1.0} L1.0, ${0.0} L1.0, ${1.0}", efcurve("M0 ||L1.0, {it}|[2]|[3]")) + assertEquals("M0 L1.0, 3.0 L1.0, 3.0", efcurve("M0 (L1.0, 3.0)[2]")) + assertEquals("M0 L1.0, ${0.0} L1.0, ${1.0}", efcurve("M0 (L1.0, {it})[2]")) + assertEquals("M0 L1.0, ${0.0} L1.0, ${1.0} L1.0, ${0.0} L1.0, ${1.0} L1.0, ${0.0} L1.0, ${1.0}", efcurve("M0 ((L1.0, {it})[2])[3]")) } } \ No newline at end of file