Refactor Type to be an int
Some checks failed
Publish Image / publish (push) Waiting to run
Test / build (push) Waiting to run
Build / build (push) Has been cancelled

This commit is contained in:
Chuck Smith
2024-03-29 11:01:15 -04:00
parent f65d7bfb1c
commit 12d43c9835
58 changed files with 164 additions and 132 deletions

View File

@@ -10,8 +10,8 @@ type Array struct {
Elements []Object
}
func (ao *Array) Type() ObjectType {
return ARRAY_OBJ
func (ao *Array) Type() Type {
return ArrayType
}
func (ao *Array) Bool() bool {

View File

@@ -12,8 +12,8 @@ func (b *Boolean) Bool() bool {
return b.Value
}
func (b *Boolean) Type() ObjectType {
return BOOLEAN_OBJ
func (b *Boolean) Type() Type {
return BooleanType
}
func (b *Boolean) Inspect() string {

View File

@@ -11,8 +11,8 @@ func (b *Builtin) Bool() bool {
return true
}
func (b *Builtin) Type() ObjectType {
return BUILTIN_OBJ
func (b *Builtin) Type() Type {
return BuiltinType
}
func (b *Builtin) Inspect() string {

View File

@@ -15,8 +15,8 @@ func (cf *CompiledFunction) Bool() bool {
return true
}
func (cf *CompiledFunction) Type() ObjectType {
return COMPILED_FUNCTION_OBJ
func (cf *CompiledFunction) Type() Type {
return CFunctionType
}
func (cf *CompiledFunction) Inspect() string {
@@ -36,8 +36,8 @@ func (c *Closure) Bool() bool {
return true
}
func (c *Closure) Type() ObjectType {
return CLOSURE_OBJ
func (c *Closure) Type() Type {
return ClosureType
}
func (c *Closure) Inspect() string {

View File

@@ -8,8 +8,8 @@ func (e *Error) Bool() bool {
return false
}
func (e *Error) Type() ObjectType {
return ERROR_OBJ
func (e *Error) Type() Type {
return ErrorType
}
func (e *Error) Inspect() string {

View File

@@ -16,8 +16,8 @@ func (f *Function) Bool() bool {
return false
}
func (f *Function) Type() ObjectType {
return FUNCTION_OBJ
func (f *Function) Type() Type {
return FunctionType
}
func (f *Function) Inspect() string {
@@ -50,8 +50,8 @@ func (rv *ReturnValue) Bool() bool {
return true
}
func (rv *ReturnValue) Type() ObjectType {
return RETURN_VALUE_OBJ
func (rv *ReturnValue) Type() Type {
return ReturnType
}
func (rv *ReturnValue) Inspect() string {

View File

@@ -8,7 +8,7 @@ import (
)
type HashKey struct {
Type ObjectType
Type Type
Value uint64
}
@@ -52,8 +52,8 @@ func (h *Hash) Bool() bool {
return len(h.Pairs) > 0
}
func (h *Hash) Type() ObjectType {
return HASH_OBJ
func (h *Hash) Type() Type {
return HashType
}
func (h *Hash) Inspect() string {

View File

@@ -10,8 +10,8 @@ func (i *Integer) Bool() bool {
return i.Value != 0
}
func (i *Integer) Type() ObjectType {
return INTEGER_OBJ
func (i *Integer) Type() Type {
return IntegerType
}
func (i *Integer) Inspect() string {

View File

@@ -12,8 +12,8 @@ func (m Module) String() string {
return m.Inspect()
}
func (m Module) Type() ObjectType {
return MODULE_OBJ
func (m Module) Type() Type {
return ModuleType
}
func (m Module) Bool() bool {

View File

@@ -6,8 +6,8 @@ func (n *Null) Bool() bool {
return false
}
func (n *Null) Type() ObjectType {
return NULL_OBJ
func (n *Null) Type() Type {
return NullType
}
func (n *Null) Inspect() string {

View File

@@ -3,24 +3,57 @@ package object
import "fmt"
// Type represents the type of an object
type ObjectType string
type Type int
const (
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"
MODULE_OBJ = "module"
NullType = iota
IntegerType
StringType
BooleanType
ReturnType
ErrorType
FunctionType
CFunctionType
BuiltinType
ClosureType
ArrayType
HashType
ModuleType
)
func (t Type) String() string {
switch t {
case NullType:
return "null"
case IntegerType:
return "int"
case StringType:
return "str"
case BooleanType:
return "bool"
case ReturnType:
return "Return"
case ErrorType:
return "error"
case FunctionType:
return "fn"
case CFunctionType:
return "CFunction"
case BuiltinType:
return "Builtin"
case ClosureType:
return "Closure"
case ArrayType:
return "array"
case HashType:
return "hash"
case ModuleType:
return "module"
default:
return "???"
}
}
// 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.
@@ -45,7 +78,7 @@ type Immutable interface {
// `Type()` and `Inspect()` functions
type Object interface {
fmt.Stringer
Type() ObjectType
Type() Type
Bool() bool
Inspect() string
}

View File

@@ -17,8 +17,8 @@ func (s *String) Bool() bool {
return s.Value != ""
}
func (s *String) Type() ObjectType {
return STRING_OBJ
func (s *String) Type() Type {
return StringType
}
func (s *String) Inspect() string {