From 12d43c98353d2dd2ad721b454adf233f6cd94173 Mon Sep 17 00:00:00 2001 From: Chuck Smith Date: Fri, 29 Mar 2024 11:01:15 -0400 Subject: [PATCH] Refactor Type to be an int --- internal/ast/ast.go | 2 +- internal/builtins/abs.go | 2 +- internal/builtins/accept.go | 2 +- internal/builtins/assert.go | 2 +- internal/builtins/bin.go | 2 +- internal/builtins/bind.go | 2 +- internal/builtins/chr.go | 2 +- internal/builtins/close.go | 2 +- internal/builtins/connect.go | 2 +- internal/builtins/divmod.go | 2 +- internal/builtins/exit.go | 2 +- internal/builtins/ffi.go | 2 +- internal/builtins/find.go | 4 +-- internal/builtins/first.go | 2 +- internal/builtins/hex.go | 2 +- internal/builtins/input.go | 2 +- internal/builtins/join.go | 2 +- internal/builtins/last.go | 2 +- internal/builtins/listen.go | 2 +- internal/builtins/lower.go | 2 +- internal/builtins/max.go | 4 +-- internal/builtins/min.go | 4 +-- internal/builtins/oct.go | 2 +- internal/builtins/open.go | 2 +- internal/builtins/ord.go | 2 +- internal/builtins/pop.go | 2 +- internal/builtins/pow.go | 2 +- internal/builtins/print.go | 2 +- internal/builtins/push.go | 2 +- internal/builtins/read.go | 2 +- internal/builtins/readfile.go | 2 +- internal/builtins/rest.go | 2 +- internal/builtins/reversed.go | 2 +- internal/builtins/seek.go | 2 +- internal/builtins/socket.go | 2 +- internal/builtins/sorted.go | 2 +- internal/builtins/split.go | 2 +- internal/builtins/typeof.go | 2 +- internal/builtins/upper.go | 2 +- internal/builtins/write.go | 2 +- internal/builtins/writefile.go | 2 +- internal/compiler/bytecode.go | 17 +++++---- internal/evaluator/evaluator.go | 36 +++++++++---------- internal/object/array.go | 4 +-- internal/object/bool.go | 4 +-- internal/object/builtin.go | 4 +-- internal/object/closure.go | 8 ++--- internal/object/error.go | 4 +-- internal/object/function.go | 8 ++--- internal/object/hash.go | 6 ++-- internal/object/int.go | 4 +-- internal/object/module.go | 4 +-- internal/object/null.go | 4 +-- internal/object/object.go | 63 +++++++++++++++++++++++++-------- internal/object/str.go | 4 +-- internal/typing/typing.go | 2 +- internal/vm/vm.go | 32 ++++++++--------- repl.go | 4 +-- 58 files changed, 164 insertions(+), 132 deletions(-) diff --git a/internal/ast/ast.go b/internal/ast/ast.go index f5ae49b..0f61463 100644 --- a/internal/ast/ast.go +++ b/internal/ast/ast.go @@ -41,7 +41,7 @@ func (p *Program) String() string { var s strings.Builder for _, stmt := range p.Statements { - s.WriteString(fmt.Sprintf("%s\n", stmt.String())) + s.WriteString(stmt.String()) } return s.String() diff --git a/internal/builtins/abs.go b/internal/builtins/abs.go index 6adfe33..586155c 100644 --- a/internal/builtins/abs.go +++ b/internal/builtins/abs.go @@ -10,7 +10,7 @@ func Abs(args ...object.Object) object.Object { if err := typing.Check( "abs", args, typing.ExactArgs(1), - typing.WithTypes(object.INTEGER_OBJ), + typing.WithTypes(object.IntegerType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/accept.go b/internal/builtins/accept.go index e1e5599..cdfc49f 100644 --- a/internal/builtins/accept.go +++ b/internal/builtins/accept.go @@ -11,7 +11,7 @@ func Accept(args ...object.Object) object.Object { if err := typing.Check( "accept", args, typing.ExactArgs(1), - typing.WithTypes(object.INTEGER_OBJ), + typing.WithTypes(object.IntegerType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/assert.go b/internal/builtins/assert.go index 5e4ef81..6d69d9a 100644 --- a/internal/builtins/assert.go +++ b/internal/builtins/assert.go @@ -12,7 +12,7 @@ func Assert(args ...object.Object) object.Object { if err := typing.Check( "assert", args, typing.ExactArgs(2), - typing.WithTypes(object.BOOLEAN_OBJ, object.STRING_OBJ), + typing.WithTypes(object.BooleanType, object.StringType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/bin.go b/internal/builtins/bin.go index a520b5e..1b6795e 100644 --- a/internal/builtins/bin.go +++ b/internal/builtins/bin.go @@ -12,7 +12,7 @@ func Bin(args ...object.Object) object.Object { if err := typing.Check( "bin", args, typing.ExactArgs(1), - typing.WithTypes(object.INTEGER_OBJ), + typing.WithTypes(object.IntegerType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/bind.go b/internal/builtins/bind.go index e8a0d74..7cc9d5b 100644 --- a/internal/builtins/bind.go +++ b/internal/builtins/bind.go @@ -11,7 +11,7 @@ func Bind(args ...object.Object) object.Object { if err := typing.Check( "bind", args, typing.ExactArgs(2), - typing.WithTypes(object.INTEGER_OBJ, object.INTEGER_OBJ), + typing.WithTypes(object.IntegerType, object.IntegerType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/chr.go b/internal/builtins/chr.go index 11e992e..9df165c 100644 --- a/internal/builtins/chr.go +++ b/internal/builtins/chr.go @@ -11,7 +11,7 @@ func Chr(args ...object.Object) object.Object { if err := typing.Check( "chr", args, typing.ExactArgs(1), - typing.WithTypes(object.INTEGER_OBJ), + typing.WithTypes(object.IntegerType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/close.go b/internal/builtins/close.go index 66d6644..60f46ed 100644 --- a/internal/builtins/close.go +++ b/internal/builtins/close.go @@ -11,7 +11,7 @@ func Close(args ...object.Object) object.Object { if err := typing.Check( "close", args, typing.ExactArgs(1), - typing.WithTypes(object.INTEGER_OBJ), + typing.WithTypes(object.IntegerType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/connect.go b/internal/builtins/connect.go index fe2b347..0837202 100644 --- a/internal/builtins/connect.go +++ b/internal/builtins/connect.go @@ -11,7 +11,7 @@ func Connect(args ...object.Object) object.Object { if err := typing.Check( "connect", args, typing.ExactArgs(2), - typing.WithTypes(object.INTEGER_OBJ, object.STRING_OBJ), + typing.WithTypes(object.IntegerType, object.StringType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/divmod.go b/internal/builtins/divmod.go index c35617b..a23ecbf 100644 --- a/internal/builtins/divmod.go +++ b/internal/builtins/divmod.go @@ -10,7 +10,7 @@ func Divmod(args ...object.Object) object.Object { if err := typing.Check( "divmod", args, typing.ExactArgs(2), - typing.WithTypes(object.INTEGER_OBJ, object.INTEGER_OBJ), + typing.WithTypes(object.IntegerType, object.IntegerType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/exit.go b/internal/builtins/exit.go index 43c0de9..3f7b9d1 100644 --- a/internal/builtins/exit.go +++ b/internal/builtins/exit.go @@ -10,7 +10,7 @@ func Exit(args ...object.Object) object.Object { if err := typing.Check( "exit", args, typing.RangeOfArgs(0, 1), - typing.WithTypes(object.INTEGER_OBJ), + typing.WithTypes(object.IntegerType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/ffi.go b/internal/builtins/ffi.go index 2c3e43c..a43c8bc 100644 --- a/internal/builtins/ffi.go +++ b/internal/builtins/ffi.go @@ -15,7 +15,7 @@ func FFI(args ...object.Object) object.Object { if err := typing.Check( "ffi", args, typing.ExactArgs(2), - typing.WithTypes(object.STRING_OBJ, object.STRING_OBJ), + typing.WithTypes(object.StringType, object.StringType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/find.go b/internal/builtins/find.go index 3e1b33a..99a3a1b 100644 --- a/internal/builtins/find.go +++ b/internal/builtins/find.go @@ -23,7 +23,7 @@ func Find(args ...object.Object) object.Object { if haystack, ok := args[0].(*object.String); ok { if err := typing.Check( "find", args, - typing.WithTypes(object.STRING_OBJ, object.STRING_OBJ), + typing.WithTypes(object.StringType, object.StringType), ); err != nil { return newError(err.Error()) } @@ -46,7 +46,7 @@ func Find(args ...object.Object) object.Object { } return newError( - "TypeError: find() expected argument #1 to be `array` or `str` got `%s`", + "TypeError: find() expected argument #1 to be `array` or `str` got `%d`", args[0].Type(), ) } diff --git a/internal/builtins/first.go b/internal/builtins/first.go index ca1e6c5..3a2604c 100644 --- a/internal/builtins/first.go +++ b/internal/builtins/first.go @@ -10,7 +10,7 @@ func First(args ...object.Object) object.Object { if err := typing.Check( "first", args, typing.ExactArgs(1), - typing.WithTypes(object.ARRAY_OBJ), + typing.WithTypes(object.ArrayType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/hex.go b/internal/builtins/hex.go index b801350..a2d48a2 100644 --- a/internal/builtins/hex.go +++ b/internal/builtins/hex.go @@ -12,7 +12,7 @@ func Hex(args ...object.Object) object.Object { if err := typing.Check( "hex", args, typing.ExactArgs(1), - typing.WithTypes(object.INTEGER_OBJ), + typing.WithTypes(object.IntegerType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/input.go b/internal/builtins/input.go index bc8236f..b8b8c58 100644 --- a/internal/builtins/input.go +++ b/internal/builtins/input.go @@ -14,7 +14,7 @@ func Input(args ...object.Object) object.Object { if err := typing.Check( "input", args, typing.RangeOfArgs(0, 1), - typing.WithTypes(object.STRING_OBJ), + typing.WithTypes(object.StringType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/join.go b/internal/builtins/join.go index 2640d59..d7f698b 100644 --- a/internal/builtins/join.go +++ b/internal/builtins/join.go @@ -11,7 +11,7 @@ func Join(args ...object.Object) object.Object { if err := typing.Check( "join", args, typing.ExactArgs(2), - typing.WithTypes(object.ARRAY_OBJ, object.STRING_OBJ), + typing.WithTypes(object.ArrayType, object.StringType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/last.go b/internal/builtins/last.go index f7d121c..3b0f314 100644 --- a/internal/builtins/last.go +++ b/internal/builtins/last.go @@ -10,7 +10,7 @@ func Last(args ...object.Object) object.Object { if err := typing.Check( "last", args, typing.ExactArgs(1), - typing.WithTypes(object.ARRAY_OBJ), + typing.WithTypes(object.ArrayType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/listen.go b/internal/builtins/listen.go index 624b72d..b72894c 100644 --- a/internal/builtins/listen.go +++ b/internal/builtins/listen.go @@ -11,7 +11,7 @@ func Listen(args ...object.Object) object.Object { if err := typing.Check( "listen", args, typing.ExactArgs(2), - typing.WithTypes(object.INTEGER_OBJ, object.INTEGER_OBJ), + typing.WithTypes(object.IntegerType, object.IntegerType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/lower.go b/internal/builtins/lower.go index af52a68..808910c 100644 --- a/internal/builtins/lower.go +++ b/internal/builtins/lower.go @@ -11,7 +11,7 @@ func Lower(args ...object.Object) object.Object { if err := typing.Check( "lower", args, typing.ExactArgs(1), - typing.WithTypes(object.STRING_OBJ), + typing.WithTypes(object.StringType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/max.go b/internal/builtins/max.go index f373134..6c3f3e3 100644 --- a/internal/builtins/max.go +++ b/internal/builtins/max.go @@ -11,7 +11,7 @@ func Max(args ...object.Object) object.Object { if err := typing.Check( "max", args, typing.ExactArgs(1), - typing.WithTypes(object.ARRAY_OBJ), + typing.WithTypes(object.ArrayType), ); err != nil { return newError(err.Error()) } @@ -23,7 +23,7 @@ func Max(args ...object.Object) object.Object { 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()) + return newError("item #%d not an `int` got=%d", n, e.Type()) } } sort.Ints(xs) diff --git a/internal/builtins/min.go b/internal/builtins/min.go index 47d5c87..ceeb999 100644 --- a/internal/builtins/min.go +++ b/internal/builtins/min.go @@ -11,7 +11,7 @@ func Min(args ...object.Object) object.Object { if err := typing.Check( "min", args, typing.ExactArgs(1), - typing.WithTypes(object.ARRAY_OBJ), + typing.WithTypes(object.ArrayType), ); err != nil { return newError(err.Error()) } @@ -23,7 +23,7 @@ func Min(args ...object.Object) object.Object { 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()) + return newError("item #%d not an `int` got=%d", n, e.Type()) } } sort.Ints(xs) diff --git a/internal/builtins/oct.go b/internal/builtins/oct.go index 9434f5c..4cd5151 100644 --- a/internal/builtins/oct.go +++ b/internal/builtins/oct.go @@ -12,7 +12,7 @@ func Oct(args ...object.Object) object.Object { if err := typing.Check( "oct", args, typing.ExactArgs(1), - typing.WithTypes(object.INTEGER_OBJ), + typing.WithTypes(object.IntegerType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/open.go b/internal/builtins/open.go index 988a871..c240b5e 100644 --- a/internal/builtins/open.go +++ b/internal/builtins/open.go @@ -55,7 +55,7 @@ func Open(args ...object.Object) object.Object { if err := typing.Check( "open", args, typing.RangeOfArgs(1, 2), - typing.WithTypes(object.STRING_OBJ, object.STRING_OBJ), + typing.WithTypes(object.StringType, object.StringType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/ord.go b/internal/builtins/ord.go index 616d346..151e22d 100644 --- a/internal/builtins/ord.go +++ b/internal/builtins/ord.go @@ -10,7 +10,7 @@ func Ord(args ...object.Object) object.Object { if err := typing.Check( "ord", args, typing.ExactArgs(1), - typing.WithTypes(object.STRING_OBJ), + typing.WithTypes(object.StringType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/pop.go b/internal/builtins/pop.go index 70d855c..af98a21 100644 --- a/internal/builtins/pop.go +++ b/internal/builtins/pop.go @@ -10,7 +10,7 @@ func Pop(args ...object.Object) object.Object { if err := typing.Check( "pop", args, typing.ExactArgs(1), - typing.WithTypes(object.ARRAY_OBJ), + typing.WithTypes(object.ArrayType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/pow.go b/internal/builtins/pow.go index 90eb239..a6d8a85 100644 --- a/internal/builtins/pow.go +++ b/internal/builtins/pow.go @@ -22,7 +22,7 @@ func Pow(args ...object.Object) object.Object { if err := typing.Check( "pow", args, typing.ExactArgs(2), - typing.WithTypes(object.INTEGER_OBJ, object.INTEGER_OBJ), + typing.WithTypes(object.IntegerType, object.IntegerType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/print.go b/internal/builtins/print.go index 0d4e5ca..1ad4fcb 100644 --- a/internal/builtins/print.go +++ b/internal/builtins/print.go @@ -11,7 +11,7 @@ func Print(args ...object.Object) object.Object { if err := typing.Check( "print", args, typing.MinimumArgs(1), - typing.WithTypes(object.STRING_OBJ), + typing.WithTypes(object.StringType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/push.go b/internal/builtins/push.go index 8298149..b0c781b 100644 --- a/internal/builtins/push.go +++ b/internal/builtins/push.go @@ -10,7 +10,7 @@ func Push(args ...object.Object) object.Object { if err := typing.Check( "push", args, typing.ExactArgs(2), - typing.WithTypes(object.ARRAY_OBJ), + typing.WithTypes(object.ArrayType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/read.go b/internal/builtins/read.go index 48bdbae..2cb33ea 100644 --- a/internal/builtins/read.go +++ b/internal/builtins/read.go @@ -14,7 +14,7 @@ func Read(args ...object.Object) object.Object { if err := typing.Check( "read", args, typing.RangeOfArgs(1, 2), - typing.WithTypes(object.INTEGER_OBJ, object.INTEGER_OBJ), + typing.WithTypes(object.IntegerType, object.IntegerType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/readfile.go b/internal/builtins/readfile.go index e934649..ec95abe 100644 --- a/internal/builtins/readfile.go +++ b/internal/builtins/readfile.go @@ -11,7 +11,7 @@ func ReadFile(args ...object.Object) object.Object { if err := typing.Check( "readfile", args, typing.ExactArgs(1), - typing.WithTypes(object.STRING_OBJ), + typing.WithTypes(object.StringType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/rest.go b/internal/builtins/rest.go index 4f8aeb6..74e9c57 100644 --- a/internal/builtins/rest.go +++ b/internal/builtins/rest.go @@ -10,7 +10,7 @@ func Rest(args ...object.Object) object.Object { if err := typing.Check( "rest", args, typing.ExactArgs(1), - typing.WithTypes(object.ARRAY_OBJ), + typing.WithTypes(object.ArrayType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/reversed.go b/internal/builtins/reversed.go index b9b0108..4b86cd8 100644 --- a/internal/builtins/reversed.go +++ b/internal/builtins/reversed.go @@ -10,7 +10,7 @@ func Reversed(args ...object.Object) object.Object { if err := typing.Check( "reversed", args, typing.ExactArgs(1), - typing.WithTypes(object.ARRAY_OBJ), + typing.WithTypes(object.ArrayType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/seek.go b/internal/builtins/seek.go index 61023a1..1725dbb 100644 --- a/internal/builtins/seek.go +++ b/internal/builtins/seek.go @@ -11,7 +11,7 @@ func Seek(args ...object.Object) object.Object { if err := typing.Check( "seek", args, typing.RangeOfArgs(1, 3), - typing.WithTypes(object.INTEGER_OBJ, object.INTEGER_OBJ, object.INTEGER_OBJ), + typing.WithTypes(object.IntegerType, object.IntegerType, object.IntegerType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/socket.go b/internal/builtins/socket.go index 8b43631..85f9242 100644 --- a/internal/builtins/socket.go +++ b/internal/builtins/socket.go @@ -12,7 +12,7 @@ func Socket(args ...object.Object) object.Object { if err := typing.Check( "socket", args, typing.ExactArgs(1), - typing.WithTypes(object.STRING_OBJ), + typing.WithTypes(object.StringType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/sorted.go b/internal/builtins/sorted.go index 52350d6..466e135 100644 --- a/internal/builtins/sorted.go +++ b/internal/builtins/sorted.go @@ -11,7 +11,7 @@ func Sorted(args ...object.Object) object.Object { if err := typing.Check( "sort", args, typing.ExactArgs(1), - typing.WithTypes(object.ARRAY_OBJ), + typing.WithTypes(object.ArrayType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/split.go b/internal/builtins/split.go index 865759b..f98973f 100644 --- a/internal/builtins/split.go +++ b/internal/builtins/split.go @@ -11,7 +11,7 @@ func Split(args ...object.Object) object.Object { if err := typing.Check( "split", args, typing.RangeOfArgs(1, 2), - typing.WithTypes(object.STRING_OBJ, object.STRING_OBJ), + typing.WithTypes(object.StringType, object.StringType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/typeof.go b/internal/builtins/typeof.go index 7d3fa09..be7c7c6 100644 --- a/internal/builtins/typeof.go +++ b/internal/builtins/typeof.go @@ -14,5 +14,5 @@ func TypeOf(args ...object.Object) object.Object { return newError(err.Error()) } - return &object.String{Value: string(args[0].Type())} + return &object.String{Value: args[0].Type().String()} } diff --git a/internal/builtins/upper.go b/internal/builtins/upper.go index 3771ca9..5e82a54 100644 --- a/internal/builtins/upper.go +++ b/internal/builtins/upper.go @@ -11,7 +11,7 @@ func Upper(args ...object.Object) object.Object { if err := typing.Check( "upper", args, typing.ExactArgs(1), - typing.WithTypes(object.STRING_OBJ), + typing.WithTypes(object.StringType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/write.go b/internal/builtins/write.go index cef0782..f6613cc 100644 --- a/internal/builtins/write.go +++ b/internal/builtins/write.go @@ -11,7 +11,7 @@ func Write(args ...object.Object) object.Object { if err := typing.Check( "write", args, typing.ExactArgs(2), - typing.WithTypes(object.INTEGER_OBJ, object.INTEGER_OBJ), + typing.WithTypes(object.IntegerType, object.IntegerType), ); err != nil { return newError(err.Error()) } diff --git a/internal/builtins/writefile.go b/internal/builtins/writefile.go index 02b2fc2..24d7016 100644 --- a/internal/builtins/writefile.go +++ b/internal/builtins/writefile.go @@ -11,7 +11,7 @@ func WriteFile(args ...object.Object) object.Object { if err := typing.Check( "writefile", args, typing.ExactArgs(2), - typing.WithTypes(object.STRING_OBJ, object.STRING_OBJ), + typing.WithTypes(object.StringType, object.StringType), ); err != nil { return newError(err.Error()) } diff --git a/internal/compiler/bytecode.go b/internal/compiler/bytecode.go index 57bfdd0..19c336a 100644 --- a/internal/compiler/bytecode.go +++ b/internal/compiler/bytecode.go @@ -67,8 +67,7 @@ func (e *encoder) WriteValue(data any) error { func (e *encoder) WriteObjects(objs ...object.Object) (err error) { for _, obj := range objs { - err = errors.Join(err, e.WriteValue(len(string(obj.Type())))) - err = errors.Join(err, e.WriteString(string(obj.Type()))) + err = errors.Join(err, e.WriteValue(obj.Type())) switch o := obj.(type) { case *object.Null: @@ -145,16 +144,16 @@ func (d *decoder) String(len int) (s string) { func (d *decoder) Objects(len int) (o []object.Object) { for i := 0; i < len; i++ { - switch t := d.String(d.Int()); t { - case object.NULL_OBJ: + switch t := object.Type(d.Int()); t { + case object.NullType: o = append(o, &object.Null{}) - case object.BOOLEAN_OBJ: + case object.BooleanType: o = append(o, &object.Boolean{Value: d.Byte() == 1}) - case object.INTEGER_OBJ: + case object.IntegerType: o = append(o, &object.Integer{Value: d.Int64()}) - case object.STRING_OBJ: + case object.StringType: o = append(o, &object.String{Value: d.String(d.Int())}) - case object.COMPILED_FUNCTION_OBJ: + case object.CFunctionType: // The order of the fields has to reflect the data layout in the encoded bytecode. o = append(o, &object.CompiledFunction{ NumParameters: d.Int(), @@ -162,7 +161,7 @@ func (d *decoder) Objects(len int) (o []object.Object) { Instructions: d.Bytes(d.Int()), }) default: - panic(fmt.Sprintf("decoder: unsupported decoding for type %s", t)) + panic(fmt.Sprintf("decoder: unsupported decoding for type %d", t)) } } return diff --git a/internal/evaluator/evaluator.go b/internal/evaluator/evaluator.go index 4f1acd1..54fbc4a 100644 --- a/internal/evaluator/evaluator.go +++ b/internal/evaluator/evaluator.go @@ -20,7 +20,7 @@ var ( func isError(obj object.Object) bool { if obj != nil { - return obj.Type() == object.ERROR_OBJ + return obj.Type() == object.ErrorType } return false } @@ -262,7 +262,7 @@ func evalBlockStatements(block *ast.BlockStatement, env *object.Environment) obj if result != nil { rt := result.Type() - if rt == object.RETURN_VALUE_OBJ || rt == object.ERROR_OBJ { + if rt == object.ReturnType || rt == object.ErrorType { return result } } @@ -281,7 +281,7 @@ func nativeBoolToBooleanObject(input bool) object.Object { func evalPrefixExpression(operator string, right object.Object) object.Object { switch operator { case "!": - if right.Type() == object.BOOLEAN_OBJ { + if right.Type() == object.BooleanType { return evalBooleanPrefixOperatorExpression(operator, right) } return evalIntegerPrefixOperatorExpression(operator, right) @@ -293,7 +293,7 @@ func evalPrefixExpression(operator string, right object.Object) object.Object { } func evalBooleanPrefixOperatorExpression(operator string, right object.Object) object.Object { - if right.Type() != object.BOOLEAN_OBJ { + if right.Type() != object.BooleanType { return newError("unknown operator: %s%s", operator, right.Type()) } @@ -310,7 +310,7 @@ func evalBooleanPrefixOperatorExpression(operator string, right object.Object) o } func evalIntegerPrefixOperatorExpression(operator string, right object.Object) object.Object { - if right.Type() != object.INTEGER_OBJ { + if right.Type() != object.IntegerType { return newError("unknown operator: -%s", right.Type()) } @@ -331,7 +331,7 @@ func evalInfixExpression(operator string, left, right object.Object) object.Obje switch { // {"a": 1} + {"b": 2} - case operator == "+" && left.Type() == object.HASH_OBJ && right.Type() == object.HASH_OBJ: + case operator == "+" && left.Type() == object.HashType && right.Type() == object.HashType: leftVal := left.(*object.Hash).Pairs rightVal := right.(*object.Hash).Pairs pairs := make(map[object.HashKey]object.HashPair) @@ -344,7 +344,7 @@ func evalInfixExpression(operator string, left, right object.Object) object.Obje return &object.Hash{Pairs: pairs} // [1] + [2] - case operator == "+" && left.Type() == object.ARRAY_OBJ && right.Type() == object.ARRAY_OBJ: + case operator == "+" && left.Type() == object.ArrayType && right.Type() == object.ArrayType: leftVal := left.(*object.Array).Elements rightVal := right.(*object.Array).Elements elements := make([]object.Object, len(leftVal)+len(rightVal)) @@ -352,7 +352,7 @@ func evalInfixExpression(operator string, left, right object.Object) object.Obje return &object.Array{Elements: elements} // [1] * 3 - case operator == "*" && left.Type() == object.ARRAY_OBJ && right.Type() == object.INTEGER_OBJ: + case operator == "*" && left.Type() == object.ArrayType && right.Type() == object.IntegerType: leftVal := left.(*object.Array).Elements rightVal := int(right.(*object.Integer).Value) elements := leftVal @@ -362,7 +362,7 @@ func evalInfixExpression(operator string, left, right object.Object) object.Obje return &object.Array{Elements: elements} // 3 * [1] - case operator == "*" && left.Type() == object.INTEGER_OBJ && right.Type() == object.ARRAY_OBJ: + case operator == "*" && left.Type() == object.IntegerType && right.Type() == object.ArrayType: leftVal := int(left.(*object.Integer).Value) rightVal := right.(*object.Array).Elements elements := rightVal @@ -372,13 +372,13 @@ func evalInfixExpression(operator string, left, right object.Object) object.Obje return &object.Array{Elements: elements} // " " * 4 - case operator == "*" && left.Type() == object.STRING_OBJ && right.Type() == object.INTEGER_OBJ: + case operator == "*" && left.Type() == object.StringType && right.Type() == object.IntegerType: leftVal := left.(*object.String).Value rightVal := right.(*object.Integer).Value return &object.String{Value: strings.Repeat(leftVal, int(rightVal))} // 4 * " " - case operator == "*" && left.Type() == object.INTEGER_OBJ && right.Type() == object.STRING_OBJ: + case operator == "*" && left.Type() == object.IntegerType && right.Type() == object.StringType: leftVal := left.(*object.Integer).Value rightVal := right.(*object.String).Value return &object.String{Value: strings.Repeat(rightVal, int(leftVal))} @@ -396,11 +396,11 @@ func evalInfixExpression(operator string, left, right object.Object) object.Obje case operator == ">": return nativeBoolToBooleanObject(left.(object.Comparable).Compare(right) == 1) - case left.Type() == object.BOOLEAN_OBJ && right.Type() == object.BOOLEAN_OBJ: + case left.Type() == object.BooleanType && right.Type() == object.BooleanType: return evalBooleanInfixExpression(operator, left, right) - case left.Type() == object.INTEGER_OBJ && right.Type() == object.INTEGER_OBJ: + case left.Type() == object.IntegerType && right.Type() == object.IntegerType: return evalIntegerInfixExpression(operator, left, right) - case left.Type() == object.STRING_OBJ && right.Type() == object.STRING_OBJ: + case left.Type() == object.StringType && right.Type() == object.StringType: return evalStringInfixExpression(operator, left, right) default: @@ -597,13 +597,13 @@ func unwrapReturnValue(obj object.Object) object.Object { func evalIndexExpression(left, index object.Object) object.Object { switch { - case left.Type() == object.STRING_OBJ && index.Type() == object.INTEGER_OBJ: + case left.Type() == object.StringType && index.Type() == object.IntegerType: return evalStringIndexExpression(left, index) - case left.Type() == object.ARRAY_OBJ && index.Type() == object.INTEGER_OBJ: + case left.Type() == object.ArrayType && index.Type() == object.IntegerType: return evalArrayIndexExpression(left, index) - case left.Type() == object.HASH_OBJ: + case left.Type() == object.HashType: return evalHashIndexExpression(left, index) - case left.Type() == object.MODULE_OBJ: + case left.Type() == object.ModuleType: return EvalModuleIndexExpression(left, index) default: return newError("index operator not supported: %s", left.Type()) diff --git a/internal/object/array.go b/internal/object/array.go index 7616977..20ca28f 100644 --- a/internal/object/array.go +++ b/internal/object/array.go @@ -10,8 +10,8 @@ type Array struct { Elements []Object } -func (ao *Array) Type() ObjectType { - return ARRAY_OBJ +func (ao *Array) Type() Type { + return ArrayType } func (ao *Array) Bool() bool { diff --git a/internal/object/bool.go b/internal/object/bool.go index 3d7cc4b..968a33f 100644 --- a/internal/object/bool.go +++ b/internal/object/bool.go @@ -12,8 +12,8 @@ func (b *Boolean) Bool() bool { return b.Value } -func (b *Boolean) Type() ObjectType { - return BOOLEAN_OBJ +func (b *Boolean) Type() Type { + return BooleanType } func (b *Boolean) Inspect() string { diff --git a/internal/object/builtin.go b/internal/object/builtin.go index 627fadc..c6afbd5 100644 --- a/internal/object/builtin.go +++ b/internal/object/builtin.go @@ -11,8 +11,8 @@ func (b *Builtin) Bool() bool { return true } -func (b *Builtin) Type() ObjectType { - return BUILTIN_OBJ +func (b *Builtin) Type() Type { + return BuiltinType } func (b *Builtin) Inspect() string { diff --git a/internal/object/closure.go b/internal/object/closure.go index 90ecad1..bf00f09 100644 --- a/internal/object/closure.go +++ b/internal/object/closure.go @@ -15,8 +15,8 @@ func (cf *CompiledFunction) Bool() bool { return true } -func (cf *CompiledFunction) Type() ObjectType { - return COMPILED_FUNCTION_OBJ +func (cf *CompiledFunction) Type() Type { + return CFunctionType } func (cf *CompiledFunction) Inspect() string { @@ -36,8 +36,8 @@ func (c *Closure) Bool() bool { return true } -func (c *Closure) Type() ObjectType { - return CLOSURE_OBJ +func (c *Closure) Type() Type { + return ClosureType } func (c *Closure) Inspect() string { diff --git a/internal/object/error.go b/internal/object/error.go index cb2f2ba..4f0e632 100644 --- a/internal/object/error.go +++ b/internal/object/error.go @@ -8,8 +8,8 @@ func (e *Error) Bool() bool { return false } -func (e *Error) Type() ObjectType { - return ERROR_OBJ +func (e *Error) Type() Type { + return ErrorType } func (e *Error) Inspect() string { diff --git a/internal/object/function.go b/internal/object/function.go index 225fbc7..c6e7406 100644 --- a/internal/object/function.go +++ b/internal/object/function.go @@ -16,8 +16,8 @@ func (f *Function) Bool() bool { return false } -func (f *Function) Type() ObjectType { - return FUNCTION_OBJ +func (f *Function) Type() Type { + return FunctionType } func (f *Function) Inspect() string { @@ -50,8 +50,8 @@ func (rv *ReturnValue) Bool() bool { return true } -func (rv *ReturnValue) Type() ObjectType { - return RETURN_VALUE_OBJ +func (rv *ReturnValue) Type() Type { + return ReturnType } func (rv *ReturnValue) Inspect() string { diff --git a/internal/object/hash.go b/internal/object/hash.go index 07f6a1a..5d5b462 100644 --- a/internal/object/hash.go +++ b/internal/object/hash.go @@ -8,7 +8,7 @@ import ( ) type HashKey struct { - Type ObjectType + Type Type Value uint64 } @@ -52,8 +52,8 @@ func (h *Hash) Bool() bool { return len(h.Pairs) > 0 } -func (h *Hash) Type() ObjectType { - return HASH_OBJ +func (h *Hash) Type() Type { + return HashType } func (h *Hash) Inspect() string { diff --git a/internal/object/int.go b/internal/object/int.go index 3edd8b3..dcb7cd9 100644 --- a/internal/object/int.go +++ b/internal/object/int.go @@ -10,8 +10,8 @@ func (i *Integer) Bool() bool { return i.Value != 0 } -func (i *Integer) Type() ObjectType { - return INTEGER_OBJ +func (i *Integer) Type() Type { + return IntegerType } func (i *Integer) Inspect() string { diff --git a/internal/object/module.go b/internal/object/module.go index a787b86..bffa438 100644 --- a/internal/object/module.go +++ b/internal/object/module.go @@ -12,8 +12,8 @@ func (m Module) String() string { return m.Inspect() } -func (m Module) Type() ObjectType { - return MODULE_OBJ +func (m Module) Type() Type { + return ModuleType } func (m Module) Bool() bool { diff --git a/internal/object/null.go b/internal/object/null.go index 91d8d19..653aa56 100644 --- a/internal/object/null.go +++ b/internal/object/null.go @@ -6,8 +6,8 @@ func (n *Null) Bool() bool { return false } -func (n *Null) Type() ObjectType { - return NULL_OBJ +func (n *Null) Type() Type { + return NullType } func (n *Null) Inspect() string { diff --git a/internal/object/object.go b/internal/object/object.go index b50c68f..7b52b1f 100644 --- a/internal/object/object.go +++ b/internal/object/object.go @@ -3,24 +3,57 @@ package object import "fmt" // Type represents the type of an object -type ObjectType string +type Type int const ( - INTEGER_OBJ = "int" - BOOLEAN_OBJ = "bool" - NULL_OBJ = "null" - RETURN_VALUE_OBJ = "return" - ERROR_OBJ = "error" - FUNCTION_OBJ = "fn" - STRING_OBJ = "str" - BUILTIN_OBJ = "builtin" - ARRAY_OBJ = "array" - HASH_OBJ = "hash" - COMPILED_FUNCTION_OBJ = "COMPILED_FUNCTION" - CLOSURE_OBJ = "closure" - MODULE_OBJ = "module" + NullType = iota + IntegerType + StringType + BooleanType + ReturnType + ErrorType + FunctionType + CFunctionType + BuiltinType + ClosureType + ArrayType + HashType + ModuleType ) +func (t Type) String() string { + switch t { + case NullType: + return "null" + case IntegerType: + return "int" + case StringType: + return "str" + case BooleanType: + return "bool" + case ReturnType: + return "Return" + case ErrorType: + return "error" + case FunctionType: + return "fn" + case CFunctionType: + return "CFunction" + case BuiltinType: + return "Builtin" + case ClosureType: + return "Closure" + case ArrayType: + return "array" + case HashType: + return "hash" + case ModuleType: + return "module" + default: + return "???" + } +} + // Comparable is the interface for comparing two Object and their underlying // values. It is the responsibility of the caller (left) to check for types. // Returns `true` iif the types and values are identical, `false` otherwise. @@ -45,7 +78,7 @@ type Immutable interface { // `Type()` and `Inspect()` functions type Object interface { fmt.Stringer - Type() ObjectType + Type() Type Bool() bool Inspect() string } diff --git a/internal/object/str.go b/internal/object/str.go index 2bcb889..796af31 100644 --- a/internal/object/str.go +++ b/internal/object/str.go @@ -17,8 +17,8 @@ func (s *String) Bool() bool { return s.Value != "" } -func (s *String) Type() ObjectType { - return STRING_OBJ +func (s *String) Type() Type { + return StringType } func (s *String) Inspect() string { diff --git a/internal/typing/typing.go b/internal/typing/typing.go index 1383786..2cc07b2 100644 --- a/internal/typing/typing.go +++ b/internal/typing/typing.go @@ -44,7 +44,7 @@ func RangeOfArgs(n, m int) CheckFunc { } } -func WithTypes(types ...object.ObjectType) CheckFunc { +func WithTypes(types ...object.Type) CheckFunc { return func(name string, args []object.Object) error { for i, t := range types { if i < len(args) && args[i].Type() != t { diff --git a/internal/vm/vm.go b/internal/vm/vm.go index 5c9db07..3218347 100644 --- a/internal/vm/vm.go +++ b/internal/vm/vm.go @@ -495,9 +495,9 @@ func (vm *VM) Run() error { func (vm *VM) executeSetItem(left, index, value object.Object) error { switch { - case left.Type() == object.ARRAY_OBJ && index.Type() == object.INTEGER_OBJ: + case left.Type() == object.ArrayType && index.Type() == object.IntegerType: return vm.executeArraySetItem(left, index, value) - case left.Type() == object.HASH_OBJ: + case left.Type() == object.HashType: return vm.executeHashSetItem(left, index, value) default: return fmt.Errorf( @@ -509,15 +509,15 @@ func (vm *VM) executeSetItem(left, index, value object.Object) error { func (vm *VM) executeGetItem(left, index object.Object) error { switch { - case left.Type() == object.STRING_OBJ && index.Type() == object.INTEGER_OBJ: + case left.Type() == object.StringType && index.Type() == object.IntegerType: return vm.executeStringGetItem(left, index) - case left.Type() == object.STRING_OBJ && index.Type() == object.STRING_OBJ: + case left.Type() == object.StringType && index.Type() == object.StringType: return vm.executeStringIndex(left, index) - case left.Type() == object.ARRAY_OBJ && index.Type() == object.INTEGER_OBJ: + case left.Type() == object.ArrayType && index.Type() == object.IntegerType: return vm.executeArrayGetItem(left, index) - case left.Type() == object.HASH_OBJ: + case left.Type() == object.HashType: return vm.executeHashGetItem(left, index) - case left.Type() == object.MODULE_OBJ: + case left.Type() == object.ModuleType: return vm.executeHashGetItem(left.(*object.Module).Attrs, index) default: return fmt.Errorf( @@ -653,7 +653,7 @@ func (vm *VM) executeBinaryOperation(op code.Opcode) error { switch { // {"a": 1} + {"b": 2} - case op == code.OpAdd && left.Type() == object.HASH_OBJ && right.Type() == object.HASH_OBJ: + case op == code.OpAdd && left.Type() == object.HashType && right.Type() == object.HashType: leftVal := left.(*object.Hash).Pairs rightVal := right.(*object.Hash).Pairs pairs := make(map[object.HashKey]object.HashPair) @@ -666,7 +666,7 @@ func (vm *VM) executeBinaryOperation(op code.Opcode) error { return vm.push(&object.Hash{Pairs: pairs}) // [1] + [2] - case op == code.OpAdd && left.Type() == object.ARRAY_OBJ && right.Type() == object.ARRAY_OBJ: + case op == code.OpAdd && left.Type() == object.ArrayType && right.Type() == object.ArrayType: leftVal := left.(*object.Array).Elements rightVal := right.(*object.Array).Elements elements := make([]object.Object, len(leftVal)+len(rightVal)) @@ -674,7 +674,7 @@ func (vm *VM) executeBinaryOperation(op code.Opcode) error { return vm.push(&object.Array{Elements: elements}) // [1] * 3 - case op == code.OpMul && left.Type() == object.ARRAY_OBJ && right.Type() == object.INTEGER_OBJ: + case op == code.OpMul && left.Type() == object.ArrayType && right.Type() == object.IntegerType: leftVal := left.(*object.Array).Elements rightVal := int(right.(*object.Integer).Value) elements := leftVal @@ -683,7 +683,7 @@ func (vm *VM) executeBinaryOperation(op code.Opcode) error { } return vm.push(&object.Array{Elements: elements}) // 3 * [1] - case op == code.OpMul && left.Type() == object.INTEGER_OBJ && right.Type() == object.ARRAY_OBJ: + case op == code.OpMul && left.Type() == object.IntegerType && right.Type() == object.ArrayType: leftVal := int(left.(*object.Integer).Value) rightVal := right.(*object.Array).Elements elements := rightVal @@ -693,21 +693,21 @@ func (vm *VM) executeBinaryOperation(op code.Opcode) error { return vm.push(&object.Array{Elements: elements}) // " " * 4 - case op == code.OpMul && left.Type() == object.STRING_OBJ && right.Type() == object.INTEGER_OBJ: + case op == code.OpMul && left.Type() == object.StringType && right.Type() == object.IntegerType: leftVal := left.(*object.String).Value rightVal := right.(*object.Integer).Value return vm.push(&object.String{Value: strings.Repeat(leftVal, int(rightVal))}) // 4 * " " - case op == code.OpMul && left.Type() == object.INTEGER_OBJ && right.Type() == object.STRING_OBJ: + case op == code.OpMul && left.Type() == object.IntegerType && right.Type() == object.StringType: leftVal := left.(*object.Integer).Value rightVal := right.(*object.String).Value return vm.push(&object.String{Value: strings.Repeat(rightVal, int(leftVal))}) - case leftType == object.BOOLEAN_OBJ && rightType == object.BOOLEAN_OBJ: + case leftType == object.BooleanType && rightType == object.BooleanType: return vm.executeBinaryBooleanOperation(op, left, right) - case leftType == object.INTEGER_OBJ && rightType == object.INTEGER_OBJ: + case leftType == object.IntegerType && rightType == object.IntegerType: return vm.executeBinaryIntegerOperation(op, left, right) - case leftType == object.STRING_OBJ && rightType == object.STRING_OBJ: + case leftType == object.StringType && rightType == object.StringType: return vm.executeBinaryStringOperation(op, left, right) default: return fmt.Errorf("unsupported types for binary operation: %s %s", leftType, rightType) diff --git a/repl.go b/repl.go index 2d910c0..5378e45 100644 --- a/repl.go +++ b/repl.go @@ -78,7 +78,7 @@ func VmREPL(args []string, debug bool) error { continue } - if val := mvm.LastPoppedStackElem(); val.Type() != object.NULL_OBJ { + if val := mvm.LastPoppedStackElem(); val.Type() != object.NullType { fmt.Fprintln(t, val.Inspect()) } } @@ -176,7 +176,7 @@ func SimpleVmREPL(args []string, debug bool) { continue } - if val := mvm.LastPoppedStackElem(); val.Type() != object.NULL_OBJ { + if val := mvm.LastPoppedStackElem(); val.Type() != object.NullType { fmt.Println(val.Inspect()) } }