restructure project
Some checks failed
Build / build (push) Failing after 5m21s
Publish Image / publish (push) Failing after 32s
Test / build (push) Failing after 5m8s

This commit is contained in:
Chuck Smith
2024-03-28 16:20:09 -04:00
parent 362138ff2e
commit fc6ceee02c
93 changed files with 479 additions and 194 deletions

112
internal/object/array.go Normal file
View File

@@ -0,0 +1,112 @@
package object
import (
"bytes"
"strings"
)
// Array is the array literal type that holds a slice of Object(s)
type Array struct {
Elements []Object
}
func (ao *Array) Type() ObjectType {
return ARRAY_OBJ
}
func (ao *Array) Bool() bool {
return len(ao.Elements) > 0
}
func (a *Array) PopLeft() Object {
if len(a.Elements) > 0 {
e := a.Elements[0]
a.Elements = a.Elements[1:]
return e
}
return &Null{}
}
func (a *Array) PopRight() Object {
if len(a.Elements) > 0 {
e := a.Elements[(len(a.Elements) - 1)]
a.Elements = a.Elements[:(len(a.Elements) - 1)]
return e
}
return &Null{}
}
func (a *Array) Prepend(obj Object) {
a.Elements = append([]Object{obj}, a.Elements...)
}
func (a *Array) Append(obj Object) {
a.Elements = append(a.Elements, obj)
}
func (ao *Array) Inspect() string {
var out bytes.Buffer
var elements []string
for _, e := range ao.Elements {
elements = append(elements, e.Inspect())
}
out.WriteString("[")
out.WriteString(strings.Join(elements, ", "))
out.WriteString("]")
return out.String()
}
func (ao *Array) String() string {
return ao.Inspect()
}
func (ao *Array) Less(i, j int) bool {
if cmp, ok := ao.Elements[i].(Comparable); ok {
return cmp.Compare(ao.Elements[j]) == -1
}
return false
}
func (ao *Array) Compare(other Object) int {
if obj, ok := other.(*Array); ok {
if len(ao.Elements) != len(obj.Elements) {
return -1
}
for i, el := range ao.Elements {
cmp, ok := el.(Comparable)
if !ok {
return -1
}
if cmp.Compare(obj.Elements[i]) != 0 {
return cmp.Compare(obj.Elements[i])
}
}
return 0
}
return -1
}
func (ao *Array) Copy() *Array {
elements := make([]Object, len(ao.Elements))
for i, e := range ao.Elements {
elements[i] = e
}
return &Array{Elements: elements}
}
func (ao *Array) Reverse() {
for i, j := 0, len(ao.Elements)-1; i < j; i, j = i+1, j-1 {
ao.Elements[i], ao.Elements[j] = ao.Elements[j], ao.Elements[i]
}
}
func (ao *Array) Len() int {
return len(ao.Elements)
}
func (ao *Array) Swap(i, j int) {
ao.Elements[i], ao.Elements[j] = ao.Elements[j], ao.Elements[i]
}

43
internal/object/bool.go Normal file
View File

@@ -0,0 +1,43 @@
package object
import (
"fmt"
)
type Boolean struct {
Value bool
}
func (b *Boolean) Bool() bool {
return b.Value
}
func (b *Boolean) Type() ObjectType {
return BOOLEAN_OBJ
}
func (b *Boolean) Inspect() string {
return fmt.Sprintf("%t", b.Value)
}
func (b *Boolean) Clone() Object {
return &Boolean{Value: b.Value}
}
func (b *Boolean) String() string {
return b.Inspect()
}
func (b *Boolean) Int() int {
if b.Value {
return 1
}
return 0
}
func (b *Boolean) Compare(other Object) int {
if obj, ok := other.(*Boolean); ok {
return b.Int() - obj.Int()
}
return 1
}

View File

@@ -0,0 +1,24 @@
package object
import "fmt"
type Builtin struct {
Name string
Fn BuiltinFunction
}
func (b *Builtin) Bool() bool {
return true
}
func (b *Builtin) Type() ObjectType {
return BUILTIN_OBJ
}
func (b *Builtin) Inspect() string {
return fmt.Sprintf("<built-in function %s>", b.Name)
}
func (b *Builtin) String() string {
return b.Inspect()
}

View File

@@ -0,0 +1,49 @@
package object
import (
"fmt"
"monkey/internal/code"
)
type CompiledFunction struct {
Instructions code.Instructions
NumLocals int
NumParameters int
}
func (cf *CompiledFunction) Bool() bool {
return true
}
func (cf *CompiledFunction) Type() ObjectType {
return COMPILED_FUNCTION_OBJ
}
func (cf *CompiledFunction) Inspect() string {
return fmt.Sprintf("CompiledFunction[%p]", cf)
}
func (cf *CompiledFunction) String() string {
return cf.Inspect()
}
type Closure struct {
Fn *CompiledFunction
Free []Object
}
func (c *Closure) Bool() bool {
return true
}
func (c *Closure) Type() ObjectType {
return CLOSURE_OBJ
}
func (c *Closure) Inspect() string {
return fmt.Sprintf("Closure[%p]", c)
}
func (c *Closure) String() string {
return c.Inspect()
}

View File

@@ -0,0 +1,47 @@
package object
import "unicode"
func NewEnclosedEnvironment(outer *Environment) *Environment {
env := NewEnvironment()
env.outer = outer
return env
}
func NewEnvironment() *Environment {
s := make(map[string]Object)
return &Environment{store: s, outer: nil}
}
type Environment struct {
store map[string]Object
outer *Environment
}
func (e *Environment) Get(name string) (Object, bool) {
obj, ok := e.store[name]
if !ok && e.outer != nil {
obj, ok = e.outer.Get(name)
}
return obj, ok
}
func (e *Environment) Set(name string, val Object) Object {
e.store[name] = val
return val
}
// ExportedHash returns a new Hash with the names and values of every publicly
// exported binding in the environment. That is every binding that starts with a
// capital letter. This is used by the module import system to wrap up the
// evaluated module into an object.
func (e *Environment) ExportedHash() *Hash {
pairs := make(map[HashKey]HashPair)
for k, v := range e.store {
if unicode.IsUpper(rune(k[0])) {
s := &String{Value: k}
pairs[s.HashKey()] = HashPair{Key: s, Value: v}
}
}
return &Hash{Pairs: pairs}
}

25
internal/object/error.go Normal file
View File

@@ -0,0 +1,25 @@
package object
type Error struct {
Message string
}
func (e *Error) Bool() bool {
return false
}
func (e *Error) Type() ObjectType {
return ERROR_OBJ
}
func (e *Error) Inspect() string {
return "Error: " + e.Message
}
func (e *Error) Clone() Object {
return &Error{Message: e.Message}
}
func (e *Error) String() string {
return e.Message
}

View File

@@ -0,0 +1,63 @@
package object
import (
"bytes"
"monkey/internal/ast"
"strings"
)
type Function struct {
Parameters []*ast.Identifier
Body *ast.BlockStatement
Env *Environment
}
func (f *Function) Bool() bool {
return false
}
func (f *Function) Type() ObjectType {
return FUNCTION_OBJ
}
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()
}
func (f *Function) String() string {
return f.Inspect()
}
type ReturnValue struct {
Value Object
}
func (rv *ReturnValue) Bool() bool {
return true
}
func (rv *ReturnValue) Type() ObjectType {
return RETURN_VALUE_OBJ
}
func (rv *ReturnValue) Inspect() string {
return rv.Value.Inspect()
}
func (rv *ReturnValue) String() string {
return rv.Inspect()
}

102
internal/object/hash.go Normal file
View File

@@ -0,0 +1,102 @@
package object
import (
"bytes"
"fmt"
"hash/fnv"
"strings"
)
type HashKey struct {
Type ObjectType
Value uint64
}
func (b *Boolean) HashKey() HashKey {
var value uint64
if b.Value {
value = 1
} else {
value = 0
}
return HashKey{Type: b.Type(), Value: value}
}
func (i *Integer) HashKey() HashKey {
return HashKey{Type: i.Type(), Value: uint64(i.Value)}
}
func (s *String) HashKey() HashKey {
h := fnv.New64a()
h.Write([]byte(s.Value))
return HashKey{Type: s.Type(), Value: h.Sum64()}
}
type HashPair struct {
Key Object
Value Object
}
type Hash struct {
Pairs map[HashKey]HashPair
}
func (h *Hash) Len() int {
return len(h.Pairs)
}
func (h *Hash) Bool() bool {
return len(h.Pairs) > 0
}
func (h *Hash) Type() ObjectType {
return HASH_OBJ
}
func (h *Hash) Inspect() string {
var out bytes.Buffer
pairs := []string{}
for _, pair := range h.Pairs {
pairs = append(pairs, fmt.Sprintf("%s: %s", pair.Key.Inspect(), pair.Value.Inspect()))
}
out.WriteString("{")
out.WriteString(strings.Join(pairs, ", "))
out.WriteString("}")
return out.String()
}
func (h *Hash) String() string {
return h.Inspect()
}
func (h *Hash) Compare(other Object) int {
if obj, ok := other.(*Hash); ok {
if len(h.Pairs) != len(obj.Pairs) {
return -1
}
for _, pair := range h.Pairs {
left := pair.Value
hashed := left.(Hashable)
right, ok := obj.Pairs[hashed.HashKey()]
if !ok {
return -1
}
cmp, ok := left.(Comparable)
if !ok {
return -1
}
if cmp.Compare(right.Value) != 0 {
return cmp.Compare(right.Value)
}
}
return 0
}
return -1
}

41
internal/object/int.go Normal file
View File

@@ -0,0 +1,41 @@
package object
import "fmt"
type Integer struct {
Value int64
}
func (i *Integer) Bool() bool {
return i.Value != 0
}
func (i *Integer) Type() ObjectType {
return INTEGER_OBJ
}
func (i *Integer) Inspect() string {
return fmt.Sprintf("%d", i.Value)
}
func (i *Integer) Clone() Object {
return &Integer{Value: i.Value}
}
func (i *Integer) String() string {
return i.Inspect()
}
func (i *Integer) Compare(other Object) int {
if obj, ok := other.(*Integer); ok {
switch {
case i.Value < obj.Value:
return -1
case i.Value > obj.Value:
return 1
default:
return 0
}
}
return -1
}

29
internal/object/module.go Normal file
View File

@@ -0,0 +1,29 @@
package object
import "fmt"
// Module is the module type used to represent a collection of variabels.
type Module struct {
Name string
Attrs Object
}
func (m Module) String() string {
return m.Inspect()
}
func (m Module) Type() ObjectType {
return MODULE_OBJ
}
func (m Module) Bool() bool {
return true
}
func (m Module) Inspect() string {
return fmt.Sprintf("<module '%s'>", m.Name)
}
func (m Module) Compare(other Object) int {
return 1
}

26
internal/object/null.go Normal file
View File

@@ -0,0 +1,26 @@
package object
type Null struct{}
func (n *Null) Bool() bool {
return false
}
func (n *Null) Type() ObjectType {
return NULL_OBJ
}
func (n *Null) Inspect() string {
return "null"
}
func (n *Null) String() string {
return n.Inspect()
}
func (n *Null) Compare(other Object) int {
if _, ok := other.(*Null); ok {
return 0
}
return 1
}

60
internal/object/object.go Normal file
View File

@@ -0,0 +1,60 @@
package object
import "fmt"
// Type represents the type of an object
type ObjectType string
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"
)
// 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 {
Compare(other Object) int
}
// Sizeable is the interface for returning the size of an Object.
// Object(s) that have a valid size must implement this interface and the
// Len() method.
type Sizeable interface {
Len() int
}
// Immutable is the interface for all immutable objects which must implement
// the Clone() method used by binding names to values.
type Immutable interface {
Clone() Object
}
// Object represents a value and implementations are expected to implement
// `Type()` and `Inspect()` functions
type Object interface {
fmt.Stringer
Type() ObjectType
Bool() bool
Inspect() string
}
// Hashable is the interface for all hashable objects which must implement
// the HashKey() method which returns a HashKey result.
type Hashable interface {
HashKey() HashKey
}
// BuiltinFunction represents the builtin function type
type BuiltinFunction func(args ...Object) Object

View File

@@ -0,0 +1,22 @@
package object
import "testing"
func TestStringHashKey(t *testing.T) {
hello1 := &String{Value: "Hello World"}
hello2 := &String{Value: "Hello World"}
diff1 := &String{Value: "My name is johnny"}
diff2 := &String{Value: "My name is johnny"}
if hello1.HashKey() != hello2.HashKey() {
t.Errorf("string with same content have different hash keys")
}
if diff1.HashKey() != diff2.HashKey() {
t.Errorf("string with same content have different hash keys")
}
if hello1.HashKey() == diff1.HashKey() {
t.Errorf("string with different content have same hash keys")
}
}

10
internal/object/state.go Normal file
View File

@@ -0,0 +1,10 @@
package object
import "io"
var (
Arguments []string
StandardInput io.Reader
StandardOutput io.Writer
ExitFunction func(int)
)

48
internal/object/str.go Normal file
View File

@@ -0,0 +1,48 @@
package object
import (
"fmt"
"unicode/utf8"
)
type String struct {
Value string
}
func (s *String) Len() int {
return utf8.RuneCountInString(s.Value)
}
func (s *String) Bool() bool {
return s.Value != ""
}
func (s *String) Type() ObjectType {
return STRING_OBJ
}
func (s *String) Inspect() string {
return fmt.Sprintf("%#v", s.Value)
}
func (s *String) Clone() Object {
return &String{Value: s.Value}
}
func (s *String) String() string {
return s.Value
}
func (s *String) Compare(other Object) int {
if obj, ok := other.(*String); ok {
switch {
case s.Value < obj.Value:
return -1
case s.Value > obj.Value:
return 1
default:
return 0
}
}
return 1
}