Skip to content

Commit

Permalink
string concatenation
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonak-Adipta-Kalita committed Apr 8, 2023
1 parent 728415b commit 6b84f18
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 6b84f18

Please sign in to comment.