Simplified Equality
This commit is contained in:
@@ -1,6 +1,9 @@
|
|||||||
package builtins
|
package builtins
|
||||||
|
|
||||||
import "monkey/object"
|
import (
|
||||||
|
"monkey/object"
|
||||||
|
"sort"
|
||||||
|
)
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
@@ -21,15 +24,14 @@ func Find(args ...object.Object) object.Object {
|
|||||||
return newError("expected arg #2 to be `str` got got=%T", args[1])
|
return newError("expected arg #2 to be `str` got got=%T", args[1])
|
||||||
}
|
}
|
||||||
} else if haystack, ok := args[0].(*object.Array); ok {
|
} else if haystack, ok := args[0].(*object.Array); ok {
|
||||||
needle := args[1]
|
needle := args[1].(object.Comparable)
|
||||||
index := -1
|
i := sort.Search(len(haystack.Elements), func(i int) bool {
|
||||||
for i, el := range haystack.Elements {
|
return needle.Compare(haystack.Elements[i]) == 0
|
||||||
if cmp, ok := el.(object.Comparable); ok && cmp.Equal(needle) {
|
})
|
||||||
index = i
|
if i < len(haystack.Elements) && needle.Compare(haystack.Elements[i]) == 0 {
|
||||||
break
|
return &object.Integer{Value: int64(i)}
|
||||||
}
|
}
|
||||||
}
|
return &object.Integer{Value: -1}
|
||||||
return &object.Integer{Value: int64(index)}
|
|
||||||
} else {
|
} else {
|
||||||
return newError("expected arg #1 to be `str` or `array` got got=%T", args[0])
|
return newError("expected arg #1 to be `str` or `array` got got=%T", args[0])
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -337,6 +337,7 @@ func evalInfixExpression(operator string, left, right object.Object) object.Obje
|
|||||||
elements = append(elements, leftVal...)
|
elements = append(elements, leftVal...)
|
||||||
}
|
}
|
||||||
return &object.Array{Elements: elements}
|
return &object.Array{Elements: elements}
|
||||||
|
|
||||||
// 3 * [1]
|
// 3 * [1]
|
||||||
case operator == "*" && left.Type() == object.INTEGER_OBJ && right.Type() == object.ARRAY_OBJ:
|
case operator == "*" && left.Type() == object.INTEGER_OBJ && right.Type() == object.ARRAY_OBJ:
|
||||||
leftVal := int(left.(*object.Integer).Value)
|
leftVal := int(left.(*object.Integer).Value)
|
||||||
@@ -352,24 +353,33 @@ func evalInfixExpression(operator string, left, right object.Object) object.Obje
|
|||||||
leftVal := left.(*object.String).Value
|
leftVal := left.(*object.String).Value
|
||||||
rightVal := right.(*object.Integer).Value
|
rightVal := right.(*object.Integer).Value
|
||||||
return &object.String{Value: strings.Repeat(leftVal, int(rightVal))}
|
return &object.String{Value: strings.Repeat(leftVal, int(rightVal))}
|
||||||
|
|
||||||
// 4 * " "
|
// 4 * " "
|
||||||
case operator == "*" && left.Type() == object.INTEGER_OBJ && right.Type() == object.STRING_OBJ:
|
case operator == "*" && left.Type() == object.INTEGER_OBJ && right.Type() == object.STRING_OBJ:
|
||||||
leftVal := left.(*object.Integer).Value
|
leftVal := left.(*object.Integer).Value
|
||||||
rightVal := right.(*object.String).Value
|
rightVal := right.(*object.String).Value
|
||||||
return &object.String{Value: strings.Repeat(rightVal, int(leftVal))}
|
return &object.String{Value: strings.Repeat(rightVal, int(leftVal))}
|
||||||
|
|
||||||
|
case operator == "==":
|
||||||
|
return nativeBoolToBooleanObject(left.(object.Comparable).Compare(right) == 0)
|
||||||
|
case operator == "!=":
|
||||||
|
return nativeBoolToBooleanObject(left.(object.Comparable).Compare(right) != 0)
|
||||||
|
case operator == "<=":
|
||||||
|
return nativeBoolToBooleanObject(left.(object.Comparable).Compare(right) < 1)
|
||||||
|
case operator == ">=":
|
||||||
|
return nativeBoolToBooleanObject(left.(object.Comparable).Compare(right) > -1)
|
||||||
|
case operator == "<":
|
||||||
|
return nativeBoolToBooleanObject(left.(object.Comparable).Compare(right) == -1)
|
||||||
|
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.BOOLEAN_OBJ && right.Type() == object.BOOLEAN_OBJ:
|
||||||
return evalBooleanInfixExpression(operator, left, right)
|
return evalBooleanInfixExpression(operator, left, right)
|
||||||
case left.Type() == object.INTEGER_OBJ && right.Type() == object.INTEGER_OBJ:
|
case left.Type() == object.INTEGER_OBJ && right.Type() == object.INTEGER_OBJ:
|
||||||
return evalIntegerInfixExpression(operator, left, right)
|
return evalIntegerInfixExpression(operator, left, right)
|
||||||
case left.Type() == object.STRING_OBJ && right.Type() == object.STRING_OBJ:
|
case left.Type() == object.STRING_OBJ && right.Type() == object.STRING_OBJ:
|
||||||
return evalStringInfixExpression(operator, left, right)
|
return evalStringInfixExpression(operator, left, right)
|
||||||
case operator == "==":
|
|
||||||
return nativeBoolToBooleanObject(left == right)
|
|
||||||
case operator == "!=":
|
|
||||||
return nativeBoolToBooleanObject(left != right)
|
|
||||||
case left.Type() != right.Type():
|
|
||||||
return newError("type mismatch: %s %s %s", left.Type(), operator, right.Type())
|
|
||||||
default:
|
default:
|
||||||
return newError("unknown operator: %s %s %s", left.Type(), operator, right.Type())
|
return newError("unknown operator: %s %s %s", left.Type(), operator, right.Type())
|
||||||
}
|
}
|
||||||
@@ -380,10 +390,6 @@ func evalBooleanInfixExpression(operator string, left, right object.Object) obje
|
|||||||
rightVal := right.(*object.Boolean).Value
|
rightVal := right.(*object.Boolean).Value
|
||||||
|
|
||||||
switch operator {
|
switch operator {
|
||||||
case "==":
|
|
||||||
return nativeBoolToBooleanObject(left == right)
|
|
||||||
case "!=":
|
|
||||||
return nativeBoolToBooleanObject(leftVal != rightVal)
|
|
||||||
case "&&":
|
case "&&":
|
||||||
return &object.Boolean{Value: leftVal && rightVal}
|
return &object.Boolean{Value: leftVal && rightVal}
|
||||||
case "||":
|
case "||":
|
||||||
@@ -400,14 +406,6 @@ func evalStringInfixExpression(operator string, left object.Object, right object
|
|||||||
switch operator {
|
switch operator {
|
||||||
case "+":
|
case "+":
|
||||||
return &object.String{Value: leftVal + rightVal}
|
return &object.String{Value: leftVal + rightVal}
|
||||||
case "<":
|
|
||||||
return nativeBoolToBooleanObject(leftVal < rightVal)
|
|
||||||
case ">":
|
|
||||||
return nativeBoolToBooleanObject(leftVal > rightVal)
|
|
||||||
case "==":
|
|
||||||
return nativeBoolToBooleanObject(leftVal == rightVal)
|
|
||||||
case "!=":
|
|
||||||
return nativeBoolToBooleanObject(leftVal != rightVal)
|
|
||||||
default:
|
default:
|
||||||
return newError("unknown operator: %s %s %s", left.Type(), operator, right.Type())
|
return newError("unknown operator: %s %s %s", left.Type(), operator, right.Type())
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -118,6 +118,7 @@ func TestIfElseExpression(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
|
t.Log(tt.input)
|
||||||
evaluated := testEval(tt.input)
|
evaluated := testEval(tt.input)
|
||||||
integer, ok := tt.expected.(int)
|
integer, ok := tt.expected.(int)
|
||||||
if ok {
|
if ok {
|
||||||
@@ -184,11 +185,11 @@ func TestErrorHandling(t *testing.T) {
|
|||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"5 + true;",
|
"5 + true;",
|
||||||
"type mismatch: int + bool",
|
"unknown operator: int + bool",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"5 + true; 5;",
|
"5 + true; 5;",
|
||||||
"type mismatch: int + bool",
|
"unknown operator: int + bool",
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"-true",
|
"-true",
|
||||||
|
|||||||
@@ -13,6 +13,7 @@ type Array struct {
|
|||||||
func (ao *Array) Type() ObjectType {
|
func (ao *Array) Type() ObjectType {
|
||||||
return ARRAY_OBJ
|
return ARRAY_OBJ
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ao *Array) Inspect() string {
|
func (ao *Array) Inspect() string {
|
||||||
var out bytes.Buffer
|
var out bytes.Buffer
|
||||||
|
|
||||||
@@ -30,25 +31,34 @@ func (ao *Array) Inspect() string {
|
|||||||
func (ao *Array) String() string {
|
func (ao *Array) String() string {
|
||||||
return ao.Inspect()
|
return ao.Inspect()
|
||||||
}
|
}
|
||||||
func (ao *Array) Equal(other Object) bool {
|
|
||||||
|
func (a *Array) Less(i, j int) bool {
|
||||||
|
if cmp, ok := a.Elements[i].(Comparable); ok {
|
||||||
|
return cmp.Compare(a.Elements[j]) == -1
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ao *Array) Compare(other Object) int {
|
||||||
if obj, ok := other.(*Array); ok {
|
if obj, ok := other.(*Array); ok {
|
||||||
if len(ao.Elements) != len(obj.Elements) {
|
if len(ao.Elements) != len(obj.Elements) {
|
||||||
return false
|
return -1
|
||||||
}
|
}
|
||||||
for i, el := range ao.Elements {
|
for i, el := range ao.Elements {
|
||||||
cmp, ok := el.(Comparable)
|
cmp, ok := el.(Comparable)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return -1
|
||||||
}
|
}
|
||||||
if !cmp.Equal(obj.Elements[i]) {
|
if cmp.Compare(obj.Elements[i]) != 0 {
|
||||||
return false
|
return cmp.Compare(obj.Elements[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return 0
|
||||||
}
|
}
|
||||||
return false
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ao *Array) Copy() *Array {
|
func (ao *Array) Copy() *Array {
|
||||||
elements := make([]Object, len(ao.Elements))
|
elements := make([]Object, len(ao.Elements))
|
||||||
for i, e := range ao.Elements {
|
for i, e := range ao.Elements {
|
||||||
@@ -70,10 +80,3 @@ func (ao *Array) Len() int {
|
|||||||
func (ao *Array) Swap(i, j int) {
|
func (ao *Array) Swap(i, j int) {
|
||||||
ao.Elements[i], ao.Elements[j] = ao.Elements[j], ao.Elements[i]
|
ao.Elements[i], ao.Elements[j] = ao.Elements[j], ao.Elements[i]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ao *Array) Less(i, j int) bool {
|
|
||||||
if cmp, ok := ao.Elements[i].(Comparable); ok {
|
|
||||||
return cmp.Less(ao.Elements[j])
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -11,31 +11,29 @@ type Boolean struct {
|
|||||||
func (b *Boolean) Type() ObjectType {
|
func (b *Boolean) Type() ObjectType {
|
||||||
return BOOLEAN_OBJ
|
return BOOLEAN_OBJ
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Boolean) Inspect() string {
|
func (b *Boolean) Inspect() string {
|
||||||
return fmt.Sprintf("%t", b.Value)
|
return fmt.Sprintf("%t", b.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Boolean) Clone() Object {
|
func (b *Boolean) Clone() Object {
|
||||||
return &Boolean{Value: b.Value}
|
return &Boolean{Value: b.Value}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Boolean) String() string {
|
func (b *Boolean) String() string {
|
||||||
return b.Inspect()
|
return b.Inspect()
|
||||||
}
|
}
|
||||||
func (b *Boolean) Equal(other Object) bool {
|
|
||||||
if obj, ok := other.(*Boolean); ok {
|
func (b *Boolean) Int() int {
|
||||||
return b.Value == obj.Value
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
func (b *Boolean) Int() int64 {
|
|
||||||
if b.Value {
|
if b.Value {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Boolean) Less(other Object) bool {
|
func (b *Boolean) Compare(other Object) int {
|
||||||
if obj, ok := other.(*Boolean); ok {
|
if obj, ok := other.(*Boolean); ok {
|
||||||
return b.Int() < obj.Int()
|
return b.Int() - obj.Int()
|
||||||
}
|
}
|
||||||
return false
|
return 1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ type Hash struct {
|
|||||||
func (h *Hash) Type() ObjectType {
|
func (h *Hash) Type() ObjectType {
|
||||||
return HASH_OBJ
|
return HASH_OBJ
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Hash) Inspect() string {
|
func (h *Hash) Inspect() string {
|
||||||
var out bytes.Buffer
|
var out bytes.Buffer
|
||||||
|
|
||||||
@@ -64,28 +65,29 @@ func (h *Hash) Inspect() string {
|
|||||||
func (h *Hash) String() string {
|
func (h *Hash) String() string {
|
||||||
return h.Inspect()
|
return h.Inspect()
|
||||||
}
|
}
|
||||||
func (h *Hash) Equal(other Object) bool {
|
|
||||||
|
func (h *Hash) Compare(other Object) int {
|
||||||
if obj, ok := other.(*Hash); ok {
|
if obj, ok := other.(*Hash); ok {
|
||||||
if len(h.Pairs) != len(obj.Pairs) {
|
if len(h.Pairs) != len(obj.Pairs) {
|
||||||
return false
|
return -1
|
||||||
}
|
}
|
||||||
for _, pair := range h.Pairs {
|
for _, pair := range h.Pairs {
|
||||||
left := pair.Value
|
left := pair.Value
|
||||||
hashed := left.(Hashable)
|
hashed := left.(Hashable)
|
||||||
right, ok := obj.Pairs[hashed.HashKey()]
|
right, ok := obj.Pairs[hashed.HashKey()]
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return -1
|
||||||
}
|
}
|
||||||
cmp, ok := left.(Comparable)
|
cmp, ok := left.(Comparable)
|
||||||
if !ok {
|
if !ok {
|
||||||
return false
|
return -1
|
||||||
}
|
}
|
||||||
if !cmp.Equal(right.Value) {
|
if cmp.Compare(right.Value) != 0 {
|
||||||
return false
|
return cmp.Compare(right.Value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return 0
|
||||||
}
|
}
|
||||||
return false
|
return -1
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,24 +9,29 @@ type Integer struct {
|
|||||||
func (i *Integer) Type() ObjectType {
|
func (i *Integer) Type() ObjectType {
|
||||||
return INTEGER_OBJ
|
return INTEGER_OBJ
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Integer) Inspect() string {
|
func (i *Integer) Inspect() string {
|
||||||
return fmt.Sprintf("%d", i.Value)
|
return fmt.Sprintf("%d", i.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Integer) Clone() Object {
|
func (i *Integer) Clone() Object {
|
||||||
return &Integer{Value: i.Value}
|
return &Integer{Value: i.Value}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *Integer) String() string {
|
func (i *Integer) String() string {
|
||||||
return i.Inspect()
|
return i.Inspect()
|
||||||
}
|
}
|
||||||
func (i *Integer) Equal(other Object) bool {
|
|
||||||
|
func (i *Integer) Compare(other Object) int {
|
||||||
if obj, ok := other.(*Integer); ok {
|
if obj, ok := other.(*Integer); ok {
|
||||||
return i.Value == obj.Value
|
switch {
|
||||||
|
case i.Value < obj.Value:
|
||||||
|
return -1
|
||||||
|
case i.Value > obj.Value:
|
||||||
|
return 1
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
func (i *Integer) Less(other Object) bool {
|
return -1
|
||||||
if obj, ok := other.(*Integer); ok {
|
|
||||||
return i.Value < obj.Value
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,16 +5,18 @@ type Null struct{}
|
|||||||
func (n *Null) Type() ObjectType {
|
func (n *Null) Type() ObjectType {
|
||||||
return NULL_OBJ
|
return NULL_OBJ
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Null) Inspect() string {
|
func (n *Null) Inspect() string {
|
||||||
return "null"
|
return "null"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (n *Null) String() string {
|
func (n *Null) String() string {
|
||||||
return n.Inspect()
|
return n.Inspect()
|
||||||
}
|
}
|
||||||
func (n *Null) Equal(other Object) bool {
|
|
||||||
_, ok := other.(*Null)
|
func (n *Null) Compare(other Object) int {
|
||||||
return ok
|
if _, ok := other.(*Null); ok {
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
func (n *Null) Less(other Object) bool {
|
return 1
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,8 +22,7 @@ const (
|
|||||||
// values. It is the responsibility of the caller (left) to check for types.
|
// values. It is the responsibility of the caller (left) to check for types.
|
||||||
// Returns `true` iif the types and values are identical, `false` otherwise.
|
// Returns `true` iif the types and values are identical, `false` otherwise.
|
||||||
type Comparable interface {
|
type Comparable interface {
|
||||||
Equal(other Object) bool
|
Compare(other Object) int
|
||||||
Less(other Object) bool
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Immutable is the interface for all immutable objects which must implement
|
// Immutable is the interface for all immutable objects which must implement
|
||||||
|
|||||||
@@ -9,24 +9,29 @@ type String struct {
|
|||||||
func (s *String) Type() ObjectType {
|
func (s *String) Type() ObjectType {
|
||||||
return STRING_OBJ
|
return STRING_OBJ
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *String) Inspect() string {
|
func (s *String) Inspect() string {
|
||||||
return fmt.Sprintf("%#v", s.Value)
|
return fmt.Sprintf("%#v", s.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *String) Clone() Object {
|
func (s *String) Clone() Object {
|
||||||
return &String{Value: s.Value}
|
return &String{Value: s.Value}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *String) String() string {
|
func (s *String) String() string {
|
||||||
return s.Value
|
return s.Value
|
||||||
}
|
}
|
||||||
func (s *String) Equal(other Object) bool {
|
|
||||||
|
func (s *String) Compare(other Object) int {
|
||||||
if obj, ok := other.(*String); ok {
|
if obj, ok := other.(*String); ok {
|
||||||
return s.Value == obj.Value
|
switch {
|
||||||
|
case s.Value < obj.Value:
|
||||||
|
return -1
|
||||||
|
case s.Value > obj.Value:
|
||||||
|
return 1
|
||||||
|
default:
|
||||||
|
return 0
|
||||||
}
|
}
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
func (s *String) Less(other Object) bool {
|
return 1
|
||||||
if obj, ok := other.(*String); ok {
|
|
||||||
return s.Value < obj.Value
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|||||||
53
vm/vm.go
53
vm/vm.go
@@ -659,39 +659,18 @@ func (vm *VM) executeComparison(op code.Opcode) error {
|
|||||||
right := vm.pop()
|
right := vm.pop()
|
||||||
left := vm.pop()
|
left := vm.pop()
|
||||||
|
|
||||||
if left.Type() == object.INTEGER_OBJ && right.Type() == object.INTEGER_OBJ {
|
|
||||||
return vm.executeIntegerComparison(op, left, right)
|
|
||||||
}
|
|
||||||
|
|
||||||
if left.Type() == object.STRING_OBJ && right.Type() == object.STRING_OBJ {
|
|
||||||
return vm.executeStringComparison(op, left, right)
|
|
||||||
}
|
|
||||||
|
|
||||||
switch op {
|
switch op {
|
||||||
case code.OpEqual:
|
case code.OpEqual:
|
||||||
return vm.push(nativeBoolToBooleanObject(right == left))
|
return vm.push(nativeBoolToBooleanObject(left.(object.Comparable).Compare(right) == 0))
|
||||||
case code.OpNotEqual:
|
case code.OpNotEqual:
|
||||||
return vm.push(nativeBoolToBooleanObject(right != left))
|
return vm.push(nativeBoolToBooleanObject(left.(object.Comparable).Compare(right) != 0))
|
||||||
default:
|
|
||||||
return fmt.Errorf("unknown operator: %d (%s %s)", op, left.Type(), right.Type())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (vm *VM) executeIntegerComparison(op code.Opcode, left, right object.Object) error {
|
|
||||||
leftValue := left.(*object.Integer).Value
|
|
||||||
rightValue := right.(*object.Integer).Value
|
|
||||||
|
|
||||||
switch op {
|
|
||||||
case code.OpEqual:
|
|
||||||
return vm.push(nativeBoolToBooleanObject(rightValue == leftValue))
|
|
||||||
case code.OpNotEqual:
|
|
||||||
return vm.push(nativeBoolToBooleanObject(rightValue != leftValue))
|
|
||||||
case code.OpGreaterThan:
|
|
||||||
return vm.push(nativeBoolToBooleanObject(leftValue > rightValue))
|
|
||||||
case code.OpGreaterThanEqual:
|
case code.OpGreaterThanEqual:
|
||||||
return vm.push(nativeBoolToBooleanObject(leftValue >= rightValue))
|
return vm.push(nativeBoolToBooleanObject(left.(object.Comparable).Compare(right) > -1))
|
||||||
|
case code.OpGreaterThan:
|
||||||
|
return vm.push(nativeBoolToBooleanObject(left.(object.Comparable).Compare(right) == 1))
|
||||||
default:
|
default:
|
||||||
return fmt.Errorf("unknown operator: %d", op)
|
return fmt.Errorf("unknown operator: %d (%s %s)",
|
||||||
|
op, left.Type(), right.Type())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -826,24 +805,6 @@ func (vm *VM) executeStringIndex(str, index object.Object) error {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (vm *VM) executeStringComparison(op code.Opcode, left, right object.Object) error {
|
|
||||||
leftValue := left.(*object.String).Value
|
|
||||||
rightValue := right.(*object.String).Value
|
|
||||||
|
|
||||||
switch op {
|
|
||||||
case code.OpEqual:
|
|
||||||
return vm.push(nativeBoolToBooleanObject(rightValue == leftValue))
|
|
||||||
case code.OpNotEqual:
|
|
||||||
return vm.push(nativeBoolToBooleanObject(rightValue != leftValue))
|
|
||||||
case code.OpGreaterThan:
|
|
||||||
return vm.push(nativeBoolToBooleanObject(leftValue > rightValue))
|
|
||||||
case code.OpGreaterThanEqual:
|
|
||||||
return vm.push(nativeBoolToBooleanObject(leftValue >= rightValue))
|
|
||||||
default:
|
|
||||||
return fmt.Errorf("unknown operator: %d", op)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func nativeBoolToBooleanObject(input bool) *object.Boolean {
|
func nativeBoolToBooleanObject(input bool) *object.Boolean {
|
||||||
if input {
|
if input {
|
||||||
return True
|
return True
|
||||||
|
|||||||
Reference in New Issue
Block a user