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 ¶
- Constants
- func RegisterFunction(fn interface{}, tag, help string, parameterAndResultNames ...string)
- type RegisteredFunction
- type Runtime
- type Script
- func (scr *Script) Context() *context.T
- func (scr *Script) ExecuteBytes(ctx *context.T, src []byte) error
- func (scr *Script) ExecuteFile(ctx *context.T, filename string) error
- func (scr *Script) ExpandEnv(s string) string
- func (scr *Script) Help(cmd string) error
- func (scr *Script) ListFunctions(tags ...string)
- func (scr *Script) Printf(format string, args ...interface{})
- func (scr *Script) RegisterConst(name string, value interface{})
- func (scr *Script) SetEnv(name, value string)
- func (scr *Script) SetStdout(stdout io.Writer)
- func (scr *Script) Stdout() io.Writer
- func (scr *Script) String() string
- func (scr *Script) Variables() map[string]interface{}
Examples ¶
Constants ¶
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 ¶
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 ¶
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) ExecuteBytes ¶
ExecuteBytes will parse, compile and execute the supplied script.
func (*Script) ExecuteFile ¶
ExecuteFile will parse, compile and execute the script contained in the specified file.
func (*Script) ListFunctions ¶
func (*Script) RegisterConst ¶
func (*Script) SetEnv ¶
SetEnv sets an environment variable that will be available to the script that's preferred to any value in the processes environment.