Skip to content

Commit 226d721

Browse files
committed
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: #79 Before only the left arg was checked for `nil` but not the right arg. Signed-off-by: Gekko Wrld <gekkowrld@gmail.com>
1 parent 3b2207a commit 226d721

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

evaluator/evaluator.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func Eval(node ast.Node, env *object.Environment) object.Object {
4545
return left
4646
}
4747
right := Eval(node.Right, env)
48-
if isError(right) {
48+
if isError(right) && right != nil {
4949
return right
5050
}
5151
return evalInfixExpression(node.Operator, left, right, node.Token.Line)

evaluator/infix.go

+3
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88
)
99

1010
func evalInfixExpression(operator string, left, right object.Object, line int) object.Object {
11+
if right == nil {
12+
return newError("Mstari %d: Umekosea hapa", line)
13+
}
1114
if left == nil {
1215
return newError("Mstari %d: Umekosea hapa", line)
1316
}

0 commit comments

Comments
 (0)