array builtins
Some checks failed
Build / build (push) Successful in 14m31s
Test / build (push) Failing after 17m13s

This commit is contained in:
Chuck Smith
2024-03-24 16:29:18 -04:00
parent 3c66b980e7
commit fea9fb9f64
13 changed files with 197 additions and 34 deletions

View File

@@ -41,6 +41,10 @@ var Builtins = map[string]*object.Builtin{
"id": {Name: "id", Fn: IdOf},
"oct": {Name: "oct", Fn: Oct},
"pow": {Name: "pow", Fn: Pow},
"min": {Name: "min", Fn: Min},
"max": {Name: "max", Fn: Max},
"sorted": {Name: "sorted", Fn: Sorted},
"reversed": {Name: "reversed", Fn: Reversed},
}
// BuiltinsIndex ...

29
builtins/max.go Normal file
View File

@@ -0,0 +1,29 @@
package builtins
import (
"monkey/object"
"sort"
)
// Max ...
func Max(args ...object.Object) object.Object {
if len(args) != 1 {
return newError("wrong number of arguments. got=%d, want=1",
len(args))
}
if a, ok := args[0].(*object.Array); ok {
// TODO: Make this more generic
xs := make([]int, len(a.Elements))
for n, e := range a.Elements {
if i, ok := e.(*object.Integer); ok {
xs = append(xs, int(i.Value))
} else {
return newError("item #%d not an `int` got=%s", n, e.Type())
}
}
sort.Ints(xs)
return &object.Integer{Value: int64(xs[len(xs)-1])}
}
return newError("argument #1 to `max` expected to be `array` got=%T", args[0].Type())
}

29
builtins/min.go Normal file
View File

@@ -0,0 +1,29 @@
package builtins
import (
"monkey/object"
"sort"
)
// Min ...
func Min(args ...object.Object) object.Object {
if len(args) != 1 {
return newError("wrong number of arguments. got=%d, want=1",
len(args))
}
if a, ok := args[0].(*object.Array); ok {
// TODO: Make this more generic
xs := make([]int, len(a.Elements))
for n, e := range a.Elements {
if i, ok := e.(*object.Integer); ok {
xs = append(xs, int(i.Value))
} else {
return newError("item #%d not an `int` got=%s", n, e.Type())
}
}
sort.Ints(xs)
return &object.Integer{Value: int64(xs[0])}
}
return newError("argument #1 to `min` expected to be `array` got=%T", args[0].Type())
}

20
builtins/reversed.go Normal file
View File

@@ -0,0 +1,20 @@
package builtins
import (
"monkey/object"
)
// Reversed ...
func Reversed(args ...object.Object) object.Object {
if len(args) != 1 {
return newError("wrong number of arguments. got=%d, want=1",
len(args))
}
if a, ok := args[0].(*object.Array); ok {
newArray := a.Copy()
newArray.Reverse()
return newArray
}
return newError("argument #1 to `reversed` expected to be `array` got=%T", args[0].Type())
}

21
builtins/sorted.go Normal file
View File

@@ -0,0 +1,21 @@
package builtins
import (
"monkey/object"
"sort"
)
// Sorted ...
func Sorted(args ...object.Object) object.Object {
if len(args) != 1 {
return newError("wrong number of arguments. got=%d, want=1",
len(args))
}
if a, ok := args[0].(*object.Array); ok {
newArray := a.Copy()
sort.Sort(newArray)
return newArray
}
return newError("argument #1 to `sorted` expected to be `array` got=%T", args[0].Type())
}

View File

@@ -903,7 +903,7 @@ func TestBuiltins(t *testing.T) {
code.Make(code.OpArray, 0),
code.Make(code.OpCall, 1),
code.Make(code.OpPop),
code.Make(code.OpGetBuiltin, 25),
code.Make(code.OpGetBuiltin, 27),
code.Make(code.OpArray, 0),
code.Make(code.OpConstant, 0),
code.Make(code.OpCall, 2),

View File

@@ -75,6 +75,9 @@ func main() {
log.Fatal("no source file given to compile")
}
f, err := os.Open(args[0])
if err != nil {
log.Fatal(err)
}
defer f.Close()
b, err := io.ReadAll(f)

View File

@@ -49,3 +49,31 @@ func (ao *Array) Equal(other Object) bool {
}
return false
}
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]
}
func (ao *Array) Less(i, j int) bool {
if cmp, ok := ao.Elements[i].(Comparable); ok {
return cmp.Less(ao.Elements[j])
}
return false
}

View File

@@ -26,3 +26,16 @@ func (b *Boolean) Equal(other Object) bool {
}
return false
}
func (b *Boolean) Int() int64 {
if b.Value {
return 1
}
return 0
}
func (b *Boolean) Less(other Object) bool {
if obj, ok := other.(*Boolean); ok {
return b.Int() < obj.Int()
}
return false
}

View File

@@ -24,3 +24,9 @@ func (i *Integer) Equal(other Object) bool {
}
return false
}
func (i *Integer) Less(other Object) bool {
if obj, ok := other.(*Integer); ok {
return i.Value < obj.Value
}
return true
}

View File

@@ -15,3 +15,6 @@ func (n *Null) Equal(other Object) bool {
_, ok := other.(*Null)
return ok
}
func (n *Null) Less(other Object) bool {
return false
}

View File

@@ -23,6 +23,7 @@ const (
// Returns `true` iif the types and values are identical, `false` otherwise.
type Comparable interface {
Equal(other Object) bool
Less(other Object) bool
}
// Immutable is the interface for all immutable objects which must implement

View File

@@ -24,3 +24,9 @@ func (s *String) Equal(other Object) bool {
}
return false
}
func (s *String) Less(other Object) bool {
if obj, ok := other.(*String); ok {
return s.Value < obj.Value
}
return false
}