Skip to content

Commit

Permalink
igop yaegi
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Oct 12, 2021
1 parent 14a0762 commit 153fe0d
Show file tree
Hide file tree
Showing 10 changed files with 335 additions and 47 deletions.
2 changes: 1 addition & 1 deletion all.bash
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
set -e

go install -tags gomacro -v ./...
go install -tags yaegi -v ./...
28 changes: 28 additions & 0 deletions cmd/igop/internal/lib/default_yaegi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//go:build yaegi
// +build yaegi

/*
Copyright 2021 The GoPlus Authors (goplus.org)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package lib

import (
"reflect"
)

var (
Symbols = map[string]map[string]reflect.Value{}
)
71 changes: 71 additions & 0 deletions cmd/igop/internal/run/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
Copyright 2021 The GoPlus Authors (goplus.org)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package run implements the ``igop run'' command.
package run

import (
"fmt"
"os"
"runtime"

"github.com/goplus/gop"
"github.com/goplus/igop/cmd/igop/internal/base"
"github.com/qiniu/x/log"
)

// -----------------------------------------------------------------------------

// Cmd - igop run
var Cmd = &base.Command{
UsageLine: "igop run [-version -quiet -debug] <gopSrcDir>",
Short: "Run a Go+ program",
}

var (
flag = &Cmd.Flag
flagAsm = flag.Bool("asm", false, "generates `asm` code of Go bytecode backend")
flagQuiet = flag.Bool("quiet", false, "don't generate any compiling stage log")
flagDebug = flag.Bool("debug", false, "print debug information")
flagVersion = flag.Bool("version", false, "print gop version")
)

func init() {
Cmd.Run = runCmd
}

func runCmd(cmd *base.Command, args []string) {
flag.Parse(args)
if *flagVersion {
fmt.Println("gop", gop.Version(), runtime.GOOS+"/"+runtime.GOARCH)
return
}
if flag.NArg() < 1 {
cmd.Usage(os.Stderr)
}

log.SetFlags(log.Ldefault &^ log.LstdFlags)
if *flagQuiet {
log.SetOutputLevel(0x7000)
} else if *flagDebug {
log.SetOutputLevel(log.Ldebug)
}

srcDir := flag.Arg(0)
runDir(srcDir, *flagAsm)
}

// -----------------------------------------------------------------------------
48 changes: 4 additions & 44 deletions cmd/igop/internal/run/run_gomacro.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,65 +17,25 @@
limitations under the License.
*/

// Package run implements the ``igop run'' command.
package run

import (
"fmt"
"os"
"runtime"

"github.com/cosmos72/gomacro/fast"
"github.com/goplus/gop"
"github.com/goplus/igop"
"github.com/goplus/igop/cmd/igop/internal/base"
"github.com/qiniu/x/log"
)

// -----------------------------------------------------------------------------

// Cmd - igop run
var Cmd = &base.Command{
UsageLine: "igop run [-version -quiet -debug] <gopSrcDir>",
Short: "Run a program powered by Go+ spx engine",
}

var (
flag = &Cmd.Flag
flagAsm = flag.Bool("asm", false, "generates `asm` code of Go bytecode backend")
flagQuiet = flag.Bool("quiet", false, "don't generate any compiling stage log")
flagDebug = flag.Bool("debug", false, "print debug information")
flagVersion = flag.Bool("version", false, "print gop version")
_ "github.com/goplus/igop/cmd/igop/internal/lib"
)

func init() {
Cmd.Run = runCmd
}

func runCmd(cmd *base.Command, args []string) {
flag.Parse(args)
if *flagVersion {
fmt.Println("gop", gop.Version(), runtime.GOOS+"/"+runtime.GOARCH)
return
}
if flag.NArg() < 1 {
cmd.Usage(os.Stderr)
}

log.SetFlags(log.Ldefault &^ log.LstdFlags)
if *flagQuiet {
log.SetOutputLevel(0x7000)
} else if *flagDebug {
log.SetOutputLevel(log.Ldebug)
}
// -----------------------------------------------------------------------------

srcDir := flag.Arg(0)
func runDir(srcDir string, asm bool) {
it := fast.New()
app, err := igop.CompileDir(it, srcDir)
if err != nil {
log.Fatalln(err)
}
if *flagAsm {
if asm {
panic("not impl")
}
it.RunExpr(app)
Expand Down
43 changes: 43 additions & 0 deletions cmd/igop/internal/run/run_yaegi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//go:build yaegi
// +build yaegi

/*
Copyright 2020 The GoPlus Authors (goplus.org)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package run

import (
"github.com/goplus/igop"
"github.com/goplus/igop/cmd/igop/internal/lib"
"github.com/qiniu/x/log"
"github.com/traefik/yaegi/interp"
"github.com/traefik/yaegi/stdlib"
)

// -----------------------------------------------------------------------------

func runDir(srcDir string, asm bool) {
it := interp.New(interp.Options{})
it.Use(stdlib.Symbols)
it.Use(lib.Symbols)
app, err := igop.CompileDir(it, srcDir)
if err != nil {
log.Fatalln(err)
}
if asm {
panic("not impl")
}
it.Execute(app)
}

// -----------------------------------------------------------------------------
2 changes: 0 additions & 2 deletions cmd/igop/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import (

"github.com/goplus/igop/cmd/igop/internal/base"
"github.com/goplus/igop/cmd/igop/internal/run"

_ "github.com/goplus/igop/cmd/igop/internal/lib"
)

func main() {
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ require (
github.com/goplus/gop v1.0.14
github.com/goplus/gox v1.7.10
github.com/qiniu/x v1.11.5
github.com/traefik/yaegi v0.10.0
)

replace (
github.com/traefik/yaegi => /Users/xushiwei/work/yaegi
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ github.com/qiniu/x v1.11.5 h1:TYr5cl4g2yoHAZeDK4MTjKF6CMoG+IHlCDvvM5qym6U=
github.com/qiniu/x v1.11.5/go.mod h1:03Ni9tj+N2h2aKnAz+6N0Xfl8FwMEDRC2PAlxekASDs=
github.com/rivo/uniseg v0.1.0 h1:+2KBaVoUmb9XzDsrx/Ct0W/EYOSFf/nWTauy++DprtY=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/traefik/yaegi v0.10.0 h1:c/0rhUcj5+KJhJX++eCrPeKXnJaOZ17X8gYCznU9Xxc=
github.com/traefik/yaegi v0.10.0/go.mod h1:RuCwD8/wsX7b6KoQHOaIFUfuH3gQIK4KWnFFmJMw5VA=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.4.0/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
Expand Down
101 changes: 101 additions & 0 deletions igop_yaegi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
//go:build yaegi
// +build yaegi

/*
Copyright 2021 The GoPlus Authors (goplus.org)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package igop

import (
"bytes"
"errors"
"go/token"
"log"
"os"
"path/filepath"

"github.com/goplus/gop/cl"
"github.com/goplus/gop/parser"
"github.com/goplus/gox"
"github.com/traefik/yaegi/interp"
)

// -----------------------------------------------------------------------------

func CompileDir(it *interp.Interpreter, srcDir string) (app *interp.Program, err error) {
fset := token.NewFileSet()
pkgs, err := parser.ParseDir(fset, srcDir, nil, 0)
if err != nil {
return
}
mainPkg, ok := pkgs["main"]
if !ok {
return nil, errors.New("not a main package")
}

modDir, noCacheFile := findGoModDir(srcDir)
conf := &cl.Config{
Dir: modDir, TargetDir: srcDir, Fset: fset, CacheLoadPkgs: true, PersistLoadPkgs: !noCacheFile}
out, err := cl.NewPackage("", mainPkg, conf)
if err != nil {
return
}
conf.PkgsLoader.Save()
var b bytes.Buffer
err = gox.WriteTo(&b, out, false)
if err != nil {
return
}
return it.Compile(b.String())
}

/*
// astFileToPkg translate ast.File to ast.Package
func astFileToPkg(file *ast.File, fileName string) (pkg *ast.Package) {
pkg = &ast.Package{
Name: file.Name.Name,
Files: make(map[string]*ast.File),
}
pkg.Files[fileName] = file
return
}
*/

func findGoModFile(dir string) (modfile string, noCacheFile bool, err error) {
modfile, err = cl.FindGoModFile(dir)
if err != nil {
home := os.Getenv("HOME")
modfile = home + "/gop/go.mod"
if fi, e := os.Lstat(modfile); e == nil && !fi.IsDir() {
return modfile, true, nil
}
modfile = home + "/goplus/go.mod"
if fi, e := os.Lstat(modfile); e == nil && !fi.IsDir() {
return modfile, true, nil
}
}
return
}

func findGoModDir(dir string) (string, bool) {
modfile, nocachefile, err := findGoModFile(dir)
if err != nil {
log.Fatalln("findGoModFile:", err)
}
return filepath.Dir(modfile), nocachefile
}

// -----------------------------------------------------------------------------
Loading

0 comments on commit 153fe0d

Please sign in to comment.