clean up builtins
This commit is contained in:
@@ -42,8 +42,8 @@ func New() *Compiler {
|
|||||||
|
|
||||||
symbolTable := NewSymbolTable()
|
symbolTable := NewSymbolTable()
|
||||||
|
|
||||||
for i, v := range object.Builtins {
|
for i, builtin := range object.BuiltinsIndex {
|
||||||
symbolTable.DefineBuiltin(i, v.Name)
|
symbolTable.DefineBuiltin(i, builtin.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &Compiler{
|
return &Compiler{
|
||||||
|
|||||||
@@ -977,11 +977,11 @@ func TestBuiltins(t *testing.T) {
|
|||||||
`,
|
`,
|
||||||
expectedConstants: []interface{}{1},
|
expectedConstants: []interface{}{1},
|
||||||
expectedInstructions: []code.Instructions{
|
expectedInstructions: []code.Instructions{
|
||||||
code.Make(code.OpGetBuiltin, 0),
|
code.Make(code.OpGetBuiltin, 4),
|
||||||
code.Make(code.OpArray, 0),
|
code.Make(code.OpArray, 0),
|
||||||
code.Make(code.OpCall, 1),
|
code.Make(code.OpCall, 1),
|
||||||
code.Make(code.OpPop),
|
code.Make(code.OpPop),
|
||||||
code.Make(code.OpGetBuiltin, 6),
|
code.Make(code.OpGetBuiltin, 7),
|
||||||
code.Make(code.OpArray, 0),
|
code.Make(code.OpArray, 0),
|
||||||
code.Make(code.OpConstant, 0),
|
code.Make(code.OpConstant, 0),
|
||||||
code.Make(code.OpCall, 2),
|
code.Make(code.OpCall, 2),
|
||||||
@@ -992,7 +992,7 @@ func TestBuiltins(t *testing.T) {
|
|||||||
input: `fn() { len([]) }`,
|
input: `fn() { len([]) }`,
|
||||||
expectedConstants: []interface{}{
|
expectedConstants: []interface{}{
|
||||||
[]code.Instructions{
|
[]code.Instructions{
|
||||||
code.Make(code.OpGetBuiltin, 0),
|
code.Make(code.OpGetBuiltin, 4),
|
||||||
code.Make(code.OpArray, 0),
|
code.Make(code.OpArray, 0),
|
||||||
code.Make(code.OpCall, 1),
|
code.Make(code.OpCall, 1),
|
||||||
code.Make(code.OpReturn),
|
code.Make(code.OpReturn),
|
||||||
|
|||||||
@@ -4,14 +4,4 @@ import (
|
|||||||
"monkey/object"
|
"monkey/object"
|
||||||
)
|
)
|
||||||
|
|
||||||
var builtins = map[string]*object.Builtin{
|
var builtins = object.Builtins
|
||||||
"len": object.GetBuiltinByName("len"),
|
|
||||||
"input": object.GetBuiltinByName("input"),
|
|
||||||
"print": object.GetBuiltinByName("print"),
|
|
||||||
"first": object.GetBuiltinByName("first"),
|
|
||||||
"last": object.GetBuiltinByName("last"),
|
|
||||||
"rest": object.GetBuiltinByName("rest"),
|
|
||||||
"push": object.GetBuiltinByName("push"),
|
|
||||||
"pop": object.GetBuiltinByName("pop"),
|
|
||||||
"exit": object.GetBuiltinByName("exit"),
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -5,16 +5,41 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"sort"
|
||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
var Builtins = []struct {
|
var Builtins = map[string]*Builtin{
|
||||||
Name string
|
"len": {Name: "len", Fn: Len},
|
||||||
Builtin *Builtin
|
"input": {Name: "input", Fn: Input},
|
||||||
}{
|
"print": {Name: "print", Fn: Print},
|
||||||
{
|
"first": {Name: "first", Fn: First},
|
||||||
"len",
|
"last": {Name: "last", Fn: Last},
|
||||||
&Builtin{Name: "len", Fn: func(args ...Object) Object {
|
"rest": {Name: "rest", Fn: Rest},
|
||||||
|
"push": {Name: "push", Fn: Push},
|
||||||
|
"pop": {Name: "pop", Fn: Pop},
|
||||||
|
"exit": {Name: "exit", Fn: Exit},
|
||||||
|
}
|
||||||
|
|
||||||
|
var BuiltinsIndex []*Builtin
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
var keys []string
|
||||||
|
for k := range Builtins {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
sort.Strings(keys)
|
||||||
|
|
||||||
|
for _, k := range keys {
|
||||||
|
BuiltinsIndex = append(BuiltinsIndex, Builtins[k])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newError(format string, a ...interface{}) *Error {
|
||||||
|
return &Error{Message: fmt.Sprintf(format, a...)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Len(args ...Object) Object {
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
return newError("wrong number of arguments. got=%d, want=1",
|
return newError("wrong number of arguments. got=%d, want=1",
|
||||||
len(args))
|
len(args))
|
||||||
@@ -29,12 +54,9 @@ var Builtins = []struct {
|
|||||||
return newError("argument to `len` not supported, got %s",
|
return newError("argument to `len` not supported, got %s",
|
||||||
args[0].Type())
|
args[0].Type())
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
},
|
|
||||||
},
|
func Input(args ...Object) Object {
|
||||||
{
|
|
||||||
"input",
|
|
||||||
&Builtin{Name: "input", Fn: func(args ...Object) Object {
|
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
obj, ok := args[0].(*String)
|
obj, ok := args[0].(*String)
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -53,27 +75,24 @@ var Builtins = []struct {
|
|||||||
return newError(fmt.Sprintf("error reading input from stdin: %s", err))
|
return newError(fmt.Sprintf("error reading input from stdin: %s", err))
|
||||||
}
|
}
|
||||||
return &String{Value: string(line)}
|
return &String{Value: string(line)}
|
||||||
}},
|
}
|
||||||
},
|
|
||||||
{
|
func Print(args ...Object) Object {
|
||||||
"print",
|
|
||||||
&Builtin{Name: "print", Fn: func(args ...Object) Object {
|
|
||||||
for _, arg := range args {
|
for _, arg := range args {
|
||||||
fmt.Println(arg.Inspect())
|
fmt.Println(arg.Inspect())
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
}
|
||||||
},
|
|
||||||
},
|
func First(args ...Object) Object {
|
||||||
{
|
|
||||||
"first",
|
|
||||||
&Builtin{Name: "first", Fn: func(args ...Object) Object {
|
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
return newError("wrong number of arguments. got=%d, want=1", len(args))
|
return newError("wrong number of arguments. got=%d, want=1",
|
||||||
|
len(args))
|
||||||
}
|
}
|
||||||
if args[0].Type() != ARRAY_OBJ {
|
if args[0].Type() != ARRAY_OBJ {
|
||||||
return newError("argument to `first` must be ARRAY, got %s", args[0].Type())
|
return newError("argument to `first` must be ARRAY, got %s",
|
||||||
|
args[0].Type())
|
||||||
}
|
}
|
||||||
|
|
||||||
arr := args[0].(*Array)
|
arr := args[0].(*Array)
|
||||||
@@ -82,17 +101,16 @@ var Builtins = []struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
}
|
||||||
},
|
|
||||||
},
|
func Last(args ...Object) Object {
|
||||||
{
|
|
||||||
"last",
|
|
||||||
&Builtin{Name: "last", Fn: func(args ...Object) Object {
|
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
return newError("wrong number of arguments. got=%d, want=1", len(args))
|
return newError("wrong number of arguments. got=%d, want=1",
|
||||||
|
len(args))
|
||||||
}
|
}
|
||||||
if args[0].Type() != ARRAY_OBJ {
|
if args[0].Type() != ARRAY_OBJ {
|
||||||
return newError("argument to `last` must be ARRAY, got %s", args[0].Type())
|
return newError("argument to `last` must be ARRAY, got %s",
|
||||||
|
args[0].Type())
|
||||||
}
|
}
|
||||||
|
|
||||||
arr := args[0].(*Array)
|
arr := args[0].(*Array)
|
||||||
@@ -102,12 +120,9 @@ var Builtins = []struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
}
|
||||||
},
|
|
||||||
},
|
func Rest(args ...Object) Object {
|
||||||
{
|
|
||||||
"rest",
|
|
||||||
&Builtin{Name: "rest", Fn: func(args ...Object) Object {
|
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
return newError("wrong number of arguments. got=%d, want=1",
|
return newError("wrong number of arguments. got=%d, want=1",
|
||||||
len(args))
|
len(args))
|
||||||
@@ -126,12 +141,9 @@ var Builtins = []struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
},
|
}
|
||||||
},
|
|
||||||
},
|
func Push(args ...Object) Object {
|
||||||
{
|
|
||||||
"push",
|
|
||||||
&Builtin{Name: "push", Fn: func(args ...Object) Object {
|
|
||||||
if len(args) != 2 {
|
if len(args) != 2 {
|
||||||
return newError("wrong number of arguments. got=%d, want=2",
|
return newError("wrong number of arguments. got=%d, want=2",
|
||||||
len(args))
|
len(args))
|
||||||
@@ -144,7 +156,7 @@ var Builtins = []struct {
|
|||||||
arr := args[0].(*Array)
|
arr := args[0].(*Array)
|
||||||
length := len(arr.Elements)
|
length := len(arr.Elements)
|
||||||
|
|
||||||
newElements := make([]Object, length+1, length+1)
|
newElements := make([]Object, length+1)
|
||||||
copy(newElements, arr.Elements)
|
copy(newElements, arr.Elements)
|
||||||
if immutable, ok := args[1].(Immutable); ok {
|
if immutable, ok := args[1].(Immutable); ok {
|
||||||
newElements[length] = immutable.Clone()
|
newElements[length] = immutable.Clone()
|
||||||
@@ -153,12 +165,9 @@ var Builtins = []struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return &Array{Elements: newElements}
|
return &Array{Elements: newElements}
|
||||||
},
|
}
|
||||||
},
|
|
||||||
},
|
func Pop(args ...Object) Object {
|
||||||
{
|
|
||||||
"pop",
|
|
||||||
&Builtin{Name: "pop", Fn: func(args ...Object) Object {
|
|
||||||
if len(args) != 1 {
|
if len(args) != 1 {
|
||||||
return newError("wrong number of arguments. got=%d, want=1",
|
return newError("wrong number of arguments. got=%d, want=1",
|
||||||
len(args))
|
len(args))
|
||||||
@@ -179,12 +188,9 @@ var Builtins = []struct {
|
|||||||
arr.Elements = arr.Elements[:length-1]
|
arr.Elements = arr.Elements[:length-1]
|
||||||
|
|
||||||
return element
|
return element
|
||||||
},
|
}
|
||||||
},
|
|
||||||
},
|
func Exit(args ...Object) Object {
|
||||||
{
|
|
||||||
"exit",
|
|
||||||
&Builtin{Name: "exit", Fn: func(args ...Object) Object {
|
|
||||||
if len(args) == 1 {
|
if len(args) == 1 {
|
||||||
if args[0].Type() != INTEGER_OBJ {
|
if args[0].Type() != INTEGER_OBJ {
|
||||||
return newError("argument to `exit` must be INTEGER, got %s",
|
return newError("argument to `exit` must be INTEGER, got %s",
|
||||||
@@ -195,20 +201,4 @@ var Builtins = []struct {
|
|||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
func newError(format string, a ...interface{}) *Error {
|
|
||||||
return &Error{Message: fmt.Sprintf(format, a...)}
|
|
||||||
}
|
|
||||||
|
|
||||||
func GetBuiltinByName(name string) *Builtin {
|
|
||||||
for _, def := range Builtins {
|
|
||||||
if def.Name == name {
|
|
||||||
return def.Builtin
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -50,8 +50,8 @@ type VMState struct {
|
|||||||
|
|
||||||
func NewVMState() *VMState {
|
func NewVMState() *VMState {
|
||||||
symbolTable := compiler.NewSymbolTable()
|
symbolTable := compiler.NewSymbolTable()
|
||||||
for i, v := range object.Builtins {
|
for i, builtin := range object.BuiltinsIndex {
|
||||||
symbolTable.DefineBuiltin(i, v.Name)
|
symbolTable.DefineBuiltin(i, builtin.Name)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &VMState{
|
return &VMState{
|
||||||
|
|||||||
4
vm/vm.go
4
vm/vm.go
@@ -281,9 +281,9 @@ func (vm *VM) Run() error {
|
|||||||
builtinIndex := code.ReadUint8(ins[ip+1:])
|
builtinIndex := code.ReadUint8(ins[ip+1:])
|
||||||
vm.currentFrame().ip += 1
|
vm.currentFrame().ip += 1
|
||||||
|
|
||||||
definition := object.Builtins[builtinIndex]
|
builtin := object.BuiltinsIndex[builtinIndex]
|
||||||
|
|
||||||
err := vm.push(definition.Builtin)
|
err := vm.push(builtin)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user