diff options
| author | Bobby <[email protected]> | 2024-03-01 20:26:52 +0000 |
|---|---|---|
| committer | Bobby <[email protected]> | 2024-03-01 20:26:52 +0000 |
| commit | 8e6447a42c363cf567ec3146bb588c7ed7ce7260 (patch) | |
| tree | 6931b526be8410cd1ae1f07ab5194b357dc2a229 /evaluator/evaluator.go | |
| parent | 4a930511b3aad05682aae69cbfbf7e7273e20264 (diff) | |
| download | mana-8e6447a42c363cf567ec3146bb588c7ed7ce7260.tar.xz mana-8e6447a42c363cf567ec3146bb588c7ed7ce7260.zip | |
`Eval` for `IfElseStatement`
Diffstat (limited to 'evaluator/evaluator.go')
| -rw-r--r-- | evaluator/evaluator.go | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/evaluator/evaluator.go b/evaluator/evaluator.go index 479e244..7aeaa24 100644 --- a/evaluator/evaluator.go +++ b/evaluator/evaluator.go @@ -21,6 +21,9 @@ func Eval(node ast.Node) object.Object { case *ast.ExpressionStatement: return Eval(node.Expression) + case *ast.BlockStatement: + return evalStatements(node.Statements) + // Expressions case *ast.IntegerLiteral: return &object.Integer{Value: node.Value} @@ -36,6 +39,9 @@ func Eval(node ast.Node) object.Object { left := Eval(node.Left) right := Eval(node.Right) return evalInfixExpression(node.Operator, left, right) + + case *ast.IfExpression: + return evalIfExpression(node) } return nil @@ -132,3 +138,27 @@ func evalIntegerInfixExpression(operator string, left, right object.Object) obje return NULL } } + +func evalIfExpression(ie *ast.IfExpression) object.Object { + condition := Eval(ie.Condition) + if isTruthy(condition) { + return Eval(ie.Consequence) + } else if ie.Alternative != nil { + return Eval(ie.Alternative) + } else { + return NULL + } +} + +func isTruthy(obj object.Object) bool { + switch obj { + case NULL: + return false + case TRUE: + return true + case FALSE: + return false + default: + return true + } +} |
