gs

package module
v0.0.0-...-efe1987 Latest Latest
Warning

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

Go to latest
Published: Apr 7, 2026 License: MIT Imports: 15 Imported by: 0

README

GS (Go Script)

GS (Go Script) is a lightweight, high-performance, embeddable scripting language for Go, built around expression-oriented programming and consistent language design principles. Its feature set, including arrow-function lambdas, data-type member functions, and fluent chaining, enables transformation-heavy code to be written as clear expressions instead of loop-and-branch boilerplate. It runs on a bytecode VM implemented in Go, making embedding and sandboxing straightforward in Go services and tools.

Quick Start

Install the cli with Go's toolchain:

go install github.com/jokruger/gs/cmd/gs@latest

Then you can run GS scripts with the gs command or using hashbang:

#!/usr/bin/env gs

fmt := import("fmt")
fmt.println("Hello", "GS")

Documentation

TODO

Contributing

Before contributing, please review docs/project.md and docs/conventions.md for project layout, coding standards and repository contracts.

  1. Fork the repository and clone your fork locally.
  2. Make your changes in a focused branch.
  3. Run the test suite.
  4. Add or update tests in tests/unit for any change that affects language or runtime behavior.
  5. Open a pull request describing the motivation for the change and any new or changed semantics.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Acknowledgements

This project is based on script language Tengo by Daniel Kang. A special thanks to Tengo's creator and contributors.

Documentation

Index

Constants

View Source
const (
	// SourceFileExtDefault is the default extension for source files.
	SourceFileExtDefault = ".gs"
)

Variables

This section is empty.

Functions

func Eval

func Eval(ctx context.Context, alloc core.Allocator, expr string, params map[string]core.Value) (any, error)

Eval compiles and executes given expr with params, and returns an evaluated value. Argument `expr` must be an expression. Otherwise it will fail to compile. Expression must not use or define variable "__res__" as it's reserved for the internal usage.

Types

type Compiled

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

Compiled is a compiled instance of the user script. Use Script.Compile() to create Compiled object.

func (*Compiled) Clone

func (c *Compiled) Clone() *Compiled

Clone creates a new copy of Compiled. Cloned copies are safe for concurrent use by multiple goroutines.

func (*Compiled) Get

func (c *Compiled) Get(name string) *Variable

Get returns a variable identified by the name.

func (*Compiled) GetAll

func (c *Compiled) GetAll() []*Variable

GetAll returns all the variables that are defined by the compiled script.

func (*Compiled) IsDefined

func (c *Compiled) IsDefined(name string) bool

IsDefined returns true if the variable name is defined (has value) before or after the execution.

func (*Compiled) Run

func (c *Compiled) Run() error

Run executes the compiled script in the virtual machine.

func (*Compiled) RunContext

func (c *Compiled) RunContext(ctx context.Context) (err error)

RunContext is like Run but includes a context.

func (*Compiled) Set

func (c *Compiled) Set(name string, val core.Value) error

Set replaces the value of a global variable identified by the name. An error will be returned if the name was not defined during compilation.

func (*Compiled) Size

func (c *Compiled) Size() int64

Size of compiled script in bytes (as much as we can calculate it without reflection and black magic)

type Compiler

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

Compiler compiles the AST into a bytecode.

func NewCompiler

func NewCompiler(
	alloc core.Allocator,
	file *parser.SourceFile,
	symbolTable *vm.SymbolTable,
	constants []core.Value,
	modules vm.ModuleGetter,
	trace io.Writer,
) *Compiler

NewCompiler creates a Compiler.

func (*Compiler) Bytecode

func (c *Compiler) Bytecode() *vm.Bytecode

Bytecode returns a compiled bytecode.

func (*Compiler) Compile

func (c *Compiler) Compile(node parser.Node) error

Compile compiles the AST node.

func (*Compiler) EnableFileImport

func (c *Compiler) EnableFileImport(enable bool)

EnableFileImport enables or disables module loading from local files. Local file modules are disabled by default.

func (*Compiler) GetImportFileExt

func (c *Compiler) GetImportFileExt() []string

GetImportFileExt returns the current list of extension name. These are the complementary suffix of the source file to search and load local module files.

func (*Compiler) SetImportDir

func (c *Compiler) SetImportDir(dir string)

SetImportDir sets the initial import directory path for file imports.

func (*Compiler) SetImportFileExt

func (c *Compiler) SetImportFileExt(exts ...string) error

SetImportFileExt sets the extension name of the source file for loading local module files. Use this method if you want other source file extension than ".gs". This function requires at least one argument, since it will replace the current list of extension name.

type CompilerError

type CompilerError struct {
	FileSet *parser.SourceFileSet
	Node    parser.Node
	Err     error
}

CompilerError represents a compiler error.

func (*CompilerError) Error

func (e *CompilerError) Error() string

type Script

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

Script can simplify compilation and execution of embedded scripts.

func NewScript

func NewScript(alloc core.Allocator, input []byte) *Script

NewScript creates a Script instance with an input script.

func (*Script) Add

func (s *Script) Add(name string, val core.Value)

Add adds a new variable or updates an existing variable to the script.

func (*Script) Compile

func (s *Script) Compile() (*Compiled, error)

Compile compiles the script with all the defined variables, and, returns Compiled object.

func (*Script) EnableFileImport

func (s *Script) EnableFileImport(enable bool)

EnableFileImport enables or disables module loading from local files. Local file modules are disabled by default.

func (*Script) Remove

func (s *Script) Remove(name string) bool

Remove removes (undefine) an existing variable for the script. It returns false if the variable name is not defined.

func (*Script) Run

func (s *Script) Run() (compiled *Compiled, err error)

Run compiles and runs the scripts. Use returned compiled object to access global variables.

func (*Script) RunContext

func (s *Script) RunContext(ctx context.Context) (compiled *Compiled, err error)

RunContext is like Run but includes a context.

func (*Script) SetImportDir

func (s *Script) SetImportDir(dir string) error

SetImportDir sets the initial import directory for script files.

func (*Script) SetImports

func (s *Script) SetImports(modules vm.ModuleGetter)

SetImports sets import modules.

func (*Script) SetMaxAllocs

func (s *Script) SetMaxAllocs(n int64)

SetMaxAllocs sets the maximum number of objects allocations during the run time. Compiled script will return gse.ErrObjectAllocLimit error if it exceeds this limit.

func (*Script) SetMaxConstObjects

func (s *Script) SetMaxConstObjects(n int)

SetMaxConstObjects sets the maximum number of objects in the compiled constants.

type Variable

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

Variable is a user-defined variable for the script.

func NewVariable

func NewVariable(name string, val core.Value) *Variable

NewVariable creates a Variable.

func (*Variable) Array

func (v *Variable) Array() []any

Array returns []interface value of the variable value. It returns 0 if the value is not convertible to []interface.

func (*Variable) Bool

func (v *Variable) Bool() bool

Bool returns bool value of the variable value. It returns 0 if the value is not convertible to bool.

func (*Variable) Bytes

func (v *Variable) Bytes() []byte

Bytes returns a byte slice of the variable value. It returns nil if the value is not convertible to byte slice.

func (*Variable) Char

func (v *Variable) Char() rune

Char returns rune value of the variable value. It returns 0 if the value is not convertible to rune.

func (*Variable) Error

func (v *Variable) Error() error

Error returns an error if the underlying value is error object. If not, this returns nil.

func (*Variable) Float

func (v *Variable) Float() float64

Float returns float64 value of the variable value. It returns 0.0 if the value is not convertible to float64.

func (*Variable) Int

func (v *Variable) Int() int64

Int returns int64 value of the variable value. It returns 0 if the value is not convertible to int64.

func (*Variable) IsUndefined

func (v *Variable) IsUndefined() bool

IsUndefined returns true if the underlying value is undefined.

func (*Variable) Map

func (v *Variable) Map() map[string]any

Map returns map[string]any value of the variable value. It returns 0 if the value is not convertible to map[string]any.

func (*Variable) Name

func (v *Variable) Name() string

Name returns the name of the variable.

func (*Variable) Object

func (v *Variable) Object() core.Value

Object returns an underlying Object of the variable value. Note that returned Object is a copy of an actual Object used in the script.

func (*Variable) String

func (v *Variable) String() string

String returns string value of the variable value. It returns 0 if the value is not convertible to string.

func (*Variable) Time

func (v *Variable) Time() time.Time

Time returns time.Time value of the variable value. It returns zero time if the value is not convertible to time.Time.

func (*Variable) Value

func (v *Variable) Value() core.Value

Value returns the value of the variable.

func (*Variable) ValueType

func (v *Variable) ValueType() string

ValueType returns the name of the value type.

Directories

Path Synopsis
cmd
bench command
gs command
internal

Jump to

Keyboard shortcuts

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