Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v1.1.0-rc2 #1220

Merged
merged 18 commits into from May 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -9,7 +9,7 @@
[![VSCode](https://img.shields.io/badge/vscode-Go+-teal.svg)](https://github.com/gopcode/vscode-goplus)

* **For engineering**: working in the simplest language that can be mastered by children.
* **For STEM education**: studying an engineering language that can be used to work in the future.
* **For STEM education**: studying an engineering language that can be used for work in the future.
* **For data science**: communicating with engineers in the same language.

## How to install
Expand Down
89 changes: 89 additions & 0 deletions ast/mod/deps.go
@@ -0,0 +1,89 @@
/*
* Copyright (c) 2022 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 mod

import (
"strconv"
"strings"

goast "go/ast"
gotoken "go/token"

"github.com/goplus/gop/ast"
"github.com/goplus/gop/token"
)

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

type Deps struct {
HandlePkg func(pkgPath string)
}

func (p Deps) Load(pkg *ast.Package, withGopStd bool) {
for _, f := range pkg.Files {
p.LoadFile(f, withGopStd)
}
for _, f := range pkg.GoFiles {
p.LoadGoFile(f)
}
}

func (p Deps) LoadGoFile(f *goast.File) {
for _, imp := range f.Imports {
path := imp.Path
if path.Kind == gotoken.STRING {
if s, err := strconv.Unquote(path.Value); err == nil {
if s == "C" {
continue
}
p.HandlePkg(s)
}
}
}
}

func (p Deps) LoadFile(f *ast.File, withGopStd bool) {
for _, imp := range f.Imports {
path := imp.Path
if path.Kind == token.STRING {
if s, err := strconv.Unquote(path.Value); err == nil {
p.gopPkgPath(s, withGopStd)
}
}
}
}

func (p Deps) gopPkgPath(s string, withGopStd bool) {
if strings.HasPrefix(s, "gop/") {
if !withGopStd {
return
}
s = "github.com/goplus/gop/" + s[4:]
} else if strings.HasPrefix(s, "C") {
if len(s) == 1 {
s = "github.com/goplus/libc"
} else if s[1] == '/' {
s = s[2:]
if strings.IndexByte(s, '/') < 0 {
s = "github.com/goplus/" + s
}
}
}
p.HandlePkg(s)
}

// ----------------------------------------------------------------------------
27 changes: 0 additions & 27 deletions cl/builtin.go
Expand Up @@ -19,7 +19,6 @@ package cl
import (
"go/token"
"go/types"
"strings"

"github.com/goplus/gox"
)
Expand Down Expand Up @@ -69,29 +68,3 @@ func newBuiltinDefault(pkg gox.PkgImporter, conf *gox.Config) *types.Package {
}

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

type gopImporter struct {
gopRoot string
impFrom types.ImporterFrom
}

func newGopImporter(gopRoot string, imp types.Importer) types.Importer {
if impFrom, ok := imp.(types.ImporterFrom); ok && gopRoot != "" {
return &gopImporter{gopRoot: gopRoot, impFrom: impFrom}
}
return imp
}

func (p *gopImporter) Import(pkgPath string) (pkg *types.Package, err error) {
const (
gop = "github.com/goplus/gop"
)
if strings.HasPrefix(pkgPath, gop) {
if suffix := pkgPath[len(gop):]; suffix == "" || suffix[0] == '/' {
return p.impFrom.ImportFrom(pkgPath, p.gopRoot, 0)
}
}
return p.impFrom.Import(pkgPath)
}

// -----------------------------------------------------------------------------
6 changes: 0 additions & 6 deletions cl/builtin_test.go
Expand Up @@ -131,12 +131,6 @@ func lookupClass(ext string) (c *gopmod.Class, ok bool) {
return
}

func TestImporter(t *testing.T) {
if newGopImporter("", nil) != nil {
t.Fatal("TestImporter failed")
}
}

func TestGetGoFile(t *testing.T) {
if f := getGoFile("a_test.gop", true); f != testingGoFile {
t.Fatal("TestGetGoFile:", f)
Expand Down
10 changes: 1 addition & 9 deletions cl/compile.go
Expand Up @@ -32,7 +32,6 @@ import (
"github.com/goplus/gop/token"
"github.com/goplus/gox"
"github.com/goplus/gox/cpackages"
"github.com/goplus/gox/packages"
"github.com/goplus/mod/modfile"
)

Expand Down Expand Up @@ -88,9 +87,6 @@ type Config struct {
// TargetDir is the directory in which to generate Go files.
TargetDir string

// GopRoot specifies the Go+ root directory.
GopRoot string

// C2goBase specifies base of standard c2go packages.
// Default is github.com/goplus/.
C2goBase string
Expand Down Expand Up @@ -385,10 +381,6 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
targetDir = workingDir
}
fset := conf.Fset
imp := conf.Importer
if imp == nil {
imp = packages.NewImporter(fset, workingDir)
}
files := pkg.Files
interp := &nodeInterp{
fset: fset, files: files, workingDir: workingDir,
Expand All @@ -398,7 +390,7 @@ func NewPackage(pkgPath string, pkg *ast.Package, conf *Config) (p *gox.Package,
}
confGox := &gox.Config{
Fset: fset,
Importer: newGopImporter(conf.GopRoot, imp),
Importer: conf.Importer,
LoadNamed: ctx.loadNamed,
HandleErr: ctx.handleErr,
NodeInterpreter: interp,
Expand Down
1 change: 0 additions & 1 deletion cl/compile_test.go
Expand Up @@ -52,7 +52,6 @@ func init() {
LookupClass: lookupClass,
LookupPub: lookupPub,
C2goBase: "github.com/goplus/gop/cl/internal",
GopRoot: gopRootDir,
NoFileLine: true,
NoAutoGenMain: true,
}
Expand Down
8 changes: 5 additions & 3 deletions cl/expr.go
Expand Up @@ -652,12 +652,14 @@ func compileCompositeLitElts(ctx *blockCtx, elts []ast.Expr, kind int, expected
func compileStructLitInKeyVal(ctx *blockCtx, elts []ast.Expr, t *types.Struct, typ types.Type) {
for _, elt := range elts {
kv := elt.(*ast.KeyValueExpr)
name := kv.Key.(*ast.Ident).Name
idx := lookupField(t, name)
name := kv.Key.(*ast.Ident)
idx := lookupField(t, name.Name)
if idx >= 0 {
ctx.cb.Val(idx)
} else {
log.Panicln("TODO: struct member not found -", name)
src, pos := ctx.LoadExpr(name)
err := newCodeErrorf(&pos, "%s undefined (type %v has no field or method %s)", src, typ, name.Name)
panic(err)
}
switch expr := kv.Value.(type) {
case *ast.LambdaExpr, *ast.LambdaExpr2:
Expand Down
8 changes: 4 additions & 4 deletions cmd/internal/build/build.go
Expand Up @@ -100,17 +100,17 @@ func build(proj gopprojs.Proj, conf *gop.Config, build *gocmd.BuildConfig) {
err = gop.BuildPkgPath("", v.Path, conf, build)
case *gopprojs.FilesProj:
err = gop.BuildFiles(v.Files, conf, build)
if err != nil {
log.Panicln(err)
}
default:
log.Panicln("`gop build` doesn't support", reflect.TypeOf(v))
}
if err == syscall.ENOENT {
fmt.Fprintf(os.Stderr, "gop build %v: not found\n", obj)
} else if err != nil {
log.Panicln(err)
fmt.Fprintln(os.Stderr, err)
} else {
return
}
os.Exit(1)
}

// -----------------------------------------------------------------------------
2 changes: 1 addition & 1 deletion cmd/internal/c2go/c2go.go
Expand Up @@ -10,7 +10,7 @@ import (

// gop c2go
var Cmd = &base.Command{
UsageLine: "gop " + c2go.ShortUsage,
UsageLine: "gop c" + c2go.ShortUsage[4:],
Short: "Run c2go (convert C to Go) tools",
}

Expand Down
5 changes: 4 additions & 1 deletion cmd/internal/gengo/go.go
Expand Up @@ -18,7 +18,9 @@
package gengo

import (
"fmt"
"log"
"os"
"reflect"

"github.com/goplus/gop"
Expand Down Expand Up @@ -74,7 +76,8 @@ func runCmd(cmd *base.Command, args []string) {
log.Panicln("`gop go` doesn't support", reflect.TypeOf(v))
}
if err != nil {
log.Panicln(err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
}
Expand Down
36 changes: 22 additions & 14 deletions cmd/internal/gopget/get.go
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/goplus/gop/cmd/internal/base"
"github.com/goplus/gop/x/gopenv"
"github.com/goplus/mod/modcache"
"github.com/goplus/mod/modfetch"
"github.com/goplus/mod/modload"
)
Expand Down Expand Up @@ -60,29 +61,36 @@ func runCmd(cmd *base.Command, args []string) {
}

func get(pkgPath string) {
modBase := ""
mod, err := modload.Load(".", 0)
hasMod := (err != syscall.ENOENT)
if hasMod {
check(err)
check(mod.UpdateGoMod(gopenv.Get(), true))
modBase = mod.Path()
}
modPath, _ := splitPkgPath(pkgPath)
modVer, isClass, err := modfetch.Get(gopenv.Get(), modPath)

pkgModVer, _, err := modfetch.GetPkg(pkgPath, modBase)
check(err)
if hasMod {
if isClass {
mod.AddRegister(modVer.Path)
fmt.Fprintf(os.Stderr, "gop get: registered %s\n", modVer.Path)
}
check(mod.AddRequire(modVer.Path, modVer.Version))
fmt.Fprintf(os.Stderr, "gop get: added %s %s\n", modVer.Path, modVer.Version)
check(mod.Save())
check(mod.UpdateGoMod(gopenv.Get(), false))
if !hasMod {
return
}
}

func splitPkgPath(pkgPath string) (modPathWithVer string, pkgPathNoVer string) {
return pkgPath, pkgPath
pkgModRoot, err := modcache.Path(pkgModVer)
check(err)

pkgMod, err := modload.Load(pkgModRoot, 0)
check(err)
if pkgMod.Classfile != nil {
mod.AddRegister(pkgModVer.Path)
fmt.Fprintf(os.Stderr, "gop get: registered %s\n", pkgModVer.Path)
}

check(mod.AddRequire(pkgModVer.Path, pkgModVer.Version))
fmt.Fprintf(os.Stderr, "gop get: added %s %s\n", pkgModVer.Path, pkgModVer.Version)

check(mod.Save())
check(mod.UpdateGoMod(gopenv.Get(), false))
}

func check(err error) {
Expand Down
8 changes: 4 additions & 4 deletions cmd/internal/install/install.go
Expand Up @@ -90,17 +90,17 @@ func install(proj gopprojs.Proj, conf *gop.Config, install *gocmd.InstallConfig)
err = gop.InstallPkgPath("", v.Path, conf, install)
case *gopprojs.FilesProj:
err = gop.InstallFiles(v.Files, conf, install)
if err != nil {
log.Panicln(err)
}
default:
log.Panicln("`gop install` doesn't support", reflect.TypeOf(v))
}
if err == syscall.ENOENT {
fmt.Fprintf(os.Stderr, "gop install %v: not found\n", obj)
} else if err != nil {
log.Panicln(err)
fmt.Fprintln(os.Stderr, err)
} else {
return
}
os.Exit(1)
}

// -----------------------------------------------------------------------------
17 changes: 4 additions & 13 deletions cmd/internal/mod/init.go
Expand Up @@ -17,8 +17,6 @@
package mod

import (
"fmt"
"os"
"runtime"
"strings"

Expand Down Expand Up @@ -49,15 +47,13 @@ Run 'gop help mod init' for more information.`)
default:
fatal("gop mod init: too many arguments")
}

modPath := args[0]
mod, err := modload.Create(".", modPath, goMainVer(), env.MainVersion)
if err != nil {
fatal(err)
}
check(err)

err = mod.Save()
if err != nil {
fatal(err)
}
check(err)
}

func goMainVer() string {
Expand All @@ -70,8 +66,3 @@ func goMainVer() string {
}
return ver
}

func fatal(msg interface{}) {
fmt.Fprintln(os.Stderr, msg)
os.Exit(1)
}