interp

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2026 License: BSD-3-Clause Imports: 20 Imported by: 0

Documentation

Overview

Package interp implements an interpreter.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatStats added in v0.3.0

func FormatStats(i *Interp) string

FormatStats returns a multi-line summary of an Interp's accumulated work for the -stat CLI flag. "packages" reports bridged stdlib (Package.Bin) vs source-loaded (derived from i.Sources, since the entry-point target is not added to i.Packages while transitive non-stdlib imports are).

func ParseTraceModes added in v0.2.0

func ParseTraceModes(s string) (line, op bool)

ParseTraceModes parses a comma-separated trace-mode value (used by both MVM_TRACE and the -x CLI flag). Recognized tokens: "1" or "line" enable line tracing; "op" or "bytecode" enable bytecode tracing; "all" enables both. Unknown or empty tokens are ignored.

Types

type ExitError added in v0.3.0

type ExitError struct{ Code int }

ExitError is the error returned from Eval when interpreted code calls os.Exit, log.Fatal*, or any other path that bottoms out in the bound os.Exit stub. Callers type-assert (or errors.As) to recover Code and decide the host process exit status; treating it as a plain error is also fine for embedders that do not need the code.

func (*ExitError) CleanExit added in v0.3.0

func (e *ExitError) CleanExit()

CleanExit marks ExitError as vm.CleanExit so the VM propagates it past any interpreted recover() (mirroring Go, where recover cannot intercept os.Exit) instead of treating it as a recoverable native panic.

func (*ExitError) Error added in v0.3.0

func (e *ExitError) Error() string

type Interp

type Interp struct {
	*comp.Compiler
	*vm.Machine

	Stats Stats
	// contains filtered or unexported fields
}

Interp represents the state of an interpreter.

func NewInterpreter

func NewInterpreter(s *lang.Spec) *Interp

NewInterpreter returns a new interpreter.

func (*Interp) AutoImportPackages

func (i *Interp) AutoImportPackages()

AutoImportPackages registers every package already loaded in i.Packages under its short name, so that REPL or -e users can reference them (e.g. time.Now()) without writing an explicit import statement.

Call it after ImportPackageValues and before Eval/Repl. Explicit import statements parsed later will cleanly overwrite the pre-registered symbol.

func (*Interp) Eval

func (i *Interp) Eval(name, src string) (res reflect.Value, err error)

Eval evaluates code string and return the last produced value if any, or an error. name labels the source for error positions (a file path or any identifier).

Example

ExampleInterp_Eval shows how to embed the interpreter, expose custom Go functions to interpreted code via a synthetic package, and read back the value of an evaluated expression.

package main

import (
	"fmt"
	"reflect"

	"github.com/mvm-sh/mvm/interp"
	"github.com/mvm-sh/mvm/lang/golang"
	"github.com/mvm-sh/mvm/stdlib"

	_ "github.com/mvm-sh/mvm/stdlib/all"
)

func main() {
	i := interp.NewInterpreter(golang.GoSpec)

	// Bring in the standard library bindings (fmt, strings, ...).
	i.ImportPackageValues(stdlib.Values)

	// Expose a custom package "host" with a function and a constant.
	i.ImportPackageValues(map[string]map[string]reflect.Value{
		"host": {
			"Greet":  reflect.ValueOf(func(s string) string { return "hello, " + s + "!" }),
			"Answer": reflect.ValueOf(42),
		},
	})

	// Register every loaded package under its short name so the snippet does
	// not need explicit import statements.
	i.AutoImportPackages()

	res, err := i.Eval("m:expr", `fmt.Sprintf("%s answer=%d", host.Greet("world"), host.Answer)`)
	if err != nil {
		fmt.Println("error:", err)
		return
	}
	fmt.Println(res.Interface())
}
Output:
hello, world! answer=42

func (*Interp) FuncNames

func (i *Interp) FuncNames(prefix string) []string

FuncNames returns names of top-level functions whose name starts with prefix and whose first character after prefix is an ASCII uppercase letter, in source-declaration order.

func (*Interp) Repl

func (i *Interp) Repl(in io.Reader) (err error)

Repl executes an interactive line oriented Read Eval Print Loop (REPL).

type Stats added in v0.3.0

type Stats struct {
	CompileTime time.Duration
	RunTime     time.Duration
}

Stats accumulates timing across all Eval calls on an Interp.

Jump to

Keyboard shortcuts

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