Skip to content

Commit

Permalink
Propagate uint32 arguments types in ast (expr-lang#438)
Browse files Browse the repository at this point in the history
  • Loading branch information
hesining authored and antonmedv committed Sep 20, 2023
1 parent 9926c50 commit f1d1b0c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
35 changes: 29 additions & 6 deletions checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -933,8 +933,13 @@ func (v *checker) checkArguments(name string, fn reflect.Type, method bool, argu
}

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

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

if t == nil {
Expand All @@ -952,19 +957,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

0 comments on commit f1d1b0c

Please sign in to comment.