vm

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2019 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// CtxChar is a context boundery character
	CtxChar = '#'
	// CtxLength is the max length of the context key
	CtxLength = 64
	// CtxDeep is the max deep in replace function
	CtxDeep = 16
)
View Source
const (

	// ErrRunIndex is returned when Run has been executed with wrong index
	ErrRunIndex = iota + 1
	// ErrDepth is returned when maximum depth of recursion has been reached
	ErrDepth
	// ErrDivZero is returned when there is division by zero
	ErrDivZero
	// ErrCycle is returned when maximum cycle count has been reached
	ErrCycle
	// ErrShift is returned when << or >> are used with the negative right operand
	ErrShift
	// ErrStrToInt is returned when the conversion string to integer is invalid
	ErrStrToInt
	// ErrStrToFloat is returned when the conversion string to float is invalid
	ErrStrToFloat
	// ErrEmptyCommand is returned if empty command is specified in $
	ErrEmptyCommand
	// ErrQuoteCommand is returned if there is an unclosed quotation mark in $ command
	ErrQuoteCommand
	// ErrIndexOut means that int index is out of the length of the array
	ErrIndexOut
	// ErrMapIndex is returned when there is not the key in the map
	ErrMapIndex
	// ErrAssignment is returned when there is a recursive assignment
	ErrAssignment
	// ErrUndefined means that the value of the variable is undefined
	ErrUndefined
	// ErrByteOut is returned when value for buf is greater 255
	ErrByteOut
	// ErrInvalidParam is returned when the function gets invalid parameter(s)
	ErrInvalidParam
	// ErrNotRun is returned when the executing unit doesn't have run function
	ErrNotRun
	// ErrFnEmpty is returned in case of calling undefined fn variable
	ErrFnEmpty
	// ErrThreadIndex is returned when the index of the thread is out of range
	ErrThreadIndex
	// ErrThreadClosed is generated when the thread has been closed
	ErrThreadClosed
	// ErrPlatform is generated when the function is not supported on the current platform
	ErrPlatform

	// ErrEmbedded means golang error in embedded functions
	ErrEmbedded = 254
	// ErrRuntime error. It means bug
	ErrRuntime = 255
)
View Source
const (
	TRACESTRUCT = iota
	TIMESTRUCT
	FINFOSTRUCT
)
View Source
const (
	// ThQueue means that the thread is in the queue to start
	ThQueue = iota
	// ThWork means that the thread is running
	ThWork
	// ThPaused means that the thread has been suspended
	ThPaused
	// ThWait means that the thread is waiting for the end of another thread
	ThWait
	// ThFinished means that the thread finished
	ThFinished
	// ThError means that the thread has been closed with an error
	ThError
	// ThClosed means that the thread has been closed
	ThClosed
)
View Source
const (
	// ThCmdClose closes the thread
	ThCmdClose = iota
	// ThCmdResume resumes the thread
	ThCmdResume
	// ThCmdContinue continues the thread after waiting
	ThCmdContinue
)
View Source
const (
	STACKSIZE = 256
	// CYCLE is the limit of loops
	CYCLE = uint64(16000000)
	// DEPTH is the maximum size of blocks stack
	DEPTH = uint32(1000)
)
View Source
const (
	// SleepStep is a tick in sleep
	SleepStep = int64(100)
)

Variables

View Source
var (
	// ErrCtxLength is returned when the key is too long
	ErrCtxLength = `key length is longer than %d characters`
	// ErrCtxLoop is returned when there is a loop in values of context
	ErrCtxLoop = `%s key refers to itself`
	// ErrCtxDeep is returned if the maximum depth reached
	ErrCtxDeep = `maximum depth reached`
)
View Source
var Embedded = []core.Embed{
	{Func: CtxSetºStrStr, Return: core.TYPESTR, Params: []uint16{core.TYPESTR, core.TYPESTR},
		Runtime: true, CanError: true},
	{Func: CtxSetºStrBool, Return: core.TYPESTR, Params: []uint16{core.TYPESTR, core.TYPEBOOL},
		Runtime: true, CanError: true},
	{Func: CtxSetºStrFloat, Return: core.TYPESTR, Params: []uint16{core.TYPESTR, core.TYPEFLOAT},
		Runtime: true, CanError: true},
	{Func: CtxSetºStrInt, Return: core.TYPESTR, Params: []uint16{core.TYPESTR, core.TYPEINT},
		Runtime: true, CanError: true},
	{Func: CtxValueºStr, Return: core.TYPESTR, Params: []uint16{core.TYPESTR}, Runtime: true},
	{Func: CtxIsºStr, Return: core.TYPEBOOL, Params: []uint16{core.TYPESTR}, Runtime: true},
	{Func: CtxºStr, Return: core.TYPESTR, Params: []uint16{core.TYPESTR},
		Runtime: true, CanError: true},
	{Func: CtxGetºStr, Return: core.TYPESTR, Params: []uint16{core.TYPESTR},
		Runtime: true, CanError: true},
	{Func: sleepºInt, Params: []uint16{core.TYPEINT}, Runtime: true},
	{Func: errorºIntStr, Params: []uint16{core.TYPEINT, core.TYPESTR}, CanError: true,
		Variadic: true},

	{Func: terminateºThread, Params: []uint16{core.TYPEINT}, Runtime: true, CanError: true},
	{Func: waitºThread, Params: []uint16{core.TYPEINT}, Runtime: true, CanError: true},
	{Func: ErrID, Return: core.TYPEINT, Params: []uint16{core.TYPEERROR}},
	{Func: ErrText, Return: core.TYPESTR, Params: []uint16{core.TYPEERROR}},
	{Func: ErrTrace, Return: core.TYPEARR, Params: []uint16{core.TYPEERROR}, Runtime: true},
	{Func: ParseTimeºStrStr, Return: core.TYPESTRUCT, Params: []uint16{core.TYPESTR, core.TYPESTR},
		Runtime: true, CanError: true},
	{Func: FileInfoºStr, Return: core.TYPESTRUCT, Params: []uint16{core.TYPESTR},
		Runtime: true, CanError: true},
	{Func: ReadDirºStr, Return: core.TYPEARR, Params: []uint16{core.TYPESTR},
		Runtime: true, CanError: true},
	{Func: SetFileTimeºStrTime, Params: []uint16{core.TYPESTR, core.TYPESTRUCT}, CanError: true},
	{Func: EqualºTimeTime, Return: core.TYPEBOOL, Params: []uint16{core.TYPESTRUCT, core.TYPESTRUCT}},

	{Func: FormatºTimeStr, Return: core.TYPESTR, Params: []uint16{core.TYPESTR, core.TYPESTRUCT}},
	{Func: GreaterºTimeTime, Return: core.TYPEBOOL, Params: []uint16{core.TYPESTRUCT, core.TYPESTRUCT}},
	{Func: LessºTimeTime, Return: core.TYPEBOOL, Params: []uint16{core.TYPESTRUCT, core.TYPESTRUCT}},
	{Func: ArgCount, Return: core.TYPEINT, Runtime: true},
	{Func: ArgºStr, Return: core.TYPESTR, Params: []uint16{core.TYPESTR}, Runtime: true},
	{Func: ArgºStrStr, Return: core.TYPESTR, Params: []uint16{core.TYPESTR, core.TYPESTR},
		Runtime: true},
	{Func: ArgºStrInt, Return: core.TYPEINT, Params: []uint16{core.TYPESTR, core.TYPEINT},
		Runtime: true, CanError: true},
	{Func: Args, Return: core.TYPEARR, Runtime: true},
	{Func: ArgsºStr, Return: core.TYPEARR, Params: []uint16{core.TYPESTR}, Runtime: true},
	{Func: ArgsTail, Return: core.TYPEARR, Runtime: true},

	{Func: IsArgºStr, Return: core.TYPEBOOL, Params: []uint16{core.TYPESTR}, Runtime: true},
	{Func: OpenºStr, Params: []uint16{core.TYPESTR}, CanError: true},
	{Func: OpenWithºStr, Params: []uint16{core.TYPESTR, core.TYPESTR}, CanError: true},
	{Func: sysRun, Params: []uint16{core.TYPESTR, core.TYPEBOOL, core.TYPEBUF, core.TYPEBUF,
		core.TYPEBUF, core.TYPEARR}, CanError: true},
	{Func: Trace, Return: core.TYPEARR, Runtime: true},
	{Func: resumeºThread, Params: []uint16{core.TYPEINT}, Runtime: true, CanError: true},
	{Func: suspendºThread, Params: []uint16{core.TYPEINT}, Runtime: true, CanError: true},
	{Func: Now, Return: core.TYPESTRUCT, Runtime: true},
	{Func: UTCºTime, Return: core.TYPESTRUCT, Params: []uint16{core.TYPESTRUCT}, Runtime: true},
	{Func: WeekdayºTime, Return: core.TYPEINT, Params: []uint16{core.TYPESTRUCT}, Runtime: true},

	{Func: YearDayºTime, Return: core.TYPEINT, Params: []uint16{core.TYPESTRUCT}},

	{Func: intºTime, Return: core.TYPEINT, Params: []uint16{core.TYPESTRUCT}},
	{Func: timeºInt, Return: core.TYPESTRUCT, Params: []uint16{core.TYPEINT}, Runtime: true},
	{Func: AddHoursºTimeInt, Return: core.TYPESTRUCT, Params: []uint16{core.TYPESTRUCT,
		core.TYPEINT}, Runtime: true},
	{Func: DateºInts, Return: core.TYPESTRUCT, Params: []uint16{core.TYPEINT, core.TYPEINT,
		core.TYPEINT}, Runtime: true},
	{Func: DateTimeºInts, Return: core.TYPESTRUCT, Params: []uint16{core.TYPEINT, core.TYPEINT,
		core.TYPEINT, core.TYPEINT, core.TYPEINT, core.TYPEINT}, Runtime: true},
	{Func: DaysºTime, Return: core.TYPEINT, Params: []uint16{core.TYPESTRUCT}},
}

Functions

func ArgCount added in v1.6.0

func ArgCount(rt *Runtime) int64

ArgCount returns the count of command-line parameters

func Args added in v1.6.0

func Args(rt *Runtime) *core.Array

Args returns the command-line parameters

func ArgsTail added in v1.6.0

func ArgsTail(rt *Runtime) *core.Array

ArgsTail returns the list of command-line parameters

func ArgsºStr added in v1.6.0

func ArgsºStr(rt *Runtime, flag string) *core.Array

ArgsºStr returns the value list of command-line option

func ArgºStr added in v1.6.0

func ArgºStr(rt *Runtime, flag string) string

ArgºStr returns the value of the command-line option

func ArgºStrInt added in v1.6.0

func ArgºStrInt(rt *Runtime, flag string, def int64) (int64, error)

ArgºStrInt returns the number value of the command-line option or the default value

func ArgºStrStr added in v1.6.0

func ArgºStrStr(rt *Runtime, flag, def string) string

ArgºStrStr returns the value of the command-line option or the default value

func CopyVar added in v1.6.0

func CopyVar(rt *Runtime, ptr *interface{}, value interface{})

CopyVar copies one object to another one

func CtxGetºStr added in v1.6.0

func CtxGetºStr(rt *Runtime, key string) (string, error)

CtxGetºStr replaces context values in the value of the key

func CtxIsºStr added in v1.6.0

func CtxIsºStr(rt *Runtime, key string) int64

CtxIsºStr returns true if a context key exists

func CtxSetºStrBool added in v1.6.0

func CtxSetºStrBool(rt *Runtime, key string, value int64) (string, error)

CtxSetºStrBool assign a bool to a context key

func CtxSetºStrFloat added in v1.6.0

func CtxSetºStrFloat(rt *Runtime, key string, value float64) (string, error)

CtxSetºStrFloat assign a float to a context key

func CtxSetºStrInt added in v1.6.0

func CtxSetºStrInt(rt *Runtime, key string, value int64) (string, error)

CtxSetºStrInt assign an integer to a context key

func CtxSetºStrStr added in v1.6.0

func CtxSetºStrStr(rt *Runtime, key, value string) (string, error)

CtxSetºStrStr sets a context value

func CtxValueºStr added in v1.6.0

func CtxValueºStr(rt *Runtime, key string) string

CtxValueºStr returns a context value

func CtxºStr added in v1.6.0

func CtxºStr(rt *Runtime, input string) (string, error)

CtxºStr replaces context values in a string

func DaysºTime added in v1.6.0

func DaysºTime(it *Struct) int64

DaysºTime returns the days of the month

func EqualºTimeTime added in v1.6.0

func EqualºTimeTime(left, right *Struct) int64

EqualºTimeTime returns true if time structures are equal

func ErrFormat added in v1.6.0

func ErrFormat(path string, line, pos int64, message string) string

ErrFormat is a function for formating error message

func ErrID added in v1.6.0

func ErrID(err *RuntimeError) int64

ErrID returns the id of the error

func ErrText added in v1.6.0

func ErrText(err *RuntimeError) string

ErrText returns the text of the error

func ErrTrace added in v1.6.0

func ErrTrace(rt *Runtime, err *RuntimeError) *core.Array

ErrTrace returns the trace of the error

func ErrorText added in v1.6.0

func ErrorText(id int) string

ErrorText returns the text of the error message

func FormatºTimeStr added in v1.6.0

func FormatºTimeStr(layout string, t *Struct) string

FormatºTimeStr formats the time

func GreaterºTimeTime added in v1.6.0

func GreaterºTimeTime(left, right *Struct) int64

GreaterºTimeTime returns true if left time structures are greater than right

func IsArgºStr added in v1.6.0

func IsArgºStr(rt *Runtime, flag string) int64

IsArgºStr returns true if the options is present

func LessºTimeTime added in v1.6.0

func LessºTimeTime(left, right *Struct) int64

LessºTimeTime returns true if left time structures are less than right

func OpenWithºStr added in v1.6.0

func OpenWithºStr(app, fname string) error

OpenWithºStr runs the application with the specified file.

func OpenºStr added in v1.6.0

func OpenºStr(fname string) error

OpenºStr runs corresponding application with the specified file.

func ReadDirºStr added in v1.6.0

func ReadDirºStr(rt *Runtime, dirname string) (*core.Array, error)

ReadDirºStr reads a directory

func Run

func Run(exec *core.Exec, settings Settings) (interface{}, error)

func SetFileTimeºStrTime added in v1.6.0

func SetFileTimeºStrTime(name string, ftime *Struct) error

SetFileTimeºStrTime changes the modification time of the named file

func Trace added in v1.6.0

func Trace(rt *Runtime) *core.Array

Trace gets trace information

func WeekdayºTime added in v1.6.0

func WeekdayºTime(rt *Runtime, t *Struct) int64

WeekdayºTime returns the day of the week specified by t.

func YearDayºTime added in v1.6.0

func YearDayºTime(t *Struct) int64

YearDayºTime returns the day of the year specified by t.

Types

type Call

type Call struct {
	IsFunc   bool
	IsLocal  bool
	Cycle    uint64
	Offset   int32
	Int      int32
	Float    int32
	Str      int32
	Any      int32
	Optional *[]OptValue
	// for loop blocks
	Flags    int16
	Start    int32
	Continue int32 // shift for continue
	Break    int32 // shift for break
	Try      int32 // shift for try
	Recover  int32 // shift for recover
	Retry    int32 // shift for retry
}

Call stores stack of blocks

type Const added in v1.6.0

type Const struct {
	Type  uint16
	Value interface{}
}

type Fn added in v1.6.0

type Fn struct {
	Func int32 // id of function
}

Fn is used for custom func types

type OptValue added in v1.6.0

type OptValue struct {
	Var   int32       // id of variable
	Type  int         // type of variable
	Value interface{} // value
}

type Runtime

type Runtime struct {
	Owner    *VM
	ParCount int32
	Calls    []Call
	Thread   Thread
	ThreadID int64
	Optional *[]OptValue
	// These are stacks for different types
	SInt   [STACKSIZE]int64       // int, char, bool
	SFloat [STACKSIZE]float64     // float
	SStr   [STACKSIZE]string      // str
	SAny   [STACKSIZE]interface{} // all other types
}

Runtime is the one thread structure

func (*Runtime) GoThread added in v1.6.0

func (rt *Runtime) GoThread(offset int64, pars []int32, top *Call) int64

GoThread executes a new thread

func (*Runtime) Run

func (rt *Runtime) Run(i int64) (result interface{}, err error)

type RuntimeError added in v1.6.0

type RuntimeError struct {
	ID      int
	Message string
	Trace   []TraceInfo
}

RuntimeError is a runtime error type

func (*RuntimeError) Error added in v1.6.0

func (re *RuntimeError) Error() string

type Settings

type Settings struct {
	CmdLine []string
	Cycle   uint64 // limit of loops
	Depth   uint32 // limit of blocks stack
}

type Struct added in v1.6.0

type Struct struct {
	Type   *core.StructInfo
	Values []interface{} // Values of fields
}

Struct is used for custom struct types

func AddHoursºTimeInt added in v1.6.0

func AddHoursºTimeInt(rt *Runtime, it *Struct, hours int64) *Struct

AddHoursºTimeInt adds/subtract hours

func DateTimeºInts added in v1.6.0

func DateTimeºInts(rt *Runtime, year, month, day, hour, minute, second int64) *Struct

DateTimeºInts returns time

func DateºInts added in v1.6.0

func DateºInts(rt *Runtime, year, month, day int64) *Struct

DateºInts returns time

func FileInfoºStr added in v1.6.0

func FileInfoºStr(rt *Runtime, name string) (*Struct, error)

FileInfoºStr returns the finfo describing the named file.

func NewStruct added in v1.6.0

func NewStruct(rt *Runtime, sInfo *core.StructInfo) *Struct

NewStruct creates a new struct object

func Now added in v1.6.0

func Now(rt *Runtime) *Struct

Now returns the current time

func ParseTimeºStrStr added in v1.6.0

func ParseTimeºStrStr(rt *Runtime, layout, value string) (*Struct, error)

ParseTimeºStrStr parses a formatted string and returns the time value it represents

func UTCºTime added in v1.6.0

func UTCºTime(rt *Runtime, local *Struct) *Struct

UTCºTime converts time to UTC time.

func (*Struct) GetIndex added in v1.6.0

func (pstruct *Struct) GetIndex(index interface{}) (interface{}, bool)

GetIndex is part of Indexer interface.

func (*Struct) Len added in v1.6.0

func (pstruct *Struct) Len() int

Len is part of Indexer interface.

func (*Struct) SetIndex added in v1.6.0

func (pstruct *Struct) SetIndex(index, value interface{}) int

SetIndex is part of Indexer interface.

func (Struct) String added in v1.6.0

func (pstruct Struct) String() string

String interface for Struct

type Thread added in v1.6.0

type Thread struct {
	Status byte
	Sleep  int64
	Chan   chan int
	Notify []int64 // who waits the end
}

Thread contains information about a thread

type TraceInfo added in v1.6.0

type TraceInfo struct {
	Path  string // the full path name of the source
	Entry string // the entry function name
	Func  string // the called function
	Line  int64  // line position in the source
	Pos   int64  // column position in the line
}

TraceInfo is a structure for stack func info

func GetTrace added in v1.6.0

func GetTrace(rt *Runtime, pos int64) []TraceInfo

GetTrace returns information about called functions

type VM

type VM struct {
	Settings    Settings
	Exec        *core.Exec
	Consts      map[int32]Const
	Runtimes    []*Runtime
	CtxMutex    sync.RWMutex
	ThreadMutex sync.RWMutex
	Context     map[string]string
	Count       int64 // count of active threads
	ChCount     chan int64
	ChError     chan error
}

VM is the main structure of the virtual machine

Jump to

Keyboard shortcuts

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