Skip to content

Commit

Permalink
feat: support logical expressions (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
prevwong authored Oct 14, 2023
1 parent 93f755e commit 6e9a337
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changeset/twenty-dragons-walk.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@rekajs/parser': patch
'@rekajs/types': patch
'@rekajs/core': patch
---

Support logical expressions
15 changes: 13 additions & 2 deletions packages/core/src/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ export const computeExpression = (
const left = computeExpression(expr.left, reka, env, ctx);
const right = computeExpression(expr.right, reka, env, ctx);

switch (expr.operator) {
const operator = expr.operator;

switch (operator) {
case '+': {
return left + right;
}
Expand Down Expand Up @@ -119,8 +121,17 @@ export const computeExpression = (
case '<=': {
return left <= right;
}
case '||': {
return left || right;
}
case '&&': {
return left && right;
}
case '??': {
return left ?? right;
}
default: {
throw new Error(`Invalid binary operator "${expr.operator}"`);
throw new Error(`Invalid binary operator "${operator}"`);
}
}
}
Expand Down
7 changes: 7 additions & 0 deletions packages/parser/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,13 @@ const jsToReka = <T extends t.ASTNode = t.ASTNode>(
case 'MemberExpression': {
return convertMemberExpression(node);
}
case 'LogicalExpression': {
return t.binaryExpression({
left: _convert(node.left),
operator: node.operator,
right: _convert(node.right),
});
}
default: {
return t.Schema.fromJSON(node) as t.Type;
}
Expand Down
2 changes: 2 additions & 0 deletions packages/types/src/generated/types.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ type BinaryExpressionParameters = {
| '>='
| '||'
| '&&'
| '??'
| '^'
| '%';
right: Expression;
Expand All @@ -216,6 +217,7 @@ export class BinaryExpression extends Expression {
| '>='
| '||'
| '&&'
| '??'
| '^'
| '%';
declare right: Expression;
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/types.definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ Schema.define('BinaryExpression', {
'>=',
'||',
'&&',
'??',
'^',
'%'
),
Expand Down

1 comment on commit 6e9a337

@vercel
Copy link

@vercel vercel bot commented on 6e9a337 Oct 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

reka – ./

reka-git-main-prevwong.vercel.app
rekajs.vercel.app
reka-prevwong.vercel.app
reka.js.org

Please sign in to comment.