Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
447 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package evaluator | ||
|
||
import "monkey-interpreter/object" | ||
|
||
var builtins = map[string]*object.Builtin{ | ||
"len": { | ||
Fn: func(args ...object.Object) object.Object { | ||
if len(args) != 1 { | ||
return newError("wrong number of arguments. got=%d, want=1", len(args)) | ||
} | ||
|
||
var size int | ||
|
||
switch arg := args[0].(type) { | ||
case *object.String: | ||
size = len(arg.Value) | ||
case *object.Array: | ||
size = len(arg.Elements) | ||
default: | ||
return newError("argument to `len` not supported, got %s", args[0].Type()) | ||
} | ||
|
||
return &object.Integer{Value: int64(size)} | ||
}, | ||
}, | ||
"first": { | ||
Fn: func(args ...object.Object) object.Object { | ||
if len(args) != 1 { | ||
return newError("wrong number of arguments. got=%d, want=1", len(args)) | ||
} | ||
|
||
switch arg := args[0].(type) { | ||
case *object.Array: | ||
if len(arg.Elements) == 0 { | ||
return NULL | ||
} | ||
return arg.Elements[0] | ||
default: | ||
return newError("argument to `first` not supported, got %s", args[0].Type()) | ||
} | ||
}, | ||
}, | ||
"last": { | ||
Fn: func(args ...object.Object) object.Object { | ||
if len(args) != 1 { | ||
return newError("wrong number of arguments. got=%d, want=1", len(args)) | ||
} | ||
|
||
switch arg := args[0].(type) { | ||
case *object.Array: | ||
if len(arg.Elements) == 0 { | ||
return NULL | ||
} | ||
return arg.Elements[len(arg.Elements)-1] | ||
default: | ||
return newError("argument to `last` not supported, got %s", args[0].Type()) | ||
} | ||
}, | ||
}, | ||
"tail": { | ||
Fn: func(args ...object.Object) object.Object { | ||
if len(args) != 1 { | ||
return newError("wrong number of arguments. got=%d, want=1", len(args)) | ||
} | ||
|
||
switch arg := args[0].(type) { | ||
case *object.Array: | ||
tailArray := make([]object.Object, len(arg.Elements)-1) | ||
|
||
if len(arg.Elements) > 1 { | ||
copy(tailArray, arg.Elements[1:]) | ||
} | ||
|
||
return &object.Array{Elements: tailArray} | ||
default: | ||
return newError("argument to `tail` not supported, got %s", args[0].Type()) | ||
} | ||
}, | ||
}, | ||
"push": { | ||
Fn: func(args ...object.Object) object.Object { | ||
if len(args) < 2 { | ||
return newError("wrong number of arguments. got=%d, wanted at least 2", len(args)) | ||
} | ||
|
||
switch arg := args[0].(type) { | ||
case *object.Array: | ||
newArray := make([]object.Object, len(arg.Elements)-1) | ||
if len(arg.Elements) > 1 { | ||
copy(newArray, arg.Elements[1:]) | ||
} | ||
|
||
for _, element := range args[1:] { | ||
newArray = append(newArray, element) | ||
} | ||
|
||
return &object.Array{Elements: newArray} | ||
default: | ||
return newError("argument to `push` not supported, got %s", args[0].Type()) | ||
} | ||
}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.