-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
335 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ./... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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{} | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
// ----------------------------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
|
||
// ----------------------------------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
|
||
// ----------------------------------------------------------------------------- |
Oops, something went wrong.