interp: fix comparison operators in if statement

At generation of operator closures, the comparison expression
was hard-coded instead of being derived from the operator name,
leading to a wrong result.

Fixes #1297.
This commit is contained in:
Marc Vertes
2021-11-02 15:02:07 +01:00
committed by GitHub
parent f46ef67180
commit c847481184
3 changed files with 8 additions and 6 deletions

View File

@@ -557,7 +557,7 @@ func {{$name}}(n *node) {
fnext := getExec(n.fnext)
n.exec = func(f *frame) bltn {
i1 := v1(f).Interface()
if i0 != i1 {
if i0 {{$op.Name}} i1 {
dest(f).SetBool(true)
return tnext
}
@@ -579,7 +579,7 @@ func {{$name}}(n *node) {
fnext := getExec(n.fnext)
n.exec = func(f *frame) bltn {
i0 := v0(f).Interface()
if i0 != i1 {
if i0 {{$op.Name}} i1 {
dest(f).SetBool(true)
return tnext
}
@@ -602,7 +602,7 @@ func {{$name}}(n *node) {
n.exec = func(f *frame) bltn {
i0 := v0(f).Interface()
i1 := v1(f).Interface()
if i0 != i1 {
if i0 {{$op.Name}} i1 {
dest(f).SetBool(true)
return tnext
}

View File

@@ -418,6 +418,8 @@ func TestEvalComparison(t *testing.T) {
{src: `2 > 1`, res: "true"},
{src: `1.2 > 1.1`, res: "true"},
{src: `"hhh" > "ggg"`, res: "true"},
{src: `a, b, c := 1, 1, false; if a == b { c = true }; c`, res: "true"},
{src: `a, b, c := 1, 2, false; if a != b { c = true }; c`, res: "true"},
{
desc: "mismatched types",
src: `

View File

@@ -2644,7 +2644,7 @@ func equal(n *node) {
fnext := getExec(n.fnext)
n.exec = func(f *frame) bltn {
i1 := v1(f).Interface()
if i0 != i1 {
if i0 == i1 {
dest(f).SetBool(true)
return tnext
}
@@ -2666,7 +2666,7 @@ func equal(n *node) {
fnext := getExec(n.fnext)
n.exec = func(f *frame) bltn {
i0 := v0(f).Interface()
if i0 != i1 {
if i0 == i1 {
dest(f).SetBool(true)
return tnext
}
@@ -2689,7 +2689,7 @@ func equal(n *node) {
n.exec = func(f *frame) bltn {
i0 := v0(f).Interface()
i1 := v1(f).Interface()
if i0 != i1 {
if i0 == i1 {
dest(f).SetBool(true)
return tnext
}