Skip to content

Commit

Permalink
Parse N-of-EDO with missing numerators
Browse files Browse the repository at this point in the history
  • Loading branch information
frostburn committed Jan 22, 2024
1 parent f55ce4c commit 199e0f2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
17 changes: 15 additions & 2 deletions src/__tests__/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,21 @@ describe('Line parser', () => {
expect(result.type).toBe('equal temperament');
});

it('rejects N-of-EDO without a numerator', () => {
expect(() => parseLine('\\8')).toThrow();
it('accepts N-of-EDO without a numerator', () => {
const result = parseLine('\\8');
expect(
result.equals(
new Interval(
ExtendedMonzo.fromEqualTemperament(
0,
2,
DEFAULT_NUMBER_OF_COMPONENTS
),
'equal temperament'
)
)
).toBeTruthy();
expect(result.type).toBe('equal temperament');
});

it('parses monzos', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type FractionLiteral = {

type EdjiFraction = {
type: 'EdjiFraction';
numerator: number;
numerator?: number;
denominator: number;
equave: null | PlainLiteral | FractionLiteral;
};
Expand Down Expand Up @@ -199,7 +199,7 @@ function evaluateAst(
);
}
if (ast.type === 'EdjiFraction') {
const fractionOfEquave = new Fraction(ast.numerator, ast.denominator);
const fractionOfEquave = new Fraction(ast.numerator ?? 0, ast.denominator);
let equave: Fraction | undefined;
if (ast.equave?.type === 'PlainLiteral') {
equave = new Fraction(ast.equave.value);
Expand Down
2 changes: 1 addition & 1 deletion src/sw2.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ EquaveExpression
= '<' _ @(SlashFraction / PlainNumber) _ '>'

BackslashFraction
= numerator:Integer '\\' denominator:SignedInteger equave:EquaveExpression? { return EdjiFraction(numerator, denominator, equave) }
= numerator:Integer? '\\' denominator:SignedInteger equave:EquaveExpression? { return EdjiFraction(numerator, denominator, equave) }

Component
= $([+-]? (SlashFraction / PlainNumber))
Expand Down

0 comments on commit 199e0f2

Please sign in to comment.