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

feat: add support for type declarations on pointer types #1733

Merged
merged 13 commits into from
Apr 10, 2024
2 changes: 1 addition & 1 deletion gnovm/pkg/gnolang/op_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ func (m *Machine) doOpStaticTypeOf() {
m.PushOp(OpStaticTypeOf)
m.Run() // XXX replace
xt := m.ReapValues(start)[0].GetType()
if pt, ok := xt.(*PointerType); ok {
if pt, ok := baseOf(xt).(*PointerType); ok {
m.PushValue(asValue(pt.Elt))
} else if _, ok := xt.(*TypeType); ok {
m.PushValue(asValue(gTypeType))
Expand Down
2 changes: 2 additions & 0 deletions gnovm/pkg/gnolang/preprocess.go
Original file line number Diff line number Diff line change
Expand Up @@ -3096,6 +3096,8 @@ func tryPredefine(store Store, last BlockNode, d Decl) (un Name) {
t = &MapType{}
case *StructTypeExpr:
t = &StructType{}
case *StarExpr:
t = &PointerType{}
case *NameExpr:
if tv := last.GetValueRef(store, tx.Name); tv != nil {
// (file) block name
Expand Down
28 changes: 28 additions & 0 deletions gnovm/tests/files/type33.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import "fmt"

// Define a base type
type Base int

// Declare a new type that is a pointer to the base type
type PtrToBase *Base

func main() {
var b Base = 42 // Initialize a variable of the base type
var p PtrToBase = &b // Initialize a variable of the new pointer type with the address of b

fmt.Printf("The value of b is: %d\n", b)

// Using the new pointer type
fmt.Printf("The value pointed to by p is: %d\n", *p)

// Modifying the value pointed to by p
*p = 100
fmt.Printf("The new value of b after modification through p is: %d\n", b)
}

// Output:
// The value of b is: 42
// The value pointed to by p is: 42
// The new value of b after modification through p is: 100
Loading