Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Propagate uint32 arguments types in ast #438

Merged
merged 9 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 30 additions & 7 deletions checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,9 +923,14 @@ func (v *checker) checkArguments(name string, fn reflect.Type, method bool, argu
in = fn.In(i + fnInOffset)
}

if isFloat(in) {
t = floatType
traverseAndReplaceIntegerNodesWithFloatNodes(&arg)
if isFloat(in) && isNumber(t) && kind(t) != kind(in) {
traverseAndReplaceIntegerNodesWithFloatNodes(&arguments[i], in)
continue
}

if isInteger(in) && isInteger(t) && kind(t) != kind(in) {
traverseAndReplaceIntegerNodesWithIntegerNodes(&arguments[i], in)
continue
}

if t == nil {
Expand All @@ -943,19 +948,37 @@ func (v *checker) checkArguments(name string, fn reflect.Type, method bool, argu
return fn.Out(0), nil
}

func traverseAndReplaceIntegerNodesWithFloatNodes(node *ast.Node) {
func traverseAndReplaceIntegerNodesWithFloatNodes(node *ast.Node, newType reflect.Type) {
switch (*node).(type) {
case *ast.IntegerNode:
*node = &ast.FloatNode{Value: float64((*node).(*ast.IntegerNode).Value)}
(*node).SetType(newType)
case *ast.UnaryNode:
unaryNode := (*node).(*ast.UnaryNode)
traverseAndReplaceIntegerNodesWithFloatNodes(&unaryNode.Node, newType)
case *ast.BinaryNode:
binaryNode := (*node).(*ast.BinaryNode)
switch binaryNode.Operator {
case "+", "-", "*":
traverseAndReplaceIntegerNodesWithFloatNodes(&binaryNode.Left, newType)
traverseAndReplaceIntegerNodesWithFloatNodes(&binaryNode.Right, newType)
}
}
}

func traverseAndReplaceIntegerNodesWithIntegerNodes(node *ast.Node, newType reflect.Type) {
switch (*node).(type) {
case *ast.IntegerNode:
(*node).SetType(newType)
case *ast.UnaryNode:
unaryNode := (*node).(*ast.UnaryNode)
traverseAndReplaceIntegerNodesWithFloatNodes(&unaryNode.Node)
traverseAndReplaceIntegerNodesWithIntegerNodes(&unaryNode.Node, newType)
case *ast.BinaryNode:
binaryNode := (*node).(*ast.BinaryNode)
switch binaryNode.Operator {
case "+", "-", "*":
traverseAndReplaceIntegerNodesWithFloatNodes(&binaryNode.Left)
traverseAndReplaceIntegerNodesWithFloatNodes(&binaryNode.Right)
traverseAndReplaceIntegerNodesWithIntegerNodes(&binaryNode.Left, newType)
traverseAndReplaceIntegerNodesWithIntegerNodes(&binaryNode.Right, newType)
}
}
}
Expand Down
12 changes: 11 additions & 1 deletion compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,17 @@ func (c *compiler) IntegerNode(node *ast.IntegerNode) {
}

func (c *compiler) FloatNode(node *ast.FloatNode) {
c.emitPush(node.Value)
t := node.Type()
if t == nil {
c.emitPush(node.Value)
return
}
switch t.Kind() {
case reflect.Float32:
c.emitPush(float32(node.Value))
case reflect.Float64:
c.emitPush(node.Value)
}
}

func (c *compiler) BoolNode(node *ast.BoolNode) {
Expand Down
29 changes: 29 additions & 0 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1969,3 +1969,32 @@ func TestMemoryBudget(t *testing.T) {
})
}
}

func TestIssue432(t *testing.T) {
env := map[string]any{
"func": func(
paramUint32 uint32,
paramUint16 uint16,
paramUint8 uint8,
paramUint uint,
paramInt32 int32,
paramInt16 int16,
paramInt8 int8,
paramInt int,
paramFloat64 float64,
paramFloat32 float32,
) float64 {
return float64(paramUint32) + float64(paramUint16) + float64(paramUint8) + float64(paramUint) +
float64(paramInt32) + float64(paramInt16) + float64(paramInt8) + float64(paramInt) +
float64(paramFloat64) + float64(paramFloat32)
},
}
code := `func(1,1,1,1,1,1,1,1,1,1)`

program, err := expr.Compile(code, expr.Env(env))
assert.NoError(t, err)

out, err := expr.Run(program, env)
assert.NoError(t, err)
assert.Equal(t, float64(10), out)
}
Loading