Skip to content

Commit

Permalink
Merge pull request #1548 from visualfc/class_member
Browse files Browse the repository at this point in the history
cl: fix compileIdent check class SelectorExpr
  • Loading branch information
xushiwei committed Nov 20, 2023
2 parents 257c036 + 1de53d0 commit 8f009d6
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 6 deletions.
61 changes: 61 additions & 0 deletions cl/compile_spx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
6 changes: 5 additions & 1 deletion cl/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
14 changes: 9 additions & 5 deletions cl/internal/spx/pkg/pkg.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ var (
)

type Vector struct {
x int
y int
X int
Y int
}

func NewVector(x, y int) *Vector {
return &Vector{x, y}
}

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
}
9 changes: 9 additions & 0 deletions cl/internal/spx/sprite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}) {
Expand Down

0 comments on commit 8f009d6

Please sign in to comment.