Skip to content

Commit

Permalink
simple maths :D
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonak-Adipta-Kalita committed Apr 8, 2023
1 parent 6e497fe commit a984059
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func Eval(node ast.Node) object.Object {
case *ast.PrefixExpression:
right := Eval(node.Right)
return evalPrefixExpression(node.Operator, right)
case *ast.InfixExpression:
left := Eval(node.Left)
right := Eval(node.Right)
return evalInfixExpression(node.Operator, left, right)

case *ast.IntegerLiteral:
return &object.Integer{Value: node.Value}
Expand Down Expand Up @@ -55,6 +59,18 @@ func evalPrefixExpression(operator string, right object.Object) object.Object {
}
}

func evalInfixExpression(
operator string,
left, right object.Object,
) object.Object {
switch {
case left.Type() == object.INTEGER_OBJ && right.Type() == object.INTEGER_OBJ:
return evalIntegerInfixExpression(operator, left, right)
default:
return NULL
}
}

func evalBangOperatorExpression(right object.Object) object.Object {
switch right {
case TRUE:
Expand All @@ -75,3 +91,23 @@ func evalMinusPrefixOperatorExpression(right object.Object) object.Object {
value := right.(*object.Integer).Value
return &object.Integer{Value: -value}
}

func evalIntegerInfixExpression(
operator string,
left, right object.Object,
) object.Object {
leftVal := left.(*object.Integer).Value
rightVal := right.(*object.Integer).Value
switch operator {
case "+":
return &object.Integer{Value: leftVal + rightVal}
case "-":
return &object.Integer{Value: leftVal - rightVal}
case "*":
return &object.Integer{Value: leftVal * rightVal}
case "/":
return &object.Integer{Value: leftVal / rightVal}
default:
return NULL
}
}

0 comments on commit a984059

Please sign in to comment.