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,210 +5,200 @@ 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},
|
||||||
if len(args) != 1 {
|
"push": {Name: "push", Fn: Push},
|
||||||
return newError("wrong number of arguments. got=%d, want=1",
|
"pop": {Name: "pop", Fn: Pop},
|
||||||
len(args))
|
"exit": {Name: "exit", Fn: Exit},
|
||||||
}
|
}
|
||||||
|
|
||||||
switch arg := args[0].(type) {
|
var BuiltinsIndex []*Builtin
|
||||||
case *Array:
|
|
||||||
return &Integer{Value: int64(len(arg.Elements))}
|
|
||||||
case *String:
|
|
||||||
return &Integer{Value: int64(utf8.RuneCountInString(arg.Value))}
|
|
||||||
default:
|
|
||||||
return newError("argument to `len` not supported, got %s",
|
|
||||||
args[0].Type())
|
|
||||||
}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"input",
|
|
||||||
&Builtin{Name: "input", Fn: func(args ...Object) Object {
|
|
||||||
if len(args) > 0 {
|
|
||||||
obj, ok := args[0].(*String)
|
|
||||||
if !ok {
|
|
||||||
return newError(
|
|
||||||
"argument to `input` not supported, got %s",
|
|
||||||
args[0].Type(),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
fmt.Fprintf(os.Stdout, obj.Value)
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer := bufio.NewReader(os.Stdin)
|
func init() {
|
||||||
|
var keys []string
|
||||||
|
for k := range Builtins {
|
||||||
|
keys = append(keys, k)
|
||||||
|
}
|
||||||
|
sort.Strings(keys)
|
||||||
|
|
||||||
line, _, err := buffer.ReadLine()
|
for _, k := range keys {
|
||||||
if err != nil && err != io.EOF {
|
BuiltinsIndex = append(BuiltinsIndex, Builtins[k])
|
||||||
return newError(fmt.Sprintf("error reading input from stdin: %s", err))
|
}
|
||||||
}
|
|
||||||
return &String{Value: string(line)}
|
|
||||||
}},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"print",
|
|
||||||
&Builtin{Name: "print", Fn: func(args ...Object) Object {
|
|
||||||
for _, arg := range args {
|
|
||||||
fmt.Println(arg.Inspect())
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"first",
|
|
||||||
&Builtin{Name: "first", Fn: func(args ...Object) Object {
|
|
||||||
if len(args) != 1 {
|
|
||||||
return newError("wrong number of arguments. got=%d, want=1", len(args))
|
|
||||||
}
|
|
||||||
if args[0].Type() != ARRAY_OBJ {
|
|
||||||
return newError("argument to `first` must be ARRAY, got %s", args[0].Type())
|
|
||||||
}
|
|
||||||
|
|
||||||
arr := args[0].(*Array)
|
|
||||||
if len(arr.Elements) > 0 {
|
|
||||||
return arr.Elements[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"last",
|
|
||||||
&Builtin{Name: "last", Fn: func(args ...Object) Object {
|
|
||||||
if len(args) != 1 {
|
|
||||||
return newError("wrong number of arguments. got=%d, want=1", len(args))
|
|
||||||
}
|
|
||||||
if args[0].Type() != ARRAY_OBJ {
|
|
||||||
return newError("argument to `last` must be ARRAY, got %s", args[0].Type())
|
|
||||||
}
|
|
||||||
|
|
||||||
arr := args[0].(*Array)
|
|
||||||
length := len(arr.Elements)
|
|
||||||
if length > 0 {
|
|
||||||
return arr.Elements[length-1]
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"rest",
|
|
||||||
&Builtin{Name: "rest", Fn: func(args ...Object) Object {
|
|
||||||
if len(args) != 1 {
|
|
||||||
return newError("wrong number of arguments. got=%d, want=1",
|
|
||||||
len(args))
|
|
||||||
}
|
|
||||||
if args[0].Type() != ARRAY_OBJ {
|
|
||||||
return newError("argument to `rest` must be ARRAY, got %s",
|
|
||||||
args[0].Type())
|
|
||||||
}
|
|
||||||
|
|
||||||
arr := args[0].(*Array)
|
|
||||||
length := len(arr.Elements)
|
|
||||||
if length > 0 {
|
|
||||||
newElements := make([]Object, length-1, length-1)
|
|
||||||
copy(newElements, arr.Elements[1:length])
|
|
||||||
return &Array{Elements: newElements}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"push",
|
|
||||||
&Builtin{Name: "push", Fn: func(args ...Object) Object {
|
|
||||||
if len(args) != 2 {
|
|
||||||
return newError("wrong number of arguments. got=%d, want=2",
|
|
||||||
len(args))
|
|
||||||
}
|
|
||||||
if args[0].Type() != ARRAY_OBJ {
|
|
||||||
return newError("argument to `push` must be ARRAY, got %s",
|
|
||||||
args[0].Type())
|
|
||||||
}
|
|
||||||
|
|
||||||
arr := args[0].(*Array)
|
|
||||||
length := len(arr.Elements)
|
|
||||||
|
|
||||||
newElements := make([]Object, length+1, length+1)
|
|
||||||
copy(newElements, arr.Elements)
|
|
||||||
if immutable, ok := args[1].(Immutable); ok {
|
|
||||||
newElements[length] = immutable.Clone()
|
|
||||||
} else {
|
|
||||||
newElements[length] = args[1]
|
|
||||||
}
|
|
||||||
|
|
||||||
return &Array{Elements: newElements}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"pop",
|
|
||||||
&Builtin{Name: "pop", Fn: func(args ...Object) Object {
|
|
||||||
if len(args) != 1 {
|
|
||||||
return newError("wrong number of arguments. got=%d, want=1",
|
|
||||||
len(args))
|
|
||||||
}
|
|
||||||
if args[0].Type() != ARRAY_OBJ {
|
|
||||||
return newError("argument to `pop` must be ARRAY, got %s",
|
|
||||||
args[0].Type())
|
|
||||||
}
|
|
||||||
|
|
||||||
arr := args[0].(*Array)
|
|
||||||
length := len(arr.Elements)
|
|
||||||
|
|
||||||
if length == 0 {
|
|
||||||
return newError("cannot pop from an empty array")
|
|
||||||
}
|
|
||||||
|
|
||||||
element := arr.Elements[length-1]
|
|
||||||
arr.Elements = arr.Elements[:length-1]
|
|
||||||
|
|
||||||
return element
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"exit",
|
|
||||||
&Builtin{Name: "exit", Fn: func(args ...Object) Object {
|
|
||||||
if len(args) == 1 {
|
|
||||||
if args[0].Type() != INTEGER_OBJ {
|
|
||||||
return newError("argument to `exit` must be INTEGER, got %s",
|
|
||||||
args[0].Type())
|
|
||||||
}
|
|
||||||
os.Exit(int(args[0].(*Integer).Value))
|
|
||||||
} else {
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func newError(format string, a ...interface{}) *Error {
|
func newError(format string, a ...interface{}) *Error {
|
||||||
return &Error{Message: fmt.Sprintf(format, a...)}
|
return &Error{Message: fmt.Sprintf(format, a...)}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetBuiltinByName(name string) *Builtin {
|
func Len(args ...Object) Object {
|
||||||
for _, def := range Builtins {
|
if len(args) != 1 {
|
||||||
if def.Name == name {
|
return newError("wrong number of arguments. got=%d, want=1",
|
||||||
return def.Builtin
|
len(args))
|
||||||
|
}
|
||||||
|
|
||||||
|
switch arg := args[0].(type) {
|
||||||
|
case *Array:
|
||||||
|
return &Integer{Value: int64(len(arg.Elements))}
|
||||||
|
case *String:
|
||||||
|
return &Integer{Value: int64(utf8.RuneCountInString(arg.Value))}
|
||||||
|
default:
|
||||||
|
return newError("argument to `len` not supported, got %s",
|
||||||
|
args[0].Type())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Input(args ...Object) Object {
|
||||||
|
if len(args) > 0 {
|
||||||
|
obj, ok := args[0].(*String)
|
||||||
|
if !ok {
|
||||||
|
return newError(
|
||||||
|
"argument to `input` not supported, got %s",
|
||||||
|
args[0].Type(),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
fmt.Fprintf(os.Stdout, obj.Value)
|
||||||
|
}
|
||||||
|
|
||||||
|
buffer := bufio.NewReader(os.Stdin)
|
||||||
|
|
||||||
|
line, _, err := buffer.ReadLine()
|
||||||
|
if err != nil && err != io.EOF {
|
||||||
|
return newError(fmt.Sprintf("error reading input from stdin: %s", err))
|
||||||
|
}
|
||||||
|
return &String{Value: string(line)}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Print(args ...Object) Object {
|
||||||
|
for _, arg := range args {
|
||||||
|
fmt.Println(arg.Inspect())
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func First(args ...Object) Object {
|
||||||
|
if len(args) != 1 {
|
||||||
|
return newError("wrong number of arguments. got=%d, want=1",
|
||||||
|
len(args))
|
||||||
|
}
|
||||||
|
if args[0].Type() != ARRAY_OBJ {
|
||||||
|
return newError("argument to `first` must be ARRAY, got %s",
|
||||||
|
args[0].Type())
|
||||||
|
}
|
||||||
|
|
||||||
|
arr := args[0].(*Array)
|
||||||
|
if len(arr.Elements) > 0 {
|
||||||
|
return arr.Elements[0]
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Last(args ...Object) Object {
|
||||||
|
if len(args) != 1 {
|
||||||
|
return newError("wrong number of arguments. got=%d, want=1",
|
||||||
|
len(args))
|
||||||
|
}
|
||||||
|
if args[0].Type() != ARRAY_OBJ {
|
||||||
|
return newError("argument to `last` must be ARRAY, got %s",
|
||||||
|
args[0].Type())
|
||||||
|
}
|
||||||
|
|
||||||
|
arr := args[0].(*Array)
|
||||||
|
length := len(arr.Elements)
|
||||||
|
if length > 0 {
|
||||||
|
return arr.Elements[length-1]
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Rest(args ...Object) Object {
|
||||||
|
if len(args) != 1 {
|
||||||
|
return newError("wrong number of arguments. got=%d, want=1",
|
||||||
|
len(args))
|
||||||
|
}
|
||||||
|
if args[0].Type() != ARRAY_OBJ {
|
||||||
|
return newError("argument to `rest` must be ARRAY, got %s",
|
||||||
|
args[0].Type())
|
||||||
|
}
|
||||||
|
|
||||||
|
arr := args[0].(*Array)
|
||||||
|
length := len(arr.Elements)
|
||||||
|
if length > 0 {
|
||||||
|
newElements := make([]Object, length-1, length-1)
|
||||||
|
copy(newElements, arr.Elements[1:length])
|
||||||
|
return &Array{Elements: newElements}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func Push(args ...Object) Object {
|
||||||
|
if len(args) != 2 {
|
||||||
|
return newError("wrong number of arguments. got=%d, want=2",
|
||||||
|
len(args))
|
||||||
|
}
|
||||||
|
if args[0].Type() != ARRAY_OBJ {
|
||||||
|
return newError("argument to `push` must be ARRAY, got %s",
|
||||||
|
args[0].Type())
|
||||||
|
}
|
||||||
|
|
||||||
|
arr := args[0].(*Array)
|
||||||
|
length := len(arr.Elements)
|
||||||
|
|
||||||
|
newElements := make([]Object, length+1)
|
||||||
|
copy(newElements, arr.Elements)
|
||||||
|
if immutable, ok := args[1].(Immutable); ok {
|
||||||
|
newElements[length] = immutable.Clone()
|
||||||
|
} else {
|
||||||
|
newElements[length] = args[1]
|
||||||
|
}
|
||||||
|
|
||||||
|
return &Array{Elements: newElements}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Pop(args ...Object) Object {
|
||||||
|
if len(args) != 1 {
|
||||||
|
return newError("wrong number of arguments. got=%d, want=1",
|
||||||
|
len(args))
|
||||||
|
}
|
||||||
|
if args[0].Type() != ARRAY_OBJ {
|
||||||
|
return newError("argument to `pop` must be ARRAY, got %s",
|
||||||
|
args[0].Type())
|
||||||
|
}
|
||||||
|
|
||||||
|
arr := args[0].(*Array)
|
||||||
|
length := len(arr.Elements)
|
||||||
|
|
||||||
|
if length == 0 {
|
||||||
|
return newError("cannot pop from an empty array")
|
||||||
|
}
|
||||||
|
|
||||||
|
element := arr.Elements[length-1]
|
||||||
|
arr.Elements = arr.Elements[:length-1]
|
||||||
|
|
||||||
|
return element
|
||||||
|
}
|
||||||
|
|
||||||
|
func Exit(args ...Object) Object {
|
||||||
|
if len(args) == 1 {
|
||||||
|
if args[0].Type() != INTEGER_OBJ {
|
||||||
|
return newError("argument to `exit` must be INTEGER, got %s",
|
||||||
|
args[0].Type())
|
||||||
|
}
|
||||||
|
os.Exit(int(args[0].(*Integer).Value))
|
||||||
|
} else {
|
||||||
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
return nil
|
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