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

@@ -8,39 +8,43 @@ import (
// Builtins ...
var Builtins = map[string]*object.Builtin{
"len": {Name: "len", Fn: Len},
"input": {Name: "input", Fn: Input},
"print": {Name: "print", Fn: Print},
"first": {Name: "first", Fn: First},
"last": {Name: "last", Fn: Last},
"rest": {Name: "rest", Fn: Rest},
"push": {Name: "push", Fn: Push},
"pop": {Name: "pop", Fn: Pop},
"exit": {Name: "exit", Fn: Exit},
"assert": {Name: "assert", Fn: Assert},
"bool": {Name: "bool", Fn: Bool},
"int": {Name: "int", Fn: Int},
"str": {Name: "str", Fn: Str},
"type": {Name: "type", Fn: TypeOf},
"args": {Name: "args", Fn: Args},
"lower": {Name: "lower", Fn: Lower},
"upper": {Name: "upper", Fn: Upper},
"join": {Name: "join", Fn: Join},
"split": {Name: "split", Fn: Split},
"find": {Name: "find", Fn: Find},
"read": {Name: "read", Fn: Read},
"write": {Name: "write", Fn: Write},
"ffi": {Name: "ffi", Fn: FFI},
"abs": {Name: "abs", Fn: Abs},
"bin": {Name: "bin", Fn: Bin},
"hex": {Name: "hex", Fn: Hex},
"ord": {Name: "ord", Fn: Ord},
"chr": {Name: "chr", Fn: Chr},
"divmod": {Name: "divmod", Fn: Divmod},
"hash": {Name: "hash", Fn: HashOf},
"id": {Name: "id", Fn: IdOf},
"oct": {Name: "oct", Fn: Oct},
"pow": {Name: "pow", Fn: Pow},
"len": {Name: "len", Fn: Len},
"input": {Name: "input", Fn: Input},
"print": {Name: "print", Fn: Print},
"first": {Name: "first", Fn: First},
"last": {Name: "last", Fn: Last},
"rest": {Name: "rest", Fn: Rest},
"push": {Name: "push", Fn: Push},
"pop": {Name: "pop", Fn: Pop},
"exit": {Name: "exit", Fn: Exit},
"assert": {Name: "assert", Fn: Assert},
"bool": {Name: "bool", Fn: Bool},
"int": {Name: "int", Fn: Int},
"str": {Name: "str", Fn: Str},
"type": {Name: "type", Fn: TypeOf},
"args": {Name: "args", Fn: Args},
"lower": {Name: "lower", Fn: Lower},
"upper": {Name: "upper", Fn: Upper},
"join": {Name: "join", Fn: Join},
"split": {Name: "split", Fn: Split},
"find": {Name: "find", Fn: Find},
"read": {Name: "read", Fn: Read},
"write": {Name: "write", Fn: Write},
"ffi": {Name: "ffi", Fn: FFI},
"abs": {Name: "abs", Fn: Abs},
"bin": {Name: "bin", Fn: Bin},
"hex": {Name: "hex", Fn: Hex},
"ord": {Name: "ord", Fn: Ord},
"chr": {Name: "chr", Fn: Chr},
"divmod": {Name: "divmod", Fn: Divmod},
"hash": {Name: "hash", Fn: HashOf},
"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())
}