Skip to content

Commit

Permalink
new engine: igo
Browse files Browse the repository at this point in the history
  • Loading branch information
xushiwei committed Oct 12, 2021
1 parent 90971c8 commit 2a7c223
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ igop - The Go+ interpreter (still in beta version)

* [yaegi](https://github.com/traefik/yaegi)
* [gomacro](https://github.com/cosmos72/gomacro)
* [igo](https://github.com/goplus/igo)

## How to build

```bash
git clone git@github.com:goplus/igop.git
cd igop
go install -tags yaegi -v ./... # you can replace `yaegi` to `gomacro` or other engines
go install -tags yaegi -v ./... # you can replace `yaegi` to `igo` or other engines
```
20 changes: 20 additions & 0 deletions cmd/igop/internal/lib/default_igo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//go:build igo
// +build igo

/*
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
43 changes: 43 additions & 0 deletions cmd/igop/internal/run/run_igo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//go:build igo
// +build igo

/*
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 (
"os"

"github.com/goplus/igo/interp"
"github.com/goplus/igop"
"github.com/qiniu/x/log"

_ "github.com/goplus/igo/lib"
)

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

func runDir(srcDir string, asm bool) {
app, err := igop.CompileDir(srcDir)
if err != nil {
log.Fatalln(err)
}
if asm {
app.Dump(os.Stdout)
return
}
interp.New().RunProgram(app)
}

// -----------------------------------------------------------------------------
5 changes: 1 addition & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ require (
github.com/cosmos72/gomacro v0.0.0-20210624153544-b4935e406a41
github.com/goplus/gop v1.0.14
github.com/goplus/gox v1.7.10
github.com/goplus/igo v0.3.0
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 @@ -4,6 +4,8 @@ github.com/goplus/gop v1.0.14 h1:tjsnvzROVe3Z1wUnAk42irwvAG5N9KR6D36mo9CaTJg=
github.com/goplus/gop v1.0.14/go.mod h1:oo5V2tps5zZxzPXqnO1ENeXOVBIjQnMRI8ZIRCQ72rk=
github.com/goplus/gox v1.7.10 h1:gjJSI7h80BfmyuuZE9YJX7xZPrd8MxipirqcI3exoyE=
github.com/goplus/gox v1.7.10/go.mod h1:orZ6Mr9qqB4BVaVCMp/cypMtkn0Fp5XUE7e5uqjZATA=
github.com/goplus/igo v0.3.0 h1:InN/Ol4xHD1/EMBEtZpB1EG1RWUccGNY75DsYrWnrsE=
github.com/goplus/igo v0.3.0/go.mod h1:uOwR7R+d95+X9zfc7W9WqpUQKNij1ITnyCPEIn3lozY=
github.com/mattn/go-runewidth v0.0.3/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-runewidth v0.0.12 h1:Y41i/hVW3Pgwr8gV+J23B9YEY0zxjptBuCWEaxmAOow=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
Expand Down
99 changes: 99 additions & 0 deletions igop_igo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//go:build igo
// +build igo

/*
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 (
"errors"
"fmt"
"go/ast"
"go/token"
"log"
"os"
"path/filepath"

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

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

func CompileDir(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 {
fmt.Fprintln(os.Stderr, err)
os.Exit(11)
}
conf.PkgsLoader.Save()
file := gox.ASTFile(out, false)
pkg := astFileToPkg(file, "gop_autogen.go")
fsetGo := token.NewFileSet()
return interp.CompileAST(pkg, fsetGo)
}

// 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
}

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

0 comments on commit 2a7c223

Please sign in to comment.