From 6b84f18fe9900ba98883a9004d6baa4fe3f51759 Mon Sep 17 00:00:00 2001 From: Jonak-Adipta-Kalita Date: Sat, 8 Apr 2023 22:04:39 +0530 Subject: [PATCH] string concatenation --- evaluator/evaluator.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/evaluator/evaluator.go b/evaluator/evaluator.go index 5f59856..41abf0b 100644 --- a/evaluator/evaluator.go +++ b/evaluator/evaluator.go @@ -163,6 +163,8 @@ func evalInfixExpression( case left.Type() != right.Type(): return newError("type mismatch: %s %s %s", left.Type(), operator, right.Type()) + case left.Type() == object.STRING_OBJ && right.Type() == object.STRING_OBJ: + return evalStringInfixExpression(operator, left, right) default: return newError("unknown operator: %s %s %s", left.Type(), operator, right.Type()) @@ -224,6 +226,19 @@ func evalIntegerInfixExpression( } } +func evalStringInfixExpression( + operator string, + left, right object.Object, +) object.Object { + if operator != "+" { + return newError("unknown operator: %s %s %s", + left.Type(), operator, right.Type()) + } + leftVal := left.(*object.String).Value + rightVal := right.(*object.String).Value + return &object.String{Value: leftVal + rightVal} +} + func evalIfExpression(ie *ast.IfExpression, env *object.Environment) object.Object { condition := Eval(ie.Condition, env) if isError(condition) {