obj

package
v1.6.6 Latest Latest
Warning

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

Go to latest
Published: Jun 29, 2024 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Stdout io.Writer = os.Stdout
	Stdin  io.Reader = os.Stdin
)
View Source
var (
	NullObj = NewNull()
	True    = NewBoolean(true)
	False   = NewBoolean(false)
)
View Source
var Builtins = []BuiltinImpl{
	{
		Name: "len",
		Builtin: func(args ...Object) Object {
			if l := len(args); l != 1 {
				return NewError("len: wrong number of arguments, expected 1, got %d", l)
			}

			switch o := args[0].(type) {
			case List:
				return Integer(len(o))
			case String:
				return Integer(len(o))
			case Bytes:
				return Integer(len(o))
			default:
				return NewError("len: object of type %q has no length", o.Type())
			}
		},
	},
	{
		Name: "println",
		Builtin: func(args ...Object) Object {
			fmt.Fprintln(Stdout, toAnySlice(args)...)
			return NullObj
		},
	},
	{
		Name: "print",
		Builtin: func(args ...Object) Object {
			fmt.Fprint(Stdout, toAnySlice(args)...)
			return NullObj
		},
	},
	{
		Name: "input",
		Builtin: func(args ...Object) Object {
			var tmp string

			switch l := len(args); l {
			case 0:
				fmt.Scanln(&tmp)

			case 1:
				fmt.Print(args[0])
				fmt.Scanln(&tmp)

			default:
				return NewError("input: wrong number of arguments, expected 1, got %d", l)
			}
			return NewString(tmp)
		},
	},
	{
		Name: "string",
		Builtin: func(args ...Object) Object {
			if len(args) == 0 {
				return NewError("string: no argument provided")
			}

			if b, ok := args[0].(Bytes); ok {
				return String(b)
			}
			return NewString(fmt.Sprint(toAnySlice(args)...))
		},
	},
	{
		Name: "error",
		Builtin: func(args ...Object) Object {
			return NewError(fmt.Sprint(toAnySlice(args)...))
		},
	},
	{
		Name: "type",
		Builtin: func(args ...Object) Object {
			if l := len(args); l != 1 {
				return NewError("type: wrong number of arguments, expected 1, got %d", l)
			}
			return NewString(args[0].Type().String())
		},
	},
	{
		Name: "int",
		Builtin: func(args ...Object) Object {
			if l := len(args); l != 1 {
				return NewError("int: wrong number of arguments, expected 1, got %d", l)
			}

			switch o := args[0].(type) {
			case Integer:
				return Integer(o)

			case Float:
				return Integer(o)

			case String:
				if a, err := strconv.ParseInt(string(o), 10, 64); err == nil {
					return Integer(a)
				}
				return NewError("%v is not a number", args[0])

			default:
				return NewError("%v is not a number", args[0])
			}
		},
	},
	{
		Name: "float",
		Builtin: func(args ...Object) Object {
			if l := len(args); l != 1 {
				return NewError("float: wrong number of arguments, expected 1, got %d", l)
			}

			switch o := args[0].(type) {
			case Integer:
				return Float(o)

			case Float:
				return Float(o)

			case String:
				if a, err := strconv.ParseFloat(string(o), 64); err == nil {
					return Float(a)
				}
				return NewError("%v is not a number", args[0])

			default:
				return NewError("%v is not a number", args[0])
			}
		},
	},
	{
		Name: "exit",
		Builtin: func(args ...Object) Object {
			switch l := len(args); l {
			case 0:
				os.Exit(0)

			case 1:
				switch o := args[0].(type) {
				case Integer:
					os.Exit(int(o))

				case String, Error:
					fmt.Fprintln(Stdout, o)
					os.Exit(0)

				default:
					return NewError("exit: argument must be an integer, string or error")
				}

			case 2:
				msg, ok := args[0].(String)
				if !ok {
					return NewError("exit: first argument must be a string")
				}
				code, ok := args[1].(Integer)
				if !ok {
					return NewError("exit: second argument must be an int")
				}

				fmt.Fprintln(Stdout, string(msg))
				os.Exit(int(code))

			default:
				return NewError("exit: wrong number of arguments, max 2, got %d", l)
			}
			return NullObj
		},
	},
	{
		Name: "append",
		Builtin: func(args ...Object) Object {
			if len(args) == 0 {
				return NewError("append: no argument provided")
			}

			lst, ok := args[0].(List)
			if !ok {
				return NewError("append: first argument must be a list")
			}

			if len(args) > 1 {
				return append(lst, args[1:]...)
			}
			return lst
		},
	},
	{
		Name: "new",
		Builtin: func(args ...Object) Object {
			if l := len(args); l != 0 {
				return NewError("new: wrong number of arguments, expected 0, got %d", l)
			}
			return NewTauObject()
		},
	},
	{
		Name: "failed",
		Builtin: func(args ...Object) Object {
			if l := len(args); l != 1 {
				return NewError("failed: wrong number of arguments, expected 1, got %d", l)
			}

			_, ok := args[0].(Error)
			return ParseBool(ok)
		},
	},
	{
		Name: "plugin",
		Builtin: func(args ...Object) Object {
			if l := len(args); l != 1 {
				return NewError("plugin: wrong number of arguments, expected 1, got %d", l)
			}

			str, ok := args[0].(String)
			if !ok {
				return NewError("plugin: first argument must be a string, got %s instead", args[0].Type())
			}

			return NewNativePlugin(str.String())
		},
	},
	{
		Name: "pipe",
		Builtin: func(args ...Object) Object {
			switch l := len(args); l {
			case 0:
				return NewPipe()

			case 1:
				n, ok := args[0].(Integer)
				if !ok {
					return NewError("pipe: first argument must be an int, got %s instead", args[0].Type())
				}
				return NewPipeBuffered(int(n))

			default:
				return NewError("pipe: wrong number of arguments, expected 0 or 1, got %d", l)
			}
		},
	},
	{
		Name: "send",
		Builtin: func(args ...Object) (o Object) {
			if l := len(args); l != 2 {
				return NewError("send: wrong number of arguments, expected 2, got %d", l)
			}

			p, ok := args[0].(Pipe)
			if !ok {
				return NewError("send: first argument must be a pipe, got %s instead", args[0].Type())
			}

			p <- args[1]
			return args[1]
		},
	},
	{
		Name: "recv",
		Builtin: func(args ...Object) (o Object) {
			if l := len(args); l != 1 {
				return NewError("recv: wrong number of arguments, expected 1, got %d", l)
			}

			p, ok := args[0].(Pipe)
			if !ok {
				return NewError("recv: first argument must be a pipe, got %s instead", args[0].Type())
			}

			if ret := <-p; ret != nil {
				return ret
			}
			return NullObj
		},
	},
	{
		Name: "close",
		Builtin: func(args ...Object) (o Object) {
			if l := len(args); l != 1 {
				return NewError("close: wrong number of arguments, expected 1, got %d", l)
			}

			p, ok := args[0].(Pipe)
			if !ok {
				return NewError("close: first argument must be a pipe, got %s instead", args[0].Type())
			}

			close(p)
			return NullObj
		},
	},
	{
		Name: "hex",
		Builtin: func(args ...Object) Object {
			if l := len(args); l != 1 {
				return NewError("hex: wrong number of arguments, expected 1, got %d", l)
			}

			i, ok := args[0].(Integer)
			if !ok {
				return NewError("hex: first argument must be an int, got %s instead", args[0].Type())
			}

			return NewString(fmt.Sprintf("0x%x", i.Val()))
		},
	},
	{
		Name: "oct",
		Builtin: func(args ...Object) Object {
			if l := len(args); l != 1 {
				return NewError("oct: wrong number of arguments, expected 1, got %d", l)
			}

			i, ok := args[0].(Integer)
			if !ok {
				return NewError("oct: first argument must be an int, got %s instead", args[0].Type())
			}

			return NewString(fmt.Sprintf("%O", i.Val()))
		},
	},
	{
		Name: "bin",
		Builtin: func(args ...Object) Object {
			if l := len(args); l != 1 {
				return NewError("bin: wrong number of arguments, expected 1, got %d", l)
			}

			i, ok := args[0].(Integer)
			if !ok {
				return NewError("bin: first argument must be an int, got %s instead", args[0].Type())
			}

			return NewString(fmt.Sprintf("0b%b", i.Val()))
		},
	},
	{
		Name: "slice",
		Builtin: func(args ...Object) Object {
			if l := len(args); l != 3 {
				return NewError("slice: wrong number of arguments, expected 3, got %d", l)
			}

			s, ok := args[1].(Integer)
			if !ok {
				return NewError("slice: second argument must be an int, got %s instead", args[1].Type())
			}

			e, ok := args[2].(Integer)
			if !ok {
				return NewError("slice: third argument must be an int, got %s instead", args[2].Type())
			}

			var start, end = int(s), int(e)

			switch slice := args[0].(type) {
			case List:
				if start < 0 || end < 0 {
					return NewError("slice: invalid argument: index arguments must not be negative")
				} else if end > len(slice) {
					return NewError("slice: list bounds out of range %d with capacity %d", end, len(slice))
				}
				return slice[start:end]

			case String:
				if start < 0 || end < 0 {
					return NewError("slice: invalid argument: index arguments must not be negative")
				} else if end > len(slice) {
					return NewError("slice: string bounds out of range %d with capacity %d", end, len(slice))
				}
				return slice[start:end]

			case Bytes:
				if start < 0 || end < 0 {
					return NewError("slice: invalid argument: index arguments must not be negative")
				} else if end > len(slice) {
					return NewError("slice: bytes bounds out of range %d with capacity %d", end, len(slice))
				}
				return slice[start:end]

			default:
				return NewError("slice: first argument must be a list or string, got %s instead", args[0].Type())
			}
		},
	},
	{
		Name: "keys",
		Builtin: func(args ...Object) Object {
			if len(args) != 1 {
				return NewError("keys: wrong number of arguments, expected 1 got %d", len(args))
			}

			if m, ok := args[0].(Map); ok {
				ret := []Object{}

				for _, v := range m {
					ret = append(ret, v.Key)
				}
				return List(ret)
			}
			return NewError("keys: argument must be a map, got %v instead", args[0].Type())
		},
	},
	{
		Name: "delete",
		Builtin: func(args ...Object) Object {
			if len(args) != 2 {
				return NewError("delete: wrong number of arguments, expected 2 got %d", len(args))
			}

			if m, ok := args[0].(Map); ok {
				if h, ok := args[1].(Hashable); ok {
					delete(m, h.KeyHash())
					return NullObj
				}
				return NewError("delete: second argument must be one of boolean integer float string error, got %s instead", args[1].Type())
			}
			return NewError("delete: first argument must be a map, got %v instead", args[0].Type())
		},
	},
	{
		Name: "bytes",
		Builtin: func(args ...Object) Object {
			if len(args) != 1 {
				return NewError("bytes: expected 1 argument but got %d", len(args))
			}

			switch a := args[0].(type) {
			case String:
				return Bytes(a)
			case Integer:
				return make(Bytes, a)
			case List:
				ret := make(Bytes, len(a))
				for i, e := range a {
					integer, ok := e.(Integer)
					if !ok {
						return NewError("bytes: list cannot be converted to bytes")
					}
					ret[i] = byte(integer)
				}
				return ret
			default:
				return NewError("bytes: %s cannot be converted to bytes", a.Type())
			}
		},
	},
}

Functions

func AssertTypes added in v1.4.6

func AssertTypes(o Object, types ...Type) bool

func IsPrimitive added in v1.4.6

func IsPrimitive(o Object) bool

func IsTruthy added in v1.4.6

func IsTruthy(o Object) bool

func Printf added in v1.4.0

func Printf(s string, a ...any)

func Println added in v1.4.0

func Println(a ...any)

func ToFloat added in v1.4.6

func ToFloat(l, r Object) (Object, Object)

Types

type Boolean

type Boolean bool

func (Boolean) KeyHash

func (b Boolean) KeyHash() KeyHash

func (Boolean) String

func (b Boolean) String() string

func (Boolean) Type

func (b Boolean) Type() Type

func (Boolean) Val

func (b Boolean) Val() bool

type Builtin

type Builtin func(args ...Object) Object

func ResolveBuiltin

func ResolveBuiltin(name string) (Builtin, bool)

func (Builtin) String

func (b Builtin) String() string

func (Builtin) Type

func (b Builtin) Type() Type

type BuiltinImpl added in v1.4.3

type BuiltinImpl struct {
	Builtin Builtin
	Name    string
}

type Bytes added in v1.3.0

type Bytes []byte

func (Bytes) String added in v1.3.0

func (bytes Bytes) String() string

func (Bytes) Type added in v1.3.0

func (b Bytes) Type() Type

func (Bytes) Val added in v1.3.0

func (b Bytes) Val() []byte

type Closure

type Closure struct {
	Fn   Function
	Free []Object
}

func NewClosure

func NewClosure(fn Function, free []Object) *Closure

func (*Closure) String

func (c *Closure) String() string

func (*Closure) Type

func (c *Closure) Type() Type

type Error

type Error string

func (Error) String

func (e Error) String() string

func (Error) Type

func (e Error) Type() Type

func (Error) Val

func (e Error) Val() string

type Float

type Float float64

func (Float) KeyHash

func (f Float) KeyHash() KeyHash

func (Float) String

func (f Float) String() string

func (Float) Type

func (f Float) Type() Type

func (Float) Val

func (f Float) Val() float64

type Function

type Function struct {
	Instructions code.Instructions
	NumLocals    int
	NumParams    int
	Bookmarks    []tauerr.Bookmark
}

func (Function) String

func (c Function) String() string

func (Function) Type

func (c Function) Type() Type

type Hashable

type Hashable interface {
	KeyHash() KeyHash
}

type Integer

type Integer int64

func (Integer) KeyHash

func (i Integer) KeyHash() KeyHash

func (Integer) String

func (i Integer) String() string

func (Integer) Type

func (i Integer) Type() Type

func (Integer) Val

func (i Integer) Val() int64

type KeyHash

type KeyHash struct {
	Type  Type
	Value uint64
}

type List

type List []Object

func (List) String

func (l List) String() string

func (List) Type

func (l List) Type() Type

func (List) Val

func (l List) Val() []Object

type Map

type Map map[KeyHash]MapPair

func NewMap

func NewMap() Map

func (Map) Get

func (m Map) Get(k KeyHash) MapPair

func (Map) Set

func (m Map) Set(k KeyHash, v MapPair)

func (Map) String

func (m Map) String() string

func (Map) Type

func (m Map) Type() Type

type MapPair

type MapPair struct {
	Key   Object
	Value Object
}

type NativePlugin

type NativePlugin struct {
	*plugin.Plugin
}

func (*NativePlugin) Get

func (n *NativePlugin) Get(name string) (Object, bool)

func (*NativePlugin) Set

func (n *NativePlugin) Set(name string, o Object) Object

func (NativePlugin) String

func (n NativePlugin) String() string

func (NativePlugin) Type

func (n NativePlugin) Type() Type

type NativeStruct

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

func (*NativeStruct) Get

func (n *NativeStruct) Get(name string) (Object, bool)

func (*NativeStruct) Set

func (n *NativeStruct) Set(name string, o Object) Object

func (NativeStruct) String

func (n NativeStruct) String() string

func (NativeStruct) Type

func (n NativeStruct) Type() Type

type Null

type Null struct{}

func (Null) String

func (n Null) String() string

func (Null) Type

func (n Null) Type() Type

func (Null) Val

func (n Null) Val() any

type Object

type Object interface {
	Type() Type
	String() string
}

func NewBoolean

func NewBoolean(b bool) Object

func NewBytes added in v1.3.0

func NewBytes(b []byte) Object

func NewError

func NewError(f string, a ...any) Object

func NewFloat

func NewFloat(f float64) Object

func NewFunction

func NewFunction(i code.Instructions, nLocals, nParams int, bookmarks []tauerr.Bookmark) Object

func NewInteger

func NewInteger(i int64) Object

func NewList

func NewList(elems ...Object) Object

func NewNativePlugin

func NewNativePlugin(path string) Object

func NewNativeStruct

func NewNativeStruct(s any) Object

func NewNull

func NewNull() Object

func NewPipe added in v1.4.0

func NewPipe() Object

func NewPipeBuffered added in v1.4.0

func NewPipeBuffered(n int) Object

func NewString

func NewString(s string) Object

func NewTauObject added in v1.6.0

func NewTauObject() Object

func ParseBool

func ParseBool(b bool) Object

type Pipe added in v1.4.0

type Pipe chan Object

func (Pipe) String added in v1.4.0

func (p Pipe) String() string

func (Pipe) Type added in v1.4.0

func (p Pipe) Type() Type

type String

type String string

func (String) KeyHash

func (s String) KeyHash() KeyHash

func (String) Quoted

func (s String) Quoted() string

func (String) String

func (s String) String() string

func (String) Type

func (s String) Type() Type

func (String) Val

func (s String) Val() string

type TauObject added in v1.6.0

type TauObject map[string]Object

func (TauObject) Get added in v1.6.0

func (to TauObject) Get(n string) (Object, bool)

func (TauObject) Module added in v1.6.0

func (to TauObject) Module() (o TauObject)

func (TauObject) Set added in v1.6.0

func (to TauObject) Set(n string, o Object) Object

func (TauObject) String added in v1.6.0

func (o TauObject) String() string

func (TauObject) Type added in v1.6.0

func (o TauObject) Type() Type

type Type

type Type int
const (
	NullType     Type = iota // null
	BoolType                 // bool
	IntType                  // int
	FloatType                // float
	BuiltinType              // builtin
	StringType               // string
	ErrorType                // error
	ListType                 // list
	MapType                  // map
	FunctionType             // function
	ClosureType              // closure
	ObjectType               // object
	PipeType                 // pipe
	BytesType                // bytes
	NativeType               // native
)

func (Type) String

func (i Type) String() string

Jump to

Keyboard shortcuts

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