igop

package module
v0.26.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2024 License: Apache-2.0 Imports: 36 Imported by: 67

README

iGo+ The Go/Go+ Interpreter

Go1.18 Go1.19 Go1.20 Go1.21 Go1.22 Go Reference

Go Version
  • Go1.18 ~ Go1.22
  • macOS Linux Windows WebAssembly GopherJS and more.
ABI

support ABI0 and ABIInternal

  • ABI0 stack-based ABI

  • ABIInternal register-based Go calling convention proposal

    • Go1.17: amd64
    • Go1.18: amd64 arm64 ppc64/ppc64le
    • Go1.19: amd64 arm64 ppc64/ppc64le riscv64
    • Go1.20: amd64 arm64 ppc64/ppc64le riscv64
    • Go1.21: amd64 arm64 ppc64/ppc64le riscv64
    • Go1.22: amd64 arm64 ppc64/ppc64le riscv64
Generics
runtime.GC

igop.Mode ExperimentalSupportGC

experimental support runtime.GC and runtime.SetFinalizer

install igop command line

Go version < 1.17:

go get -u github.com/goplus/igop/cmd/igop

Go version >= 1.17:

go install github.com/goplus/igop/cmd/igop@latest
igop command
igop             # igop repl mode
igop run         # run a Go/Go+ package
igop build       # compile a Go/Go+ package
igop test        # test a package
igop verson      # print version
igop export      # export Go package to igop builtin package
igop run mode
Usage: igop run [build flags] [package] [arguments...]
  -exp-gc
    	experimental support runtime.GC
  -mod value
    	module download mode to use: readonly, vendor, or mod.
  -ssa
    	print SSA instruction code
  -ssa-trace
    	trace SSA interpreter code
  -tags value
    	a comma-separated list of build tags to consider satisfied during the build
  -v	print the names of packages as they are compiled.
  -x	print the commands.
igop repl mode
igop                       # run repl mode, support Go/Go+
igop repl                  # run repl mode, support Go/Go+
igop repl -go              # run repl mode, disable Go+ syntax
igop test unsupport features
  • test -fuzz
  • test -cover
igop demo
go js playground (gopherjs)
go repl playground (gopherjs/wasm)
ispx

https://github.com/goplus/ispx

run simple Go source demo
package main

import (
	"github.com/goplus/igop"
	_ "github.com/goplus/igop/pkg/fmt"
)

var source = `
package main
import "fmt"
type T struct {}
func (t T) String() string {
	return "Hello, World"
}
func main() {
	fmt.Println(&T{})
}
`

func main() {
	_, err := igop.RunFile("main.go", source, nil, 0)
	if err != nil {
		panic(err)
	}
}
run simple Go+ source demo
package main

import (
	"github.com/goplus/igop"
	_ "github.com/goplus/igop/gopbuild"
)

var gopSrc string = `
fields := [
	"engineering",
	"STEM education", 
	"and data science",
]

println "The Go+ language for", fields.join(", ")
`

func main() {
	_, err := igop.RunFile("main.gop", gopSrc, nil, 0)
	if err != nil {
		panic(err)
	}
}
igop more demo

https://github.com/visualfc/igop_demo

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFoundMain    = errors.New("not found main package")
	ErrTestFailed      = errors.New("test failed")
	ErrNotFoundPackage = errors.New("not found package")
	ErrGoexitDeadlock  = errors.New("fatal error: no goroutines (main called runtime.Goexit) - deadlock!")
	ErrNoFunction      = errors.New("no function")
	ErrNoTestFiles     = errors.New("[no test files]")
)

Functions

func IcallStat added in v0.14.0

func IcallStat() (capacity int, allocate int, aviable int)

IcallStat return reflectx icall allocate stat

func IsConstNil

func IsConstNil(v ssa.Value) bool

func IsNil

func IsNil(v reflect.Value) bool

func PackageList added in v0.7.4

func PackageList() (list []string)

PackageList return all register packages

func ParseBuiltin added in v0.7.4

func ParseBuiltin(fset *token.FileSet, pkg string) (*ast.File, error)

func RegisterCustomBuiltin

func RegisterCustomBuiltin(key string, fn interface{}) error

func RegisterExternal

func RegisterExternal(key string, i interface{})

RegisterExternal is register external variable address or func

func RegisterFileProcess

func RegisterFileProcess(ext string, fn SourceProcessFunc)

func RegisterPackage

func RegisterPackage(pkg *Package)

RegisterPackage register pkg

func ResetAllIcall added in v0.14.1

func ResetAllIcall()

ResetAllIcall is reset all reflectx icall, all interp methods invalid.

func Run

func Run(path string, args []string, mode Mode) (exitCode int, err error)

func RunFile

func RunFile(filename string, src interface{}, args []string, mode Mode) (exitCode int, err error)

func RunTest

func RunTest(path string, args []string, mode Mode) error

func SetValue

func SetValue(v reflect.Value, x reflect.Value)

Types

type Context

type Context struct {
	Loader       Loader          // types loader
	BuildContext build.Context   // build context, default build.Default
	RunContext   context.Context // run context, default unset

	FileSet *token.FileSet // file set

	Lookup func(root, path string) (dir string, found bool) // lookup external import

	Mode        Mode            // mode
	BuilderMode ssa.BuilderMode // ssa builder mode
	// contains filtered or unexported fields
}

Context ssa context

func NewContext

func NewContext(mode Mode) *Context

NewContext create a new Context

func (*Context) AddImport added in v0.8.1

func (ctx *Context) AddImport(path string, dir string) (err error)

func (*Context) AddImportFile added in v0.8.4

func (ctx *Context) AddImportFile(path string, filename string, src interface{}) (err error)

func (*Context) IsEvalMode

func (ctx *Context) IsEvalMode() bool

func (*Context) LoadAstFile

func (ctx *Context) LoadAstFile(path string, file *ast.File) (*ssa.Package, error)

func (*Context) LoadAstPackage

func (ctx *Context) LoadAstPackage(path string, apkg *ast.Package) (*ssa.Package, error)

func (*Context) LoadDir

func (ctx *Context) LoadDir(dir string, test bool) (pkg *ssa.Package, err error)

func (*Context) LoadFile

func (ctx *Context) LoadFile(filename string, src interface{}) (*ssa.Package, error)

func (*Context) LoadInterp added in v0.14.0

func (ctx *Context) LoadInterp(filename string, src interface{}) (*Interp, error)

func (*Context) NewInterp

func (ctx *Context) NewInterp(mainPkg *ssa.Package) (*Interp, error)

func (*Context) ParseFile

func (ctx *Context) ParseFile(filename string, src interface{}) (*ast.File, error)

func (*Context) RegisterExternal added in v0.10.0

func (ctx *Context) RegisterExternal(key string, i interface{})

RegisterExternal register external value must variable address or func.

func (*Context) Run

func (ctx *Context) Run(path string, args []string) (exitCode int, err error)

func (*Context) RunFile

func (ctx *Context) RunFile(filename string, src interface{}, args []string) (exitCode int, err error)

func (*Context) RunFunc

func (ctx *Context) RunFunc(mainPkg *ssa.Package, fnname string, args ...Value) (ret Value, err error)

func (*Context) RunInterp added in v0.8.7

func (ctx *Context) RunInterp(interp *Interp, input string, args []string) (exitCode int, err error)

func (*Context) RunPkg

func (ctx *Context) RunPkg(mainPkg *ssa.Package, input string, args []string) (exitCode int, err error)

func (*Context) RunTest

func (ctx *Context) RunTest(dir string, args []string) error

func (*Context) SetDebug

func (ctx *Context) SetDebug(fn func(*DebugInfo))

func (*Context) SetEvalMode added in v0.8.4

func (ctx *Context) SetEvalMode(b bool)

func (*Context) SetLeastCallForEnablePool

func (ctx *Context) SetLeastCallForEnablePool(count int)

SetLeastCallForEnablePool set least call count for enable function pool, default 64

func (*Context) SetPrintOutput

func (ctx *Context) SetPrintOutput(output *bytes.Buffer)

SetPrintOutput is captured builtin print/println output

func (*Context) SetUnsafeSizes added in v0.8.9

func (ctx *Context) SetUnsafeSizes(sizes types.Sizes)

SetUnsafeSizes set the sizing functions for package unsafe.

func (*Context) TestPkg

func (ctx *Context) TestPkg(pkg *ssa.Package, input string, args []string) error

func (*Context) UnsafeRelease added in v0.14.0

func (ctx *Context) UnsafeRelease()

type DebugInfo

type DebugInfo struct {
	*ssa.DebugRef
	// contains filtered or unexported fields
}

func (*DebugInfo) AsFunc

func (i *DebugInfo) AsFunc() (*types.Func, bool)

func (*DebugInfo) AsVar

func (i *DebugInfo) AsVar() (*types.Var, interface{}, bool)

func (*DebugInfo) Position

func (i *DebugInfo) Position() token.Position

type Eval added in v0.23.0

type Eval struct {
	Value interface{} // constant.Value or interface{}
	Type  reflect.Type
}

func (*Eval) String added in v0.23.0

func (e *Eval) String() string

type ExitError added in v0.7.4

type ExitError int

func (ExitError) Error added in v0.7.4

func (r ExitError) Error() string

type FindMethod

type FindMethod interface {
	FindMethod(mtyp reflect.Type, fn *types.Func) func([]reflect.Value) []reflect.Value
}

type Importer

type Importer struct {
	// contains filtered or unexported fields
}

func NewImporter

func NewImporter(ctx *Context) *Importer

func (*Importer) Import

func (i *Importer) Import(path string) (*types.Package, error)

type Interp

type Interp struct {
	// contains filtered or unexported fields
}

func NewInterp

func NewInterp(ctx *Context, mainpkg *ssa.Package) (*Interp, error)

func (*Interp) Abort added in v0.12.1

func (i *Interp) Abort()

func (*Interp) Exit added in v0.7.4

func (i *Interp) Exit(code int)

func (*Interp) ExitCode

func (i *Interp) ExitCode() int

func (*Interp) FindMethod

func (i *Interp) FindMethod(mtyp reflect.Type, fn *types.Func) func([]reflect.Value) []reflect.Value

func (*Interp) GetConst

func (i *Interp) GetConst(key string) (constant.Value, bool)

func (*Interp) GetFunc

func (i *Interp) GetFunc(key string) (interface{}, bool)

func (*Interp) GetSymbol added in v0.7.4

func (i *Interp) GetSymbol(key string) (m ssa.Member, v interface{}, ok bool)

func (*Interp) GetType

func (i *Interp) GetType(key string) (reflect.Type, bool)

func (*Interp) GetVarAddr

func (i *Interp) GetVarAddr(key string) (interface{}, bool)

func (*Interp) IcallAlloc added in v0.14.0

func (i *Interp) IcallAlloc() int

icall allocate

func (*Interp) MainPkg added in v0.7.4

func (i *Interp) MainPkg() *ssa.Package

func (*Interp) ResetIcall added in v0.14.1

func (i *Interp) ResetIcall()

ResetIcall is reset reflectx icall, all methods invalid.

func (*Interp) RunFunc

func (i *Interp) RunFunc(name string, args ...Value) (r Value, err error)

func (*Interp) RunInit

func (i *Interp) RunInit() (err error)

func (*Interp) RunMain

func (i *Interp) RunMain() (exitCode int, err error)

func (*Interp) UnsafeRelease added in v0.14.0

func (i *Interp) UnsafeRelease()

UnsafeRelease is unsafe release interp. interp all invalid.

type Loader

type Loader interface {
	Import(path string) (*types.Package, error)
	Installed(path string) (*Package, bool)
	Packages() []*types.Package
	LookupReflect(typ types.Type) (reflect.Type, bool)
	LookupTypes(typ reflect.Type) (types.Type, bool)
	SetImport(path string, pkg *types.Package, load func() error) error
}

Loader types loader interface

func NewTypesLoader

func NewTypesLoader(ctx *Context, mode Mode) Loader

NewTypesLoader install package and readonly

type Mode

type Mode uint

Mode is a bitmask of options affecting the interpreter.

const (
	DisableRecover        Mode = 1 << iota // Disable recover() in target programs; show interpreter crash instead.
	DisableCustomBuiltin                   // Disable load custom builtin func
	EnableDumpImports                      // print import packages
	EnableDumpInstr                        // Print packages & SSA instruction code
	EnableTracing                          // Print a trace of all instructions as they are interpreted.
	EnablePrintAny                         // Enable builtin print for any type ( struct/array )
	EnableNoStrict                         // Enable no strict mode
	ExperimentalSupportGC                  // experimental support runtime.GC
	SupportMultipleInterp                  // Support multiple interp, must manual release interp reflectx icall.
	CheckGopOverloadFunc                   // Check and skip gop overload func
)

type Package

type Package struct {
	Interfaces    map[string]reflect.Type
	NamedTypes    map[string]reflect.Type
	AliasTypes    map[string]reflect.Type
	Vars          map[string]reflect.Value
	Funcs         map[string]reflect.Value
	TypedConsts   map[string]TypedConst
	UntypedConsts map[string]UntypedConst
	Deps          map[string]string // path -> name
	Name          string
	Path          string
	Source        string
}

func LookupPackage

func LookupPackage(name string) (pkg *Package, ok bool)

LookupPackage lookup register pkgs

type PanicError added in v0.13.0

type PanicError struct {
	Value value
	// contains filtered or unexported fields
}

If the target program panics, the interpreter panics with this type.

func (PanicError) Error added in v0.13.0

func (p PanicError) Error() string

func (PanicError) Stack added in v0.13.0

func (p PanicError) Stack() []byte

type Repl

type Repl struct {
	// contains filtered or unexported fields
}

func NewRepl

func NewRepl(ctx *Context) *Repl

func (*Repl) Eval

func (r *Repl) Eval(expr string) (tok token.Token, dump []*Eval, err error)

func (*Repl) Interp added in v0.7.4

func (r *Repl) Interp() *Interp

func (*Repl) SetFileName

func (r *Repl) SetFileName(filename string)

func (*Repl) Source

func (r *Repl) Source() string

type SourceProcessFunc

type SourceProcessFunc func(ctx *Context, filename string, src interface{}) ([]byte, error)

type Tuple

type Tuple = tuple

type TypedConst

type TypedConst struct {
	Typ   reflect.Type
	Value constant.Value
}

type TypesLoader

type TypesLoader struct {
	// contains filtered or unexported fields
}

func (*TypesLoader) GetPackage

func (r *TypesLoader) GetPackage(pkg string) *types.Package

func (*TypesLoader) Import

func (r *TypesLoader) Import(path string) (*types.Package, error)

func (*TypesLoader) Insert

func (r *TypesLoader) Insert(v reflect.Value)

func (*TypesLoader) InsertAlias

func (r *TypesLoader) InsertAlias(p *types.Package, name string, rt reflect.Type)

func (*TypesLoader) InsertConst

func (r *TypesLoader) InsertConst(p *types.Package, name string, typ types.Type, c constant.Value)

func (*TypesLoader) InsertFunc

func (r *TypesLoader) InsertFunc(p *types.Package, name string, v reflect.Value)

func (*TypesLoader) InsertInterface

func (r *TypesLoader) InsertInterface(p *types.Package, name string, rt reflect.Type)

func (*TypesLoader) InsertNamedType

func (r *TypesLoader) InsertNamedType(p *types.Package, name string, rt reflect.Type)

func (*TypesLoader) InsertTypedConst

func (r *TypesLoader) InsertTypedConst(p *types.Package, name string, v TypedConst)

func (*TypesLoader) InsertUntypedConst

func (r *TypesLoader) InsertUntypedConst(p *types.Package, name string, v UntypedConst)

func (*TypesLoader) InsertVar

func (r *TypesLoader) InsertVar(p *types.Package, name string, v reflect.Value)

func (*TypesLoader) Installed

func (r *TypesLoader) Installed(path string) (pkg *Package, ok bool)

func (*TypesLoader) LookupPackage

func (r *TypesLoader) LookupPackage(pkgpath string) (*types.Package, bool)

func (*TypesLoader) LookupReflect

func (r *TypesLoader) LookupReflect(typ types.Type) (reflect.Type, bool)

func (*TypesLoader) LookupType

func (r *TypesLoader) LookupType(typ string) types.Type

func (*TypesLoader) LookupTypes

func (r *TypesLoader) LookupTypes(typ reflect.Type) (types.Type, bool)

func (*TypesLoader) Packages

func (r *TypesLoader) Packages() (pkgs []*types.Package)

func (*TypesLoader) SetImport added in v0.8.1

func (r *TypesLoader) SetImport(path string, pkg *types.Package, load func() error) error

func (*TypesLoader) ToType

func (r *TypesLoader) ToType(rt reflect.Type) types.Type

type TypesRecord

type TypesRecord struct {
	// contains filtered or unexported fields
}

func NewTypesRecord

func NewTypesRecord(rctx *reflectx.Context, loader Loader, finder FindMethod, nested map[*types.Named]int) *TypesRecord

func (*TypesRecord) EnterInstance added in v0.9.8

func (r *TypesRecord) EnterInstance(fn *ssa.Function)

func (*TypesRecord) LeaveInstance added in v0.9.8

func (r *TypesRecord) LeaveInstance(fn *ssa.Function)

func (*TypesRecord) Load

func (r *TypesRecord) Load(pkg *ssa.Package)

func (*TypesRecord) LookupLocalTypes

func (r *TypesRecord) LookupLocalTypes(rt reflect.Type) (typ types.Type, ok bool)

func (*TypesRecord) LookupReflect

func (r *TypesRecord) LookupReflect(typ types.Type) (rt reflect.Type, ok bool, nested bool)

func (*TypesRecord) LookupTypes

func (r *TypesRecord) LookupTypes(rt reflect.Type) (typ types.Type, ok bool)

func (*TypesRecord) Release added in v0.14.0

func (r *TypesRecord) Release()

func (*TypesRecord) ToType

func (r *TypesRecord) ToType(typ types.Type) (reflect.Type, bool)

func (*TypesRecord) ToTypeList

func (r *TypesRecord) ToTypeList(tuple *types.Tuple) ([]reflect.Type, bool)

type UntypedConst

type UntypedConst struct {
	Typ   string
	Value constant.Value
}

type Value

type Value = value

Directories

Path Synopsis
cmd
internal/base
Package base defines shared xtype pieces of the gop command, in particular logging and the Command structure.
Package base defines shared xtype pieces of the gop command, in particular logging and the Command structure.
internal/build
Package build implements the “igop build” command.
Package build implements the “igop build” command.
internal/help
Package help implements the “igop help” command.
Package help implements the “igop help” command.
internal/run
Package run implements the “gop run” command.
Package run implements the “gop run” command.
internal/test
Package test implements the “igop test” command.
Package test implements the “igop test” command.
internal/version
Package build implements the “igop version” command.
Package build implements the “igop version” command.
pkg
cmp
fmt
io
log
net
os

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL