object

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2025 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Environment is a map of string to Object that represents the environment in which an object is evaluated. It also contains a reference to an outer environment, which is used to implement lexical scoping (eg. enables closures)

object package is a collection of types that represent the objects in our language (eg. Integer).

The object package contains the definition of the Object interface, which is implemented by all objects in our language. All object types have a Type method that returns the type of the object, and an Inspect method that returns a string representation of the object. The object package also contains the definition of the ObjectType type, which is a string that represents the type of an object.

Index

Constants

View Source
const (
	NULL_OBJ         = "NULL"
	ERROR_OBJ        = "ERROR"
	CSV_OBJ          = "CSV"
	CSV_ROW          = "CSV_ROW"
	CSV_VAL          = "CSV_VAL"
	STRING_OBJ       = "STRING"
	INTEGER_OBJ      = "INTEGER"
	BOOLEAN_OBJ      = "BOOLEAN"
	RETURN_VALUE_OBJ = "RETURN_VALUE"
	FUNCTION_OBJ     = "FUNCTION"
	ARRAY            = "ARRAY"

	BUILTIN_OBJ = "BUILTIN"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Array

type Array struct {
	Elements []Object
}

Array struct represents an array object in our language.

func (*Array) Inspect

func (a *Array) Inspect() string

func (*Array) ToCSV

func (arr *Array) ToCSV(env *Environment) (*CSV, error)

func (*Array) Type

func (a *Array) Type() ObjectType

type Boolean

type Boolean struct {
	Value bool
}

Bool struct represents a boolean object in our language.

func (*Boolean) Inspect

func (b *Boolean) Inspect() string

func (*Boolean) ToCSV

func (b *Boolean) ToCSV(env *Environment) (*CSV, error)

func (*Boolean) Type

func (b *Boolean) Type() ObjectType

type Builtin

type Builtin struct {
	Fn BuiltinFunction
}

Built-in functionality to our lang which the host lang (Go) doesn't provide This allows us to add new functions to our language without modifying the host language (eg. fill_empty())

func (*Builtin) Inspect

func (b *Builtin) Inspect() string

func (*Builtin) ToCSV

func (b *Builtin) ToCSV(env *Environment) (*CSV, error)

func (*Builtin) Type

func (b *Builtin) Type() ObjectType

type BuiltinFunction

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

type CSV

type CSV struct {
	Headers     []string
	ColumnTypes []ColumnType
	Rows        []map[string]string
}

CSV struct represents a CSV object in our language.

func ArrayToCSV

func ArrayToCSV(arr *Array, env *Environment) (*CSV, error)

Array to CSV utils

func (*CSV) InferColumnTypes

func (c *CSV) InferColumnTypes()

InferColumnTypes infers the data types of the columns in the CSV object.

func (*CSV) Inspect

func (c *CSV) Inspect() string

func (*CSV) ToCSV

func (csv *CSV) ToCSV(env *Environment) (*CSV, error)

func (*CSV) Type

func (c *CSV) Type() ObjectType

type ColumnType

type ColumnType struct {
	Name     string
	DataType ObjectType // STRING_OBJ or INTEGER_OBJ
}

ColumnType struct stores data type info about columns in a CSV object

func InferType

func InferType(obj Object) ColumnType

TODO: this sticks out in object godoc, see how we want to position this fn

type Environment

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

Environment is a map of string to Object that represents the environment in which an object is evaluated. It also contains a reference to an outer environment, which is used to implement lexical scoping (eg. enables closures)

func NewEnclosedEnvironment

func NewEnclosedEnvironment(outer *Environment) *Environment

NewEnclosedEnvironment creates a new environment with the given outer environment.

func NewEnvironment

func NewEnvironment() *Environment

NewEnvironment creates a new environment without an outer environment.

func (*Environment) Get

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

Get retrieves the object with the given name from the environment.

func (*Environment) GetStore

func (e *Environment) GetStore() map[string]Object

GetStore returns the store of the environment.

func (*Environment) Set

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

Set sets the object with the given name in the environment.

func (*Environment) Unset

func (e *Environment) Unset(name string)

Unset removes the object with the given name from the environment.

type Error

type Error struct {
	Message string
}

Error struct represents an error object in our language.

func (*Error) Inspect

func (e *Error) Inspect() string

func (*Error) ToCSV

func (e *Error) ToCSV(env *Environment) (*CSV, error)

func (*Error) Type

func (e *Error) Type() ObjectType

type Function

type Function struct {
	Parameters []*ast.Identifier
	Body       *ast.BlockStatement
	Env        *Environment
}

Function struct represents a function object in our language.

func (*Function) Inspect

func (f *Function) Inspect() string

func (*Function) ToCSV

func (f *Function) ToCSV(env *Environment) (*CSV, error)

func (*Function) Type

func (f *Function) Type() ObjectType

type Integer

type Integer struct {
	Value int64
}

Integer struct represents an integer object in our language.

func (*Integer) Inspect

func (i *Integer) Inspect() string

func (*Integer) ToCSV

func (i *Integer) ToCSV(env *Environment) (*CSV, error)

func (*Integer) Type

func (i *Integer) Type() ObjectType

type Null

type Null struct{}

Null struct represents a null object in our language.

func (*Null) Inspect

func (n *Null) Inspect() string

func (*Null) ToCSV

func (n *Null) ToCSV(env *Environment) (*CSV, error)

func (*Null) Type

func (n *Null) Type() ObjectType

type Object

type Object interface {
	Type() ObjectType
	Inspect() string
	// Add method to attempt conversion to CSV
	ToCSV(env *Environment) (*CSV, error)
}

Object interface specifies the methods that all objects in our language must implement.

- Type method returns the type of the object

- Inspect method returns a string representation of the object.

- ToCSV method attempts to convert the object to a CSV object. It is idempotent, meaning it should return the same CSV object if called multiple times.

type ObjectType

type ObjectType string

type ReturnValue

type ReturnValue struct {
	Value Object
}

Return struct represents a return value object in our language.

func (*ReturnValue) Inspect

func (rv *ReturnValue) Inspect() string

func (*ReturnValue) ToCSV

func (rv *ReturnValue) ToCSV(env *Environment) (*CSV, error)

func (*ReturnValue) Type

func (rv *ReturnValue) Type() ObjectType

type String

type String struct {
	Value string
}

String struct represents a string object in our language.

func (*String) Inspect

func (s *String) Inspect() string

func (*String) ToCSV

func (s *String) ToCSV(env *Environment) (*CSV, error)

func (*String) Type

func (s *String) Type() ObjectType

Jump to

Keyboard shortcuts

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