Skip to content

Commit

Permalink
Small comments and changes in conditions of == and != in interpreter.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeshecdom committed Dec 31, 2024
1 parent 2d6593d commit 258e023
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
3 changes: 3 additions & 0 deletions src/grammar/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,9 @@ export function __DANGER_resetNodeId() {
}

// Test equality of AstExpressions.
// Note this is syntactical equality of expressions.
// For example, two struct instances are equal if they have the same
// type and same fields in the same order.
export function eqExpressions(
ast1: AstExpression,
ast2: AstExpression,
Expand Down
32 changes: 30 additions & 2 deletions src/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,28 @@ export function ensureInt(val: AstLiteral): AstNumber {
}
}

function ensureArgumentForEquality(val: AstLiteral): AstLiteral {
switch (val.kind) {
case "address":
case "boolean":
case "cell":
case "comment_value":
case "null":
case "number":
case "simplified_string":
case "slice":
return val;
case "struct_value":
throwErrorConstEval(
`struct ${showValue(val)} cannot be argument to == operator`,
val.loc,
);
break;
default:
throwInternalCompilerError("Unrecognized ast literal kind");
}
}

function ensureRepeatInt(val: AstLiteral): AstNumber {
if (val.kind !== "number") {
throwErrorConstEval(
Expand Down Expand Up @@ -392,7 +414,10 @@ export function evalBinaryOp(
source,
);
}
const result = eqExpressions(valLeft, valR);
const valLeft_ = ensureArgumentForEquality(valLeft);
const valR_ = ensureArgumentForEquality(valR);

const result = eqExpressions(valLeft_, valR_); // Changed to equality testing (instead of ===) because cells, slices, address are equal by hashing
return makeBooleanLiteral(result, source);
}
case "!=": {
Expand All @@ -410,7 +435,10 @@ export function evalBinaryOp(
source,
);
}
const result = !eqExpressions(valLeft, valR);
const valLeft_ = ensureArgumentForEquality(valLeft);
const valR_ = ensureArgumentForEquality(valR);

const result = !eqExpressions(valLeft_, valR_); // Changed to equality testing (instead of ===) because cells, slices are equal by hashing
return makeBooleanLiteral(result, source);
}
case "&&": {
Expand Down

0 comments on commit 258e023

Please sign in to comment.