slang

package
v0.1.18 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2021 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Overview

Package slang provides a simple, type checked scripting language.

The string constants Summary, Literal and Examples describe the language and are provided as exported strings to allow clients of the this package to display them at run-time.

Index

Examples

Constants

View Source
const (
	Summary = `` /* 397-byte string literal not displayed */

	Literals = `Literal values are supported as per Go's syntax for int's, float's, bool's, string's and time.Duration
`

	Examples = `` /* 226-byte string literal not displayed */

)

Variables

This section is empty.

Functions

func RegisterFunction

func RegisterFunction(fn interface{}, tag, help string, parameterAndResultNames ...string)

RegisterFunction registers a new function that can be called from a slang script and an associated 'help' describe describing its use. The names of each parameter and named results must be explicitly provided since they cannot be obtained via the reflect package. They should be provided in the order that they are defined, i.e. left-to-right, parameters first, then optionally any named results. The tag parameter can be used to group related functions for ease of retrieving/constructing documentation.

The type of registered functions must of the form:

func<Name>(Runtime, [zero-or-more-arguments]) ([zero-or-more-results] error)

For example:

func save(Runtime, filename string) (int, error)

RegisterFunction will panic if the type of the function does not meet these requirements.

Runtime provides access to the underlying environment in which the function is run and includes access to a context and various builtin functions etc.

Types

type RegisteredFunction

type RegisteredFunction struct {
	Function string
	Tag      string
	Help     string
}

RegisteredFunction represents the name and help message for a registered function.

func RegisteredFunctions

func RegisteredFunctions(tags ...string) []RegisteredFunction

RegisteredFunctions returns all currently registered functions.

type Runtime

type Runtime interface {
	// Context returns the context specified when the script was executed.
	Context() *context.T
	// Printf is like fmt.Printf.
	Printf(format string, args ...interface{})
	// ListFunctions prints a list of the currently available functions.
	ListFunctions(tags ...string)
	// Help prints the function prototype and its help message.
	Help(function string) error
	// ExpandEnv provides access to the environment variables defined and
	// the process, preferring those defined for the script.
	ExpandEnv(name string) string
	// Stdout returns the appropriate io.Writer for accessing stdout.
	Stdout() io.Writer
}

Runtime represents the set of functions available to script functions.

type Script

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

Script represents the execution of a single script.

Example
package main

import (
	"v.io/v23/context"
	"v.io/x/ref/lib/slang"
)

func main() {
	ctx, cancel := context.RootContext()
	defer cancel()
	scr := &slang.Script{}
	err := scr.ExecuteBytes(ctx, []byte(`
	s:=sprintf("hello %v", "world")
	printf("%s\n",s)
	`))
	if err != nil {
		panic(err)
	}

}
Output:

hello world

func (*Script) Context

func (scr *Script) Context() *context.T

func (*Script) ExecuteBytes

func (scr *Script) ExecuteBytes(ctx *context.T, src []byte) error

ExecuteBytes will parse, compile and execute the supplied script.

func (*Script) ExecuteFile

func (scr *Script) ExecuteFile(ctx *context.T, filename string) error

ExecuteFile will parse, compile and execute the script contained in the specified file.

func (*Script) ExpandEnv

func (scr *Script) ExpandEnv(s string) string

func (*Script) Help

func (scr *Script) Help(cmd string) error

func (*Script) ListFunctions

func (scr *Script) ListFunctions(tags ...string)

func (*Script) Printf

func (scr *Script) Printf(format string, args ...interface{})

func (*Script) RegisterConst

func (scr *Script) RegisterConst(name string, value interface{})

func (*Script) SetEnv

func (scr *Script) SetEnv(name, value string)

SetEnv sets an environment variable that will be available to the script that's preferred to any value in the processes environment.

func (*Script) SetStdout

func (scr *Script) SetStdout(stdout io.Writer)

func (*Script) Stdout

func (scr *Script) Stdout() io.Writer

func (*Script) String

func (scr *Script) String() string

String returns a string representation of the compiled script and any variables defined during its execution.

func (*Script) Variables

func (scr *Script) Variables() map[string]interface{}

Variables returns a copy of the currently defined variables.

Jump to

Keyboard shortcuts

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