Skip to content

Commit

Permalink
Merge pull request #950 from visualfc/println2
Browse files Browse the repository at this point in the history
cl: fix print/println cmd no params call
  • Loading branch information
xushiwei authored Nov 27, 2021
2 parents cf6ddd4 + 0c9ca70 commit 3df8c50
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
23 changes: 23 additions & 0 deletions cl/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3475,3 +3475,26 @@ func (p *Point) String() string {
}
`)
}

func TestCallPrintln(t *testing.T) {
gopClTest(t, `
print
print "hello"
print("hello")
println
println "hello"
println("hello")
`, `package main
import fmt "fmt"
func main() {
fmt.Print()
fmt.Print("hello")
fmt.Print("hello")
fmt.Println()
fmt.Println("hello")
fmt.Println("hello")
}
`)
}
22 changes: 20 additions & 2 deletions cl/stmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,14 @@ func compileStmt(ctx *blockCtx, stmt ast.Stmt) {
commentStmt(ctx, stmt)
switch v := stmt.(type) {
case *ast.ExprStmt:
compileExpr(ctx, v.X)
if canAutoCall(v.X) && isFunc(ctx.cb.InternalStack().Get(-1).Type) {
if obj, ok := isBuiltinAutoCall(ctx, v.X); ok {
ctx.cb.Val(obj)
ctx.cb.Call(0)
} else {
compileExpr(ctx, v.X)
if canAutoCall(v.X) && isFunc(ctx.cb.InternalStack().Get(-1).Type) {
ctx.cb.Call(0)
}
}
case *ast.AssignStmt:
compileAssignStmt(ctx, v)
Expand Down Expand Up @@ -142,6 +147,19 @@ retry:
return false
}

func isBuiltinAutoCall(ctx *blockCtx, expr ast.Expr) (types.Object, bool) {
if ident, ok := expr.(*ast.Ident); ok {
switch ident.Name {
case "print", "println":
_, builtin := lookupType(ctx, ident.Name)
if isBuiltin(builtin) {
return builtin, true
}
}
}
return nil, false
}

func compileReturnStmt(ctx *blockCtx, expr *ast.ReturnStmt) {
var n = -1
var results *types.Tuple
Expand Down

0 comments on commit 3df8c50

Please sign in to comment.