Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make harmonic segments part of chord enumerations #242

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,11 @@ export type ArrowFunction = {
export type EnumeratedChord = {
type: 'EnumeratedChord';
mirror: boolean;
intervals: Expression[];
enumerals: Expression[];
};

export type HarmonicSegment = {
type: 'HarmonicSegment';
mirror: boolean;
root: Expression;
end: Expression;
};
Expand Down
35 changes: 20 additions & 15 deletions src/grammars/sonic-weave.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ Expression
= LestExpression

LestExpression
= fallback: ConditionalExpression tail: (LestToken @ConditionalExpression)? {
= fallback: ConditionalExpression tail: (__ LestToken _ @ConditionalExpression)? {
if (tail) {
return {
type: 'LestExpression',
Expand All @@ -504,7 +504,7 @@ LestExpression
}

ConditionalExpression
= consequent: CoalescingExpression tail: (IfToken @CoalescingExpression ElseToken @CoalescingExpression)? {
= consequent: CoalescingExpression tail: (__ IfToken _ @CoalescingExpression _ ElseToken _ @CoalescingExpression)? {
if (tail) {
const [test, alternate] = tail;
return {
Expand Down Expand Up @@ -564,39 +564,44 @@ RoundingOperator
= $(ToToken / ByToken)

RoundingExpression
= head: Segment tail: (__ @'~'? @RoundingOperator @'~'? _ @Segment)* {
= head: EnumeratedChord tail: (__ @'~'? @RoundingOperator @'~'? _ @EnumeratedChord)* {
return tail.reduce(operatorReducer, head);
}

Segment
= __ @(HarmonicSegment / EnumeratedChord) __

HarmonicSegment
= mirror: '/'? __ root: ExtremumExpression _ '::' _ end: ExtremumExpression {
= root: ExtremumExpression _ '::' _ end: ExtremumExpression {
return {
type: 'HarmonicSegment',
mirror: !!mirror,
root,
end,
};
}

Enumeral = HarmonicSegment / ExtremumExpression

EnumeratedChord
= '/' __ intervals: ExtremumExpression|2.., _ ':' _| {
= '/' __ enumerals: Enumeral|2.., _ ':' _| {
return {
type: 'EnumeratedChord',
mirror: true,
intervals,
enumerals,
};
}
/ '/' __ segment: HarmonicSegment {
return {
type: 'EnumeratedChord',
mirror: true,
enumerals: [segment],
};
}
/ intervals: ExtremumExpression|1.., _ ':' _| {
if (intervals.length === 1) {
return intervals[0];
/ enumerals: Enumeral|1.., _ ':' _| {
if (enumerals.length === 1) {
return enumerals[0];
}
return {
type: 'EnumeratedChord',
mirror: false,
intervals,
enumerals,
};
}

Expand Down Expand Up @@ -905,7 +910,7 @@ Comprehension
}

ArrayComprehension
= '[' _ expression: Expression comprehensions: Comprehension+ _ test: (IfToken @Expression)? _ ']' {
= '[' _ expression: Expression comprehensions: Comprehension+ _ test: (IfToken _ @Expression)? _ ']' {
return {
type: 'ArrayComprehension',
expression,
Expand Down
25 changes: 24 additions & 1 deletion src/parser/__tests__/sonic-weave-ast.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ describe('SonicWeave Abstract Syntax Tree parser', () => {
expression: {
type: 'EnumeratedChord',
mirror: false,
intervals: [
enumerals: [
{
type: 'CallExpression',
callee: {type: 'Identifier', id: 'root'},
Expand Down Expand Up @@ -701,6 +701,25 @@ describe('SonicWeave Abstract Syntax Tree parser', () => {
});
});

it('accepts conditional array comprehensions', () => {
const ast = parseSingle('[foo for foo of bar if baz]');
expect(ast).toEqual({
type: 'ExpressionStatement',
expression: {
type: 'ArrayComprehension',
expression: {type: 'Identifier', id: 'foo'},
comprehensions: [
{
element: {type: 'Parameter', id: 'foo', defaultValue: null},
kind: 'of',
container: {type: 'Identifier', id: 'bar'},
},
],
test: {type: 'Identifier', id: 'baz'},
},
});
});

it('accepts ranges spanning multiple rows', () => {
const ast = parseSingle('[\n1\n..\n10\n]');
expect(ast.expression.type).toBe('Range');
Expand Down Expand Up @@ -1037,4 +1056,8 @@ describe('Negative tests', () => {
expect(rejected).toBe(true);
}
});

it('rejects potentially ambiguous harmonic segment concatenations', () => {
expect(() => parse('4::7::16')).toThrow();
});
});
28 changes: 28 additions & 0 deletions src/parser/__tests__/source.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1250,4 +1250,32 @@ describe('SonicWeave parser', () => {
const scale = parseSource('C4 = 263Hz;[A4, C5] tmpr [email protected];str');
expect(scale).toEqual(['9\\12', '12\\12']);
});

it('supports harmonic segments as parts of enumerated chords', () => {
const scale = parseSource('2:4::7:11:14::17;str');
expect(scale).toEqual([
'4/2',
'5/2',
'6/2',
'7/2',
'11/2',
'14/2',
'15/2',
'16/2',
'17/2',
]);
});

it('supports subharmonic segments as parts of reflected enumerated chords', () => {
const scale = parseSource('/16:14::9:5;str');
expect(scale).toEqual([
'16/14',
'16/13',
'16/12',
'16/11',
'16/10',
'16/9',
'16/5',
]);
});
});
46 changes: 30 additions & 16 deletions src/parser/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1594,17 +1594,26 @@ export class ExpressionVisitor {
const intervals: Interval[] = [];
const domains: IntervalDomain[] = [];
const monzos: (TimeMonzo | TimeReal)[] = [];
for (const expression of node.intervals) {
let interval = this.visit(expression);
if (typeof interval === 'boolean') {
interval = upcastBool(interval);
}
if (interval instanceof Interval) {
intervals.push(interval);
monzos.push(interval.value);
domains.push(interval.domain);
for (const expression of node.enumerals) {
if (expression.type === 'HarmonicSegment') {
const segment = this.visitHarmonicSegment(expression, true);
for (const interval of segment) {
intervals.push(interval);
monzos.push(interval.value);
domains.push(interval.domain);
}
} else {
throw new Error('Type error: Can only stack intervals in a chord.');
let interval = this.visit(expression);
if (typeof interval === 'boolean') {
interval = upcastBool(interval);
}
if (interval instanceof Interval) {
intervals.push(interval);
monzos.push(interval.value);
domains.push(interval.domain);
} else {
throw new Error('Type error: Can only stack intervals in a chord.');
}
}
}
const rootInterval = intervals.shift()!;
Expand Down Expand Up @@ -1692,7 +1701,10 @@ export class ExpressionVisitor {
throw new Error('Range step must not be zero.');
}

protected visitHarmonicSegment(node: HarmonicSegment): Interval[] {
protected visitHarmonicSegment(
node: HarmonicSegment,
enumeral = false
): Interval[] {
let root = this.visit(node.root);
let end = this.visit(node.end);
if (typeof root === 'boolean') {
Expand All @@ -1707,20 +1719,22 @@ export class ExpressionVisitor {
this.spendGas(Math.abs(end.value.valueOf() - root.value.valueOf()));
const one = linearOne();
const result: Interval[] = [];
let next = root;
if (root.compare(end) <= 0) {
let next = root.add(one);
while (next.compare(end) <= 0) {
result.push(node.mirror ? root.div(next) : next.div(root));
result.push(next);
next = next.add(one);
}
} else {
let next = root.sub(one);
while (next.compare(end) >= 0) {
result.push(node.mirror ? root.div(next) : next.div(root));
result.push(next);
next = next.sub(one);
}
}
return result;
if (enumeral) {
return result;
}
return result.slice(1).map(i => i.div(root as Interval));
}

/**
Expand Down
Loading