Documentation ¶
Overview ¶
Package ligo provides implementations for running lisp like scripts
Usage ¶
Using this package is very simple.
Create a new VM instance - Define some functions in the VM - Run any ligo code
Sample ¶
In this sample, a new function called printHello is added to VM This can be called from the ligo code. Running ligo is as simple as vm.Eval(code). The package itself contains only the basic parsing and running functionality. No arithmetic or logical functions are added. For implementing it yourslef, just look at the docs in the github repo : https://github.com/aki237/ligo
func printHello(vm *VM, a ...ligo.Variable) ligo.Variable { fmt.Println ("Hello from ligo!!") return ligo.Variable{ligo.TypeNil, nil} } func main() { vm := ligo.NewVM() vm.Funcs["hello"] = printHello vm.Eval("(hello)") }
The above gives an output :
Hello from ligo!!
Index ¶
- func MatchChars(ltxt string, off int64, open byte, close byte) int64
- func ScanTokens(ltxt string) ([]string, error)
- func StripComments(ltxt string) string
- type Defined
- type Error
- type InBuilt
- type Map
- type ProcessCommon
- type Type
- type VM
- func (vm VM) BreakChunk(ltxt string) ([]string, error)
- func (vm *VM) Clone() *VM
- func (vm *VM) CreateNamespace(ns string) *VM
- func (vm *VM) Eval(stmt string) (Variable, error)
- func (vm *VM) GetNameSpace(ns string) *VM
- func (vm *VM) GetVariable(token string) (Variable, error)
- func (vm *VM) LoadReader(input io.Reader) error
- func (vm *VM) NewScope() *VM
- func (vm *VM) Resume()
- func (vm *VM) RunDefined(function Defined, vars []Variable) (Variable, error)
- func (vm *VM) Stop()
- func (vm *VM) Throw(exception string) Variable
- type Variable
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MatchChars ¶
MatchChars function is used to return the offset at which the matching character of the passed character is found in the passed string. Generally used to match brackets.
func ScanTokens ¶
ScanTokens is used to get token list from a passed ligo expression. This is one of the important functions for parsing a ligo source code.
func StripComments ¶
StripComments function is used to strip the comments from the passed ligo source
Types ¶
type Defined ¶
type Defined struct {
// contains filtered or unexported fields
}
Defined struct contains variables needed for storing a function defined in ligo script itself
type Error ¶
type Error string
Error is a type string used to denote errors from the VM
type ProcessCommon ¶
ProcessCommon is a struct type for process control and signal dispatch
type Type ¶
type Type int
Type is a type to denote the type of Variables in the VM
const ( TypeErr Type = -0x00 TypeInt Type = 0x000 TypeFloat Type = 0x001 TypeBool Type = 0x002 TypeString Type = 0x003 TypeNil Type = 0x004 TypeIFunc Type = 0x005 TypeDFunc Type = 0x006 TypeExp Type = 0x007 TypeArray Type = 0x100 TypeMap Type = 0x300 TypeStruct Type = 0x400 )
Required constants for the variable type
type VM ¶
type VM struct { Vars map[string]Variable Funcs map[string]InBuilt LFuncs map[string]Defined // contains filtered or unexported fields }
VM struct is a State Struct contains all the variable maps, defined function maps, in-built function maps and a global scope pointing to the global Scope VM
func (VM) BreakChunk ¶
BreakChunk is used to break a chunk of ligo code into string list of subexps
func (*VM) CreateNamespace ¶
CreateNamespace method is used to create a new namespace if it doesn't exist
func (*VM) Eval ¶
Eval method is used to parse a passed string and evaluate it. This is the entry point for any proper execution.
func (*VM) GetNameSpace ¶
GetNameSpace method is used to get the namespace scope corresponding to the name passed
func (*VM) GetVariable ¶
GetVariable method is used to process the token string passed and get the corresponding value from the VM's memory. This is a crucial function as, if the token passed is a sub expression this method knows to evaluate and return the value of that sub expression.
func (*VM) LoadReader ¶
LoadReader method is used to load script from a io.Reader and evaluate it
func (*VM) NewScope ¶
NewScope method is used to create a new vm with global scope set from the current VM. (if the current vm is the parent vm, then it is set as the global, else the global of the current vm is set )
func (*VM) Resume ¶
func (vm *VM) Resume()
Resume method is used to resume the normal evaluation by releasing the lock on the mutex of the process control. This should never be called in this package itself. Resume should be used only when a error returned is ErrSignalRecieved in the main package. See the sample interpreter implementation in https://github.com/aki237/ligo/tree/master/cmd/ligo.
func (*VM) RunDefined ¶
RunDefined method is an outlet of the runDefinedFunction function
type Variable ¶
type Variable struct { Type Type Value interface{} }
Variable is a struct denoting a value in the VM
func (Variable) GetTypeString ¶
GetTypeString method returns a string value corresponding to the type of it's value