Skip to content

Commit

Permalink
simplify interpret
Browse files Browse the repository at this point in the history
  • Loading branch information
leonid-shutov committed Jan 23, 2025
1 parent 9b3efcb commit 4167db5
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 5 additions & 5 deletions lib/expressions.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,16 +129,16 @@ class ConditionExpression {
}
}

// eslint-disable-next-line consistent-return
interpret(context) {
for (const { condition, consequents } of this.clauses) {
if (condition.interpret(context) !== INTERPRETED_NIL) {
return consequents.reduce(
(_, consequent) => consequent.interpret(context),
undefined,
);
for (let i = 0; i < consequents.length; i++) {
const interpreted = consequents[i].interpret(context);
if (i === consequents.length - 1) return interpreted;
}
}
}
return INTERPRETED_NIL;
}

toExpression() {
Expand Down
2 changes: 1 addition & 1 deletion test/cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ test('Evaluate cond (has a match)', () => {
test('Evaluate cond (no match)', () => {
const program = '(cond ((eq x 100)(+ 6 5)(list 1 4)) ((eq y x)(list 5 7)))';
const result = evaluate(program, { x: 99, y: 98 });
const expected = undefined;
const expected = false;
assert.strictEqual(result, expected, 'cond failed');
});

0 comments on commit 4167db5

Please sign in to comment.