runtime

package
v0.14.2 Latest Latest
Warning

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

Go to latest
Published: Feb 17, 2022 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	UNKNOWN      ObjectType = "unknown object"
	UNDEFINED    ObjectType = "undefined value"
	ERROR        ObjectType = "runtime error"
	BREAK        ObjectType = "break event"
	CONTINUE     ObjectType = "continue event"
	RETURN_VALUE ObjectType = "return value"
	INTEGER      ObjectType = "integer"
	FLOAT        ObjectType = "float"
	BOOL         ObjectType = "boolean"
	STRING       ObjectType = "string"
	BITSTRING    ObjectType = "bitstring"
	FUNCTION     ObjectType = "function"
	LIST         ObjectType = "list"
	RECORD       ObjectType = "record"
	MAP          ObjectType = "map"
	BUILTIN_OBJ  ObjectType = "builtin function"
	VERDICT      ObjectType = "verdict"

	Bit    Unit = 1
	Hex    Unit = 4
	Octett Unit = 8

	NoneVerdict   Verdict = "none"
	PassVerdict   Verdict = "pass"
	InconcVerdict Verdict = "inconc"
	FailVerdict   Verdict = "fail"
	ErrorVerdict  Verdict = "error"
)

Variables

View Source
var (
	ErrSyntax = errors.New("invalid syntax")
	Undefined = &singelton{typ: UNDEFINED}
	Break     = &singelton{typ: BREAK}
	Continue  = &singelton{typ: CONTINUE}
)
View Source
var Builtins = map[string]*Builtin{
	"lengthof": {Fn: func(args ...Object) Object {
		if len(args) != 1 {
			return Errorf("wrong number of arguments. got=%d, want=1", len(args))
		}

		switch arg := args[0].(type) {
		case *String:
			return Int{Int: big.NewInt(int64(len(arg.Value)))}
		case *Bitstring:
			return Int{Int: big.NewInt(int64(arg.Value.BitLen() / int(arg.Unit)))}

		}
		return Errorf("%s arguments not supported", args[0].Type())
	}},

	"rnd": {Fn: func(args ...Object) Object {
		if len(args) != 0 {
			return Errorf("wrong number of arguments. got=%d, want=0", len(args))
		}

		return Float(rand.Float64())
	}},

	"int2float": {Fn: func(args ...Object) Object {
		if len(args) != 1 {
			return Errorf("wrong number of arguments. got=%d, want=1", len(args))
		}

		i, ok := args[0].(Int)
		if !ok {
			return Errorf("%s arguments not supported", args[0].Type())
		}

		f, _ := new(big.Float).SetInt(i.Int).Float64()
		return Float(f)
	}},

	"float2int": {Fn: func(args ...Object) Object {
		if len(args) != 1 {
			return Errorf("wrong number of arguments. got=%d, want=1", len(args))
		}

		f, ok := args[0].(Float)
		if !ok {
			return Errorf("%s arguments not supported", args[0].Type())
		}

		i, _ := new(big.Float).SetFloat64(float64(f)).Int(nil)
		return Int{Int: i}
	}},

	"log": {Fn: func(args ...Object) Object {
		var ss []string
		for _, arg := range args {
			ss = append(ss, arg.Inspect())
		}
		fmt.Println(strings.Join(ss, " "))
		return nil
	}},
}

Functions

func EqualObjects added in v0.9.6

func EqualObjects(a, b []Object) bool

EqualObjects compares two Object slices for equality.

func IsError

func IsError(v interface{}) bool

Types

type Bitstring added in v0.9.5

type Bitstring struct {
	Value *big.Int
	Unit  Unit
}

func NewBitstring added in v0.9.5

func NewBitstring(s string) (*Bitstring, error)

func (*Bitstring) Equal added in v0.9.6

func (b *Bitstring) Equal(obj Object) bool

func (*Bitstring) Inspect added in v0.9.5

func (b *Bitstring) Inspect() string

func (*Bitstring) Type added in v0.9.5

func (b *Bitstring) Type() ObjectType

type Bool

type Bool bool

func NewBool

func NewBool(b bool) Bool

func (Bool) Bool

func (b Bool) Bool() bool

func (Bool) Equal added in v0.9.6

func (b Bool) Equal(obj Object) bool

func (Bool) Inspect

func (b Bool) Inspect() string

func (Bool) Type

func (b Bool) Type() ObjectType

type Builtin added in v0.9.5

type Builtin struct {
	Fn func(args ...Object) Object
}

func (*Builtin) Equal added in v0.9.6

func (b *Builtin) Equal(obj Object) bool

func (*Builtin) Inspect added in v0.9.5

func (b *Builtin) Inspect() string

func (*Builtin) Type added in v0.9.5

func (b *Builtin) Type() ObjectType

type Env

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

func NewEnv

func NewEnv(outer Scope) *Env

func (*Env) Get

func (env *Env) Get(name string) (Object, bool)

func (*Env) Set

func (env *Env) Set(name string, val Object) Object

type Error

type Error struct {
	Message string
}

func Errorf

func Errorf(format string, a ...interface{}) *Error

func (*Error) Equal added in v0.9.6

func (e *Error) Equal(obj Object) bool

func (*Error) Error

func (e *Error) Error() string

func (*Error) Inspect

func (e *Error) Inspect() string

func (*Error) Type

func (e *Error) Type() ObjectType

type Float

type Float float64

func NewFloat

func NewFloat(s string) Float

func (Float) Equal added in v0.9.6

func (f Float) Equal(obj Object) bool

func (Float) Inspect

func (f Float) Inspect() string

func (Float) Type

func (f Float) Type() ObjectType

type Function

type Function struct {
	Params *ast.FormalPars
	Body   *ast.BlockStmt
	Env    Scope
}

func (*Function) Equal added in v0.9.6

func (f *Function) Equal(obj Object) bool

func (*Function) Inspect

func (f *Function) Inspect() string

func (*Function) Type

func (f *Function) Type() ObjectType

type Int

type Int struct{ *big.Int }

func NewInt

func NewInt(s string) Int

func (Int) Equal added in v0.9.6

func (i Int) Equal(obj Object) bool

func (Int) Inspect

func (i Int) Inspect() string

func (Int) Type

func (i Int) Type() ObjectType

func (Int) Value

func (i Int) Value() *big.Int

type List added in v0.9.5

type List struct {
	Elements []Object
}

func (*List) Equal added in v0.9.6

func (l *List) Equal(obj Object) bool

func (*List) Inspect added in v0.9.5

func (l *List) Inspect() string

func (*List) Type added in v0.9.5

func (l *List) Type() ObjectType

type Map added in v0.9.6

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

Map is a map of objects.

func NewMap added in v0.9.6

func NewMap() *Map

func (*Map) Equal added in v0.9.6

func (m *Map) Equal(obj Object) bool

func (*Map) Get added in v0.9.6

func (m *Map) Get(key Object) (Object, bool)

Get returns the value for the given key.

func (*Map) Inspect added in v0.9.6

func (m *Map) Inspect() string

func (*Map) Set added in v0.9.6

func (m *Map) Set(key Object, val Object) Object

func (*Map) Type added in v0.9.6

func (m *Map) Type() ObjectType

type Object

type Object interface {
	Inspect() string
	Type() ObjectType
	Equal(Object) bool
}

type ObjectType

type ObjectType string

type Record added in v0.9.6

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

TODO(5nord) For simplicity we reuse the Map implementation. We should implement proper record semantics later.

func NewRecord added in v0.9.6

func NewRecord() *Record

func (*Record) Equal added in v0.9.6

func (r *Record) Equal(obj Object) bool

func (*Record) Get added in v0.9.6

func (r *Record) Get(name string) (Object, bool)

func (*Record) Inspect added in v0.9.6

func (r *Record) Inspect() string

func (*Record) Set added in v0.9.6

func (r *Record) Set(name string, val Object) Object

func (*Record) Type added in v0.9.6

func (r *Record) Type() ObjectType

type ReturnValue

type ReturnValue struct {
	Value Object
}

func (*ReturnValue) Equal added in v0.9.6

func (r *ReturnValue) Equal(obj Object) bool

func (*ReturnValue) Inspect

func (r *ReturnValue) Inspect() string

func (*ReturnValue) Type

func (r *ReturnValue) Type() ObjectType

type Scope added in v0.9.6

type Scope interface {
	Get(name string) (Object, bool)
	Set(name string, val Object) Object
}

type String

type String struct {
	Value string
}

func (*String) Equal added in v0.9.6

func (s *String) Equal(obj Object) bool

func (*String) Inspect

func (s *String) Inspect() string

func (*String) Type

func (s *String) Type() ObjectType

type Unit

type Unit int

func (Unit) Base

func (u Unit) Base() int

type Verdict

type Verdict string

func (Verdict) Equal added in v0.9.6

func (v Verdict) Equal(obj Object) bool

func (Verdict) Inspect

func (v Verdict) Inspect() string

func (Verdict) Type

func (v Verdict) Type() ObjectType

Jump to

Keyboard shortcuts

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