Skip to content

Commit

Permalink
Merge pull request #116 from goplus/repl_demp
Browse files Browse the repository at this point in the history
repl: ?expr for dump expr info
  • Loading branch information
visualfc authored Jun 21, 2022
2 parents d91885e + c184d22 commit 070ddfb
Show file tree
Hide file tree
Showing 11 changed files with 698 additions and 101 deletions.
24 changes: 22 additions & 2 deletions cmd/internal/repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"io"
"runtime"
"strings"

"github.com/goplus/igop"
"github.com/goplus/igop/cmd/internal/base"
Expand Down Expand Up @@ -70,6 +71,12 @@ func runCmd(cmd *base.Command, args []string) {

// state.SetCtrlCAborts(true)
state.SetMultiLineMode(true)
state.SetCompleter(func(line string) []string {
if strings.TrimSpace(line) == "" {
return []string{line + " "}
}
return nil
})
ui := &LinerUI{state: state}
var mode igop.Mode
if flagDumpInstr {
Expand All @@ -78,7 +85,11 @@ func runCmd(cmd *base.Command, args []string) {
if flagTrace {
mode |= igop.EnableTracing
}
r := repl.NewREPL(mode)
var r *repl.REPL
igop.RegisterCustomBuiltin("exit", func() {
r.Interp().Exit(0)
})
r = repl.NewREPL(mode)
r.SetUI(ui)
if supportGoplus && flagGoplus {
r.SetFileName("main.gop")
Expand All @@ -96,6 +107,15 @@ func runCmd(cmd *base.Command, args []string) {
if line != "" {
state.AppendHistory(line)
}
r.Run(line)
err = r.Run(line)
switch e := err.(type) {
case nil:
//
case igop.ExitError:
fmt.Printf("exit %v\n", int(e))
return
default:
fmt.Printf("error: %v\n", err)
}
}
}
11 changes: 10 additions & 1 deletion errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package igop

import "errors"
import (
"errors"
"fmt"
)

var (
ErrNoPackage = errors.New("no package")
Expand All @@ -13,3 +16,9 @@ var (
ErrNoFunction = errors.New("no function")
ErrNoCustomBuiltin = errors.New("no custom builtin")
)

type ExitError int

func (r ExitError) Error() string {
return fmt.Sprintf("exit %v", int(r))
}
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ module github.com/goplus/igop
go 1.14

require (
github.com/goplus/gop v1.1.0
github.com/goplus/gox v1.11.8
github.com/goplus/gop v1.1.2
github.com/goplus/gox v1.11.12
github.com/goplus/reflectx v0.8.10
github.com/peterh/liner v1.2.2
github.com/petermattis/goid v0.0.0-20220331194723-8ee3e6ded87a
github.com/visualfc/funcval v0.1.3
github.com/visualfc/xtype v0.1.0
golang.org/x/tools v0.1.10
golang.org/x/tools v0.1.11
)
28 changes: 12 additions & 16 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/goplus/c2go v0.7.6/go.mod h1:NedKEtme4+2tPEK3IEtACPvUPm/Bq9+myFVcbxQK8yg=
github.com/goplus/gop v1.1.0 h1:DbIk7KGfp6KCBflcXxOfweNfWZWZljCEezuIUigvqjA=
github.com/goplus/gop v1.1.0/go.mod h1:mmGmU1rFKCuy658hmht6Lxj9vgb/fJTyHBwq/2AvMCE=
github.com/goplus/gox v1.11.8 h1:XVyhun8eq2gIAPpIKq+5MFoFqe+epALYUo00c66qH8g=
github.com/goplus/gox v1.11.8/go.mod h1:gu7fuQF8RmWPZUjd+tEJGuV3m/vOEv0bHrct0x/KatM=
github.com/goplus/libc v0.3.8/go.mod h1:5jwI9WTo0535XwnR5dFZuKgIXm2b2dOqulFon7C1yIE=
github.com/goplus/mod v0.9.10 h1:z7wrTT241dPSGJDeMs5uuqv/5CgXLomhOZGWEAZ+OpE=
github.com/goplus/mod v0.9.10/go.mod h1:NHU13OjeNV3ez1f+R9FLJIlIun0KNSuZb0jnmP3N3o8=
github.com/goplus/c2go v0.7.8/go.mod h1:6D9HpX+q/8/dmTT08l7sCpWyDTh22Sx3rJk83OvyVu8=
github.com/goplus/gop v1.1.2 h1:eUyYN5Ibn+GBEHkrAh80oM4OH6ePFSPGhE8R2E6R4BE=
github.com/goplus/gop v1.1.2/go.mod h1:RxPV2a2tM4gfAhdFRmELe/a2MW4XOQLf51CtufdVvbI=
github.com/goplus/gox v1.11.12 h1:zb+hNzKbUJ16tDyGlwDIHzdArRLeDz+wXaBsNXeSDew=
github.com/goplus/gox v1.11.12/go.mod h1:wRCRSNukie4cDqADF4w0Btc2Gk6V3p3V6hI5+rsVqa8=
github.com/goplus/libc v0.3.10/go.mod h1:gSQ8dVF/iKdoBn1ABeQfAF/ybaYX0pij3iPtC72FtJc=
github.com/goplus/mod v0.9.12 h1:CjgBGQIYqUTPGl3MrAS5CICzJwxbIfSa4OlEb141Gs4=
github.com/goplus/mod v0.9.12/go.mod h1:YoPIowz71rnLLROA4YG0AC8bzDtPRyMaQwgTRLr8ri4=
github.com/goplus/reflectx v0.8.10 h1:lcB+wJiT8IxfSrhpqkLPZc5TWkT/m8xMzjHVDhyD6dU=
github.com/goplus/reflectx v0.8.10/go.mod h1:XPDe5lYQ/8FN05bhqv6r1hhLxwmYIkZ5UvIkN1GNRYg=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
Expand All @@ -21,8 +21,8 @@ github.com/peterh/liner v1.2.2/go.mod h1:xFwJyiKIXJZUKItq5dGHZSTBRAuG/CpeNpWLyiN
github.com/petermattis/goid v0.0.0-20220331194723-8ee3e6ded87a h1:VXRRto5GMJPNfB7MNbUVoFhtxwoYjBEsIt/NpWg42U0=
github.com/petermattis/goid v0.0.0-20220331194723-8ee3e6ded87a/go.mod h1:pxMtw7cyUw6B2bRH0ZBANSPg+AoSud1I1iyJHI69jH4=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
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/qiniu/x v1.11.9 h1:IfQNdeNcK43Q1+b/LdrcqmWjlhxq051YVBnua8J2qN8=
github.com/qiniu/x v1.11.9/go.mod h1:03Ni9tj+N2h2aKnAz+6N0Xfl8FwMEDRC2PAlxekASDs=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/visualfc/funcval v0.1.3 h1:hvqP5bkW2jeA4cf7QNM8bz+duJECFzQmSBmMJkkIt3A=
Expand All @@ -32,7 +32,6 @@ github.com/visualfc/xtype v0.1.0/go.mod h1:VYIH9S2bmdWKlBb7c725ES6yKF9+pyHBU2SFN
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220106191415-9b9b3d81d5e3/go.mod h1:3p9vT2HGsQu2K1YbXdKPJLVgG5VJdoTa1poYQBtP1AY=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 h1:6zppjxzCulZykYSLyVDYbneBfbaBIQPYMevg0bEwv2s=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
Expand All @@ -54,9 +53,6 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.10 h1:QjFRCZxdOhBJ/UNgnBZLbNV13DlbnK0quyivTnXJM20=
golang.org/x/tools v0.1.10/go.mod h1:Uh6Zz+xoGYZom868N8YTex3t7RhtHDBrE8Gzo9bV56E=
golang.org/x/tools v0.1.11 h1:loJ25fNOEhSXfHrpoGj91eCUThwdNX6u24rO1xnNteY=
golang.org/x/tools v0.1.11/go.mod h1:SgwaegtQh8clINPpECJMqnxLv9I09HLqnW3RMqW0CA4=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
58 changes: 55 additions & 3 deletions interp.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,13 @@ import (
"go/types"
"reflect"
"runtime"
"strings"
"sync"
"sync/atomic"
"unsafe"

"github.com/visualfc/xtype"
"github.com/petermattis/goid"
"github.com/visualfc/xtype"
"golang.org/x/tools/go/ssa"
)

Expand Down Expand Up @@ -113,6 +114,10 @@ type Interp struct {
exited int32 // is call os.Exit
}

func (i *Interp) MainPkg() *ssa.Package {
return i.mainpkg
}

func (i *Interp) installed(path string) (pkg *Package, ok bool) {
pkg, ok = i.loader.Installed(path)
return
Expand Down Expand Up @@ -175,7 +180,7 @@ func (i *Interp) FindMethod(mtyp reflect.Type, fn *types.Func) func([]reflect.Va
panic(fmt.Sprintf("Not found method %v", fn))
}

func (i *Interp) makeFunc(typ reflect.Type, pfn *function, env []value) reflect.Value {
func (i *Interp) makeFunction(typ reflect.Type, pfn *function, env []value) reflect.Value {
return reflect.MakeFunc(typ, func(args []reflect.Value) []reflect.Value {
return i.callFunctionByReflect(i.tryDeferFrame(), typ, pfn, args, env)
})
Expand Down Expand Up @@ -1048,7 +1053,7 @@ func (i *Interp) GetFunc(key string) (interface{}, bool) {
if !ok {
return nil, false
}
return i.makeFunc(i.toType(fn.Type()), i.funcs[fn], nil).Interface(), true
return i.makeFunction(i.toType(fn.Type()), i.funcs[fn], nil).Interface(), true
}

func (i *Interp) GetVarAddr(key string) (interface{}, bool) {
Expand Down Expand Up @@ -1088,6 +1093,53 @@ func (i *Interp) GetType(key string) (reflect.Type, bool) {
return i.toType(t.Type()), true
}

func (i *Interp) GetSymbol(key string) (m ssa.Member, v interface{}, ok bool) {
ar := strings.Split(key, ".")
var pkg *ssa.Package
switch len(ar) {
case 1:
pkg = i.mainpkg
case 2:
pkgs := i.prog.AllPackages()
for _, p := range pkgs {
if p.Pkg.Path() == ar[0] || p.Pkg.Name() == ar[0] {
pkg = p
break
}
}
if pkg == nil {
return
}
key = ar[1]
default:
return
}
m, ok = pkg.Members[key]
if !ok {
return
}
switch p := m.(type) {
case *ssa.NamedConst:
v = p.Value.Value
case *ssa.Global:
v = i.globals[p]
case *ssa.Function:
typ := i.toType(p.Type())
v = i.makeFunction(typ, i.funcs[p], nil)
case *ssa.Type:
v = i.toType(p.Type())
}
return
}

func (i *Interp) Exit(code int) {
if i != nil && atomic.LoadInt32(&i.goexited) == 1 {
i.chexit <- code
} else {
panic(exitPanic(code))
}
}

// deref returns a pointer's element type; otherwise it returns typ.
// TODO(adonovan): Import from ssa?
func deref(typ types.Type) types.Type {
Expand Down
4 changes: 2 additions & 2 deletions opblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (p *function) regInstr(v ssa.Value) uint32 {
if v.Blocks != nil {
typ := p.Interp.preToType(v.Type())
pfn := p.Interp.loadFunction(v)
vs = p.Interp.makeFunc(typ, pfn, nil).Interface()
vs = p.Interp.makeFunction(typ, pfn, nil).Interface()
} else {
ext, ok := findExternFunc(p.Interp, v)
if !ok {
Expand Down Expand Up @@ -407,7 +407,7 @@ func makeInstr(interp *Interp, pfn *function, instr ssa.Instruction) func(fr *fr
for i, _ := range instr.Bindings {
bindings = append(bindings, fr.reg(ib[i]))
}
v := interp.makeFunc(typ, pfn, bindings)
v := interp.makeFunction(typ, pfn, bindings)
fr.setReg(ir, v.Interface())
}
case *ssa.MakeChan:
Expand Down
10 changes: 10 additions & 0 deletions package.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,22 @@ package igop
import (
"go/constant"
"reflect"
"sort"
)

var (
registerPkgs = make(map[string]*Package)
)

// PackageList return all register packages
func PackageList() (list []string) {
for pkg, _ := range registerPkgs {
list = append(list, pkg)
}
sort.Strings(list)
return
}

// LookupPackage lookup register pkgs
func LookupPackage(name string) (pkg *Package, ok bool) {
pkg, ok = registerPkgs[name]
Expand Down
Loading

0 comments on commit 070ddfb

Please sign in to comment.