obj

package
v1.1.0-beta.1 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2022 License: GPL-3.0 Imports: 10 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)
	ContinueObj = NewContinue()
	BreakObj    = NewBreak()
)
View Source
var Builtins = []struct {
	Name    string
	Builtin Builtin
}{
	{
		"len",
		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 NewInteger(int64(len(o)))
			case *String:
				return NewInteger(int64(len(*o)))
			default:
				return NewError("len: object of type %q has no length", o.Type())
			}
		},
	},
	{
		"native",
		func(args ...Object) Object {
			if l := len(args); l != 1 {
				return NewError("native: wrong number of arguments, expected 1, got %d", l)
			}

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

			return NewNativeModule(str.String())
		},
	},
	{
		"println",
		func(args ...Object) Object {
			var arguments []interface{}

			for _, a := range args {
				arguments = append(arguments, a.String())
			}
			fmt.Fprintln(Stdout, arguments...)
			return NullObj
		},
	},
	{
		"print",
		func(args ...Object) Object {
			var arguments []interface{}

			for _, a := range args {
				arguments = append(arguments, a.String())
			}
			fmt.Fprint(Stdout, arguments...)
			return NullObj
		},
	},
	{
		"input",
		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)
		},
	},
	{
		"string",
		func(args ...Object) Object {
			var buf strings.Builder

			for _, a := range args {
				buf.WriteString(a.String())
			}
			return NewString(buf.String())
		},
	},
	{
		"int",
		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 NewInteger(int64(*o))

			case *Float:
				return NewInteger(int64(*o))

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

			default:
				return NewError("%v is not a number", args[0])
			}
		},
	},
	{
		"float",
		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 NewFloat(float64(*o))

			case *Float:
				return NewFloat(float64(*o))

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

			default:
				return NewError("%v is not a number", args[0])
			}
		},
	},
	{
		"exit",
		func(args ...Object) Object {
			var l = len(args)

			if l == 0 {
				os.Exit(0)
			} else if l > 2 {
				return NewError("exit: wrong number of arguments, max 2, got %d", l)
			} else if l == 1 {
				switch o := args[0].(type) {
				case *Integer:
					os.Exit(int(*o))

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

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

			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))
			return NullObj
		},
	},
	{
		"append",
		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
		},
	},
	{
		"push",
		func(args ...Object) Object {
			if len(args) == 0 {
				return NewError("push: no argument provided")
			}

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

			if len(args) > 1 {
				var tmp List

				for i := len(args) - 1; i > 0; i-- {
					tmp = append(tmp, args[i])
				}

				return append(tmp, lst...)
			}
			return lst
		},
	},
	{
		"range",
		func(args ...Object) Object {
			switch len(args) {
			case 1:
				if stop, ok := args[0].(*Integer); ok {
					return listify(0, int(*stop), 1)
				}
				return NewError("range: start value must be an int")

			case 2:
				start, ok := args[0].(*Integer)
				if !ok {
					return NewError("range: start value must be an int")
				}

				stop, ok := args[1].(*Integer)
				if !ok {
					return NewError("range: stop value must be an int")
				}
				return listify(int(*start), int(*stop), 1)

			case 3:
				start, ok := args[0].(*Integer)
				if !ok {
					return NewError("range: start value must be an int")
				}

				stop, ok := args[1].(*Integer)
				if !ok {
					return NewError("range: stop value must be an int")
				}

				step, ok := args[2].(*Integer)
				if !ok {
					return NewError("range: step value must be an int")
				}

				if s := int(*step); s != 0 {
					return listify(int(*start), int(*stop), s)
				}
				return NewError("range: step value must not be zero")

			default:
				return NewError("range: wrong number of arguments, max 3, got %d", len(args))
			}
		},
	},
	{
		"first",
		func(args ...Object) Object {
			if l := len(args); l != 1 {
				return NewError("first: wrong number of arguments, expected 1, got %d", l)
			}

			switch o := args[0].(type) {
			case List:
				return o[0]
			case *String:
				return NewString(string(string(*o)[0]))
			default:
				return NewError("first: wrong argument type, expected list, got %s", args[0].Type())
			}
		},
	},
	{
		"last",
		func(args ...Object) Object {
			if l := len(args); l != 1 {
				return NewError("last: wrong number of arguments, expected 1, got %d", l)
			}

			switch o := args[0].(type) {
			case List:
				return o[len(o)-1]
			case *String:
				s := string(*o)
				return NewString(string(s[len(s)-1]))
			default:
				return NewError("last: wrong argument type, expected list, got %s", args[0].Type())
			}
		},
	},
	{
		"tail",
		func(args ...Object) Object {
			if l := len(args); l != 1 {
				return NewError("tail: wrong number of arguments, expected 1, got %d", l)
			}

			switch o := args[0].(type) {
			case List:
				return o[1:]
			case *String:
				s := string(*o)
				return NewString(s[1:])
			default:
				return NewError("tail: wrong argument type, expected list, got %s", args[0].Type())
			}
		},
	},
	{
		"new",
		func(args ...Object) Object {
			if l := len(args); l != 0 {
				return NewError("new: wrong number of arguments, expected 0, got %d", l)
			}
			return NewClass()
		},
	},
}
View Source
var NativeLibs = map[string]map[string]interface{}{
	"strings": {
		"Compare":        strings.Compare,
		"Contains":       strings.Contains,
		"ContainsAny":    strings.ContainsAny,
		"ContainsRune":   strings.ContainsRune,
		"Count":          strings.Count,
		"EqualFold":      strings.EqualFold,
		"Fields":         strings.Fields,
		"FieldsFunc":     strings.FieldsFunc,
		"HasPrefix":      strings.HasPrefix,
		"HasSuffix":      strings.HasSuffix,
		"Index":          strings.Index,
		"IndexAny":       strings.IndexAny,
		"IndexByte":      strings.IndexByte,
		"IndexFunc":      strings.IndexFunc,
		"IndexRune":      strings.IndexRune,
		"Join":           strings.Join,
		"LastIndex":      strings.LastIndex,
		"LastIndexAny":   strings.LastIndexAny,
		"LastIndexByte":  strings.LastIndexByte,
		"LastIndexFunc":  strings.LastIndexFunc,
		"Map":            strings.Map,
		"Repeat":         strings.Repeat,
		"Replace":        strings.Replace,
		"ReplaceAll":     strings.ReplaceAll,
		"Split":          strings.Split,
		"SplitAfter":     strings.SplitAfter,
		"splitAfterN":    strings.SplitAfterN,
		"SplitN":         strings.SplitN,
		"Title":          strings.Title,
		"ToLower":        strings.ToLower,
		"ToLowerSpecial": strings.ToLowerSpecial,
		"ToTitle":        strings.ToTitle,
		"ToTitleSpecial": strings.ToTitleSpecial,
		"ToUpper":        strings.ToUpper,
		"ToUpperSpecial": strings.ToUpperSpecial,
		"ToValidUTF8":    strings.ToValidUTF8,
		"Trim":           strings.Trim,
		"TrimFunc":       strings.TrimFunc,
		"TrimLeft":       strings.TrimLeft,
		"TrimLeftFunc":   strings.TrimLeftFunc,
		"TrimPrefix":     strings.TrimPrefix,
		"TrimRight":      strings.TrimRight,
		"TrimRightFunc":  strings.TrimRightFunc,
		"TrimSpace":      strings.TrimSpace,
		"TrimSuffix":     strings.TrimSuffix,
	},
	"regexp": {
		"MatchString":      regexp.MatchString,
		"QuoteMeta":        regexp.QuoteMeta,
		"Compile":          regexp.Compile,
		"CompilePOSIX":     regexp.CompilePOSIX,
		"MustCompile":      regexp.MustCompile,
		"MustCompilePOSIX": regexp.MustCompilePOSIX,
	},
}

Functions

This section is empty.

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 Break

type Break struct{}

func (Break) String

func (b Break) String() string

func (Break) Type

func (b Break) Type() Type

type Builtin

type Builtin func(arg ...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 Class

type Class struct {
	*Env
}

func (Class) String

func (c Class) String() string

func (Class) Type

func (c Class) Type() Type

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 Continue

type Continue struct{}

func (Continue) String

func (c Continue) String() string

func (Continue) Type

func (c Continue) Type() Type

type Env

type Env struct {
	Outer *Env
	Store map[string]Object
}

func NewEnv

func NewEnv() *Env

func NewEnvWrap

func NewEnvWrap(e *Env) *Env

func (*Env) Get

func (e *Env) Get(n string) (Object, bool)

func (*Env) Set

func (e *Env) Set(n string, o Object) Object

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 {
	Params       []string
	Body         interface{}
	Env          *Env
	Instructions code.Instructions
	NumLocals    int
	NumParams    int
}

func (Function) String

func (f Function) String() string

func (Function) Type

func (f Function) Type() Type

type GetSetter

type GetSetter interface {
	Getter
	Setter
}

type Getter

type Getter interface {
	Object() Object
}

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 MapGetSetter

type MapGetSetter interface {
	MapGetter
	MapSetter
}

type MapGetter

type MapGetter interface {
	Get(string) (Object, bool)
}

type MapPair

type MapPair struct {
	Key   Object
	Value Object
}

type MapSetter

type MapSetter interface {
	Set(string, Object) Object
}

type NativeModule

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

func (*NativeModule) Get

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

func (*NativeModule) Set

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

func (NativeModule) String

func (n NativeModule) String() string

func (NativeModule) Type

func (n NativeModule) Type() Type

type NativeStruct added in v1.1.0

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

func (*NativeStruct) Get added in v1.1.0

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

func (*NativeStruct) Set added in v1.1.0

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

func (NativeStruct) String added in v1.1.0

func (n NativeStruct) String() string

func (NativeStruct) Type added in v1.1.0

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() interface{}

type Object

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

func NewBoolean

func NewBoolean(b bool) Object

func NewBreak

func NewBreak() Object

func NewClass

func NewClass() Object

func NewContinue

func NewContinue() Object

func NewError

func NewError(f string, a ...interface{}) Object

func NewFloat

func NewFloat(f float64) Object

func NewFunction

func NewFunction(params []string, env *Env, body interface{}) Object

func NewFunctionCompiled

func NewFunctionCompiled(i code.Instructions, nLocals, nParams int) Object

func NewGetSetter

func NewGetSetter(s MapGetSetter, n string) Object

func NewInteger

func NewInteger(i int64) Object

func NewList

func NewList(elems ...Object) Object

func NewNativeModule

func NewNativeModule(name string) Object

func NewNativeStruct added in v1.1.0

func NewNativeStruct(s interface{}) Object

func NewNull

func NewNull() Object

func NewReturn

func NewReturn(o Object) Object

func NewString

func NewString(s string) Object

func ParseBool

func ParseBool(b bool) Object

func Unwrap

func Unwrap(o Object) Object

type Return

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

func (Return) String

func (r Return) String() string

func (Return) Type

func (r Return) Type() Type

func (Return) Val

func (r Return) Val() Object

type Setter

type Setter interface {
	Set(Object) Object
}

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 Type

type Type int
const (
	NullType Type = iota
	ErrorType
	IntType
	FloatType
	BoolType
	StringType
	ClassType
	ReturnType
	FunctionType
	ClosureType
	BuiltinType
	ListType
	MapType
	ContinueType
	BreakType
)

func (Type) String

func (t Type) String() string

Jump to

Keyboard shortcuts

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