runtime

package
v0.0.0-...-d63bf21 Latest Latest
Warning

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

Go to latest
Published: Dec 16, 2021 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// BlockExitNormally ..
	BlockExitNormally = newObject(ObjectType)
	// BlockExitReturn ..
	BlockExitReturn = newObject(ObjectType)
	// BlockExitContinue ..
	BlockExitContinue = newObject(ObjectType)
	// BlockExitBreak ..
	BlockExitBreak = newObject(ObjectType)
)
View Source
var (
	// Eq ..
	Eq = makeCompareFn(compare, compareEq)
	// Ne ..
	Ne = makeCompareFn(compare, compareNe)
	// Lt ..
	Lt = makeCompareFn(compare, compareLt)
	// Gt ..
	Gt = makeCompareFn(compare, compareGt)
	// Le ..
	Le = makeCompareFn(compare, compareLe)
	// Ge ..
	Ge = makeCompareFn(compare, compareGe)
)
View Source
var (
	// True ..
	True = newInt(BoolType, 1)
	// False ..
	False = newInt(BoolType, 0)
)
View Source
var (
	// ExceptionType ..
	ExceptionType = newBuiltinType("Exception", BaseExceptionType)
	// TypeErrorType ..
	TypeErrorType = newBuiltinType("TypeError", ExceptionType)
	// ValueErrorType ..
	ValueErrorType = newBuiltinType("ValueError", ExceptionType)
	// RuntimeErrorType ..
	RuntimeErrorType = newBuiltinType("RuntimeError", ExceptionType)
	// NotImplementedErrorType ..
	NotImplementedErrorType = newBuiltinType("NotImplementedError", RuntimeErrorType)
	// LookupErrorType ..
	LookupErrorType = newBuiltinType("LookupError", ExceptionType)
	// KeyErrorType ..
	KeyErrorType = newBuiltinType("KeyError", LookupErrorType)
	// IndexErrorType ..
	IndexErrorType = newBuiltinType("IndexError", LookupErrorType)
	// StopIterationType ..
	StopIterationType = newBuiltinType("StopIteration", ExceptionType)
	// AttributeErrorType ..
	AttributeErrorType = newBuiltinType("AttributeError", ExceptionType)
	// AssertionErrorType ..
	AssertionErrorType = newBuiltinType("AssertionError", ExceptionType)
	// ImportErrorType ..
	ImportErrorType = newBuiltinType("ImportError", ExceptionType)
	// UnicodeErrorType ..
	UnicodeErrorType = newBuiltinType("UnicodeError", ValueErrorType)
	// UnicodeEncodeErrorType ..
	UnicodeEncodeErrorType = newBuiltinType("UnicodeEncodeError", UnicodeErrorType)
	// UnicodeDecodeErrorType ..
	UnicodeDecodeErrorType = newBuiltinType("UnicodeDecodeError", UnicodeErrorType)
	// OSErrorType ..
	OSErrorType = newBuiltinType("OSError", ExceptionType)
	// ArithmeticErrorType ..
	ArithmeticErrorType = newBuiltinType("ArithmeticError", ExceptionType)
	// OverflowErrorType ..
	OverflowErrorType = newBuiltinType("OverflowError", ArithmeticErrorType)
	// ZeroDivisionErrorType ..
	ZeroDivisionErrorType = newBuiltinType("ZeroDivisionError", ArithmeticErrorType)
)
View Source
var (
	// NoneType ..
	NoneType = newBuiltinType("NoneType")
	// None ..
	None = newObject(NoneType)
)
View Source
var (
	// NotImplementedType ..
	NotImplementedType = newBuiltinType("NotImplementedType")
	// NotImplemented ..
	NotImplemented = newObject(NotImplementedType)
)
View Source
var AfterLast = newObject(ObjectType)

AfterLast is a singleton object used to signal iteration end in Next(iter, AfterLast) calls.

View Source
var BaseExceptionType = newBuiltinType("BaseException")

BaseExceptionType ..

View Source
var BoolType = newBuiltinType("bool", IntType)

BoolType ..

View Source
var BuiltinAll = newBuiltinFunction("all", func(args Args, kwArgs KWArgs) Object {
	checkFunctionArgs("all", args, kwArgs, ObjectType)
	hasFalse := seqFindFirst(args[0], func(o Object) bool {
		return !IsTrue(o)
	})
	return NewBool(!hasFalse)
})

BuiltinAll ..

View Source
var BuiltinAny = newBuiltinFunction("any", func(args Args, kwArgs KWArgs) Object {
	checkFunctionArgs("any", args, kwArgs, ObjectType)
	hasTrue := seqFindFirst(args[0], func(o Object) bool {
		return IsTrue(o)
	})
	return NewBool(hasTrue)
})

BuiltinAny ..

View Source
var BuiltinCallable = newBuiltinFunction("callable", func(args Args, kwArgs KWArgs) Object {
	checkFunctionArgs("callable", args, kwArgs, ObjectType)
	callFn := args[0].Type().Slots().Call
	return NewBool(callFn != nil)
})

BuiltinCallable ..

View Source
var BuiltinChr = newBuiltinFunction("chr", func(args Args, kwArgs KWArgs) Object {
	checkFunctionArgs("ord", args, kwArgs, IntType)
	i := args[0].(Int).Value()
	if i < 0 || i >= 0x110000 {
		panic(RaiseType(ValueErrorType, "chr() arg not in range(0x110000)"))
	}
	return NewStr(string(rune(i)))
})

BuiltinChr ..

View Source
var BuiltinGetAttr = newBuiltinFunction("getattr", func(args Args, kwArgs KWArgs) Object {
	checkFunctionArgsMin("getattr", args, kwArgs, 2, ObjectType, StrType, ObjectType)
	o := args[0]
	name := args[1].(Str).Value()
	var def Object
	if len(args) >= 3 {
		def = args[2]
	}
	return GetAttr(o, name, def)
})

BuiltinGetAttr ..

View Source
var BuiltinHasAttr = newBuiltinFunction("hasattr", func(args Args, kwArgs KWArgs) Object {
	checkFunctionArgs("hasattr", args, kwArgs, ObjectType, StrType)
	o := args[0]
	name := args[1].(Str).Value()
	return NewBool(HasAttr(o, name))
})

BuiltinHasAttr ..

View Source
var BuiltinHash = newBuiltinFunction("hash", func(args Args, kwArgs KWArgs) Object {
	checkFunctionArgs("hash", args, kwArgs, ObjectType)
	return Hash(args[0])
})

BuiltinHash ..

View Source
var BuiltinIsInstance = newBuiltinFunction("isinstance", func(args Args, kwArgs KWArgs) Object {
	checkFunctionArgs("isinstance", args, kwArgs, ObjectType, ObjectType)
	return NewBool(IsInstance(args[0], args[1]))
})

BuiltinIsInstance ..

View Source
var BuiltinIsSubclass = newBuiltinFunction("issubclass", func(args Args, kwArgs KWArgs) Object {
	checkFunctionArgs("issubclass", args, kwArgs, ObjectType, ObjectType)
	return NewBool(IsSubclass(args[0], args[1]))
})

BuiltinIsSubclass ..

View Source
var BuiltinIter = newBuiltinFunction("iter", func(args Args, kwArgs KWArgs) Object {
	checkFunctionArgsMin("iter", args, kwArgs, 1, ObjectType, ObjectType)
	if len(args) == 2 {

		panic(RaiseType(NotImplementedErrorType, "iter(callable, sentinel) not implemented"))
	}
	return Iter(args[0])
})

BuiltinIter ..

View Source
var BuiltinLen = newBuiltinFunction("len", func(args Args, kwArgs KWArgs) Object {
	checkFunctionArgs("len", args, kwArgs, ObjectType)
	return Len(args[0])
})

BuiltinLen ..

View Source
var BuiltinMax = newBuiltinFunction("max", func(args Args, kwArgs KWArgs) Object {
	return builtinMinMax(true, args, kwArgs)
})

BuiltinMax ..

View Source
var BuiltinMin = newBuiltinFunction("min", func(args Args, kwArgs KWArgs) Object {
	return builtinMinMax(false, args, kwArgs)
})

BuiltinMin ..

View Source
var BuiltinNext = newBuiltinFunction("next", func(args Args, kwArgs KWArgs) Object {
	checkFunctionArgsMin("next", args, kwArgs, 1, ObjectType, ObjectType)
	if len(args) == 2 {
		return NextDefault(args[0], args[1])
	}
	return Next(args[0])
})

BuiltinNext ..

View Source
var BuiltinOpen = newBuiltinFunction("open", func(args Args, kwArgs KWArgs) Object {
	checkFunctionArgs("open", args, nil, ObjectType)
	panic(RaiseType(NotImplementedErrorType, "open() not implemented"))
})

BuiltinOpen ..

View Source
var BuiltinOrd = newBuiltinFunction("ord", func(args Args, kwArgs KWArgs) Object {
	checkFunctionArgs("ord", args, kwArgs, ObjectType)
	o := args[0]
	errLen := "ord() expected a character, but string of length %d found"
	if o.IsInstance(StrType) {
		r := []rune(o.(Str).Value())
		if len(r) != 1 {
			panic(RaiseType(TypeErrorType, fmt.Sprintf(errLen, len(r))))
		}
		return NewInt(int(r[0]))
	} else if o.IsInstance(BytesType) {
		b := o.(Bytes).Value()
		if len(b) != 1 {
			panic(RaiseType(TypeErrorType, fmt.Sprintf(errLen, len(b))))
		}
		return NewInt(int(b[0]))
	} else {
		panic(RaiseType(TypeErrorType, fmt.Sprintf(
			"ord() expected string of length 1, but %s found", o.Type().Name())))
	}
})

BuiltinOrd ..

View Source
var BuiltinPrint = newBuiltinFunction("print", func(args Args, kwArgs KWArgs) Object {
	checkFunctionKWArgs("print", kwArgs, map[string]Type{
		"sep": StrType,
		"end": StrType,
	})
	sep := kwArgs.get("sep", NewStr(" ")).(Str)
	end := kwArgs.get("end", NewStr("\n")).(Str)
	Print(args, sep, end)
	return None
})

BuiltinPrint ..

View Source
var BuiltinRepr = newBuiltinFunction("repr", func(args Args, kwArgs KWArgs) Object {
	checkFunctionArgs("repr", args, kwArgs, ObjectType)
	return Repr(args[0])
})

BuiltinRepr ..

View Source
var BuiltinSetAttr = newBuiltinFunction("setattr", func(args Args, kwArgs KWArgs) Object {
	checkFunctionArgs("setattr", args, kwArgs, ObjectType, StrType, ObjectType)
	o := args[0]
	name := args[1].(Str).Value()
	value := args[2]
	SetAttr(o, name, value)
	return None
})

BuiltinSetAttr ..

View Source
var BuiltinSorted = newBuiltinFunction("sorted", func(args Args, kwArgs KWArgs) Object {
	checkFunctionArgs("sorted", args, nil, ObjectType)
	checkFunctionKWArgs("sorted", kwArgs, map[string]Type{
		"key":     ObjectType,
		"reverse": IntType,
	})
	l := Cal(ListType, args[0])
	listSort(Args{l}, kwArgs)
	return l
})

BuiltinSorted ..

View Source
var BuiltinSum = newBuiltinFunction("sum", func(args Args, kwArgs KWArgs) Object {
	checkFunctionArgsMin("sum", args, kwArgs, 1, ObjectType, ObjectType)
	var res Object
	if len(args) == 1 {
		res = NewInt(0)
	} else {
		res = args[1]
	}
	seqForEach(args[0], func(o Object) {
		res = Add(res, o)
	})
	return res
})

BuiltinSum ..

View Source
var BytesType = newBuiltinType("bytes")

BytesType ..

View Source
var ClassMethodType = newBuiltinType("classmethod")

ClassMethodType ..

View Source
var DictType = newBuiltinType("dict")

DictType ..

View Source
var EnumerateIteratorType = newBuiltinType("enumerate")

EnumerateIteratorType ..

View Source
var FilterIteratorType = newBuiltinType("filter")

FilterIteratorType ..

View Source
var FloatType = newBuiltinType("float")

FloatType ..

View Source
var FunctionType = newBuiltinType("function")

FunctionType ..

View Source
var GeneratorType = newBuiltinType("generator")

GeneratorType ..

View Source
var IntType = newBuiltinType("int")

IntType ..

View Source
var ListType = newBuiltinType("list")

ListType ..

View Source
var MapIteratorType = newBuiltinType("map")

MapIteratorType ..

View Source
var MethodType = newBuiltinType("method")

MethodType ..

View Source
var NativeType = newBuiltinType("native")

NativeType is the type object of Native objects

View Source
var ObjectType = &typeStruct{
	name:    "object",
	builtin: true,
}

ObjectType ..

View Source
var (
	// PrintingEnabled ..
	PrintingEnabled = true
)
View Source
var PropertyType = newBuiltinType("property")

PropertyType ..

View Source
var RangeIteratorType = newBuiltinType("range_iterator")

RangeIteratorType ..

View Source
var RangeType = newBuiltinType("range")

RangeType ..

View Source
var ReversedIteratorType = newBuiltinType("reversed")

ReversedIteratorType ..

View Source
var SetType = newBuiltinType("set")

SetType ..

View Source
var SliceType = newBuiltinType("slice")

SliceType ..

View Source
var StaticMethodType = newBuiltinType("staticmethod")

StaticMethodType ..

View Source
var StrType = newBuiltinType("str")

StrType ..

View Source
var SuperType = newBuiltinType("super")

SuperType ..

View Source
var TupleType = newBuiltinType("tuple")

TupleType ..

View Source
var TypeType = &typeStruct{
	name:    "type",
	bases:   []Type{ObjectType},
	builtin: true,
}

TypeType ..

View Source
var ZipIteratorType = newBuiltinType("zip")

ZipIteratorType ..

Functions

func Catch

func Catch(o Object, h func(BaseException), e func())

Catch ..

func CatchMulti

func CatchMulti(e func(), catchers ...*Catcher)

CatchMulti ..

func CollectUsedSymbols

func CollectUsedSymbols(c bool)

CollectUsedSymbols sets whether used symbol collection is enabled.

func Contains

func Contains(o Object, needle Object) bool

Contains ..

func DelAttr

func DelAttr(o Object, name string)

DelAttr ..

func DelItem

func DelItem(o Object, key Object)

DelItem ..

func FinalizeGenerators

func FinalizeGenerators()

FinalizeGenerators kills all running generators.

func GetUsedSymbols

func GetUsedSymbols() []string

GetUsedSymbols returns the set of used symbols.

func HasAttr

func HasAttr(o Object, name string) bool

HasAttr ..

func InitModule

func InitModule(init func())

InitModule registers the supplied module initializer.

func InitModules

func InitModules()

InitModules runs the registered module initializers.

func IsInstance

func IsInstance(o Object, classinfo Object) bool

IsInstance ..

func IsSubclass

func IsSubclass(o Object, classinfo Object) bool

IsSubclass ..

func IsTrue

func IsTrue(o Object) bool

IsTrue ..

func Print

func Print(objects []Object, sep Str, end Str)

Print ..

func SetAttr

func SetAttr(o Object, name string, value Object)

SetAttr ..

func SetItem

func SetItem(o Object, key Object, value Object)

SetItem ..

func UnwrapNative

func UnwrapNative(o Object) interface{}

UnwrapNative ..

Types

type Args

type Args []Object

Args ..

func NewArgs

func NewArgs(args ...Object) Args

NewArgs ...

type BaseException

type BaseException interface {
	Object
	Args() Tuple

	String() string
	// contains filtered or unexported methods
}

BaseException ..

type Bytes

type Bytes interface {
	Object
	Value() []byte
}

Bytes ..

func NewBytes

func NewBytes(value ...byte) Bytes

NewBytes ..

type Catcher

type Catcher struct {
	O Object
	H func(BaseException)
}

Catcher ..

type Codec

type Codec interface {
	Name() string
	Encode(obj Object, errors Str) (Object, Int)
	Decode(obj Object, errors Str) (Object, Int)
}

Codec ..

func GetCodec

func GetCodec(name string) Codec

GetCodec ..

type Dict

type Dict interface {
	Object

	GetItem(key Object) Object
	SetItem(key Object, value Object)
	DelItem(key Object) Object
	SetDefault(key Object, value Object) Object
	Len() int
	Iterator() hashtableIterator
	Keys() []Object

	Update(iterable Object)
}

Dict ..

func ClassDictLiteral

func ClassDictLiteral(m interface{}) Dict

ClassDictLiteral ..

func DictLiteral

func DictLiteral(m interface{}) Dict

DictLiteral ..

func NewDict

func NewDict() Dict

NewDict ..

type Float

type Float interface {
	Object
	Value() float64
}

Float ..

func NewFloat

func NewFloat(value float64) Float

NewFloat ..

type FloatLiteral

type FloatLiteral float64

FloatLiteral ..

func (FloatLiteral) Dict

func (ic FloatLiteral) Dict() Dict

Dict ..

func (FloatLiteral) IsInstance

func (ic FloatLiteral) IsInstance(t Type) bool

IsInstance ..

func (FloatLiteral) Type

func (ic FloatLiteral) Type() Type

Type ..

func (FloatLiteral) Value

func (ic FloatLiteral) Value() float64

Value ..

type Function

type Function interface {
	Object
	Name() string
	Params() []Param
	NumKWOnly() int
	HasVarArgs() bool
	HasKWArgs() bool
	Fn() fun
}

Function ..

func NewFunction

func NewFunction(name string, params []Param, numKWOnly int,
	hasVarArgs bool, hasKwArgs bool, fn fun) Function

NewFunction ..

func NewSimpleFunction

func NewSimpleFunction(name string, parnames []string, fn fun) Function

NewSimpleFunction ..

type Generator

type Generator interface {
	Object
	Send(Object) Object
}

Generator ..

func NewGenerator

func NewGenerator(fn func(Yielder) Object) Generator

NewGenerator ..

type Int

type Int interface {
	Object
	Value() int
}

Int ..

func Hash

func Hash(o Object) Int

Hash ..

func Len

func Len(o Object) Int

Len ..

func NewBool

func NewBool(value bool) Int

NewBool ..

func NewInt

func NewInt(value int) Int

NewInt ..

func ToInt

func ToInt(o Object) Int

ToInt ..

type IntLiteral

type IntLiteral int

IntLiteral ..

func (IntLiteral) Dict

func (ic IntLiteral) Dict() Dict

Dict ..

func (IntLiteral) IsInstance

func (ic IntLiteral) IsInstance(t Type) bool

IsInstance ..

func (IntLiteral) Type

func (ic IntLiteral) Type() Type

Type ..

func (IntLiteral) Value

func (ic IntLiteral) Value() int

Value ..

type KWArg

type KWArg struct {
	Name  string
	Value Object
}

KWArg ..

type KWArgs

type KWArgs []KWArg

KWArgs ..

type List

type List interface {
	Seq
}

List ..

func NewList

func NewList(elems ...Object) List

NewList ..

type Method

type Method interface {
	Object
	Self() Object
	Fun() Object
}

Method ..

type Native

type Native interface {
	Object
	Value() interface{}
}

Native objects wrap a Go object

func NewNative

func NewNative(value interface{}) Native

NewNative wraps value into a Native

type Object

type Object interface {
	Type() Type
	Dict() Dict

	IsInstance(t Type) bool
	// contains filtered or unexported methods
}

Object ..

func Add

func Add(a, b Object) Object

Add ..

func And

func And(a, b Object) Object

And ..

func Cal

func Cal(o Object, args ...Object) Object

Cal ..

func Call

func Call(o Object, args Args, kwArgs KWArgs) Object

Call ..

func Calm

func Calm(o Object, attr string, args ...Object) Object

Calm ..

func FloorDiv

func FloorDiv(a, b Object) Object

FloorDiv ..

func GetAttr

func GetAttr(o Object, name string, def Object) (value Object)

GetAttr ..

func GetItem

func GetItem(o Object, key Object) Object

GetItem ..

func IAdd

func IAdd(a, b Object) Object

IAdd ..

func IFloorDiv

func IFloorDiv(a, b Object) Object

IFloorDiv ..

func IMod

func IMod(a, b Object) Object

IMod ..

func IMul

func IMul(a, b Object) Object

IMul ..

func IPow

func IPow(a, b Object) Object

IPow ..

func ISub

func ISub(a, b Object) Object

ISub ..

func ITrueDiv

func ITrueDiv(a, b Object) Object

ITrueDiv ..

func Iter

func Iter(o Object) Object

Iter ..

func LShift

func LShift(a, b Object) Object

LShift ..

func Mod

func Mod(a, b Object) Object

Mod ..

func Mul

func Mul(a, b Object) Object

Mul ..

func Neg

func Neg(o Object) Object

Neg ..

func Next

func Next(o Object) Object

Next ..

func NextDefault

func NextDefault(o Object, def Object) (ret Object)

NextDefault ..

func Or

func Or(a, b Object) Object

Or ..

func Placeholder

func Placeholder(name string, reason string) Object

Placeholder ..

func Pos

func Pos(o Object) Object

Pos ..

func Pow

func Pow(a, b Object) Object

Pow ..

func RShift

func RShift(a, b Object) Object

RShift ..

func Raise

func Raise(o Object) Object

Raise ..

func RaiseType

func RaiseType(t Type, msg string) Object

RaiseType ..

func Sub

func Sub(a, b Object) Object

Sub ..

func TrueDiv

func TrueDiv(a, b Object) Object

TrueDiv ..

func Unpack

func Unpack(elems ...Object) []Object

Unpack ..

func UnpackIterable

func UnpackIterable(o Object, cnt int) Object

UnpackIterable ..

func UsedSymbol

func UsedSymbol(name string, v Object) Object

UsedSymbol marks the symbol having the specified name, as used.

func Xor

func Xor(a, b Object) Object

Xor ..

type Param

type Param struct {
	Name string
	Def  Object
}

Param ..

type Property

type Property interface {
	Object
	Get() Object

	Set() Object

	Del() Object
	// contains filtered or unexported methods
}

Property ..

type Range

type Range interface {
	Object
	Start() int
	Stop() int
	Step() int
	// contains filtered or unexported methods
}

Range ..

type RangeIterator

type RangeIterator interface {
	Object
	Curr() int
	Stop() int
	Step() int
	// contains filtered or unexported methods
}

RangeIterator ..

type Seq

type Seq interface {
	Object
	Elems() []Object
	// contains filtered or unexported methods
}

Seq ..

type Set

type Set interface {
	Object
	SDict() Dict

	Add(Object)
	Update(Object)
	Contains(Object) bool
}

Set ..

func NewSet

func NewSet(elems ...Object) Set

NewSet ..

type Slice

type Slice interface {
	Object
	Start() Object
	Stop() Object
	Step() Object
	CalcSlice(elems int) (start, stop, step, len int)
}

Slice ..

func NewSlice

func NewSlice(start, stop, step Object) Slice

NewSlice ..

type Starred

type Starred interface {
	Object
	Value() Object
}

Starred ..

func AsStarred

func AsStarred(value Object) Starred

AsStarred ..

type Str

type Str interface {
	Object
	Value() string
}

Str ..

func NewStr

func NewStr(value string) Str

NewStr ..

func Repr

func Repr(o Object) Str

Repr ..

func ToStr

func ToStr(o Object) Str

ToStr ..

type StrLiteral

type StrLiteral string

StrLiteral ..

func (StrLiteral) Dict

func (sc StrLiteral) Dict() Dict

Dict ..

func (StrLiteral) IsInstance

func (sc StrLiteral) IsInstance(t Type) bool

IsInstance ..

func (StrLiteral) Type

func (sc StrLiteral) Type() Type

Type ..

func (StrLiteral) Value

func (sc StrLiteral) Value() string

Value ..

type Super

type Super interface {
	Object
	Sub() Type
	Obj() Object
	ObjType() Type
}

Super ..

type Tuple

type Tuple interface {
	Seq
}

Tuple ..

func NewTuple

func NewTuple(elems ...Object) Tuple

NewTuple ..

type Type

type Type interface {
	Object
	Name() string
	Bases() []Type
	Mro() []Type

	Slots() *typeSlots
	Builtin() bool

	IsSubType(super Type) bool
	// contains filtered or unexported methods
}

Type ..

type Yielder

type Yielder interface {
	Yield(Object) Object
}

Yielder ..

Jump to

Keyboard shortcuts

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