2022-08-07 09:44:11 -05:00
|
|
|
package object
|
|
|
|
|
2022-08-26 14:00:32 -05:00
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2024-08-28 19:31:35 -05:00
|
|
|
"monna/ast"
|
2022-08-26 14:00:32 -05:00
|
|
|
"strings"
|
|
|
|
)
|
2022-08-07 09:44:11 -05:00
|
|
|
|
|
|
|
type ObjectType string
|
|
|
|
|
|
|
|
const (
|
2022-08-11 00:04:44 -05:00
|
|
|
INTEGER_OBJECT = "INTEGER"
|
|
|
|
BOOLEAN_OBJECT = "BOOLEAN"
|
|
|
|
NULL_OBJECT = "NULL"
|
|
|
|
RETURN_VALUE_OBJECT = "RETURN_VALUE"
|
2022-08-11 01:55:07 -05:00
|
|
|
ERROR_OBJECT = "ERROR"
|
2022-08-26 14:00:32 -05:00
|
|
|
FUNCTION_OBJECT = "FUNCTION"
|
2023-05-09 14:59:18 -05:00
|
|
|
STRING_OBJECT = "STRING"
|
2023-08-14 14:40:04 -05:00
|
|
|
BUILTIN_OBJ = "BUILTIN"
|
2022-08-07 09:44:11 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
type Object interface {
|
|
|
|
Type() ObjectType
|
|
|
|
Inspect() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Integer
|
|
|
|
type Integer struct {
|
|
|
|
Value int64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Integer) Type() ObjectType {
|
|
|
|
return INTEGER_OBJECT
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *Integer) Inspect() string {
|
|
|
|
return fmt.Sprintf("%d", i.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Booleans
|
|
|
|
type Boolean struct {
|
|
|
|
Value bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Boolean) Type() ObjectType {
|
|
|
|
return BOOLEAN_OBJECT
|
|
|
|
}
|
|
|
|
|
2022-08-07 09:58:35 -05:00
|
|
|
func (b *Boolean) Inspect() string {
|
2022-08-07 09:44:11 -05:00
|
|
|
return fmt.Sprintf("%t", b.Value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Null
|
|
|
|
type Null struct{}
|
|
|
|
|
|
|
|
func (n *Null) Type() ObjectType {
|
|
|
|
return NULL_OBJECT
|
|
|
|
}
|
|
|
|
|
2022-08-07 09:58:35 -05:00
|
|
|
func (n *Null) Inspect() string {
|
2022-08-07 09:44:11 -05:00
|
|
|
return "null"
|
|
|
|
}
|
2022-08-11 00:04:44 -05:00
|
|
|
|
|
|
|
// Return
|
|
|
|
type ReturnValue struct {
|
|
|
|
Value Object
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rv *ReturnValue) Type() ObjectType { return RETURN_VALUE_OBJECT }
|
|
|
|
func (rv *ReturnValue) Inspect() string { return rv.Value.Inspect() }
|
2022-08-11 01:55:07 -05:00
|
|
|
|
|
|
|
// Error
|
|
|
|
type Error struct {
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (err *Error) Type() ObjectType { return ERROR_OBJECT }
|
|
|
|
func (err *Error) Inspect() string { return "ERROR: " + err.Message }
|
2022-08-26 14:00:32 -05:00
|
|
|
|
|
|
|
// Function
|
|
|
|
type Function struct {
|
|
|
|
Parameters []*ast.Identifier
|
|
|
|
Body *ast.BlockStatement
|
|
|
|
Env *Environment
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *Function) Type() ObjectType { return FUNCTION_OBJECT }
|
|
|
|
func (f *Function) Inspect() string {
|
|
|
|
var out bytes.Buffer
|
|
|
|
|
|
|
|
params := []string{}
|
|
|
|
for _, p := range f.Parameters {
|
|
|
|
params = append(params, p.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
out.WriteString("fn")
|
|
|
|
out.WriteString("(")
|
|
|
|
out.WriteString(strings.Join(params, ", "))
|
|
|
|
out.WriteString(") {\n")
|
|
|
|
out.WriteString(f.Body.String())
|
|
|
|
out.WriteString("\n}")
|
|
|
|
|
|
|
|
return out.String()
|
|
|
|
}
|
2023-05-09 14:59:18 -05:00
|
|
|
|
|
|
|
// String
|
|
|
|
type String struct {
|
|
|
|
Value string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *String) Type() ObjectType { return STRING_OBJECT }
|
2023-08-14 14:40:04 -05:00
|
|
|
func (s *String) Inspect() string { return s.Value }
|
|
|
|
|
|
|
|
// Built-in functions
|
|
|
|
type BuiltinFunction func(args ...Object) Object
|
|
|
|
|
|
|
|
type Builtin struct {
|
|
|
|
Fn BuiltinFunction
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Builtin) Type() ObjectType { return BUILTIN_OBJ }
|
|
|
|
func (b *Builtin) Inspect() string { return "Builtin Function" }
|