From 258e02366fe437c1a4f49edd0608f49261ca0b05 Mon Sep 17 00:00:00 2001 From: jeshecdom Date: Tue, 31 Dec 2024 15:50:23 +0100 Subject: [PATCH] Small comments and changes in conditions of == and != in interpreter. --- src/grammar/ast.ts | 3 +++ src/interpreter.ts | 32 ++++++++++++++++++++++++++++++-- 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/grammar/ast.ts b/src/grammar/ast.ts index 85e5c8c8a..455591335 100644 --- a/src/grammar/ast.ts +++ b/src/grammar/ast.ts @@ -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, diff --git a/src/interpreter.ts b/src/interpreter.ts index 07c35b60d..73138e0d5 100644 --- a/src/interpreter.ts +++ b/src/interpreter.ts @@ -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( @@ -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 "!=": { @@ -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 "&&": {