Repository: goplus/gop
Pull request #993 (2021-12-01 00:08:14)

goprun


Patch:
diff --git a/.gitattributes b/.gitattributes
deleted file mode 100644
index b541ff26..00000000
--- a/.gitattributes
+++ /dev/null
@@ -1,5 +0,0 @@
-*.expect text eol=lf
-*.golden text eol=lf
-
-*.gop text eol=lf
-*.go text eol=lf
\ No newline at end of file
diff --git a/.github/workflows/release-build.yml b/.github/workflows/release-build.yml
deleted file mode 100644
index f2406b3b..00000000
--- a/.github/workflows/release-build.yml
+++ /dev/null
@@ -1,38 +0,0 @@
-name: Release Build
-
-on:
-  push:
-    tags:
-      - "v*.*.*"
-
-jobs:
-  build:
-    runs-on: ubuntu-latest
-    steps:
-    - uses: actions/checkout@v2
-
-    - name: Checkout tag
-      run: |
-        git fetch --depth=1 origin +refs/tags/*:refs/tags/*
-        tag_name="${GITHUB_REF##*/}"
-        echo Current tag: $tag_name
-        git checkout $tag_name
-        echo "TAG_NAME=${tag_name}" >> $GITHUB_ENV
-
-    - name: Set up Go
-      uses: actions/setup-go@v2
-      with:
-        go-version: 1.17
-
-    - name: Build
-      run: |
-        make release-all -j$(nproc)
-
-    - name: Release
-      uses: svenstaro/upload-release-action@v2
-      with:
-        repo_token: ${{ secrets.GITHUB_TOKEN }}
-        tag: ${{ env.TAG_NAME }}
-        file: ./gop-*.zip
-        file_glob: true
-        prerelease: true
\ No newline at end of file
diff --git a/Makefile b/Makefile
deleted file mode 100644
index bce14f8a..00000000
--- a/Makefile
+++ /dev/null
@@ -1,119 +0,0 @@
-NAME := gop
-PACKAGE_NAME := github.com/goplus/gop
-RELEASE_VERSION := `git describe --tags`
-COMMIT := `git rev-parse --short HEAD`
-BUILD_DATE := `date -u +"%Y-%m-%d_%H-%M-%S"`
-
-PLATFORM := linux
-BUILD_DIR=.
-BUILD_ROOT_DIR := build-dir
-BUILD_LDFLAGS := -X 'github.com/goplus/gop/env.buildDate=$(BUILD_DATE)'
-BUILD_LDFLAGS +=  -X '$(PACKAGE_NAME).buildCommit=$(COMMIT)'
-BUILD_LDFLAGS +=  -X '$(PACKAGE_NAME).buildVersion=$(RELEASE_VERSION)'
-
-EXE_SUFFIX=
-
-.PHONY: clean all
-all:
-	cd cmd; go install -v ./...
-
-clean:
-	rm -rf $(BUILD_ROOT_DIR)
-	rm -f *.zip
-	rm -f gop gopfmt
-
-
-release:
-  # the release action expects the generated binaries placed in the path where make runs.
-  # Refer: https://github.com/marketplace/actions/go-release-binaries
-	CGO_ENABLED=0 go build -o $(BUILD_DIR)/gop$(EXE_SUFFIX) -ldflags "$(BUILD_LDFLAGS)" github.com/goplus/gop/cmd/gop
-	CGO_ENABLED=0 go build -o $(BUILD_DIR)/gopfmt$(EXE_SUFFIX) -ldflags "$(BUILD_LDFLAGS)" github.com/goplus/gop/cmd/gopfmt
-
-gop:
-	mkdir -p $(BUILD_DIR)
-	$(MAKE) release
-
-release-all: darwin-amd64.zip darwin-arm64.zip linux-386.zip linux-amd64.zip \
-	linux-arm.zip linux-armv5.zip linux-armv6.zip linux-armv7.zip linux-armv8.zip \
-	linux-mips-softfloat.zip linux-mips-hardfloat.zip linux-mipsle-softfloat.zip linux-mipsle-hardfloat.zip \
-	linux-mips64.zip linux-mips64le.zip freebsd-386.zip freebsd-amd64.zip \
-	windows-386.zip windows-amd64.zip windows-arm.zip windows-armv6.zip windows-armv7.zip windows-arm64.zip
-
-%.zip: %
-	@echo "Building $(NAME)-$(RELEASE_VERSION)-$@"
-	@mkdir -p $(BUILD_ROOT_DIR)/$</bin
-	
-	@rm -f $(NAME)-$(RELEASE_VERSION)-$@
-	@cd $(BUILD_ROOT_DIR)/$</ && zip -r ../../$(NAME)-$(RELEASE_VERSION)-$@ bin
-	@zip -dur $(NAME)-$(RELEASE_VERSION)-$@ builtin go.mod go.sum LICENSE README*.md
-	@echo "$(NAME)-$(RELEASE_VERSION)-$@ Done"
-	
-
-darwin-amd64:
-	$(MAKE) GOARCH=amd64 GOOS=darwin BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-darwin-arm64:
-	$(MAKE) GOARCH=arm64 GOOS=darwin BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-386:
-	$(MAKE) GOARCH=386 GOOS=linux BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-amd64:
-	$(MAKE) GOARCH=amd64 GOOS=linux BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-arm:
-	$(MAKE) GOARCH=arm GOOS=linux BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-armv5:
-	$(MAKE) GOARCH=arm GOOS=linux GOARM=5 BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-armv6:
-	$(MAKE) GOARCH=arm GOOS=linux GOARM=6 BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-armv7:
-	$(MAKE) GOARCH=arm GOOS=linux GOARM=7 BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-armv8:
-	$(MAKE) GOARCH=arm64 GOOS=linux BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-mips-softfloat:
-	$(MAKE) GOARCH=mips GOMIPS=softfloat GOOS=linux BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-mips-hardfloat:
-	$(MAKE) GOARCH=mips GOMIPS=hardfloat GOOS=linux BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-mipsle-softfloat:
-	$(MAKE) GOARCH=mipsle GOMIPS=softfloat GOOS=linux BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-mipsle-hardfloat:
-	$(MAKE) GOARCH=mipsle GOMIPS=hardfloat GOOS=linux BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-mips64:
-	$(MAKE) GOARCH=mips64 GOOS=linux BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-linux-mips64le:
-	$(MAKE) GOARCH=mips64le GOOS=linux BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-freebsd-386:
-	$(MAKE) GOARCH=386 GOOS=freebsd BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-freebsd-amd64:
-	$(MAKE) GOARCH=amd64 GOOS=freebsd BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-windows-386:
-	$(MAKE) GOARCH=386 GOOS=windows EXE_SUFFIX=.exe BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-windows-amd64:
-	$(MAKE) GOARCH=amd64 GOOS=windows EXE_SUFFIX=.exe BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-windows-arm:
-	$(MAKE) GOARCH=arm GOOS=windows EXE_SUFFIX=.exe BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-windows-armv6:
-	$(MAKE) GOARCH=arm GOOS=windows EXE_SUFFIX=.exe GOARM=6 BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-windows-armv7:
-	$(MAKE) GOARCH=arm GOOS=windows EXE_SUFFIX=.exe GOARM=7 BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
-
-windows-arm64:
-	$(MAKE) GOARCH=arm64 GOOS=windows EXE_SUFFIX=.exe BUILD_DIR=$(BUILD_ROOT_DIR)/$@/bin release
\ No newline at end of file
diff --git a/README.md b/README.md
index 6df130a6..369fcfdd 100644
--- a/README.md
+++ b/README.md
@@ -21,7 +21,7 @@ For now, we suggest you install Go+ from source code.
 git clone https://github.com/goplus/gop.git
 cd gop
 
-# On mac/linux run:
+# On linux run:
 ./all.bash
 # On Windows run:
 all.bat
diff --git a/README_zh.md b/README_zh.md
index 93c1f8a0..03734118 100644
--- a/README_zh.md
+++ b/README_zh.md
@@ -7,11 +7,11 @@
 [![Tutorials](https://img.shields.io/badge/tutorial-Go+-blue.svg)](https://tutorial.goplus.org/)
 [![Playground](https://img.shields.io/badge/playground-Go+-blue.svg)](https://play.goplus.org/)
 [![VSCode](https://img.shields.io/badge/vscode-Go+-teal.svg)](https://github.com/gopcode/vscode-goplus)
-[![Readme](https://img.shields.io/badge/README-%E4%B8%AD%E6%96%87-teal.svg)](https://github.com/goplus/gop/blob/main/README.md)
+[![Readme](https://img.shields.io/badge/README-%E4%B8%AD%E6%96%87-teal.svg)](https://github.com/goplus/gop/blob/main/README_zh.md)
 
--   **对于工程领域而言** ：意味着大家将会使用一门简单、易学、好用的编程语言。
--   **对于 STEM 教育而言** ：掌握一门可能会在将来工作中用到的工程语言将大有裨益。
--   **对于数据科学家而言** ：意味着你们将和工程师使用同一种语言进行合作和交流。
+* **对于工程领域而言** ：意味着大家将会使用一门简单、易学、好用的编程语言。
+* **对于 STEM 教育而言** ：掌握一门可能会在将来工作中用到的工程语言将大有裨益。
+* **对于数据科学家而言** ：意味着你们将和工程师使用同一种语言进行合作和交流。
 
 ## 如何安装 Go+?
 
@@ -20,15 +20,9 @@
 ```bash
 git clone https://github.com/goplus/gop.git
 cd gop
-
-# On linux run:
 ./all.bash
-# On Windows run:
-all.bat
 ```
 
-实际上， `all.bash` 和 `all.bat` 内部都会执行 `go run cmd/install.go`
-
 ## 代码风格（很重要）
 
 -   [https://tutorial.goplus.org/hello-world](https://tutorial.goplus.org/hello-world)
@@ -39,7 +33,7 @@ all.bat
 
 -   一种静态类型语言。
 -   完全兼容 [Go 语言](https://github.com/golang/go) 。
--   类似脚本语言的风格，代码比 Go 更易读。
+-   脚本化的风格，以及比 Go 更易读的数据科学代码。
 
 例如，以下是合法的 Go+ 源代码：
 
diff --git a/cl/compile.go b/cl/compile.go
index 4bf51367..5c150ebc 100644
--- a/cl/compile.go
+++ b/cl/compile.go
@@ -560,15 +560,6 @@ func preloadFile(p *gox.Package, parent *pkgCtx, file string, f *ast.File, targe
 				}
 				for _, v := range specs {
 					spec := v.(*ast.ValueSpec)
-					if spec.Type == nil {
-						pos := ctx.Position(v.Pos())
-						ctx.handleCodeErrorf(&pos, "missing field type in class file")
-						continue
-					}
-					if len(spec.Values) > 0 {
-						pos := ctx.Position(v.Pos())
-						ctx.handleCodeErrorf(&pos, "cannot assign value to field in class file")
-					}
 					typ := toType(ctx, spec.Type)
 					for _, name := range spec.Names {
 						flds = append(flds, types.NewField(name.Pos(), pkg, name.Name, typ, false))
diff --git a/cmd/goprun/goprun.go b/cmd/goprun/goprun.go
new file mode 100644
index 00000000..dad15276
--- /dev/null
+++ b/cmd/goprun/goprun.go
@@ -0,0 +1,37 @@
+package main
+
+import (
+	"fmt"
+	"log"
+	"os"
+	"os/exec"
+
+	"github.com/goplus/gop/x/gopmod"
+)
+
+func main() {
+	if len(os.Args) < 2 {
+		fmt.Fprint(os.Stderr, "Usage: gorun <file.go> [switch ...]\n\n")
+		return
+	}
+	ctx := gopmod.New("")
+	goProj, err := ctx.OpenProject(os.Args[1])
+	if err != nil {
+		log.Println(err)
+	}
+	goProj.ExecArgs = os.Args[2:]
+	cmd := ctx.GoCommand("run", goProj)
+	cmd.Stdin = os.Stdin
+	cmd.Stdout = os.Stdout
+	cmd.Stderr = os.Stderr
+	cmd.Env = os.Environ()
+	err = cmd.Run()
+	if err != nil {
+		switch e := err.(type) {
+		case *exec.ExitError:
+			os.Exit(e.ExitCode())
+		default:
+			log.Fatalln(err)
+		}
+	}
+}
diff --git a/cmd/gorun/gorun.go b/cmd/gorun/gorun.go
deleted file mode 100644
index d311410a..00000000
--- a/cmd/gorun/gorun.go
+++ /dev/null
@@ -1,69 +0,0 @@
-package main
-
-import (
-	"crypto/sha1"
-	"fmt"
-	"log"
-	"os"
-	"os/exec"
-	"path/filepath"
-
-	"github.com/goplus/gop/x/gopmod"
-)
-
-type goFile struct {
-	file string
-	code []byte
-}
-
-func newGoProj(file string, args ...string) *gopmod.Project {
-	code, err := os.ReadFile(file)
-	if err != nil {
-		log.Fatalln(err)
-	}
-	src := &goFile{file: file, code: code}
-	return &gopmod.Project{
-		Source:        src,
-		AutoGenFile:   file,
-		FriendlyFname: filepath.Base(file),
-		ExecArgs:      args,
-	}
-}
-
-func (p *goFile) Fingerp() [20]byte { // source code fingerprint
-	return sha1.Sum(p.code)
-}
-
-func (p *goFile) IsDirty(outFile string, temp bool) bool {
-	return true
-}
-
-func (p *goFile) GenGo(outFile string) error {
-	if p.file != outFile {
-		return os.WriteFile(outFile, p.code, 0666)
-	}
-	return nil
-}
-
-func main() {
-	if len(os.Args) < 2 {
-		fmt.Fprint(os.Stderr, "Usage: gorun <file.go> [switch ...]\n\n")
-		return
-	}
-	goProj := newGoProj(os.Args[1], os.Args[2:]...)
-	ctx := gopmod.New("")
-	cmd := ctx.GoCommand("run", goProj)
-	cmd.Stdin = os.Stdin
-	cmd.Stdout = os.Stdout
-	cmd.Stderr = os.Stderr
-	cmd.Env = os.Environ()
-	err := cmd.Run()
-	if err != nil {
-		switch e := err.(type) {
-		case *exec.ExitError:
-			os.Exit(e.ExitCode())
-		default:
-			log.Fatalln(err)
-		}
-	}
-}
diff --git a/cmd/internal/clean/clean.go b/cmd/internal/clean/clean.go
index e0e896b2..cac9e97b 100644
--- a/cmd/internal/clean/clean.go
+++ b/cmd/internal/clean/clean.go
@@ -18,13 +18,12 @@ package clean
 
 import (
 	"fmt"
+	"github.com/qiniu/x/log"
 	"io/ioutil"
 	"os"
 	"path/filepath"
 	"strings"
 
-	"github.com/qiniu/x/log"
-
 	"github.com/goplus/gop/cmd/internal/base"
 )
 
@@ -84,7 +83,7 @@ func removeGopDir(dir string) {
 
 // -----------------------------------------------------------------------------
 
-// Cmd - gop clean
+// Cmd - gop build
 var Cmd = &base.Command{
 	UsageLine: "gop clean [-v] <gopSrcDir>",
 	Short:     "Clean all Go+ auto generated files",
diff --git a/cmd/internal/env/env.go b/cmd/internal/env/env.go
index bc502bab..1bba50ff 100644
--- a/cmd/internal/env/env.go
+++ b/cmd/internal/env/env.go
@@ -74,23 +74,10 @@ func runCmd(_ *base.Command, args []string) {
 	gopEnv["GOPMOD"], _ = env.GOPMOD("")
 	gopEnv["HOME"] = env.HOME()
 
+	onlyValues := false
 	vars := flag.Args()
-
-	outputEnvVars(gopEnv, vars, *envJson)
-}
-
-func outputEnvVars(gopEnv map[string]interface{}, vars []string, outputJson bool) {
-	onlyValues := true
-
-	if len(vars) == 0 {
-		onlyValues = false
-
-		vars = make([]string, 0, len(gopEnv))
-		for k := range gopEnv {
-			vars = append(vars, k)
-		}
-		sort.Strings(vars)
-	} else {
+	if len(vars) > 0 {
+		onlyValues = true
 		newEnv := make(map[string]interface{})
 		for _, v := range vars {
 			if value, ok := gopEnv[v]; ok {
@@ -102,6 +89,10 @@ func outputEnvVars(gopEnv map[string]interface{}, vars []string, outputJson bool
 		gopEnv = newEnv
 	}
 
+	outputEnvVars(gopEnv, onlyValues, *envJson)
+}
+
+func outputEnvVars(gopEnv map[string]interface{}, onlyValues bool, outputJson bool) {
 	if outputJson {
 		b, err := json.Marshal(gopEnv)
 		if err != nil {
@@ -109,10 +100,15 @@ func outputEnvVars(gopEnv map[string]interface{}, vars []string, outputJson bool
 		}
 
 		var out bytes.Buffer
-		json.Indent(&out, b, "", "\t")
+		json.Indent(&out, b, "", "  ")
 		fmt.Println(out.String())
 	} else {
-		for _, k := range vars {
+		keys := make([]string, 0, len(gopEnv))
+		for k := range gopEnv {
+			keys = append(keys, k)
+		}
+		sort.Strings(keys)
+		for _, k := range keys {
 			v := gopEnv[k]
 			if onlyValues {
 				fmt.Printf("%v\n", v)
diff --git a/cmd/internal/modload/modload.go b/cmd/internal/modload/modload.go
index cd30b4c1..ece6af3e 100644
--- a/cmd/internal/modload/modload.go
+++ b/cmd/internal/modload/modload.go
@@ -219,9 +219,6 @@ func CreateModFile(modPath string) {
 
 func Load() {
 	LoadModFile()
-	if modRoot == "" {
-		return
-	}
 	SyncGoMod()
 	if classModFile != nil && classModFile.Classfile != nil {
 		gengo.RegisterPkgFlags(classModFile.Classfile.ProjExt, gengo.PkgFlagGmx)
@@ -263,9 +260,6 @@ func fixGoVersion(fixed *bool) gomodfile.VersionFixer {
 // -mod wasn't set explicitly and automatic vendoring should be enabled.
 func LoadModFile() {
 	Init()
-	if modRoot == "" {
-		return
-	}
 	// If gop.mod does not exist, then modroot does not exist,
 	// and if go.mod exists then a copy of go.mod will be synchronized to gop.mod
 	gopmod := GopModFilePath()
diff --git a/parser/parser_gop.go b/parser/parser_gop.go
index dec7339e..9be2ad19 100644
--- a/parser/parser_gop.go
+++ b/parser/parser_gop.go
@@ -176,6 +176,30 @@ func RegisterFileType(ext string, format ast.FileType) {
 
 // -----------------------------------------------------------------------------
 
+func ParseFiles(fset *token.FileSet, files []string, mode Mode) (map[string]*ast.Package, error) {
+	return ParseFSFiles(fset, local, files, mode)
+}
+
+func ParseFSFiles(fset *token.FileSet, fs FileSystem, files []string, mode Mode) (map[string]*ast.Package, error) {
+	ret := map[string]*ast.Package{}
+	for _, file := range files {
+		f, err := ParseFSFile(fset, fs, file, nil, mode)
+		if err != nil {
+			return nil, err
+		}
+		pkgName := f.Name.Name
+		pkg, ok := ret[pkgName]
+		if !ok {
+			pkg = &ast.Package{Name: pkgName, Files: make(map[string]*ast.File)}
+			ret[pkgName] = pkg
+		}
+		pkg.Files[file] = f
+	}
+	return ret, nil
+}
+
+// -----------------------------------------------------------------------------
+
 // ParseFile parses the source code of a single Go+ source file and returns the corresponding ast.File node.
 func ParseFile(fset *token.FileSet, filename string, src interface{}, mode Mode) (f *ast.File, err error) {
 	return ParseFSFile(fset, local, filename, src, mode)
diff --git a/x/gopmod/gocmd.go b/x/gopmod/gocmd.go
index 9068e16f..e5c7be00 100644
--- a/x/gopmod/gocmd.go
+++ b/x/gopmod/gocmd.go
@@ -1,3 +1,19 @@
+/*
+ * Copyright (c) 2021 The GoPlus Authors (goplus.org). All rights reserved.
+ *
+ * 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 gopmod
 
 import (
diff --git a/x/gopmod/gofile.go b/x/gopmod/gofile.go
new file mode 100644
index 00000000..1ceb2988
--- /dev/null
+++ b/x/gopmod/gofile.go
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2021 The GoPlus Authors (goplus.org). All rights reserved.
+ *
+ * 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 gopmod
+
+import (
+	"crypto/sha1"
+	"os"
+	"path/filepath"
+)
+
+// -----------------------------------------------------------------------------
+
+type goFile struct {
+	file string
+	code []byte
+}
+
+func openFromGoFile(file string) (proj *Project, err error) {
+	code, err := os.ReadFile(file)
+	if err != nil {
+		return
+	}
+	proj = &Project{
+		Source:        &goFile{file: file, code: code},
+		AutoGenFile:   file,
+		FriendlyFname: filepath.Base(file),
+	}
+	return
+}
+
+func (p *goFile) Fingerp() [20]byte { // source code fingerprint
+	return sha1.Sum(p.code)
+}
+
+func (p *goFile) IsDirty(outFile string, temp bool) bool {
+	return true
+}
+
+func (p *goFile) GenGo(outFile, modFile string) error {
+	if p.file != outFile {
+		return os.WriteFile(outFile, p.code, 0666)
+	}
+	return nil
+}
+
+// -----------------------------------------------------------------------------
diff --git a/x/gopmod/gopfiles.go b/x/gopmod/gopfiles.go
new file mode 100644
index 00000000..7a6ebfc6
--- /dev/null
+++ b/x/gopmod/gopfiles.go
@@ -0,0 +1,104 @@
+/*
+ * Copyright (c) 2021 The GoPlus Authors (goplus.org). All rights reserved.
+ *
+ * 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 gopmod
+
+import (
+	"bytes"
+	"crypto/sha1"
+	"log"
+	"os"
+	"path/filepath"
+	"sort"
+
+	"github.com/goplus/gop/cl"
+	"github.com/goplus/gop/parser"
+	"github.com/goplus/gop/token"
+	"github.com/goplus/gox"
+)
+
+// -----------------------------------------------------------------------------
+
+type gopFiles struct {
+	files []string
+}
+
+func (p *Context) openFromGopFiles(files []string) (proj *Project, err error) {
+	proj = &Project{
+		Source: &gopFiles{files: files},
+	}
+	return
+}
+
+func (p *gopFiles) Fingerp() [20]byte { // source code fingerprint
+	files := make([]string, len(p.files))
+	for i, file := range p.files {
+		files[i], _ = filepath.Abs(file)
+	}
+	sort.Strings(files)
+	var buf bytes.Buffer
+	for _, file := range files {
+		fi, err := os.Stat(file)
+		if err != nil {
+			log.Panicln(err)
+		}
+		buf.WriteString(file)
+		buf.WriteByte('\t')
+		buf.WriteString(fi.ModTime().UTC().String())
+		buf.WriteByte('\n')
+	}
+	return sha1.Sum(buf.Bytes())
+}
+
+func (p *gopFiles) IsDirty(outFile string, temp bool) bool {
+	return true
+}
+
+const (
+	parserMode = parser.ParseComments
+)
+
+func (p *gopFiles) GenGo(outFile, modFile string) error {
+	fset := token.NewFileSet()
+	pkgs, err := parser.ParseFiles(fset, p.files, parserMode)
+	if err != nil {
+		return err
+	}
+	if len(pkgs) != 1 {
+		log.Panicln("TODO: mutli packages -", len(pkgs))
+	}
+	mainPkg, ok := pkgs["main"]
+	if !ok {
+		panic("TODO: main package not found")
+	}
+
+	srcDir, _ := filepath.Split(outFile)
+	modDir, _ := filepath.Split(modFile)
+	conf := &cl.Config{
+		Dir: modDir, TargetDir: srcDir, Fset: fset, CacheLoadPkgs: true, PersistLoadPkgs: true}
+	out, err := cl.NewPackage("", mainPkg, conf)
+	if err != nil {
+		return err
+	}
+	err = gox.WriteFile(outFile, out, false)
+	if err != nil {
+		return err
+	}
+	conf.PkgsLoader.Save()
+	return nil
+}
+
+// -----------------------------------------------------------------------------
diff --git a/x/gopmod/gopmod.go b/x/gopmod/gopmod.go
index 26281fde..38a87191 100644
--- a/x/gopmod/gopmod.go
+++ b/x/gopmod/gopmod.go
@@ -34,7 +34,7 @@ import (
 type Source interface {
 	Fingerp() [20]byte // source code fingerprint
 	IsDirty(outFile string, defctx bool) bool
-	GenGo(outFile string) error
+	GenGo(outFile, modFile string) error
 }
 
 type Project struct {
@@ -71,7 +71,7 @@ func (p *Context) GoCommand(op string, src *Project) GoCmd {
 			dir, _ := filepath.Split(out.goFile)
 			os.Mkdir(dir, 0755)
 		}
-		if err := src.GenGo(out.goFile); err != nil {
+		if err := src.GenGo(out.goFile, p.modfile); err != nil {
 			log.Panicln(err)
 		}
 	}
@@ -95,7 +95,7 @@ func (p *Context) out(src *Project) (ret goTarget) {
 	ret.outFile = dir + "g" + base64.RawURLEncoding.EncodeToString(hash[:])
 	ret.proj = src
 	ret.defctx = p.defctx
-	if ret.defctx {
+	if ret.defctx || src.AutoGenFile == "" {
 		ret.goFile = ret.outFile + fname
 	} else {
 		ret.goFile = src.AutoGenFile
diff --git a/x/gopmod/gopproj.go b/x/gopmod/gopproj.go
new file mode 100644
index 00000000..e9d80440
--- /dev/null
+++ b/x/gopmod/gopproj.go
@@ -0,0 +1,50 @@
+/*
+ * Copyright (c) 2021 The GoPlus Authors (goplus.org). All rights reserved.
+ *
+ * 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 gopmod
+
+import (
+	"os"
+	"path/filepath"
+)
+
+// -----------------------------------------------------------------------------
+
+func (p *Context) OpenProject(args ...string) (proj *Project, err error) {
+	if len(args) != 1 {
+		return p.openFromGopFiles(args)
+	}
+	src := args[0]
+	fi, err := os.Stat(src)
+	if err != nil {
+		return
+	}
+	if fi.IsDir() {
+		return p.openFromDir(src)
+	}
+	if filepath.Ext(src) == ".go" {
+		return openFromGoFile(src)
+	}
+	return p.openFromGopFiles(args)
+}
+
+// -----------------------------------------------------------------------------
+
+func (p *Context) openFromDir(dir string) (proj *Project, err error) {
+	panic("todo")
+}
+
+// -----------------------------------------------------------------------------


Test patch:
diff --git a/cl/compile_spx_test.go b/cl/compile_spx_test.go
index 04b20236..db874b23 100644
--- a/cl/compile_spx_test.go
+++ b/cl/compile_spx_test.go
@@ -73,50 +73,6 @@ func gopSpxTestEx(t *testing.T, gmx, spxcode, expected, gmxfile, spxfile string)
 	}
 }
 
-func gopSpxErrorTestEx(t *testing.T, msg, gmx, spxcode, gmxfile, spxfile string) {
-	fs := newTwoFileFS("/foo", spxfile, spxcode, gmxfile, gmx)
-	pkgs, err := parser.ParseFSDir(gblFset, fs, "/foo", nil, 0)
-	if err != nil {
-		scanner.PrintError(os.Stderr, err)
-		t.Fatal("ParseFSDir:", err)
-	}
-	conf := *baseConf.Ensure()
-	conf.NoFileLine = false
-	conf.WorkingDir = "/foo"
-	conf.TargetDir = "/foo"
-	bar := pkgs["main"]
-	_, err = cl.NewPackage("", bar, &conf)
-	if err == nil {
-		t.Fatal("no error?")
-	}
-	if ret := err.Error(); ret != msg {
-		t.Fatalf("\nError: \"%s\"\nExpected: \"%s\"\n", ret, msg)
-	}
-}
-
-func TestSpxError(t *testing.T) {
-	gopSpxErrorTestEx(t, `./Game.tgmx:4:2: cannot assign value to field in class file`, `
-var (
-	Kai Kai
-	userScore int = 100
-)
-`, `
-println "hi"
-`, "Game.tgmx", "Kai.tspx")
-
-	gopSpxErrorTestEx(t, `./Kai.tspx:3:2: missing field type in class file`, `
-var (
-	Kai Kai
-	userScore int
-)
-`, `
-var (
-	id = 100
-)
-println "hi"
-`, "Game.tgmx", "Kai.tspx")
-}
-
 func TestSpxBasic(t *testing.T) {
 	gopSpxTestEx(t, `
 func onInit() {
diff --git a/parser/parserdir_test.go b/parser/parserdir_test.go
index 4c59d526..3624a7dc 100644
--- a/parser/parserdir_test.go
+++ b/parser/parserdir_test.go
@@ -22,6 +22,7 @@ import (
 	"os"
 	"path"
 	"reflect"
+	"runtime"
 	"strings"
 	"testing"
 
@@ -54,10 +55,10 @@ func TestReadSource(t *testing.T) {
 	}
 }
 
-func TestParseFile(t *testing.T) {
+func TestParseFiles(t *testing.T) {
 	fset := token.NewFileSet()
-	if _, err := ParseFile(fset, "/foo/bar/not-exists", nil, PackageClauseOnly); err == nil {
-		t.Fatal("ParseFile failed: no error?")
+	if _, err := ParseFiles(fset, []string{"/foo/bar/not-exists"}, PackageClauseOnly); err == nil {
+		t.Fatal("ParseFiles failed: no error?")
 	}
 }
 
@@ -120,7 +121,21 @@ func TestParseGo(t *testing.T) {
 	}
 }
 
+func TestParseGoFiles(t *testing.T) {
+	fset := token.NewFileSet()
+	pkgs, err := ParseFiles(fset, []string{"./_testdata/functype/functype.go"}, Trace)
+	if err != nil {
+		t.Fatal("TestParseGoFiles: ParseFiles failed -", err)
+	}
+	if len(pkgs) != 1 {
+		t.Fatal("TestParseGoFiles failed: len(pkgs) =", len(pkgs))
+	}
+}
+
 func TestFromTestdata(t *testing.T) {
+	if runtime.GOOS == "windows" {
+		return // TODO: how to pass this testcase in windows
+	}
 	sel := ""
 	dir, err := os.Getwd()
 	if err != nil {
diff --git a/printer/gop_test.go b/printer/gop_test.go
index 13cd9703..aa776a31 100644
--- a/printer/gop_test.go
+++ b/printer/gop_test.go
@@ -21,6 +21,7 @@ import (
 	"io/ioutil"
 	"os"
 	"path/filepath"
+	"runtime"
 	"testing"
 
 	"github.com/goplus/gop/ast"
@@ -117,6 +118,9 @@ func testFrom(t *testing.T, fpath string) {
 }
 
 func TestFromTestdata(t *testing.T) {
+	if runtime.GOOS == "windows" {
+		return // TODO: how to pass this testcase in windows
+	}
 	dir, err := os.Getwd()
 	if err != nil {
 		t.Fatal("Getwd failed:", err)
@@ -135,6 +139,9 @@ func TestFromTestdata(t *testing.T) {
 }
 
 func TestFromParse(t *testing.T) {
+	if runtime.GOOS == "windows" {
+		return // TODO: how to pass this testcase in windows
+	}
 	dir, err := os.Getwd()
 	if err != nil {
 		t.Fatal("Getwd failed:", err)
diff --git a/x/mod/modfile/read_test.go b/x/mod/modfile/read_test.go
index 054013a9..c278ef07 100644
--- a/x/mod/modfile/read_test.go
+++ b/x/mod/modfile/read_test.go
@@ -23,6 +23,7 @@ import (
 	"os/exec"
 	"path/filepath"
 	"reflect"
+	"runtime"
 	"strings"
 	"testing"
 )
@@ -30,6 +31,9 @@ import (
 // Test that reading and then writing the golden files
 // does not change their output.
 func TestPrintGolden(t *testing.T) {
+	if runtime.GOOS == "windows" {
+		return // TODO: how to pass this testcase in windows
+	}
 	outs, err := filepath.Glob("testdata/*.golden")
 	if err != nil {
 		t.Fatal(err)
@@ -150,6 +154,9 @@ func TestParseLax(t *testing.T) {
 // and printed and parsed again, we get the same parse tree
 // both times.
 func TestPrintParse(t *testing.T) {
+	if runtime.GOOS == "windows" {
+		return // TODO: how to pass this testcase in windows
+	}
 	outs, err := filepath.Glob("testdata/*")
 	if err != nil {
 		t.Fatal(err)