interpreter

package
v0.0.0-...-d60a78d Latest Latest
Warning

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

Go to latest
Published: Jul 29, 2023 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package interpreter contains basic skylark interpreter with some features that make it useful for non-trivial scripts:

  • Scripts can load other script from the file system.
  • There's one designated "interpreter root" directory that can be used to load scripts given their path relative to this root. For example, load("//some/script.sky", ...).
  • Scripts can load protobuf message descriptors (built into the interpreter binary) and instantiate protobuf messages defined there.
  • Scripts have access to some built-in skylark modules (aka 'stdlib'), supplied by whoever instantiates Interpreter.
  • All symbols from "init.sky" stdlib module are available globally to all non-stdlib scripts.

Additionally, all script have access to some predefined symbols:

`proto`, a library with protobuf helpers, see skylarkproto.ProtoLib.

def struct(**kwargs):
  """Returns an object resembling namedtuple from Python.

  kwargs will become attributes of the returned object.
  See also skylarkstruct package.

def fail(msg):
  """Aborts the script execution with an error message."""

def mutable(value=None):
  """Returns an object with get and set methods.

  Allows modules to explicitly declare that they wish to keep some
  mutable (non-frozen) state, which can be modified after the module has
  loaded (e.g. from an exported function).
  """

def to_json(value):
  """Serializes a value to compact JSON.

  Args:
    value: a skylark value: scalars, lists, tuples, dicts containing only
      skylark values.

  Returns:
    A string with its compact JSON serialization.
  """

Index

Constants

This section is empty.

Variables

View Source
var ErrNoStdlibModule = errors.New("no such stdlib module")

ErrNoStdlibModule should be returned by the stdlib loader function if the requested module doesn't exist.

Functions

This section is empty.

Types

type Interpreter

type Interpreter struct {
	// Root is a path to a root directory with scripts.
	//
	// Scripts will be able to load other scripts from this directory using
	// "//path/rel/to/root" syntax in load(...).
	//
	// Default is ".".
	Root string

	// Predeclared is a dict with predeclared symbols that are available globally.
	//
	// They are available when loading stdlib and when executing user scripts.
	Predeclared skylark.StringDict

	// Usercode knows how to load source code files from the file system.
	//
	// It takes a path relative to Root and returns its content (or an error).
	// Used by ExecFile and by load("<path>") statements inside skylark scripts.
	//
	// The default implementation justs uses io.ReadFile.
	Usercode func(path string) (string, error)

	// Stdlib is a set of scripts that constitute a builtin standard library.
	//
	// It is defined as function that takes a script path and returns its body
	// or ErrNoStdlibModule error if there's no such stdlib module.
	//
	// The stdlib is preloaded before execution of other scripts. The stdlib
	// loading starts with init.sky script that can load other stdlib scripts
	// using load("builtin:<path>", ...).
	//
	// All globals of init.sky script that do not start with '_' will become
	// available as globals in all user scripts.
	Stdlib func(path string) (string, error)

	// Logger is called by skylark's print(...) function.
	//
	// The callback takes the position in the skylark source code where print(...)
	// was called and the message it received.
	//
	// The default implementation just prints the message to stderr.
	Logger func(file string, line int, message string)
	// contains filtered or unexported fields
}

Interpreter knows how to load skylark scripts that can load other skylark scripts, instantiate protobuf messages (with descriptors compiled into the interpreter binary) and have access to some built-in standard library.

func (*Interpreter) ExecFile

func (intr *Interpreter) ExecFile(path string) (skylark.StringDict, error)

ExecFile executes a skylark script file in an environment that has all builtin symbols and all stdlib symbols.

Returns the global dict of the executed script.

func (*Interpreter) Init

func (intr *Interpreter) Init() error

Init initializes the interpreter and loads the stdlib.

Registers most basic built in symbols first (like 'struct' and 'fail'). Then executes 'init.sky' from stdlib, which may define more symbols or override already defined ones. Initializes intr.Usercode and intr.Logger if they are not set.

Whatever symbols end up in the global dict of init.sky stdlib script will become available as global symbols in all scripts executed via ExecFile (or transitively loaded by them).

Jump to

Keyboard shortcuts

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