diff options
Diffstat (limited to 'evaluator/evaluator.go')
| -rw-r--r-- | evaluator/evaluator.go | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/evaluator/evaluator.go b/evaluator/evaluator.go index 8d1b26d..0cc003f 100644 --- a/evaluator/evaluator.go +++ b/evaluator/evaluator.go @@ -5,6 +5,12 @@ import ( "mana/object" ) +var ( + NULL = &object.Null{} + TRUE = &object.Boolean{Value: true} + FALSE = &object.Boolean{Value: false} +) + // Eval evaluates the given ast.Node and returns an object.Object. func Eval(node ast.Node) object.Object { switch node := node.(type) { @@ -18,6 +24,13 @@ func Eval(node ast.Node) object.Object { // Expressions case *ast.IntegerLiteral: return &object.Integer{Value: node.Value} + + case *ast.Boolean: + return nativeBoolToBooleanObject(node.Value) + + case *ast.PrefixExpression: + right := Eval(node.Right) + return evalPrefixExpression(node.Operator, right) } return nil @@ -32,3 +45,32 @@ func evalStatements(stmts []ast.Statement) object.Object { return result } + +func nativeBoolToBooleanObject(input bool) *object.Boolean { + if input { + return TRUE + } + return FALSE +} + +func evalPrefixExpression(operator string, right object.Object) object.Object { + switch operator { + case "!": + return evalBangOperatorExpression(right) + default: + return NULL + } +} + +func evalBangOperatorExpression(right object.Object) object.Object { + switch right { + case TRUE: + return FALSE + case FALSE: + return TRUE + case NULL: + return TRUE + default: + return FALSE + } +} |
