-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add support for strings * add support for concatenating strings * add support for builtin functions: len * add support for arrays and indexing * add some more builtin functions for arrays * add support for hashes * add builtin puts function
- Loading branch information
Showing
11 changed files
with
871 additions
and
25 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 ( | ||
"fmt" | ||
|
||
"github.com/juandspy/monkey-lang/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)) | ||
} | ||
switch arg := args[0].(type) { | ||
case *object.String: | ||
return &object.Integer{Value: int64(len(arg.Value))} | ||
case *object.Array: | ||
return &object.Integer{Value: int64(len(arg.Elements))} | ||
default: | ||
return newError("argument to `len` not supported, got %s", args[0].Type()) | ||
} | ||
}, | ||
}, | ||
"first": { | ||
Fn: func(args ...object.Object) object.Object { | ||
if len(args) != 1 { | ||
return newError("wrong number of arguments. got=%d, want=1", | ||
len(args)) | ||
} | ||
if args[0].Type() != object.ARRAY_OBJ { | ||
return newError("argument to `first` must be ARRAY, got %s", args[0].Type()) | ||
} | ||
arr := args[0].(*object.Array) | ||
if len(arr.Elements) > 0 { | ||
return arr.Elements[0] | ||
} | ||
return NULL | ||
}, | ||
}, | ||
"last": { | ||
Fn: func(args ...object.Object) object.Object { | ||
if len(args) != 1 { | ||
return newError("wrong number of arguments. got=%d, want=1", | ||
len(args)) | ||
} | ||
if args[0].Type() != object.ARRAY_OBJ { | ||
return newError("argument to `last` must be ARRAY, got %s", args[0].Type()) | ||
} | ||
arr := args[0].(*object.Array) | ||
length := len(arr.Elements) | ||
if len(arr.Elements) > 0 { | ||
return arr.Elements[length-1] | ||
} | ||
return NULL | ||
}, | ||
}, | ||
// returns all the elements except the first one | ||
"rest": { | ||
Fn: func(args ...object.Object) object.Object { | ||
if len(args) != 1 { | ||
return newError("wrong number of arguments. got=%d, want=1", | ||
len(args)) | ||
} | ||
if args[0].Type() != object.ARRAY_OBJ { | ||
return newError("argument to `rest` must be ARRAY, got %s", args[0].Type()) | ||
} | ||
arr := args[0].(*object.Array) | ||
length := len(arr.Elements) | ||
if length > 0 { | ||
newElements := make([]object.Object, length-1, length-1) | ||
copy(newElements, arr.Elements[1:length]) | ||
return &object.Array{Elements: newElements} | ||
} | ||
return NULL | ||
}, | ||
}, | ||
"push": { | ||
Fn: func(args ...object.Object) object.Object { | ||
if len(args) != 2 { | ||
return newError("wrong number of arguments. got=%d, want=2", | ||
len(args)) | ||
} | ||
if args[0].Type() != object.ARRAY_OBJ { | ||
return newError("argument to `push` must be ARRAY, got %s", args[0].Type()) | ||
} | ||
arr := args[0].(*object.Array) | ||
length := len(arr.Elements) | ||
newElements := make([]object.Object, length+1, length+1) | ||
copy(newElements, arr.Elements) | ||
newElements[length] = args[1] | ||
return &object.Array{Elements: newElements} | ||
}, | ||
}, | ||
"puts": { | ||
Fn: func(args ...object.Object) object.Object { | ||
for _, arg := range args { | ||
fmt.Println(arg.Inspect()) | ||
} | ||
return NULL | ||
}, | ||
}, | ||
} |
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.