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

cl: EnvExpr #1810

Merged
merged 2 commits into from
Mar 9, 2024
Merged
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
3 changes: 3 additions & 0 deletions ast/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,9 @@ func Walk(v Visitor, node Node) {
Walk(v, n.Name)
walkExprList(v, n.Funcs)

case *EnvExpr:
Walk(v, n.Name)

default:
panic(fmt.Sprintf("ast.Walk: unexpected node type %T", n))
}
Expand Down
30 changes: 30 additions & 0 deletions cl/compile_spx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,36 @@ func (this *bar) Classfname() string {
`)
}

func TestEnvOp(t *testing.T) {
gopSpxTest(t, `
echo ${PATH}, $id
`, ``, `package main

import (
"fmt"
"github.com/goplus/gop/cl/internal/spx"
)

type bar struct {
spx.Sprite
*index
}
type index struct {
*spx.MyGame
}

func (this *index) MainEntry() {
fmt.Println(this.Gop_Env("PATH"), this.Gop_Env("id"))
}
func main() {
spx.Gopt_MyGame_Main(new(index))
}
func (this *bar) Classfname() string {
return "bar"
}
`)
}

func TestSpxGopEnv(t *testing.T) {
gopSpxTest(t, `
echo "${PATH}"
Expand Down
9 changes: 9 additions & 0 deletions cl/error_msg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,15 @@ func (p *[]byte) foo() {
`)
}

func TestErrEnvOp(t *testing.T) {
codeErrorTest(t, `bar.gop:2:6: operator $name undefined`, `
echo ${name}
`)
codeErrorTest(t, `bar.gop:2:1: operator $id undefined`, `
$id
`)
}

func TestErrStringLit(t *testing.T) {
codeErrorTest(t, `bar.gop:2:9: [].string undefined (type []interface{} has no field or method string)`, `
echo "${[]}"
Expand Down
21 changes: 21 additions & 0 deletions cl/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,21 @@ find:
return
}

func compileEnvExpr(ctx *blockCtx, v *ast.EnvExpr) {
cb := ctx.cb
if ctx.isClass { // in a Go+ class file
if recv := classRecv(cb); recv != nil {
if gopMember(cb, recv, "Gop_Env", v) == nil {
name := v.Name
cb.Val(name.Name, name).CallWith(1, 0, v)
return
}
}
}
invalidVal(cb)
ctx.handleErrorf(v.Pos(), "operator $%v undefined", v.Name)
}

func classRecv(cb *gogen.CodeBuilder) *types.Var {
if fn := cb.Func(); fn != nil {
sig := fn.Ancestor().Type().(*types.Signature)
Expand Down Expand Up @@ -346,6 +361,8 @@ func compileExpr(ctx *blockCtx, expr ast.Expr, inFlags ...int) {
compileErrWrapExpr(ctx, v, 0)
case *ast.FuncType:
ctx.cb.Typ(toFuncType(ctx, v, nil, nil), v)
case *ast.EnvExpr:
compileEnvExpr(ctx, v)
default:
log.Panicf("compileExpr failed: unknown - %T\n", v)
}
Expand Down Expand Up @@ -938,6 +955,10 @@ func compileBasicLit(ctx *blockCtx, v *ast.BasicLit) {
}
}

func invalidVal(cb *gogen.CodeBuilder) {
cb.Val(&gogen.Element{Type: types.Typ[types.Invalid]})
}

func basicLit(cb *gogen.CodeBuilder, v *ast.BasicLit) {
cb.Val(&goast.BasicLit{Kind: gotoken.Token(v.Kind), Value: v.Value}, v)
}
Expand Down