Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@
custom_tests.json
pro/
test/avs/
.codebuddy/
.DS_Store
6 changes: 6 additions & 0 deletions ast/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ type IntegerNode struct {
Value int // Value of the integer.
}

// UintegerNode represents an unsigned integer literal (values > MaxInt64).
type UintegerNode struct {
base
Value uint64 // Value of the unsigned integer.
}

// FloatNode represents a float.
type FloatNode struct {
base
Expand Down
4 changes: 4 additions & 0 deletions ast/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func (n *IntegerNode) String() string {
return fmt.Sprintf("%d", n.Value)
}

func (n *UintegerNode) String() string {
return fmt.Sprintf("%d", n.Value)
}

func (n *FloatNode) String() string {
return fmt.Sprintf("%v", n.Value)
}
Expand Down
1 change: 1 addition & 0 deletions ast/visitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func Walk(node *Node, v Visitor) {
case *NilNode:
case *IdentifierNode:
case *IntegerNode:
case *UintegerNode:
case *FloatNode:
case *BoolNode:
case *StringNode:
Expand Down
8 changes: 8 additions & 0 deletions checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var (
anyType = reflect.TypeOf(new(any)).Elem()
boolType = reflect.TypeOf(true)
intType = reflect.TypeOf(0)
uint64Type = reflect.TypeOf(uint64(0))
floatType = reflect.TypeOf(float64(0))
stringType = reflect.TypeOf("")
arrayType = reflect.TypeOf([]any{})
Expand Down Expand Up @@ -189,6 +190,8 @@ func (v *Checker) visit(node ast.Node) Nature {
nt = v.identifierNode(n)
case *ast.IntegerNode:
nt = v.config.NtCache.FromType(intType)
case *ast.UintegerNode:
nt = v.config.NtCache.FromType(uint64Type)
case *ast.FloatNode:
nt = v.config.NtCache.FromType(floatType)
case *ast.BoolNode:
Expand Down Expand Up @@ -1177,6 +1180,9 @@ func traverseAndReplaceIntegerNodesWithFloatNodes(node *ast.Node, newNature Natu
case *ast.IntegerNode:
*node = &ast.FloatNode{Value: float64((*node).(*ast.IntegerNode).Value)}
(*node).SetType(newNature.Type)
case *ast.UintegerNode:
*node = &ast.FloatNode{Value: float64((*node).(*ast.UintegerNode).Value)}
(*node).SetType(newNature.Type)
case *ast.UnaryNode:
unaryNode := (*node).(*ast.UnaryNode)
traverseAndReplaceIntegerNodesWithFloatNodes(&unaryNode.Node, newNature)
Expand All @@ -1194,6 +1200,8 @@ func traverseAndReplaceIntegerNodesWithIntegerNodes(node *ast.Node, newNature Na
switch (*node).(type) {
case *ast.IntegerNode:
(*node).SetType(newNature.Type)
case *ast.UintegerNode:
(*node).SetType(newNature.Type)
case *ast.UnaryNode:
(*node).SetType(newNature.Type)
unaryNode := (*node).(*ast.UnaryNode)
Expand Down
62 changes: 62 additions & 0 deletions compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,8 @@ func (c *compiler) compile(node ast.Node) {
c.IdentifierNode(n)
case *ast.IntegerNode:
c.IntegerNode(n)
case *ast.UintegerNode:
c.UintegerNode(n)
case *ast.FloatNode:
c.FloatNode(n)
case *ast.BoolNode:
Expand Down Expand Up @@ -389,6 +391,66 @@ func (c *compiler) IntegerNode(node *ast.IntegerNode) {
}
}

func (c *compiler) UintegerNode(node *ast.UintegerNode) {
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(float64(node.Value))
case reflect.Uint:
c.emitPush(uint(node.Value))
case reflect.Uint8:
if node.Value > math.MaxUint8 {
panic(fmt.Sprintf("constant %d overflows uint8", node.Value))
}
c.emitPush(uint8(node.Value))
case reflect.Uint16:
if node.Value > math.MaxUint16 {
panic(fmt.Sprintf("constant %d overflows uint16", node.Value))
}
c.emitPush(uint16(node.Value))
case reflect.Uint32:
if node.Value > math.MaxUint32 {
panic(fmt.Sprintf("constant %d overflows uint32", node.Value))
}
c.emitPush(uint32(node.Value))
case reflect.Uint64:
c.emitPush(node.Value)
case reflect.Int:
if node.Value > uint64(math.MaxInt) {
panic(fmt.Sprintf("constant %d overflows int", node.Value))
}
c.emitPush(int(node.Value))
case reflect.Int8:
if node.Value > uint64(math.MaxInt8) {
panic(fmt.Sprintf("constant %d overflows int8", node.Value))
}
c.emitPush(int8(node.Value))
case reflect.Int16:
if node.Value > uint64(math.MaxInt16) {
panic(fmt.Sprintf("constant %d overflows int16", node.Value))
}
c.emitPush(int16(node.Value))
case reflect.Int32:
if node.Value > uint64(math.MaxInt32) {
panic(fmt.Sprintf("constant %d overflows int32", node.Value))
}
c.emitPush(int32(node.Value))
case reflect.Int64:
if node.Value > uint64(math.MaxInt64) {
panic(fmt.Sprintf("constant %d overflows int64", node.Value))
}
c.emitPush(int64(node.Value))
default:
c.emitPush(node.Value)
}
}

func (c *compiler) FloatNode(node *ast.FloatNode) {
switch node.Type().Kind() {
case reflect.Float32:
Expand Down
16 changes: 8 additions & 8 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,14 +803,14 @@ func TestExpr(t *testing.T) {
`Int + 0`,
0,
},
{
`Uint64 + 0`,
0,
},
{
`Uint64 + Int64`,
0,
},
{
`Uint64 + 0`,
uint64(0),
},
{
`Uint64 + Int64`,
uint64(0),
},
{
`Int32 + Int64`,
0,
Expand Down
Loading