Skip to content

Commit

Permalink
fix(gnovm): add recursiv check on unary & binary operands
Browse files Browse the repository at this point in the history
  • Loading branch information
MikaelVallenet committed Aug 1, 2024
1 parent 2933d8a commit 64fe55e
Showing 1 changed file with 25 additions and 6 deletions.
31 changes: 25 additions & 6 deletions gnovm/pkg/gnolang/type_check.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,17 +215,36 @@ func assertAssignableTo(xt, dt Type, autoNative bool) {
}
}

// TODO: check operands unary & binary expr are const
func checkValConstType(d *ValueDecl) {
for _, vx := range d.Values {
switch vx.(type) {
case *BasicLitExpr, *BinaryExpr, *UnaryExpr:
// Valid constant expression
break
if d.Type != nil {
switch d.Type.(type) {
case *BasicLitExpr, *NameExpr:
// Valid constant type expression, in case of NameExpr should evaluate if underlying type is a basic type
default:
panic("const type should be a basic type")
}
}
for _, vx := range d.Values {
isBasicExpr(vx)
}
}

// Function to validate an operand, which could be another expression
// TODO: Verify the typed value linked to the expression is a constant
func isBasicExpr(expr Expr) {
switch x := expr.(type) {
case *BasicLitExpr, *NameExpr:
// Valid, as basic literals are always constant
case *BinaryExpr:
// Recursively validate the operands of the binary expression
isBasicExpr(x.Left)
isBasicExpr(x.Right)
case *UnaryExpr:
// Recursively validate the operand of the unary expression
isBasicExpr(x.X)
default:
panic("const values should be basic literals")
}
}

// checkValDefineMismatch checks for mismatch between the number of variables and values in a ValueDecl or AssignStmt.
Expand Down

0 comments on commit 64fe55e

Please sign in to comment.