From 226d721b728d6d2d34637685847b4ad992abce9e Mon Sep 17 00:00:00 2001 From: Gekko Wrld Date: Thu, 21 Mar 2024 15:20:21 +0300 Subject: [PATCH] fix: Handle `nil` dereference in expressions The code will fail if the second arg is `nil` then the code will fail with a panic. Fixes: https://github.com/NuruProgramming/Nuru/issues/79 Before only the left arg was checked for `nil` but not the right arg. Signed-off-by: Gekko Wrld --- evaluator/evaluator.go | 2 +- evaluator/infix.go | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/evaluator/evaluator.go b/evaluator/evaluator.go index 758f4dd..313ed4a 100644 --- a/evaluator/evaluator.go +++ b/evaluator/evaluator.go @@ -45,7 +45,7 @@ func Eval(node ast.Node, env *object.Environment) object.Object { return left } right := Eval(node.Right, env) - if isError(right) { + if isError(right) && right != nil { return right } return evalInfixExpression(node.Operator, left, right, node.Token.Line) diff --git a/evaluator/infix.go b/evaluator/infix.go index f25ef9d..d11d867 100644 --- a/evaluator/infix.go +++ b/evaluator/infix.go @@ -8,6 +8,9 @@ import ( ) func evalInfixExpression(operator string, left, right object.Object, line int) object.Object { + if right == nil { + return newError("Mstari %d: Umekosea hapa", line) + } if left == nil { return newError("Mstari %d: Umekosea hapa", line) }