Repository: goplus/gop
Pull request #1548 (2023-11-20 03:13:51)

cl: fix compileIdent check class SelectorExpr


Patch:
diff --git a/cl/expr.go b/cl/expr.go
index 3752c652..b8dc8c84 100644
--- a/cl/expr.go
+++ b/cl/expr.go
@@ -100,7 +100,11 @@ func compileIdent(ctx *blockCtx, ident *ast.Ident, flags int) (pkg *gox.PkgRef,
 			sig := fn.Ancestor().Type().(*types.Signature)
 			if recv := sig.Recv(); recv != nil {
 				ctx.cb.Val(recv)
-				if compileMember(ctx, ident, name, flags&^clCommandWithoutArgs) == nil { // class member object
+				chkFlag := flags &^ clCommandWithoutArgs
+				if chkFlag&clIdentSelectorExpr != 0 {
+					chkFlag = clIdentCanAutoCall
+				}
+				if compileMember(ctx, ident, name, chkFlag) == nil { // class member object
 					return
 				}
 				ctx.cb.InternalStack().PopN(1)
diff --git a/cl/internal/spx/pkg/pkg.go b/cl/internal/spx/pkg/pkg.go
index 649406b5..9c77e7e7 100644
--- a/cl/internal/spx/pkg/pkg.go
+++ b/cl/internal/spx/pkg/pkg.go
@@ -5,8 +5,8 @@ var (
 )
 
 type Vector struct {
-	x int
-	y int
+	X int
+	Y int
 }
 
 func NewVector(x, y int) *Vector {
@@ -14,10 +14,14 @@ func NewVector(x, y int) *Vector {
 }
 
 func (v *Vector) Add__0(x int, y int) {
-	v.x += x
-	v.y += y
+	v.X += x
+	v.Y += y
 }
 
 func (v *Vector) Add__1(o *Vector) {
-	v.Add__0(o.x, o.y)
+	v.Add__0(o.X, o.Y)
+}
+
+func (v *Vector) Self() *Vector {
+	return v
 }
diff --git a/cl/internal/spx/sprite.go b/cl/internal/spx/sprite.go
index 00963bb8..b6b0615f 100644
--- a/cl/internal/spx/sprite.go
+++ b/cl/internal/spx/sprite.go
@@ -22,6 +22,15 @@ import (
 
 type Sprite struct {
 	pos pkg.Vector
+	Entry
+}
+
+type Entry struct {
+	vec pkg.Vector
+}
+
+func (p *Entry) Vector() *pkg.Vector {
+	return &p.vec
 }
 
 func (p *Sprite) SetCostume(costume interface{}) {


Test patch:
diff --git a/cl/compile_spx_test.go b/cl/compile_spx_test.go
index 9d387a78..cbb010e7 100644
--- a/cl/compile_spx_test.go
+++ b/cl/compile_spx_test.go
@@ -731,3 +731,64 @@ func (this *Kai) onMsg(msg string) {
 }
 `, "Game.tgmx", "Kai.tspx")
 }
+
+func TestSpxSelection(t *testing.T) {
+	gopSpxTestEx(t, `
+println "hi"
+`, `
+import "fmt"
+func onMsg(msg string) {
+	fmt.println msg
+	this.position.add 100,200
+	position.add 100,200
+	position.X += 100
+	println position.X
+	this.vector.add 100,200
+	vector.add 100,200
+	vector.X += 100
+	vector.self.X += 100
+	vector.self.Y += 200
+	vector.self.add position.X,position.Y
+	println vector.X
+	println vector.self.self
+}
+`, `package main
+
+import (
+	"fmt"
+	"github.com/goplus/gop/cl/internal/spx"
+)
+
+type Game struct {
+	*spx.MyGame
+}
+
+func (this *Game) MainEntry() {
+	fmt.Println("hi")
+}
+func main() {
+	spx.Gopt_MyGame_Main(new(Game))
+}
+
+type Kai struct {
+	spx.Sprite
+	*Game
+}
+
+func (this *Kai) onMsg(msg string) {
+	fmt.Println(msg)
+	this.Position().Add__0(100, 200)
+	this.Position().Add__0(100, 200)
+	this.Position().X += 100
+	fmt.Println(this.Position().X)
+	this.Vector().Add__0(100, 200)
+	this.Vector().Add__0(100, 200)
+	this.Vector().X += 100
+	this.Vector().Self().X += 100
+	this.Vector().Self().Y += 200
+	this.Vector().Self().Add__0(this.Position().X, this.Position().Y)
+	fmt.Println(this.Vector().X)
+	fmt.Println(this.Vector().Self().Self())
+}
+`, "Game.tgmx", "Kai.tspx")
+}