Skip to content

Commit

Permalink
fixed #164's bug by focusing on the operator instead of the value
Browse files Browse the repository at this point in the history
  • Loading branch information
sio4 committed Oct 1, 2022
1 parent a16e005 commit 4684281
Showing 1 changed file with 29 additions and 22 deletions.
51 changes: 29 additions & 22 deletions compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,30 +451,29 @@ func (c *compiler) evalIdentifier(node *ast.Identifier) (interface{}, error) {
func (c *compiler) evalInfixExpression(node *ast.InfixExpression) (interface{}, error) {

lres, err := c.evalExpression(node.Left)
if err != nil {
if _, ok := err.(*ErrUnknownIdentifier); ok {
lres = false
} else {
return nil, err
}
}
if node.Operator == "&&" {
if !c.isTruthy(lres) {
return false, nil
}
if err != nil && node.Operator != "==" && node.Operator != "!=" {
return nil, err
} // nil lres is acceptable only for '==' and '!='

switch { // fast return
case node.Operator == "&&" && !c.isTruthy(lres):
return false, nil
case node.Operator == "||" && c.isTruthy(lres):
return true, nil
}

rres, err := c.evalExpression(node.Right)
if err != nil {
if _, ok := err.(*ErrUnknownIdentifier); !ok {
return nil, err
} else {
rres = false
}
}
if err != nil && node.Operator != "==" && node.Operator != "!=" {
return nil, err
} // nil rres is acceptable only for '==' and '!='

switch node.Operator {
case "&&", "||":
return c.boolsOperator(lres, rres, node.Operator)
return c.isTruthy(rres), nil
} // fast return or this. '&&' and '||' end here

if nil == lres || nil == rres {
return c.simpleOperator(lres, rres, node.Operator)
}

switch t := lres.(type) {
Expand All @@ -493,10 +492,7 @@ func (c *compiler) evalInfixExpression(node *ast.InfixExpression) (interface{},
return c.floatsOperator(t, r, node.Operator)
}
case bool:

return c.boolsOperator(lres, rres, node.Operator)
case nil:
return nil, nil
}
return nil, fmt.Errorf("unable to operate (%s) on %T and %T ", node.Operator, lres, rres)
}
Expand Down Expand Up @@ -605,6 +601,17 @@ func (c *compiler) stringsOperator(l string, r interface{}, op string) (interfac
return nil, fmt.Errorf("unknown operator for string %s", op)
}

func (c *compiler) simpleOperator(l interface{}, r interface{}, op string) (interface{}, error) {
switch op {
case "!=":
return l != r, nil
case "==":
return l == r, nil
default:
return nil, fmt.Errorf("unknown operator '%s' on '%T' and '%T' ", op, l, r)
}
}

func (c *compiler) evalCallExpression(node *ast.CallExpression) (interface{}, error) {
var rv reflect.Value
if node.Callee != nil {
Expand Down

0 comments on commit 4684281

Please sign in to comment.