Skip to content

Commit

Permalink
Re-enable irrational ranges.
Browse files Browse the repository at this point in the history
Minor cleanup.
  • Loading branch information
frostburn committed May 3, 2024
1 parent a420906 commit d588937
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
7 changes: 7 additions & 0 deletions src/parser/__tests__/expression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2317,4 +2317,11 @@ describe('Poor grammar / Fun with "<"', () => {
const {fraction} = parseSingle('{defer 2; 3};$[-1]');
expect(fraction).toBe('2');
});

it('has irrational ranges', () => {
const soReal = evaluate('[E,PI..4] str');
expect(soReal).toEqual(
'[2.718281828459045r, 3.141592653589793r, 3.564903478720541r, 3.988214303851289r]'
);
});
});
25 changes: 9 additions & 16 deletions src/parser/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -854,23 +854,23 @@ export class ExpressionVisitor {
if (!(interval instanceof Interval)) {
throw new Error('Slice indices must consist of intervals.');
}
start = Number(interval.value.toBigInteger());
start = interval.toInteger();
}

if (node.end) {
const interval = this.visit(node.end);
if (!(interval instanceof Interval)) {
throw new Error('Slice indices must consist of intervals.');
}
end = Number(interval.value.toBigInteger());
end = interval.toInteger();
}

if (node.second) {
const second = this.visit(node.second);
if (!(second instanceof Interval)) {
throw new Error('Slice indices must consist of intervals.');
}
step = Number(second.value.toBigInteger()) - start;
step = second.toInteger() - start;
}

if (start < 0) {
Expand Down Expand Up @@ -1969,18 +1969,11 @@ export class ExpressionVisitor {
}
step = second.sub(start);
}
if (step.value instanceof TimeReal) {
// TODO: Re-enable.
throw new Error('Irrational ranges disabled for now.');
const stepValue = step.valueOf();
if (stepValue) {
this.spendGas(Math.abs((end.valueOf() - start.valueOf()) / stepValue));
}
if (step.value.residual.s !== 0) {
this.spendGas(
Math.abs(
(end.value.valueOf() - start.value.valueOf()) / step.value.valueOf()
)
);
}
if (step.value.residual.s > 0) {
if (stepValue > 0) {
if (start.compare(end) > 0) {
return [];
}
Expand All @@ -1991,7 +1984,7 @@ export class ExpressionVisitor {
next = next.add(step);
}
return result;
} else if (step.value.residual.s < 0) {
} else if (stepValue < 0) {
if (start.compare(end) < 0) {
return [];
}
Expand Down Expand Up @@ -2021,7 +2014,7 @@ export class ExpressionVisitor {
if (!(root instanceof Interval && end instanceof Interval)) {
throw new Error('Harmonic segments must be built from intervals.');
}
this.spendGas(Math.abs(end.value.valueOf() - root.value.valueOf()));
this.spendGas(Math.abs(end.valueOf() - root.valueOf()));
const one = linearOne();
const result: Interval[] = [];
let next = root;
Expand Down

0 comments on commit d588937

Please sign in to comment.