Add tons of builtin helpers and array operations.
Some checks failed
Test / build (push) Waiting to run
Build / build (push) Has been cancelled

This commit is contained in:
Chuck Smith
2024-03-24 12:11:46 -04:00
parent c9d96236dd
commit 99cea83f57
23 changed files with 413 additions and 49 deletions

View File

@@ -12,20 +12,27 @@ import (
type ObjectType string
const (
INTEGER_OBJ = "INTEGER"
BOOLEAN_OBJ = "BOOLEAN"
NULL_OBJ = "NULL"
RETURN_VALUE_OBJ = "RETURN_VALUE"
ERROR_OBJ = "ERROR"
FUNCTION_OBJ = "FUNCTION"
STRING_OBJ = "STRING"
BUILTIN_OBJ = "BUILTIN"
ARRAY_OBJ = "ARRAY"
HASH_OBJ = "HASH"
INTEGER_OBJ = "int"
BOOLEAN_OBJ = "bool"
NULL_OBJ = "null"
RETURN_VALUE_OBJ = "return"
ERROR_OBJ = "error"
FUNCTION_OBJ = "fn"
STRING_OBJ = "str"
BUILTIN_OBJ = "builtin"
ARRAY_OBJ = "array"
HASH_OBJ = "hash"
COMPILED_FUNCTION_OBJ = "COMPILED_FUNCTION"
CLOSURE_OBJ = "CLOSURE"
CLOSURE_OBJ = "closure"
)
// Comparable is the interface for comparing two Object and their underlying
// values. It is the responsibility of the caller (left) to check for types.
// Returns `true` iif the types and values are identical, `false` otherwise.
type Comparable interface {
Equal(other Object) bool
}
// Immutable is the interface for all immutable objects which must implement
// the Clone() method used by binding names to values.
type Immutable interface {
@@ -60,6 +67,12 @@ func (i *Integer) Clone() Object {
func (i *Integer) String() string {
return i.Inspect()
}
func (i *Integer) Equal(other Object) bool {
if obj, ok := other.(*Integer); ok {
return i.Value == obj.Value
}
return false
}
type Boolean struct {
Value bool
@@ -77,6 +90,12 @@ func (b *Boolean) Clone() Object {
func (b *Boolean) String() string {
return b.Inspect()
}
func (b *Boolean) Equal(other Object) bool {
if obj, ok := other.(*Boolean); ok {
return b.Value == obj.Value
}
return false
}
type Null struct{}
@@ -89,6 +108,10 @@ func (n *Null) Inspect() string {
func (n *Null) String() string {
return n.Inspect()
}
func (n *Null) Equal(other Object) bool {
_, ok := other.(*Null)
return ok
}
type ReturnValue struct {
Value Object
@@ -167,6 +190,12 @@ func (s *String) Clone() Object {
func (s *String) String() string {
return s.Value
}
func (s *String) Equal(other Object) bool {
if obj, ok := other.(*String); ok {
return s.Value == obj.Value
}
return false
}
type BuiltinFunction func(args ...Object) Object
@@ -209,6 +238,25 @@ func (ao *Array) Inspect() string {
func (ao *Array) String() string {
return ao.Inspect()
}
func (ao *Array) Equal(other Object) bool {
if obj, ok := other.(*Array); ok {
if len(ao.Elements) != len(obj.Elements) {
return false
}
for i, el := range ao.Elements {
cmp, ok := el.(Comparable)
if !ok {
return false
}
if !cmp.Equal(obj.Elements[i]) {
return false
}
}
return true
}
return false
}
type HashKey struct {
Type ObjectType
@@ -271,6 +319,31 @@ func (h *Hash) Inspect() string {
func (h *Hash) String() string {
return h.Inspect()
}
func (h *Hash) Equal(other Object) bool {
if obj, ok := other.(*Hash); ok {
if len(h.Pairs) != len(obj.Pairs) {
return false
}
for _, pair := range h.Pairs {
left := pair.Value
hashed := left.(Hashable)
right, ok := obj.Pairs[hashed.HashKey()]
if !ok {
return false
}
cmp, ok := left.(Comparable)
if !ok {
return false
}
if !cmp.Equal(right.Value) {
return false
}
}
return true
}
return false
}
func (cf *CompiledFunction) Type() ObjectType {
return COMPILED_FUNCTION_OBJ