ligo

package
v0.0.0-...-6abaa43 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2019 License: GPL-3.0 Imports: 7 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func MatchChars

func MatchChars(ltxt string, off int64, open byte, close byte) int64

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

func ScanTokens(ltxt string) ([]string, error)

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

func StripComments(ltxt string) string

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

const (
	ErrSyntaxError         Error = "Syntax Error"
	ErrNoVariable          Error = "Variable not found in scope"
	ErrFuncNotFound        Error = "Function not defined in scope"
	ErrSignalRecieved      Error = "Caught cancellation amidst evaluation"
	ErrExceptionNotHandled Error = "Exception not handled"
)

Error contants

func (Error) Error

func (le Error) Error() string

Error method implements the error interface for the type Error

type InBuilt

type InBuilt func(*VM, ...Variable) Variable

InBuilt type is a function format that is callable from the ligo script

type Map

type Map map[Variable]Variable

Map type is a ligo equivalent for dictionaly or hash maps

type ProcessCommon

type ProcessCommon struct {
	*sync.Mutex
	// contains filtered or unexported fields
}

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 NewVM

func NewVM() *VM

NewVM returns a new VM object pointer after initializing the values

func (VM) BreakChunk

func (vm VM) BreakChunk(ltxt string) ([]string, error)

BreakChunk is used to break a chunk of ligo code into string list of subexps

func (*VM) Clone

func (vm *VM) Clone() *VM

Clone method is used to clone the VM and return the clone one.

func (*VM) CreateNamespace

func (vm *VM) CreateNamespace(ns string) *VM

CreateNamespace method is used to create a new namespace if it doesn't exist

func (*VM) Eval

func (vm *VM) Eval(stmt string) (Variable, error)

Eval method is used to parse a passed string and evaluate it. This is the entry point for any proper execution.

func (*VM) GetNameSpace

func (vm *VM) GetNameSpace(ns string) *VM

GetNameSpace method is used to get the namespace scope corresponding to the name passed

func (*VM) GetVariable

func (vm *VM) GetVariable(token string) (Variable, error)

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

func (vm *VM) LoadReader(input io.Reader) error

LoadReader method is used to load script from a io.Reader and evaluate it

func (*VM) NewScope

func (vm *VM) NewScope() *VM

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

func (vm *VM) RunDefined(function Defined, vars []Variable) (Variable, error)

RunDefined method is an outlet of the runDefinedFunction function

func (*VM) Stop

func (vm *VM) Stop()

Stop method is used to stop the current process and return an error value.

func (*VM) Throw

func (vm *VM) Throw(exception string) Variable

Throw method is used to throw an exception in the VM.

type Variable

type Variable struct {
	Type  Type
	Value interface{}
}

Variable is a struct denoting a value in the VM

func (Variable) GetTypeString

func (v Variable) GetTypeString() (tp string)

GetTypeString method returns a string value corresponding to the type of it's value

func (Variable) String

func (v Variable) String() string

String method implements the Stringer interface for the Variable type

Jump to

Keyboard shortcuts

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