From 8fa9fda9b4c89ffd6418fe8e97f554d8a8850d2a Mon Sep 17 00:00:00 2001 From: Felix Roos Date: Thu, 10 Feb 2022 09:08:40 +0100 Subject: [PATCH] timecat mini notation --- repl/src/parse.ts | 22 +++++++++++++++++++++- repl/src/tunes.ts | 6 ++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/repl/src/parse.ts b/repl/src/parse.ts index 512a424c..6564eb96 100644 --- a/repl/src/parse.ts +++ b/repl/src/parse.ts @@ -31,6 +31,13 @@ function reify(thing: any) { return pure(thing); } +function minify(thing: any) { + if (typeof thing === 'string') { + return mini(thing); + } + return reify(thing); +} + const applyOptions = (parent: any) => (pat: any, i: number) => { const ast = parent.source_[i]; const options = ast.options_; @@ -44,7 +51,10 @@ const applyOptions = (parent: any) => (pat: any, i: number) => { } console.warn(`operator "${operator.type_}" not implemented`); } - // TODO: options.weight = "@" + if (options?.weight) { + // weight is handled by parent + return pat; + } // TODO: bjorklund e.g. "c3(5,8)" const unimplemented = Object.keys(options || {}).filter((key) => key !== 'operator'); if (unimplemented.length) { @@ -62,6 +72,10 @@ export function patternifyAST(ast: any): any { if (ast.arguments_.alignment === 'v') { return stack(...children); } + const weightedChildren = ast.source_.some((child) => !!child.options_?.weight); + if (weightedChildren) { + return timeCat(...ast.source_.map((child, i) => [child.options_?.weight || 1, children[i]])); + } return sequence(...children); case 'element': if (ast.source_ === '~') { @@ -113,7 +127,13 @@ export const mini = (...strings: string[]) => { return pattern; }; +// shorthand for mini const m = mini; +// shorthand for stack, automatically minifying strings +const s = (...strings) => { + const patternified = strings.map((s) => minify(s)); + return stack(...patternified); +}; // includes haskell style (raw krill parsing) export const h = (string: string) => { diff --git a/repl/src/tunes.ts b/repl/src/tunes.ts index fa0011f2..6b5980fa 100644 --- a/repl/src/tunes.ts +++ b/repl/src/tunes.ts @@ -1,3 +1,9 @@ +export const timeCatMini = `s( + 'c3@3 [eb3, g3, [c4 d4]/2]', + 'c2 g2', + m\`[eb4@5 [f4 eb4 d4]@3] [eb4 c4]/2\`.slow(8) +)`; + export const timeCat = `stack( timeCat([3, c3], [1, stack(eb3, g3, m(c4, d4).slow(2))]), m(c2, g2),