gl

package
v1.0.0-beta7 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2021 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	TrueValue  = BoolValue(true)
	FalseValue = BoolValue(false)
)

Boolean literals

View Source
var (
	ErrEvaluation               = errors.New("Evaluation error")
	ErrNotCallable              = errors.New("Not callable")
	ErrNotIndexable             = errors.New("Not indexable")
	ErrNoEdgesInResult          = errors.New("No edges in result")
	ErrMultipleEdgesInResult    = errors.New("Multiple edges in result")
	ErrNoNodesInResult          = errors.New("No nodes in result")
	ErrMultipleNodesInResult    = errors.New("Multiple nodes in result")
	ErrNotANumber               = errors.New("Not a number")
	ErrNotAString               = errors.New("Not a string")
	ErrIncomparable             = errors.New("Incomparable values")
	ErrNotLValue                = errors.New("Not LValue")
	ErrCannotIterate            = errors.New("Cannot iterate")
	ErrCannotAccumulate         = errors.New("Cannot accumulate collection of values")
	ErrIncompatibleValue        = errors.New("Incompatible value")
	ErrNotAClosure              = errors.New("Not a closure")
	ErrClosureOrBooleanExpected = errors.New("Closure or a boolean value expected")
	ErrInvalidArgumentType      = errors.New("Invalid argument type")
	ErrInvalidStatementList     = errors.New("Invalid statement list")
	ErrInvalidExpression        = errors.New("Invalid expression")
	ErrNotExpression            = errors.New("Not an expression")
)

Script compilation/evaluation errors

Functions

func AsBool

func AsBool(v Value, err error) (bool, error)

AsBool is a convenience function that will return an error if the input contains an error, or if the value cannot be converted to a bool value. Use it as:

b, err:=AsBool(expression.Evaluate(ctx))

Types

type BoolValue

type BoolValue bool

BoolValue is a boolean value on the stack

func (BoolValue) AsBool

func (v BoolValue) AsBool() (bool, error)

AsBool returns the value

func (BoolValue) AsInt

func (v BoolValue) AsInt() (int, error)

AsInt returns 0 or 1

func (BoolValue) AsString

func (v BoolValue) AsString() (string, error)

AsString returns "true" or "false"

func (BoolValue) Call

func (BoolValue) Call(*Scope, []Value) (Value, error)

Call returns ErrNotCallable

func (BoolValue) Eq

func (b BoolValue) Eq(v Value) (bool, error)

Eq returns true of the value.AsBool has the same value as this

func (BoolValue) Index

func (BoolValue) Index(Value) (Value, error)

Index returns ErrNotIndexable

func (BoolValue) Selector

func (BoolValue) Selector(sel string) (Value, error)

Selector returns ErrUnknownSelector

type Closure

type Closure struct {
	Symbol string
	F      Evaluatable
	// contains filtered or unexported fields
}

A Closure is a function that captures the current scope, with an argument

func (Closure) AsBool

func (Closure) AsBool() (bool, error)

func (Closure) AsInt

func (Closure) AsInt() (int, error)

func (Closure) AsString

func (Closure) AsString() (string, error)

func (Closure) Call

func (Closure) Call(*Scope, []Value) (Value, error)

func (Closure) Eq

func (Closure) Eq(Value) (bool, error)

func (Closure) Evaluate

func (c Closure) Evaluate(arg Value, scope *Scope) (Value, error)

Evaluate the closure with a new scope containing the argument set to arg

func (Closure) Index

func (Closure) Index(i Value) (Value, error)

func (Closure) Selector

func (Closure) Selector(sel string) (Value, error)

type Compiler

type Compiler struct {
	*parser.BaseglListener
	// contains filtered or unexported fields
}

Compiler is the graph language script compiler

func NewCompiler

func NewCompiler() *Compiler

NewCompiler returns a new gl compiler

func (*Compiler) EnterStatementBlock

func (compiler *Compiler) EnterStatementBlock(c *parser.StatementBlockContext)

func (*Compiler) EnterStatementList

func (compiler *Compiler) EnterStatementList(c *parser.StatementListContext)

func (*Compiler) Error

func (c *Compiler) Error() error

Error returns the first detected error

func (*Compiler) ExitArguments

func (compiler *Compiler) ExitArguments(c *parser.ArgumentsContext)

func (*Compiler) ExitAssignmentExpression

func (compiler *Compiler) ExitAssignmentExpression(c *parser.AssignmentExpressionContext)

func (*Compiler) ExitBlockClosureExpression

func (compiler *Compiler) ExitBlockClosureExpression(c *parser.BlockClosureExpressionContext)

func (*Compiler) ExitClosureExpression

func (compiler *Compiler) ExitClosureExpression(c *parser.ClosureExpressionContext)

func (*Compiler) ExitDefinitionExpression

func (compiler *Compiler) ExitDefinitionExpression(c *parser.DefinitionExpressionContext)

func (*Compiler) ExitDotExpression

func (compiler *Compiler) ExitDotExpression(c *parser.DotExpressionContext)

func (*Compiler) ExitEqualityExpression

func (compiler *Compiler) ExitEqualityExpression(c *parser.EqualityExpressionContext)

func (*Compiler) ExitExpressionStatement

func (compiler *Compiler) ExitExpressionStatement(c *parser.ExpressionStatementContext)

func (*Compiler) ExitFunctionCallExpression

func (compiler *Compiler) ExitFunctionCallExpression(c *parser.FunctionCallExpressionContext)

func (*Compiler) ExitIdentifierExpression

func (compiler *Compiler) ExitIdentifierExpression(c *parser.IdentifierExpressionContext)

func (*Compiler) ExitIndexExpression

func (compiler *Compiler) ExitIndexExpression(c *parser.IndexExpressionContext)

func (*Compiler) ExitLiteralExpression

func (compiler *Compiler) ExitLiteralExpression(c *parser.LiteralExpressionContext)

func (*Compiler) ExitLogicalAndExpression

func (compiler *Compiler) ExitLogicalAndExpression(c *parser.LogicalAndExpressionContext)

func (*Compiler) ExitLogicalOrExpression

func (compiler *Compiler) ExitLogicalOrExpression(c *parser.LogicalOrExpressionContext)

func (*Compiler) ExitLvalue

func (compiler *Compiler) ExitLvalue(c *parser.LvalueContext)

func (*Compiler) ExitNotExpression

func (compiler *Compiler) ExitNotExpression(c *parser.NotExpressionContext)

func (*Compiler) ExitStatementBlock

func (compiler *Compiler) ExitStatementBlock(c *parser.StatementBlockContext)

func (*Compiler) ExitStatementList

func (compiler *Compiler) ExitStatementList(c *parser.StatementListContext)

type EdgeValue

type EdgeValue struct {
	Edges map[ls.Edge]struct{}
	// contains filtered or unexported fields
}

EdgeValue is zero or more edges

func (EdgeValue) Add

func (e EdgeValue) Add(v2 Value) (Value, error)

Add returns the set union of to edge sets

func (EdgeValue) AsBool

func (e EdgeValue) AsBool() (bool, error)

AsBool returns true if edge value is nonempty

func (EdgeValue) AsInt

func (EdgeValue) AsInt() (int, error)

func (EdgeValue) AsString

func (e EdgeValue) AsString() (string, error)

AsString returns error

func (EdgeValue) Call

func (EdgeValue) Call(*Scope, []Value) (Value, error)

func (EdgeValue) Eq

func (e EdgeValue) Eq(val Value) (bool, error)

Eq compares two edge sets

func (EdgeValue) Index

func (EdgeValue) Index(i Value) (Value, error)

func (EdgeValue) Selector

func (e EdgeValue) Selector(sel string) (Value, error)

Selector selects one of the selectors of the edge

type ErrInvalidArguments

type ErrInvalidArguments struct {
	Method string
	Msg    string
}

ErrInvalidArgument is returned when a method is called with an incompatible argument

func (ErrInvalidArguments) Error

func (e ErrInvalidArguments) Error() string

type ErrInvalidFunctionCall

type ErrInvalidFunctionCall string

ErrInvalidFunctionCall is returned if the function call cannot be completed

func (ErrInvalidFunctionCall) Error

func (e ErrInvalidFunctionCall) Error() string

type ErrSyntax

type ErrSyntax string

ErrSyntax is returned for generic syntax errors

func (ErrSyntax) Error

func (e ErrSyntax) Error() string

type ErrUnknownIdentifier

type ErrUnknownIdentifier string

ErrUnknownIdentifier is returned if an identifier is not in scope

func (ErrUnknownIdentifier) Error

func (e ErrUnknownIdentifier) Error() string

type ErrUnknownSelector

type ErrUnknownSelector struct {
	Selector string
}

ErrUnknownSelector is returned when a dot-expression uses an unknown selector for the type

func (ErrUnknownSelector) Error

func (e ErrUnknownSelector) Error() string

type Evaluatable

type Evaluatable interface {
	Evaluate(*Scope) (Value, error)
}

func Parse

func Parse(input string) (Evaluatable, error)

Parse parses the given input string as a script and returns an evaluatable object

type EvaluatableFunc

type EvaluatableFunc func(*Scope) (Value, error)

func (EvaluatableFunc) Evaluate

func (e EvaluatableFunc) Evaluate(s *Scope) (Value, error)

type FunctionValue

type FunctionValue struct {
	MinArgs int
	MaxArgs int
	Name    string
	Closure func(*Scope, []Value) (Value, error)
	// contains filtered or unexported fields
}

func (FunctionValue) AsBool

func (FunctionValue) AsBool() (bool, error)

func (FunctionValue) AsInt

func (FunctionValue) AsInt() (int, error)

func (FunctionValue) AsString

func (FunctionValue) AsString() (string, error)

func (FunctionValue) Call

func (f FunctionValue) Call(scope *Scope, args []Value) (Value, error)

func (FunctionValue) Eq

func (FunctionValue) Eq(Value) (bool, error)

func (FunctionValue) Index

func (FunctionValue) Index(i Value) (Value, error)

func (FunctionValue) Selector

func (FunctionValue) Selector(sel string) (Value, error)

type NodeValue

type NodeValue struct {
	Nodes ls.NodeSet
	// contains filtered or unexported fields
}

NodeValue is zero or mode nodes on the stack

func NewNodeValue

func NewNodeValue(nodes ...ls.Node) NodeValue

func (NodeValue) Add

func (v NodeValue) Add(v2 Value) (Value, error)

func (NodeValue) AsBool

func (v NodeValue) AsBool() (bool, error)

func (NodeValue) AsInt

func (NodeValue) AsInt() (int, error)

func (NodeValue) AsString

func (v NodeValue) AsString() (string, error)

func (NodeValue) Call

func (NodeValue) Call(*Scope, []Value) (Value, error)

func (NodeValue) Eq

func (v NodeValue) Eq(val Value) (bool, error)

func (NodeValue) Index

func (NodeValue) Index(i Value) (Value, error)

func (NodeValue) Selector

func (v NodeValue) Selector(sel string) (Value, error)

type NullValue

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

NullValue is the null script result value

func (NullValue) AsBool

func (NullValue) AsBool() (bool, error)

func (NullValue) AsInt

func (NullValue) AsInt() (int, error)

func (NullValue) AsString

func (NullValue) AsString() (string, error)

AsString returns "null"

func (NullValue) Call

func (NullValue) Call(*Scope, []Value) (Value, error)

func (NullValue) Eq

func (NullValue) Eq(v Value) (bool, error)

Eq returns true only if the value is a null value

func (NullValue) Index

func (NullValue) Index(i Value) (Value, error)

func (NullValue) Selector

func (NullValue) Selector(sel string) (Value, error)

type NumberValue

type NumberValue string

NumberValue is a numeric value on the stack

func (NumberValue) AsBool

func (v NumberValue) AsBool() (bool, error)

AsBool returns true if value is nonzero

func (NumberValue) AsInt

func (v NumberValue) AsInt() (int, error)

AsInt returns the inv value, or error if value cannot be converted to int

func (NumberValue) AsString

func (v NumberValue) AsString() (string, error)

AsString returns value as string

func (NumberValue) Call

func (NumberValue) Call(*Scope, []Value) (Value, error)

Call returns ErrNotCallable

func (NumberValue) Eq

func (v NumberValue) Eq(value Value) (bool, error)

Eq compares the string values of the numbers

func (NumberValue) Index

func (NumberValue) Index(Value) (Value, error)

Index returns ErrNotIndexable

func (NumberValue) Selector

func (NumberValue) Selector(sel string) (Value, error)

Selector returns ErrUnknownSelector

type PropertiesValue

type PropertiesValue struct {
	Properties map[string]*ls.PropertyValue
	// contains filtered or unexported fields
}

PropertiesValue is a map[string]*PropertyValue on the stack

func (PropertiesValue) AsBool

func (value PropertiesValue) AsBool() (bool, error)

func (PropertiesValue) AsInt

func (PropertiesValue) AsInt() (int, error)

func (PropertiesValue) AsString

func (PropertiesValue) AsString() (string, error)

func (PropertiesValue) Call

func (PropertiesValue) Call(*Scope, []Value) (Value, error)

func (PropertiesValue) Eq

func (value PropertiesValue) Eq(v Value) (bool, error)

func (PropertiesValue) Index

func (value PropertiesValue) Index(index Value) (Value, error)

func (PropertiesValue) Selector

func (value PropertiesValue) Selector(sel string) (Value, error)

type Scope

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

Scope keeps the symbols visible at any point during the execution of a script. A nil scope is a valid scope

func NewScope

func NewScope() *Scope

NewScope returns a new empty scope

func (*Scope) Get

func (scope *Scope) Get(symbol string) Value

Get looks up the symbol in the enclosing scopes

func (*Scope) NewScope

func (scope *Scope) NewScope() *Scope

NewScope returns a nested scope

func (*Scope) Set

func (scope *Scope) Set(key string, value interface{}) *Scope

Set the symbol value in the active scope

func (*Scope) SetValue

func (scope *Scope) SetValue(key string, value interface{}) error

type StringSliceValue

type StringSliceValue []string

StringSliceValue is a []string on the stack

func (StringSliceValue) Add

func (value StringSliceValue) Add(v2 Value) (Value, error)

func (StringSliceValue) AsBool

func (value StringSliceValue) AsBool() (bool, error)

func (StringSliceValue) AsInt

func (StringSliceValue) AsInt() (int, error)

func (StringSliceValue) AsString

func (value StringSliceValue) AsString() (string, error)

func (StringSliceValue) Call

func (StringSliceValue) Call(*Scope, []Value) (Value, error)

func (StringSliceValue) Eq

func (value StringSliceValue) Eq(v Value) (bool, error)

func (StringSliceValue) Index

func (value StringSliceValue) Index(index Value) (Value, error)

func (StringSliceValue) Selector

func (value StringSliceValue) Selector(sel string) (Value, error)

type StringValue

type StringValue string

StringValue is a string on the stack

func (StringValue) AsBool

func (value StringValue) AsBool() (bool, error)

func (StringValue) AsInt

func (value StringValue) AsInt() (int, error)

func (StringValue) AsString

func (value StringValue) AsString() (string, error)

func (StringValue) Call

func (StringValue) Call(*Scope, []Value) (Value, error)

func (StringValue) Eq

func (value StringValue) Eq(v Value) (bool, error)

func (StringValue) Index

func (StringValue) Index(Value) (Value, error)

func (StringValue) Selector

func (value StringValue) Selector(sel string) (Value, error)

type Value

type Value interface {
	// Selector selects a field/method from the underlying value.
	Selector(string) (Value, error)
	// Index selects an value from an indexable value
	Index(Value) (Value, error)
	// Call a method/function with arguments
	Call(*Scope, []Value) (Value, error)

	// Eq checks equivalence
	Eq(Value) (bool, error)

	// Return value as an integer, or return error if it cannot be represented as an integer
	AsInt() (int, error)
	// Return value as a boolean. Returns false for empty string, empty collection, etc.
	AsBool() (bool, error)
	AsString() (string, error)
}

Value represents a value on the evaluation stack

func Evaluate

func Evaluate(input string) (Value, error)

Evaluate parses and evaluates a script with empty scope

func EvaluateWith

func EvaluateWith(scope *Scope, input string) (Value, error)

EvaluateWith parses and evaluates a script with the given scope

func ValueOf

func ValueOf(value interface{}) Value

type ValueArrayValue

type ValueArrayValue []Value

ValueArrayValue is a []Value on the stack

func (ValueArrayValue) Add

func (value ValueArrayValue) Add(v2 Value) (Value, error)

func (ValueArrayValue) AsBool

func (value ValueArrayValue) AsBool() (bool, error)

func (ValueArrayValue) AsInt

func (ValueArrayValue) AsInt() (int, error)

func (ValueArrayValue) AsString

func (value ValueArrayValue) AsString() (string, error)

func (ValueArrayValue) Call

func (ValueArrayValue) Call(*Scope, []Value) (Value, error)

func (ValueArrayValue) Eq

func (ValueArrayValue) Index

func (value ValueArrayValue) Index(index Value) (Value, error)

func (ValueArrayValue) Selector

func (value ValueArrayValue) Selector(sel string) (Value, error)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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