Skip to content

Commit

Permalink
cl: support python; TestPyCall
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed May 17, 2024
1 parent 1aa8d59 commit 009d4b6
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 5 deletions.
8 changes: 7 additions & 1 deletion cl/c.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import (
// -----------------------------------------------------------------------------

const (
pathLibc = "github.com/goplus/llgo/c"
pathLibc = "github.com/goplus/llgo/c"
pathLibpy = "github.com/goplus/llgo/py"
)

func simplifyGopPackage(pkgPath string) string {
Expand All @@ -37,7 +38,12 @@ func simplifyPkgPath(pkgPath string) string {
switch pkgPath {
case "c":
return pathLibc
case "py":
return pathLibpy
default:
if strings.HasPrefix(pkgPath, "py/") {
return pathLibpy + pkgPath[2:]
}
return simplifyGopPackage(pkgPath)
}
}
Expand Down
27 changes: 26 additions & 1 deletion cl/compile_llgo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestHelloLLGo(t *testing.T) {
gopClTest(t, `
import "c"
c.Printf C"Hello, world!\n"
c.printf C"Hello, world!\n"
`, `package main
import "github.com/goplus/llgo/c"
Expand All @@ -34,3 +34,28 @@ func main() {
}
`)
}

func TestPyCall(t *testing.T) {
gopClTest(t, `
import (
"c"
"py"
"py/math"
)
x := math.sqrt(py.float(2))
c.printf C"sqrt(2) = %f\n", x.float64
`, `package main
import (
"github.com/goplus/llgo/c"
"github.com/goplus/llgo/py"
"github.com/goplus/llgo/py/math"
)
func main() {
x := math.Sqrt(py.Float(2))
c.Printf(c.Str("sqrt(2) = %f\n"), x.Float64())
}
`)
}
8 changes: 8 additions & 0 deletions testdata/_llgo/callpy/callpy.gop
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import (
"c"
"py"
"py/math"
)

x := math.sqrt(py.float(2))
c.printf C"sqrt(2) = %f\n", x.float64
2 changes: 1 addition & 1 deletion testdata/_llgo/hello/hello.gop
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import "c"

c.Printf C"Hello world\n"
c.printf C"Hello world\n"
4 changes: 2 additions & 2 deletions testdata/_llgo/hellollgo/hello.gop
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import "c"

c.Printf C"Hello, llgo!\n"
c.Fprintf c.Stderr, C"Hi, %7.1f\n", 3.14
c.printf C"Hello, llgo!\n"
c.fprintf c.Stderr, C"Hi, %7.1f\n", 3.14

0 comments on commit 009d4b6

Please sign in to comment.