object

package
v0.0.0-...-2577f20 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2023 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	NULL_OBJ  = "NULL"
	ERROR_OBJ = "ERROR"

	NUMBER_OBJ  = "NUMBER"
	BOOLEAN_OBJ = "BOOLEAN"
	STRING_OBJ  = "STRING"

	RETURN_VALUE_OBJ = "RETURN_VALUE"

	// ANY_OBJ represents any ABS type
	ANY_OBJ      = "ANY"
	FUNCTION_OBJ = "FUNCTION"
	BUILTIN_OBJ  = "BUILTIN"

	ARRAY_OBJ = "ARRAY"
	HASH_OBJ  = "HASH"
)

Variables

View Source
var (
	NULL  = &Null{}
	EOF   = &Error{Message: "EOF"}
	TRUE  = &Boolean{Value: true}
	FALSE = &Boolean{Value: false}
)

Functions

func Equal

func Equal(obj1 Object, obj2 Object) bool

Equal compares 2 objects and makes sure they represent the same value.

func GenerateEqualityString

func GenerateEqualityString(o Object) string

GenerateEqualityString is used to compare 2 objects, for example from different arrays.

This function will generate a string that can be used to compare the 2, including their type and their string representation. Only using their string representation wouldn't work as "1" would be the same as 1.

Types

type Array

type Array struct {
	Token    token.Token
	Elements []Object
	// ... is aliased to an array of arguments.
	//
	// Since this is a special case of an array,
	// we need a flag to make sure we know when
	// to unpack them, else if we do func(...),
	// func would receive only one array argument
	// as opposd to the unpacked arguments.
	IsCurrentArgs bool
	// contains filtered or unexported fields
}

func (*Array) Empty

func (ao *Array) Empty() bool

func (*Array) Homogeneous

func (ao *Array) Homogeneous() bool

Homogeneous returns whether the array is homogeneous, meaning all of its elements are of a single type

func (*Array) Inspect

func (ao *Array) Inspect() string

func (*Array) Json

func (ao *Array) Json() string

func (*Array) Next

func (ao *Array) Next() (Object, Object)

func (*Array) Reset

func (ao *Array) Reset()

func (*Array) Type

func (ao *Array) Type() ObjectType

type Boolean

type Boolean struct {
	Token token.Token
	Value bool
}

func (*Boolean) Inspect

func (b *Boolean) Inspect() string

func (*Boolean) Json

func (b *Boolean) Json() string

func (*Boolean) Type

func (b *Boolean) Type() ObjectType

type BreakError

type BreakError struct {
	Error
}

type Builtin

type Builtin struct {
	Token    token.Token
	Fn       BuiltinFunction
	Next     func() (Object, Object)
	Types    []string
	Iterable bool
}

func (*Builtin) Inspect

func (b *Builtin) Inspect() string

func (*Builtin) Json

func (b *Builtin) Json() string

func (*Builtin) Type

func (b *Builtin) Type() ObjectType

type BuiltinFunction

type BuiltinFunction func(tok token.Token, env *Environment, args ...Object) Object

type ContinueError

type ContinueError struct {
	Error
}

type Environment

type Environment struct {

	// Arguments this environment was created in.
	// When we call function(1, 2, 3), a new environment
	// for the function to execute is created, and 1/2/3
	// are recorded as arguments for this environment.
	//
	// Later, if we need to access the arguments passed
	// to the function, we can refer back to them
	// through env.CurrentArgs. This is how ... is
	// implemented.
	CurrentArgs []Object

	// Used to capture output. This is typically os.Stdout,
	// but you could capture in any io.Writer of choice
	Writer io.Writer
	// Dir represents the directory from which we're executing code.
	// It starts as the directory from which we invoke the ABS
	// executable, but changes when we call require("...") as each
	// require call resets the dir to its own directory, so that
	// relative imports work.
	//
	// If we have script A and B in /tmp, A can require("B")
	// wihout having to specify its full absolute path
	// eg. require("/tmp/B")
	Dir string
	// Version of the ABS runtime
	Version string
	// contains filtered or unexported fields
}

Environment represent the environment associated with the execution context of an ABS script: it holds all variables etc.

func NewEnclosedEnvironment

func NewEnclosedEnvironment(outer *Environment, args []Object) *Environment

NewEnclosedEnvironment creates an environment with another one embedded to it, so that the new environment has access to identifiers stored in the outer one.

func NewEnvironment

func NewEnvironment(w io.Writer, dir string, version string) *Environment

NewEnvironment creates a new environment to run ABS in, specifying a writer for the output of the program and the base dir (which is used to require other scripts)

func (*Environment) Delete

func (e *Environment) Delete(name string)

Delete deletes an identifier from the environment

func (*Environment) Get

func (e *Environment) Get(name string) (Object, bool)

Get returns an identifier stored within the environment

func (*Environment) GetKeys

func (e *Environment) GetKeys() []string

GetKeys returns the list of all identifiers stored in this environment

func (*Environment) Set

func (e *Environment) Set(name string, val Object) Object

Set sets an identifier in the environment

type Error

type Error struct {
	Message string
}

func (*Error) Inspect

func (e *Error) Inspect() string

func (*Error) Json

func (e *Error) Json() string

func (*Error) Type

func (e *Error) Type() ObjectType

type Function

type Function struct {
	Token      token.Token
	Name       string
	Parameters []*ast.Parameter
	Body       *ast.BlockStatement
	Env        *Environment
	Node       *ast.FunctionLiteral
}

func (*Function) Inspect

func (f *Function) Inspect() string

func (*Function) Json

func (f *Function) Json() string

func (*Function) Type

func (f *Function) Type() ObjectType

type Hash

type Hash struct {
	Token    token.Token
	Pairs    map[HashKey]HashPair
	Position int
}

func (*Hash) GetKeyType

func (h *Hash) GetKeyType(k string) ObjectType

GetKeyType returns the type of a given key in the hash. If no key is found, it is considered to be a NULL.

func (*Hash) GetPair

func (h *Hash) GetPair(key string) (HashPair, bool)

func (*Hash) Inspect

func (h *Hash) Inspect() string

func (*Hash) Json

func (h *Hash) Json() string

func (*Hash) Next

func (h *Hash) Next() (Object, Object)

Pretty convoluted logic here we could refactor. First we sort the hash keys alphabetically and then we loop through them until we reach the required position within the loop.

func (*Hash) Reset

func (h *Hash) Reset()

func (*Hash) Type

func (h *Hash) Type() ObjectType

type HashKey

type HashKey struct {
	Token token.Token
	Type  ObjectType
	Value string
}

type HashPair

type HashPair struct {
	Key   Object
	Value Object
}

type Hashable

type Hashable interface {
	HashKey() HashKey
}

type Iterable

type Iterable interface {
	Next() (Object, Object)
	Reset()
}

type Null

type Null struct {
	Token token.Token
}

func (*Null) Inspect

func (n *Null) Inspect() string

func (*Null) Json

func (n *Null) Json() string

func (*Null) Type

func (n *Null) Type() ObjectType

type Number

type Number struct {
	Token token.Token
	Value float64
}

func (*Number) Inspect

func (n *Number) Inspect() string

If the number we're dealing with is an integer, print it as such (1.0000 becomes 1). If it's a float, let's remove as many zeroes as possible (1.10000 becomes 1.1).

func (*Number) Int

func (n *Number) Int() int

func (*Number) IsInt

func (n *Number) IsInt() bool

func (*Number) Json

func (n *Number) Json() string

func (*Number) Type

func (n *Number) Type() ObjectType

func (*Number) ZeroValue

func (n *Number) ZeroValue() float64

type Object

type Object interface {
	Type() ObjectType
	Inspect() string
	Json() string
}

type ObjectType

type ObjectType string

type ReturnValue

type ReturnValue struct {
	Token token.Token
	Value Object
}

func (*ReturnValue) Inspect

func (rv *ReturnValue) Inspect() string

func (*ReturnValue) Json

func (rv *ReturnValue) Json() string

func (*ReturnValue) Type

func (rv *ReturnValue) Type() ObjectType

type String

type String struct {
	Token  token.Token
	Value  string
	Ok     *Boolean  // A special property to check whether a command exited correctly
	Cmd    *exec.Cmd // A special property to access the underlying command
	Stdout *bytes.Buffer
	Stderr *bytes.Buffer
	Done   *Boolean
	// contains filtered or unexported fields
}

The String is a special fella.

Like ints, or bools, you might think it will only have a Value property, the string itself.

TA-DA! No, we also have an Ok and Done properties that are used when running shell commands -- since the shell will return strings.

So, look at this:

cmd = `ls -la` type(cmd) // STRING cmd.ok // TRUE

cmd = `curlzzzzz` type(cmd) // STRING cmd.ok // FALSE

cmd = `sleep 10 &` type(cmd) // STRING cmd.done // FALSE cmd.wait() // ... cmd.done // TRUE

func (*String) HashKey

func (s *String) HashKey() HashKey

func (*String) Inspect

func (s *String) Inspect() string

func (*String) Json

func (s *String) Json() string

func (*String) Kill

func (s *String) Kill() error

To be called when we want to kill the background command

func (*String) SetCmdResult

func (s *String) SetCmdResult(Ok *Boolean)

Sets the result of the underlying command on the string. 3 things are set: - the string itself (output of the command) - str.ok - str.done

func (*String) SetDone

func (s *String) SetDone()

To be called when the command is done. Releases the internal mutex.

func (*String) SetRunning

func (s *String) SetRunning()

To be called when the command is starting in background, so that anyone accessing it will be blocked.

func (*String) Type

func (s *String) Type() ObjectType

func (*String) Wait

func (s *String) Wait()

To be called when we want to wait on the background command to be done.

func (*String) ZeroValue

func (s *String) ZeroValue() string

Jump to

Keyboard shortcuts

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