optimizations
This commit is contained in:
@@ -63,11 +63,8 @@ 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 (a *Array) Less(i, j int) bool {
|
||||
return a.Elements[i].Compare(a.Elements[j]) == -1
|
||||
}
|
||||
|
||||
func (a *Array) Add(other Object) (Object, error) {
|
||||
@@ -129,18 +126,15 @@ func (ao *Array) Set(index, other Object) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ao *Array) Compare(other Object) int {
|
||||
func (a *Array) Compare(other Object) int {
|
||||
if obj, ok := other.(*Array); ok {
|
||||
if len(ao.Elements) != len(obj.Elements) {
|
||||
if len(a.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])
|
||||
for i, el := range a.Elements {
|
||||
val := el.Compare(obj.Elements[i])
|
||||
if val != 0 {
|
||||
return val
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,19 @@ func (b Builtin) Inspect() string {
|
||||
return fmt.Sprintf("<built-in function %s>", b.Name)
|
||||
}
|
||||
|
||||
func (b Builtin) Compare(other Object) int {
|
||||
if obj, ok := other.(Builtin); ok {
|
||||
if b.Name > obj.Name {
|
||||
return 1
|
||||
}
|
||||
if b.Name == obj.Name {
|
||||
return 0
|
||||
}
|
||||
return -1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func (b Builtin) String() string {
|
||||
return b.Inspect()
|
||||
}
|
||||
|
||||
@@ -23,11 +23,16 @@ func (cf *CompiledFunction) Inspect() string {
|
||||
return fmt.Sprintf("CompiledFunction[%p]", cf)
|
||||
}
|
||||
|
||||
func (cf CompiledFunction) Compare(other Object) int {
|
||||
return -1
|
||||
}
|
||||
|
||||
func (cf *CompiledFunction) String() string {
|
||||
return cf.Inspect()
|
||||
}
|
||||
|
||||
type Closure struct {
|
||||
BaseObject
|
||||
Fn *CompiledFunction
|
||||
Free []Object
|
||||
}
|
||||
|
||||
@@ -23,6 +23,23 @@ func (e Error) Copy() Object {
|
||||
return Error{Message: e.Message}
|
||||
}
|
||||
|
||||
func (e Error) Compare(other Object) int {
|
||||
if obj, ok := other.(Error); ok {
|
||||
if e.Message > obj.Message {
|
||||
return 1
|
||||
}
|
||||
if e.Message == obj.Message {
|
||||
return 0
|
||||
}
|
||||
return -1
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
func (e Error) Error() string {
|
||||
return e.Message
|
||||
}
|
||||
|
||||
func (e Error) String() string {
|
||||
return e.Message
|
||||
}
|
||||
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
)
|
||||
|
||||
type Function struct {
|
||||
BaseObject
|
||||
|
||||
Parameters []*ast.Identifier
|
||||
Body *ast.BlockStatement
|
||||
Env *Environment
|
||||
@@ -43,6 +45,7 @@ func (f Function) String() string {
|
||||
}
|
||||
|
||||
type ReturnValue struct {
|
||||
BaseObject
|
||||
Value Object
|
||||
}
|
||||
|
||||
|
||||
@@ -97,18 +97,14 @@ func (h *Hash) Compare(other Object) int {
|
||||
return -1
|
||||
}
|
||||
for _, pair := range h.Pairs {
|
||||
left := pair.Value
|
||||
hashed := left.(Hasher)
|
||||
hashed := pair.Value.(Hasher)
|
||||
right, ok := obj.Pairs[hashed.Hash()]
|
||||
if !ok {
|
||||
return -1
|
||||
}
|
||||
cmp, ok := left.(Comparable)
|
||||
if !ok {
|
||||
return -1
|
||||
}
|
||||
if cmp.Compare(right.Value) != 0 {
|
||||
return cmp.Compare(right.Value)
|
||||
val := pair.Value.Compare(right.Value)
|
||||
if val != 0 {
|
||||
return val
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -57,11 +57,6 @@ func (t Type) String() string {
|
||||
}
|
||||
}
|
||||
|
||||
// Comparable is the interface for objects to implement suitable comparisons
|
||||
type Comparable interface {
|
||||
Compare(other Object) int
|
||||
}
|
||||
|
||||
// Copyable is the interface for creating copies of objects
|
||||
type Copyable interface {
|
||||
Copy() Object
|
||||
@@ -73,9 +68,14 @@ type Object interface {
|
||||
fmt.Stringer
|
||||
Type() Type
|
||||
Bool() bool
|
||||
Compare(Object) int
|
||||
Inspect() string
|
||||
}
|
||||
|
||||
type BaseObject struct{}
|
||||
|
||||
func (o BaseObject) Compare(other Object) int { return -1 }
|
||||
|
||||
// Hasher is the interface for objects to provide suitable hash keys
|
||||
type Hasher interface {
|
||||
Hash() HashKey
|
||||
|
||||
Reference in New Issue
Block a user