jsmm

package
v0.0.0-...-064bbc6 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2016 License: Apache-2.0 Imports: 12 Imported by: 0

README

To update the parser parser.y run the following command:

go tool yacc parser.y

To do some unit testing, run

go test

Documentation

Index

Constants

View Source
const (

	/**/
	I_COUNT
)
View Source
const (
	TAG = "jsmm"
)
View Source
const UNARY = 57362

Variables

View Source
var NullConst = &Null{}
View Source
var Ops = [I_COUNT]Op{
	{"load_const", 4,
		func(m *Machine) *MachineException {
			m.Push(m.GetConst(m.GetIParam()))
			return nil
		}},
	{"get_global", 1,
		func(m *Machine) *MachineException {
			m.Push(&m.context)
			return nil
		}},
	{"get_index", 1,
		func(m *Machine) *MachineException {
			objectref := m.Get(-2)
			key := m.Get(-1).ToString()
			m.Pop(2)
			r, err := objectref.GetProperty(key)
			if err != nil {
				return err
			}
			m.Push(r)
			return nil
		}},
	{"set_index", 1,
		func(m *Machine) *MachineException {
			objectref := m.Get(-3)
			key := m.Get(-2).ToString()
			val := m.Get(-1)
			m.Pop(2)
			return objectref.SetProperty(key, val)
		}},
	{"array_append", 1,
		func(m *Machine) *MachineException {
			arrayref := m.Get(-2)
			val := m.Get(-1)
			m.Pop(1)
			if array, ok := arrayref.(*Array); ok {
				array.Append(val)
				return nil
			}
			return NewMachineException("array_append called on something not a table")
		}},
	{"add", 1,
		func(m *Machine) *MachineException {
			a := m.Get(-2)
			b := m.Get(-1)
			m.Pop(2)

			if a.Type() == TypeString || b.Type() == TypeString {
				m.Push(NewString(a.ToString() + b.ToString()))
			} else {
				m.Push(NewNumber(a.ToNumber() + b.ToNumber()))
			}
			return nil
		}},
	{"sub", 1,
		func(m *Machine) *MachineException {
			a := m.Get(-2).ToNumber()
			b := m.Get(-1).ToNumber()
			m.Pop(2)
			m.Push(NewNumber(a - b))
			return nil
		}},
	{"mul", 1,
		func(m *Machine) *MachineException {
			a := m.Get(-2).ToNumber()
			b := m.Get(-1).ToNumber()
			m.Pop(2)
			m.Push(NewNumber(a * b))
			return nil
		}},
	{"div", 1,
		func(m *Machine) *MachineException {
			a := m.Get(-2).ToNumber()
			b := m.Get(-1).ToNumber()
			m.Pop(2)
			if b == 0 {
				if a == 0 {
					m.Push(NewNumber(math.NaN()))
				} else {
					m.Push(NewNumber(math.Inf(int(a))))
				}
			} else {
				m.Push(NewNumber(a / b))
			}
			return nil
		}},
	{"mod", 1,
		func(m *Machine) *MachineException {
			a := m.Get(-2).ToNumber()
			b := m.Get(-1).ToNumber()
			m.Pop(2)
			m.Push(NewNumber(math.Mod(a, b)))
			return nil
		}},
	{"equ", 1,
		func(m *Machine) *MachineException {
			a := m.Get(-2)
			b := m.Get(-1)
			m.Pop(2)
			m.Push(NewBoolean(equal(a, b)))
			return nil
		}},
	{"neq", 1,
		func(m *Machine) *MachineException {
			a := m.Get(-2)
			b := m.Get(-1)
			m.Pop(2)
			m.Push(NewBoolean(!equal(a, b)))
			return nil
		}},
	{"lt", 1,
		func(m *Machine) *MachineException {
			a := m.Get(-2)
			b := m.Get(-1)
			m.Pop(2)
			m.Push(NewBoolean(lessThan(a, b)))
			return nil
		}},
	{"gt", 1,
		func(m *Machine) *MachineException {
			a := m.Get(-2)
			b := m.Get(-1)
			m.Pop(2)
			m.Push(NewBoolean(!equal(a, b) && !lessThan(a, b)))
			return nil
		}},
	{"lte", 1,
		func(m *Machine) *MachineException {
			a := m.Get(-2)
			b := m.Get(-1)
			m.Pop(2)
			m.Push(NewBoolean(lessThan(a, b) || equal(a, b)))
			return nil
		}},
	{"gte", 1,
		func(m *Machine) *MachineException {
			a := m.Get(-2)
			b := m.Get(-1)
			m.Pop(2)
			m.Push(NewBoolean(!lessThan(a, b)))
			return nil
		}},
	{"and", 1,
		func(m *Machine) *MachineException {
			a := m.Get(-2)
			b := m.Get(-1)
			m.Pop(2)
			if a.ToBoolean() == false {
				m.Push(a)
			} else {
				m.Push(b)
			}
			return nil
		}},
	{"or", 1,
		func(m *Machine) *MachineException {
			a := m.Get(-2)
			b := m.Get(-1)
			m.Pop(2)
			if a.ToBoolean() == true {
				m.Push(a)
			} else {
				m.Push(b)
			}
			return nil
		}},
	{"not", 1,
		func(m *Machine) *MachineException {
			a := m.Get(-1).ToBoolean()
			m.Pop(1)
			m.Push(NewBoolean(!a))
			return nil
		}},
	{"neg", 1,
		func(m *Machine) *MachineException {
			a := m.Get(-1).ToNumber()
			m.Pop(1)
			m.Push(NewNumber(-a))
			return nil
		}},
	{"call", 4,
		func(m *Machine) *MachineException {
			objectref := m.Get(-2)
			key := m.Get(-1).ToString()
			paramcount := m.GetIParam()
			m.Pop(1)

			functionref, err := objectref.GetProperty(key)
			if err != nil {
				return err
			}
			if functionref.Type() == TypeFunction {
				if function, ok := functionref.(*Function); ok {
					result_len, merror := function.Call(m, paramcount)
					if merror == nil {
						if result_len == 1 {
							result := m.Get(-1)
							m.Pop(paramcount + 1)
							m.Push(result)
						} else {
							m.Pop(paramcount)
						}
					}
					return merror
				}
			}
			return NewMachineException("TypeError: '%s' is not a function", key)
		}},
	{"newarray", 1,
		func(m *Machine) *MachineException {

			array := NewArray()

			m.Push(array)
			return nil
		}},
	{"newobject", 1,
		func(m *Machine) *MachineException {
			object := NewObject()
			m.Push(object)
			return nil
		}},
}

Functions

func DumpCode

func DumpCode(w io.Writer, m *Machine)

func DumpStack

func DumpStack(w io.Writer, m *Machine)

func ImportGlobal

func ImportGlobal(m *Machine, name string, item interface{}) error

func IsUInt32

func IsUInt32(s string) (uint32, bool)

func ToUInt32

func ToUInt32(val MachineValue) uint32

func TypeOf

func TypeOf(m MachineValue) string

Types

type Array

type Array struct {
	Object
	// contains filtered or unexported fields
}

func NewArray

func NewArray(init ...MachineValue) *Array

func (*Array) Append

func (a *Array) Append(val MachineValue)

func (*Array) GetProperty

func (a *Array) GetProperty(prop string) (MachineValue, *MachineException)

func (*Array) GetUInt32Property

func (a *Array) GetUInt32Property(prop uint32) (MachineValue, *MachineException)

func (*Array) Push

func (a *Array) Push(val MachineValue)

func (*Array) SetProperty

func (a *Array) SetProperty(prop string, val MachineValue) *MachineException

func (*Array) SetUInt32Property

func (a *Array) SetUInt32Property(prop uint32, val MachineValue) *MachineException

func (*Array) ToJSON

func (a *Array) ToJSON() string

func (*Array) ToString

func (a *Array) ToString() string

func (*Array) Type

func (a *Array) Type() MachineType

type Boolean

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

The Boolean type

func (*Boolean) GetProperty

func (b *Boolean) GetProperty(prop string) (MachineValue, *MachineException)

func (*Boolean) SetProperty

func (b *Boolean) SetProperty(prop string, val MachineValue) *MachineException

func (*Boolean) ToBoolean

func (b *Boolean) ToBoolean() bool

func (*Boolean) ToJSON

func (b *Boolean) ToJSON() string

func (*Boolean) ToNumber

func (b *Boolean) ToNumber() float64

func (*Boolean) ToString

func (b *Boolean) ToString() string

func (*Boolean) Type

func (b *Boolean) Type() MachineType

type Expression

type Expression interface {
	String() string
	Compile(m *Machine)
}

func Parse

func Parse(s string) (Expression, error)

type ExpressionList

type ExpressionList []Expression

func NewExpressionList

func NewExpressionList() ExpressionList

func (ExpressionList) Append

func (ExpressionList) Get

func (e ExpressionList) Get(i int) Expression

func (ExpressionList) Length

func (e ExpressionList) Length() int

func (ExpressionList) String

func (e ExpressionList) String() string

type Function

type Function struct {
	Object
	// contains filtered or unexported fields
}

func NewFunction

func NewFunction(name string, call JSFunction) *Function

func (*Function) Call

func (f *Function) Call(m *Machine, paramcount int) (int, *MachineException)

func (*Function) ToBoolean

func (f *Function) ToBoolean() bool

func (*Function) ToJSON

func (f *Function) ToJSON() string

func (*Function) ToNumber

func (f *Function) ToNumber() float64

func (*Function) ToString

func (f *Function) ToString() string

func (*Function) Type

func (f *Function) Type() MachineType

type JSFunction

type JSFunction func(*Machine, *Function, int) (int, *MachineException)

type Machine

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

func Compile

func Compile(expr string) (*Machine, error)

func NewMachine

func NewMachine() *Machine

func (*Machine) AddConst

func (m *Machine) AddConst(c MachineValue) int

func (*Machine) AddI

func (m *Machine) AddI(i byte) *Machine

func (*Machine) AddIParam

func (m *Machine) AddIParam(i byte, p int) *Machine

func (*Machine) Call

func (m *Machine) Call(pc int) *MachineException

func (*Machine) DebugMode

func (m *Machine) DebugMode(debug bool)

func (*Machine) Execute

func (m *Machine) Execute() (MachineValue, *MachineException)

func (*Machine) Get

func (m *Machine) Get(pos int) MachineValue

func (*Machine) GetConst

func (m *Machine) GetConst(pos int) MachineValue

func (*Machine) GetIParam

func (m *Machine) GetIParam() int

func (*Machine) GetIParamAt

func (m *Machine) GetIParamAt(index int) int

func (*Machine) GlobalObject

func (m *Machine) GlobalObject() *Object

func (*Machine) Pop

func (m *Machine) Pop(count int) int

func (*Machine) Push

func (m *Machine) Push(e MachineValue) int

func (*Machine) Top

func (m *Machine) Top() int

type MachineException

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

func ArrayMax

func ArrayMax(m *Machine, fn *Function, paramCount int) (int, *MachineException)

func ArrayMin

func ArrayMin(m *Machine, fn *Function, paramCount int) (int, *MachineException)

func MatchRegexp

func MatchRegexp(m *Machine, fn *Function, paramCount int) (int, *MachineException)

func NewMachineException

func NewMachineException(format string, args ...interface{}) *MachineException

func Select

func Select(m *Machine, fn *Function, paramCount int) (int, *MachineException)

func TimeUTC

func TimeUTC(m *Machine, fn *Function, paramCount int) (int, *MachineException)

func ToArray

func ToArray(m *Machine, fn *Function, paramCount int) (int, *MachineException)

func ToBoolean

func ToBoolean(m *Machine, fn *Function, paramCount int) (int, *MachineException)

func ToJSON

func ToJSON(m *Machine, fn *Function, paramCount int) (int, *MachineException)

func ToNumber

func ToNumber(m *Machine, fn *Function, paramCount int) (int, *MachineException)

func ToString

func ToString(m *Machine, fn *Function, paramCount int) (int, *MachineException)

func (*MachineException) Error

func (e *MachineException) Error() string

type MachineType

type MachineType int
const (
	TypeError MachineType = iota
	TypeNull
	TypeBoolean
	TypeNumber
	TypeString
	TypeObject
	TypeArray
	TypeFunction
)

type MachineValue

type MachineValue interface {
	Type() MachineType
	ToString() string
	ToNumber() float64
	ToBoolean() bool
	ToJSON() string
	GetProperty(prop string) (MachineValue, *MachineException)
	SetProperty(prop string, val MachineValue) *MachineException
}

func Import

func Import(item interface{}) (MachineValue, error)

func NewBoolean

func NewBoolean(b bool) MachineValue

func NewBooleanString

func NewBooleanString(v string) MachineValue

func NewNull

func NewNull() MachineValue

func NewNumber

func NewNumber(f float64) MachineValue

func NewNumberString

func NewNumberString(v string) MachineValue

func NewString

func NewString(v string) MachineValue

type Null

type Null struct{}

The null type

func (*Null) GetProperty

func (u *Null) GetProperty(prop string) (MachineValue, *MachineException)

func (*Null) SetProperty

func (u *Null) SetProperty(prop string, val MachineValue) *MachineException

func (*Null) ToBoolean

func (u *Null) ToBoolean() bool

func (*Null) ToJSON

func (u *Null) ToJSON() string

func (*Null) ToNumber

func (u *Null) ToNumber() float64

func (*Null) ToString

func (u *Null) ToString() string

func (*Null) Type

func (u *Null) Type() MachineType

type Number

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

The Number type

func (*Number) GetProperty

func (n *Number) GetProperty(prop string) (MachineValue, *MachineException)

func (*Number) SetProperty

func (n *Number) SetProperty(prop string, val MachineValue) *MachineException

func (*Number) ToBoolean

func (n *Number) ToBoolean() bool

func (*Number) ToJSON

func (n *Number) ToJSON() string

func (*Number) ToNumber

func (n *Number) ToNumber() float64

func (*Number) ToString

func (n *Number) ToString() string

func (*Number) Type

func (n *Number) Type() MachineType

type Object

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

func CreateObjectWithPrototype

func CreateObjectWithPrototype(klass string, prototype MachineValue) Object

func NewObject

func NewObject() *Object

func (*Object) GetProperty

func (o *Object) GetProperty(prop string) (MachineValue, *MachineException)

func (*Object) SetProperty

func (o *Object) SetProperty(prop string, val MachineValue) *MachineException

func (*Object) ToBoolean

func (o *Object) ToBoolean() bool

func (*Object) ToJSON

func (o *Object) ToJSON() string

func (*Object) ToNumber

func (o *Object) ToNumber() float64

func (*Object) ToString

func (o *Object) ToString() string

func (*Object) Type

func (o *Object) Type() MachineType

type Op

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

type ParseError

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

func NewParseError

func NewParseError(format string, args ...interface{}) *ParseError

func (*ParseError) Error

func (e *ParseError) Error() string

type String

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

The String type

func (*String) GetProperty

func (s *String) GetProperty(prop string) (MachineValue, *MachineException)

func (*String) SetProperty

func (s *String) SetProperty(prop string, val MachineValue) *MachineException

func (*String) ToBoolean

func (s *String) ToBoolean() bool

func (*String) ToJSON

func (s *String) ToJSON() string

func (*String) ToNumber

func (s *String) ToNumber() float64

func (*String) ToString

func (s *String) ToString() string

func (*String) Type

func (s *String) Type() MachineType

Jump to

Keyboard shortcuts

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