Strings
Some checks failed
Build / build (push) Failing after 1m35s
Test / build (push) Failing after 1m31s

This commit is contained in:
Chuck Smith
2024-02-21 16:29:53 -05:00
parent 8caeaca559
commit e4bca02235
5 changed files with 189 additions and 5 deletions

View File

@@ -177,11 +177,14 @@ func (vm *VM) executeBinaryOperation(op code.Opcode) error {
leftType := left.Type()
rightRight := right.Type()
if leftType == object.INTEGER_OBJ && rightRight == object.INTEGER_OBJ {
switch {
case leftType == object.INTEGER_OBJ && rightRight == object.INTEGER_OBJ:
return vm.executeBinaryIntegerOperation(op, left, right)
case leftType == object.STRING_OBJ && rightRight == object.STRING_OBJ:
return vm.executeBinaryStringOperation(op, left, right)
default:
return fmt.Errorf("unsupported types for binary operation: %s %s", leftType, rightRight)
}
return fmt.Errorf("unsupported types for binary operation: %s %s", leftType, rightRight)
}
func (vm *VM) executeBinaryIntegerOperation(op code.Opcode, left, right object.Object) error {
@@ -206,6 +209,17 @@ func (vm *VM) executeBinaryIntegerOperation(op code.Opcode, left, right object.O
return vm.push(&object.Integer{Value: result})
}
func (vm *VM) executeBinaryStringOperation(op code.Opcode, left, right object.Object) error {
if op != code.OpAdd {
return fmt.Errorf("unknown string operator: %d", op)
}
leftValue := left.(*object.String).Value
rightValue := right.(*object.String).Value
return vm.push(&object.String{Value: leftValue + rightValue})
}
func (vm *VM) executeComparison(op code.Opcode) error {
right := vm.pop()
left := vm.pop()

View File

@@ -55,6 +55,12 @@ func testExpectedObject(t *testing.T, expected interface{}, actual object.Object
t.Errorf("testBooleanObject failed: %s", err)
}
case string:
err := testStringObject(expected, actual)
if err != nil {
t.Errorf("testStringObject failed: %s", err)
}
case *object.Null:
if actual != Null {
t.Errorf("object is not Null: %T (%+v)", actual, actual)
@@ -68,7 +74,7 @@ func parse(input string) *ast.Program {
return p.ParseProgram()
}
func testIntegerObject(expected int64, actual object.Object) interface{} {
func testIntegerObject(expected int64, actual object.Object) error {
result, ok := actual.(*object.Integer)
if !ok {
return fmt.Errorf("object is not Integer. got=%T (%+v", actual, actual)
@@ -81,7 +87,7 @@ func testIntegerObject(expected int64, actual object.Object) interface{} {
return nil
}
func testBooleanObject(expected bool, actual object.Object) interface{} {
func testBooleanObject(expected bool, actual object.Object) error {
result, ok := actual.(*object.Boolean)
if !ok {
return fmt.Errorf("object is not Boolean. got=%T (%+v", actual, actual)
@@ -94,6 +100,19 @@ func testBooleanObject(expected bool, actual object.Object) interface{} {
return nil
}
func testStringObject(expected string, actual object.Object) error {
result, ok := actual.(*object.String)
if !ok {
return fmt.Errorf("object is not String. got=%T (%+v", actual, actual)
}
if result.Value != expected {
return fmt.Errorf("object has wrong value. got=%q, want=%q", result.Value, expected)
}
return nil
}
func TestIntegerArithmetic(t *testing.T) {
tests := []vmTestCase{
{"1", 1},
@@ -175,3 +194,13 @@ func TestGlobalLetStatements(t *testing.T) {
runVmTests(t, tests)
}
func TestStringExpressions(t *testing.T) {
tests := []vmTestCase{
{`"monkey"`, "monkey"},
{`"mon" + "key"`, "monkey"},
{`"mon" + "key" + "banana"`, "monkeybanana"},
}
runVmTests(t, tests)
}