Skip to content

Commit

Permalink
fix: use branch operation in || and && operators, fix storage for ! (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mvertes authored and ldez committed Jan 7, 2020
1 parent f3f54a5 commit 9a8a88d
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 1 deletion.
9 changes: 9 additions & 0 deletions _test/or0.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func main() {
c := false
println(c || !c)
}

// Output:
// true
9 changes: 9 additions & 0 deletions _test/or1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

func main() {
c := false
println(!c || c)
}

// Output:
// true
6 changes: 6 additions & 0 deletions interp/cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,9 @@ func (interp *Interpreter) cfg(root *node) ([]*node, error) {
n.child[1].tnext = n
n.typ = n.child[0].typ
n.findex = sc.add(n.typ)
if n.start.action == aNop {
n.start.gen = branch
}

case lorExpr:
n.start = n.child[0].start
Expand All @@ -1031,6 +1034,9 @@ func (interp *Interpreter) cfg(root *node) ([]*node, error) {
n.child[1].tnext = n
n.typ = n.child[0].typ
n.findex = sc.add(n.typ)
if n.start.action == aNop {
n.start.gen = branch
}

case parenExpr:
wireChild(n)
Expand Down
2 changes: 1 addition & 1 deletion interp/dot.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func (n *node) astDot(out io.Writer, name string) {
func (n *node) cfgDot(out io.Writer) {
fmt.Fprintf(out, "digraph cfg {\n")
n.Walk(nil, func(n *node) {
if n.kind == basicLit || n.kind == identExpr || n.tnext == nil {
if n.kind == basicLit || n.tnext == nil {
return
}
var label string
Expand Down
3 changes: 3 additions & 0 deletions interp/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,13 +345,16 @@ func not(n *node) {
dest := genValue(n)
value := genValue(n.child[0])
tnext := getExec(n.tnext)
i := n.findex

if n.fnext != nil {
fnext := getExec(n.fnext)
n.exec = func(f *frame) bltn {
if !value(f).Bool() {
f.data[i].SetBool(true)
return tnext
}
f.data[i].SetBool(false)
return fnext
}
} else {
Expand Down

0 comments on commit 9a8a88d

Please sign in to comment.