Skip to content

Commit

Permalink
feat(eval): error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
vit0rr committed Oct 2, 2024
1 parent 3dba307 commit 83aca52
Showing 1 changed file with 51 additions and 9 deletions.
60 changes: 51 additions & 9 deletions evaluator/evaluator.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package evaluator

import (
"fmt"

"github.com/vit0rr/mumu/ast"
"github.com/vit0rr/mumu/object"
)
Expand All @@ -14,26 +16,42 @@ var (
func Eval(node ast.Node) object.Object {
switch node := node.(type) {
case *ast.Program:
return evalStatements(node.Statements)
return evalProgram(node)
case *ast.ExpressionStatement:
return Eval(node.Expression)
case *ast.Boolean:
return nativeBoolToBooleanObject(node.Value)
case *ast.PrefixExpression:
right := Eval(node.Right)
if isError(right) {
return right
}

return evalPrefixExpression(node.Operator, right)
case *ast.BlockStatement:
return evalBlockStatement(node)
case *ast.IfExpression:
return evalIfExpression(node)
case *ast.ReturnStatement:
val := Eval(node.ReturnValue)
if isError(val) {
return val
}

return &object.ReturnValue{Value: val}
case *ast.IntegerLiteral:
return &object.Integer{Value: node.Value}
case *ast.InfixExpression:
left := Eval(node.Left)
if isError(left) {
return left
}

right := Eval(node.Right)
if isError(right) {
return right
}

return evalInfixExpression(node.Operator, left, right)
}

Expand All @@ -46,8 +64,12 @@ func evalBlockStatement(block *ast.BlockStatement) object.Object {
for _, statement := range block.Statements {
result = Eval(statement)

if result != nil && result.Type() == object.RETURN_VALUE_OBJ {
return result
if result != nil {
rt := result.Type()

if rt == object.RETURN_VALUE_OBJ || rt == object.ERROR_OBJ {
return result
}
}
}

Expand All @@ -60,8 +82,11 @@ func evalProgram(program *ast.Program) object.Object {
for _, statement := range program.Statements {
result = Eval(statement)

if returnValue, ok := result.(*object.ReturnValue); ok {
return returnValue.Value
switch result := result.(type) {
case *object.ReturnValue:
return result.Value
case *object.Error:
return result
}
}

Expand All @@ -70,6 +95,9 @@ func evalProgram(program *ast.Program) object.Object {

func evalIfExpression(ie *ast.IfExpression) object.Object {
condition := Eval(ie.Condition)
if isError(condition) {
return condition
}

if isTruthy(condition) {
return Eval(ie.Consequence)
Expand Down Expand Up @@ -101,8 +129,10 @@ func evalInfixExpression(operator string, left, right object.Object) object.Obje
return nativeBoolToBooleanObject(left == right)
case operator == "!=":
return nativeBoolToBooleanObject(left != right)
case left.Type() != right.Type():
return newError("type mismatch: %s %s %s", left.Type(), operator, right.Type())
default:
return NULL
return newError("unknown operator: %s %s %s", left.Type(), operator, right.Type())
}
}

Expand All @@ -128,7 +158,7 @@ func evalIntegerInfixExpression(operator string, left, right object.Object) obje
case "!=":
return nativeBoolToBooleanObject(leftVal != rightVal)
default:
return NULL
return newError("unknown operator: %s %s %s", left.Type(), operator, right.Type())
}
}

Expand All @@ -139,13 +169,13 @@ func evalPrefixExpression(operator string, right object.Object) object.Object {
case "-":
return evalMinusPrefixOperatorExpression(right)
default:
return NULL
return newError("unknown operator: %s%s", operator, right.Type())
}
}

func evalMinusPrefixOperatorExpression(right object.Object) object.Object {
if right.Type() != object.INTEGER_OBJ {
return NULL
return newError("unknown operator: -%s", right.Type())
}

value := right.(*object.Integer).Value
Expand Down Expand Up @@ -186,3 +216,15 @@ func evalStatements(stmts []ast.Statement) object.Object {

return result
}

func newError(format string, a ...interface{}) *object.Error {
return &object.Error{Message: fmt.Sprintf(format, a...)}
}

func isError(obj object.Object) bool {
if obj != nil {
return obj.Type() == object.ERROR_OBJ
}

return false
}

0 comments on commit 83aca52

Please sign in to comment.