Skip to content

Commit

Permalink
Add support for 'lest' and 'and' in assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
frostburn committed Apr 24, 2024
1 parent 526a792 commit 7fd5c1e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/grammars/sonic-weave.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,22 @@ ExpressionStatement
Expression
= LestExpression

AssigningOperator
= LestOperator
/ CoalescingOperator
/ ConjunctOperator
/ RoundingOperator
/ ExtremumOperator
/ AdditiveOperator
/ MiscOperator
/ MultiplicativeOperator
/ ExponentiationOperator
/ FractionOperator

LestOperator = $LestToken

LestExpression
= head: ConditionalExpression tail: (__ @$LestToken _ @LestExpression)* {
= head: ConditionalExpression tail: (__ @LestOperator _ @LestExpression)* {
return tail.reduce(operatorReducerLite, head);
}

Expand All @@ -527,27 +541,19 @@ ConditionalExpression
);
}

AssigningOperator
= CoalescingOperator
/ RoundingOperator
/ ExtremumOperator
/ AdditiveOperator
/ MiscOperator
/ MultiplicativeOperator
/ ExponentiationOperator
/ FractionOperator

CoalescingOperator = '??' / $(OrToken)

CoalescingExpression
= head: ConjunctionExpression tail: (__ @CoalescingOperator _ @ConjunctionExpression)* {
return tail.reduce(operatorReducerLite, head);
}

ConjunctOperator = $AndToken

Conjunct = NotExpression / RelationalExpression

ConjunctionExpression
= head: NotExpression tail: (__ @$AndToken _ @NotExpression)* {
= head: NotExpression tail: (__ @ConjunctOperator _ @NotExpression)* {
return tail.reduce(operatorReducerLite, head);
}

Expand Down
13 changes: 13 additions & 0 deletions src/parser/__tests__/expression.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1482,6 +1482,14 @@ describe('SonicWeave expression evaluator', () => {
expect(pi.valueOf()).toBeCloseTo(Math.PI);
});

it('has inline analogue of assignment inside try..catch', () => {
const pi = evaluate(
'let halfTau = PI;halfTau lest= fraction(halfTau);halfTau'
) as Interval;
expect(pi.value.isFractional()).toBe(false);
expect(pi.valueOf()).toBeCloseTo(Math.PI);
});

it('has atan2 with swapped arguments', () => {
const interval = evaluate('atanXY(S5, -E)') as Interval;
expect(interval.domain).toBe('linear');
Expand Down Expand Up @@ -2036,4 +2044,9 @@ describe('SonicWeave expression evaluator', () => {
'Only identifiers, array elements or record values may be incremented or decremented.'
);
});

it('supports assigning boolean and', () => {
const {fraction} = parseSingle('let foo = 1;foo and= 0;foo');
expect(fraction).toBe('0');
});
});

0 comments on commit 7fd5c1e

Please sign in to comment.