interp

package
v0.7.6 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2020 License: Apache-2.0 Imports: 24 Imported by: 0

Documentation

Overview

Package interp provides a complete Go interpreter.

For the Go language itself, refer to the official Go specification https://golang.org/ref/spec.

Importing packages

Packages can be imported in source or binary form, using the standard Go import statement. In source form, packages are searched first in the vendor directory, the preferred way to store source dependencies. If not found in vendor, sources modules will be searched in GOPATH. Go modules are not supported yet by yaegi.

Binary form packages are compiled and linked with the interpreter executable, and exposed to scripts with the Use method. The goexports command can be used to generate package wrappers.

Custom build tags

Custom build tags allow to control which files in imported source packages are interpreted, in the same way as the "-tags" option of the "go build" command. Setting a custom build tag spans globally for all future imports of the session.

A build tag is a line comment that begins

// yaegi:tags

that lists the build constraints to be satisfied by the further imports of source packages.

For example the following custom build tag

// yaegi:tags noasm

Will ensure that an import of a package will exclude files containing

// +build !noasm

And include files containing

// +build noasm
Example (Eval)

Generic example

package main

import (
	"fmt"
	"log"

	"github.com/containous/yaegi/interp"
)

func main() {
	// Create a new interpreter context
	i := interp.New(interp.Options{})

	// Run some code: define a new function
	_, err := i.Eval("func f(i int) int { return 2 * i }")
	if err != nil {
		log.Fatal(err)
	}

	// Access the interpreted f function with Eval
	v, err := i.Eval("f")
	if err != nil {
		log.Fatal(err)
	}

	// Returned v is a reflect.Value, so we can use its interface
	f, ok := v.Interface().(func(int) int)
	if !ok {
		log.Fatal("type assertion failed")
	}

	// Use interpreted f as it was pre-compiled
	fmt.Println(f(2))

}
Output:

4

Index

Examples

Constants

This section is empty.

Variables

View Source
var Symbols = Exports{
	// contains filtered or unexported fields
}

Symbols exposes interpreter values

Functions

This section is empty.

Types

type Exports

type Exports map[string]map[string]reflect.Value

Exports stores the map of binary packages per package path

type Interpreter

type Interpreter struct {
	Name string // program name
	// contains filtered or unexported fields
}

Interpreter contains global resources and state

func New

func New(options Options) *Interpreter

New returns a new interpreter

func (*Interpreter) Eval

func (interp *Interpreter) Eval(src string) (reflect.Value, error)

Eval evaluates Go code represented as a string. It returns a map on current interpreted package exported symbols

func (*Interpreter) EvalWithContext added in v0.7.0

func (interp *Interpreter) EvalWithContext(ctx context.Context, src string) (reflect.Value, error)

EvalWithContext evaluates Go code represented as a string. It returns a map on current interpreted package exported symbols.

func (*Interpreter) REPL added in v0.7.0

func (interp *Interpreter) REPL(in io.Reader, out io.Writer)

REPL performs a Read-Eval-Print-Loop on input reader. Results are printed on output writer.

func (*Interpreter) Repl

func (interp *Interpreter) Repl(in, out *os.File)

Repl performs a Read-Eval-Print-Loop on input file descriptor. Results are printed on output. Deprecated: use REPL instead

func (*Interpreter) Use

func (interp *Interpreter) Use(values Exports)

Use loads binary runtime symbols in the interpreter context so they can be used in interpreted code

type Options

type Options struct {
	// GoPath sets GOPATH for the interpreter
	GoPath string
	// BuildTags sets build constraints for the interpreter
	BuildTags []string
}

Options are the interpreter options.

Notes

Bugs

  • Type checking is not implemented yet.

Jump to

Keyboard shortcuts

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