Documentation
¶
Index ¶
- Constants
- func Eval(ctx context.Context, expr string, params map[string]core.Value) (any, error)
- type AssignmentMode
- type Compiled
- func (c *Compiled) Clone(a *core.Arena) (*Compiled, error)
- func (c *Compiled) Get(name string) *Variable
- func (c *Compiled) GetAll() []*Variable
- func (c *Compiled) GetValue(name string) core.Value
- func (c *Compiled) IsDefined(name string) bool
- func (c *Compiled) Run() error
- func (c *Compiled) RunContext(ctx context.Context) (err error)
- func (c *Compiled) Set(name string, val core.Value) error
- func (c *Compiled) Size() int64
- type Compiler
- func (c *Compiler) Bytecode() *vm.Bytecode
- func (c *Compiler) Compile(node parser.Node) error
- func (c *Compiler) EnableFileImport(enable bool)
- func (c *Compiler) GetAssignmentMode() AssignmentMode
- func (c *Compiler) GetImportFileExt() []string
- func (c *Compiler) SetAssignmentMode(mode AssignmentMode)
- func (c *Compiler) SetImportDir(dir string)
- func (c *Compiler) SetImportFileExt(exts ...string) error
- type CompilerError
- type Script
- func (s *Script) Add(name string, val core.Value)
- func (s *Script) Compile(cta *core.Arena, rta *core.Arena) (*Compiled, error)
- func (s *Script) EnableFileImport(enable bool)
- func (s *Script) Remove(name string) bool
- func (s *Script) Run() (*Compiled, error)
- func (s *Script) SetAssignmentMode(mode AssignmentMode)
- func (s *Script) SetImportDir(dir string) error
- func (s *Script) SetImports(modules vm.ModuleGetter)
- func (s *Script) SetMaxConstObjects(n int)
- func (s *Script) SetMaxFrames(n int)
- func (s *Script) SetMaxStack(n int)
- type Variable
- func (v *Variable) Array() []any
- func (v *Variable) Bool() bool
- func (v *Variable) Bytes() []byte
- func (v *Variable) Error() error
- func (v *Variable) Float() float64
- func (v *Variable) Int() int64
- func (v *Variable) IsUndefined() bool
- func (v *Variable) Map() map[string]any
- func (v *Variable) Name() string
- func (v *Variable) Object() core.Value
- func (v *Variable) Rune() rune
- func (v *Variable) String() string
- func (v *Variable) Time() time.Time
- func (v *Variable) Value() core.Value
- func (v *Variable) ValueType() string
Constants ¶
const (
// SourceFileExtDefault is the default extension for source files.
SourceFileExtDefault = ".kvn"
)
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AssignmentMode ¶
type AssignmentMode int
AssignmentMode controls how plain '=' handles unresolved identifiers.
const ( // AssignmentModeSmart declares a variable in current scope for unresolved '=' assignments. AssignmentModeSmart AssignmentMode = iota // AssignmentModeStrict requires variables to already exist for '=' assignments. AssignmentModeStrict )
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 ¶
Clone creates a new copy of Compiled. Cloned copies are safe for concurrent use by multiple goroutines.
func (*Compiled) IsDefined ¶
IsDefined returns true if the variable name is defined (has value) before or after the execution.
func (*Compiled) RunContext ¶
RunContext is like Run but includes a context.
type Compiler ¶
type Compiler struct {
// contains filtered or unexported fields
}
Compiler compiles the AST into a bytecode.
func NewCompiler ¶
func NewCompiler( alloc *core.Arena, file *parser.SourceFile, symbolTable *vm.SymbolTable, constants []core.Value, modules vm.ModuleGetter, trace io.Writer, ) *Compiler
NewCompiler creates a Compiler.
func (*Compiler) EnableFileImport ¶
EnableFileImport enables or disables module loading from local files. Local file modules are disabled by default.
func (*Compiler) GetAssignmentMode ¶
func (c *Compiler) GetAssignmentMode() AssignmentMode
GetAssignmentMode returns the active assignment mode.
func (*Compiler) GetImportFileExt ¶
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) SetAssignmentMode ¶
func (c *Compiler) SetAssignmentMode(mode AssignmentMode)
SetAssignmentMode sets how plain '=' handles unresolved identifiers.
func (*Compiler) SetImportDir ¶
SetImportDir sets the initial import directory path for file imports.
func (*Compiler) SetImportFileExt ¶
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 ".kvn". 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 (*Script) Compile ¶
Compile compiles the script with all the defined variables, and, returns Compiled object. Receives compile-time and runtime arenas for better memory management. If arenas are not provided, it will create new ones internally.
func (*Script) EnableFileImport ¶
EnableFileImport enables or disables module loading from local files. Local file modules are disabled by default.
func (*Script) Remove ¶
Remove removes (undefine) an existing variable for the script. It returns false if the variable name is not defined.
func (*Script) Run ¶
Run compiles and runs the script, and returns the compiled instance. Note: prefer to use Compile() and Run() separately for better performance and control over the execution.
func (*Script) SetAssignmentMode ¶
func (s *Script) SetAssignmentMode(mode AssignmentMode)
SetAssignmentMode sets how plain '=' handles unresolved identifiers during compilation.
func (*Script) SetImportDir ¶
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) SetMaxConstObjects ¶
SetMaxConstObjects sets the maximum number of objects in the compiled constants.
func (*Script) SetMaxFrames ¶ added in v0.0.7
SetMaxFrames sets the maximum number of frames for the compiled script.
func (*Script) SetMaxStack ¶ added in v0.0.7
SetMaxStack sets the maximum stack size for the compiled script.
type Variable ¶
type Variable struct {
// contains filtered or unexported fields
}
Variable is a user-defined variable for the script.
func NewVariable ¶
NewVariable creates a Variable.
func (*Variable) Array ¶
Array returns []interface value of the variable value. It returns 0 if the value is not convertible to []interface.
func (*Variable) Bool ¶
Bool returns bool value of the variable value. It returns 0 if the value is not convertible to bool.
func (*Variable) Bytes ¶
Bytes returns a byte slice of the variable value. It returns nil if the value is not convertible to byte slice.
func (*Variable) Error ¶
Error returns an error if the underlying value is error object. If not, this returns nil.
func (*Variable) Float ¶
Float returns float64 value of the variable value. It returns 0.0 if the value is not convertible to float64.
func (*Variable) Int ¶
Int returns int64 value of the variable value. It returns 0 if the value is not convertible to int64.
func (*Variable) IsUndefined ¶
IsUndefined returns true if the underlying value is undefined.
func (*Variable) Map ¶
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) Object ¶
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) Rune ¶ added in v0.0.6
Rune returns rune value of the variable value. It returns 0 if the value is not convertible to rune.
func (*Variable) String ¶
String returns string value of the variable value. It returns 0 if the value is not convertible to string.
func (*Variable) Time ¶
Time returns time.Time value of the variable value. It returns zero time if the value is not convertible to time.Time.
