Skip to content

Commit

Permalink
Replace '??' with 'al'
Browse files Browse the repository at this point in the history
ref #283
  • Loading branch information
frostburn committed May 3, 2024
1 parent 5d3fc4b commit a1c828f
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 12 deletions.
6 changes: 3 additions & 3 deletions documentation/intermediate-dsl.md
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ In `foo() lest bar() lest baz()` execution proceeds from left to right until an
| ------------------ | ------------- | ------ |
| Logical AND | `2 and 0` | `0` |
| Logical OR | `0 or 2` | `2` |
| Nullish coalescing | `niente ?? 2` | `2` |
| Niente coalescing | `niente al 2` | `2` |

Logical operators check for *truthiness*. The falsy values are `false`, `niente`, `0`, `""` and `[]` while everything else is truthy. Note that this means that `0.0` representing zero cents or `1/1` as a linear frequency ratio is truthy.

Expand All @@ -436,8 +436,8 @@ Coalescing operators short-circuit. Execution stops once the value of the expres
false or print('This executes too')
true or print("This won't execute")

niente ?? print('This executes as well')
0 ?? print("This won't execute")
niente al print('This executes as well')
0 al print("This won't execute")
```
#### Array
Expand Down
2 changes: 1 addition & 1 deletion documentation/technical.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ All operations are left-associative except exponentiation, recipropower, and log
| `==`, `<>`, `~=`, `<=`, `>=`, `<`, `>`, `of`, `not of`, `~of`, `not ~of`, `in`, `not in`, `~in`, `not ~in` | Strict equality, size equality, comparisons, strict/non-strict value inclusion, strict/non-strict index/key inclusion |
| `not x`, `vnot x` | Boolean not, vector not |
| `and`, `vand` | Boolean and, vector and |
| `or`, `vor`, `??` | Boolean or, vector or, niente coalescing |
| `or`, `vor`, `al` | Boolean or, vector or, niente coalescing |
| `x if y else z` | Ternary conditional |
| `lest` | Fallback[^1] |

Expand Down
2 changes: 1 addition & 1 deletion src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type UpdateOperator = '++' | '--';

export type BinaryOperator =
| 'lest'
| '??'
| 'al'
| 'or'
| 'vor'
| 'and'
Expand Down
4 changes: 3 additions & 1 deletion src/grammars/sonic-weave.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
};

const RESERVED_WORDS = new Set([
'al',
'and',
'break',
'by',
Expand Down Expand Up @@ -127,6 +128,7 @@ Program
};
}

AlToken = @'al' !IdentifierPart
AndToken = @'and' !IdentifierPart
BreakToken = @'break' !IdentifierPart
ByToken = @'by' !IdentifierPart
Expand Down Expand Up @@ -608,7 +610,7 @@ ConditionalExpression
return result;
}

CoalescingOperator = '??' / OrToken / VectorOrToken
CoalescingOperator = AlToken / OrToken / VectorOrToken

CoalescingExpression
= head: ConjunctionExpression tail: (__ @CoalescingOperator _ @ConjunctionExpression)* {
Expand Down
4 changes: 2 additions & 2 deletions src/parser/__tests__/sonic-weave-ast.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -301,13 +301,13 @@ describe('SonicWeave Abstract Syntax Tree parser', () => {
});

it('parses coalescing reassignment', () => {
const ast = parseSingle('x ??= 42');
const ast = parseSingle('x al= 42');
expect(ast).toEqual({
type: 'AssignmentStatement',
name: {type: 'Identifier', id: 'x'},
value: {
type: 'BinaryExpression',
operator: '??',
operator: 'al',
left: {type: 'Identifier', id: 'x'},
right: {type: 'IntegerLiteral', value: 42n},
preferLeft: false,
Expand Down
6 changes: 3 additions & 3 deletions src/parser/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1217,7 +1217,7 @@ export class ExpressionVisitor {
case 'tmpr':
throw new Error('Tempering needs an interval and a val.');
case 'lest':
case '??':
case 'al':
case 'or':
case 'and':
case '==':
Expand Down Expand Up @@ -1334,7 +1334,7 @@ export class ExpressionVisitor {
case 'tmpr':
throw new Error('Tempering needs an interval and a val.');
case 'lest':
case '??':
case 'al':
case 'or':
case 'and':
case 'of':
Expand Down Expand Up @@ -1635,7 +1635,7 @@ export class ExpressionVisitor {
}
}
const left = this.visit(node.left);
if (operator === '??') {
if (operator === 'al') {
if (left !== undefined) {
return left;
}
Expand Down
2 changes: 1 addition & 1 deletion src/stdlib/prelude.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ riff realizeWord(word, sizes, equave = niente) {
throw "Only a single step size may be omitted.";
}
if (numMissing == 1) {
equave ??= 2;
equave al= 2;
let total = 1;
for (const [letter, count] of entries(signature)) {
if (letter == missingLetter)
Expand Down

0 comments on commit a1c828f

Please sign in to comment.