Implement the "!" evaluation operator
git-svn-id: https://svn.tlawal.org/svn/monkey@46 f6afcba9-9ef1-4bdd-9b72-7484f5705bac
This commit is contained in:
parent
886a16e706
commit
4b67c7d6e1
@ -5,6 +5,12 @@ import (
|
|||||||
"monkey/object"
|
"monkey/object"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
NULL = &object.Null{}
|
||||||
|
TRUE = &object.Boolean{Value: true}
|
||||||
|
FALSE = &object.Boolean{Value: false}
|
||||||
|
)
|
||||||
|
|
||||||
func Eval(node ast.Node) object.Object {
|
func Eval(node ast.Node) object.Object {
|
||||||
switch node := node.(type) {
|
switch node := node.(type) {
|
||||||
// Statements
|
// Statements
|
||||||
@ -19,7 +25,12 @@ func Eval(node ast.Node) object.Object {
|
|||||||
return &object.Integer{Value: node.Value}
|
return &object.Integer{Value: node.Value}
|
||||||
|
|
||||||
case *ast.Boolean:
|
case *ast.Boolean:
|
||||||
return &object.Boolean{Value: node.Value}
|
return native_bool_to_boolean_object(node.Value)
|
||||||
|
|
||||||
|
case *ast.PrefixExpression:
|
||||||
|
right := Eval(node.Right)
|
||||||
|
return eval_prefix_expression(node.Operator, right)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
@ -33,3 +44,36 @@ func eval_statements(statements []ast.Statement) object.Object {
|
|||||||
}
|
}
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func native_bool_to_boolean_object(input bool) *object.Boolean {
|
||||||
|
if input {
|
||||||
|
return TRUE
|
||||||
|
}
|
||||||
|
return FALSE
|
||||||
|
}
|
||||||
|
|
||||||
|
func eval_prefix_expression(operator string, right object.Object) object.Object {
|
||||||
|
switch operator {
|
||||||
|
case "!":
|
||||||
|
return eval_bang_operator_expression(right)
|
||||||
|
|
||||||
|
default:
|
||||||
|
return NULL
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func eval_bang_operator_expression(right object.Object) object.Object {
|
||||||
|
switch right {
|
||||||
|
case TRUE:
|
||||||
|
return FALSE
|
||||||
|
|
||||||
|
case FALSE:
|
||||||
|
return TRUE
|
||||||
|
|
||||||
|
case NULL:
|
||||||
|
return TRUE
|
||||||
|
|
||||||
|
default:
|
||||||
|
return FALSE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -37,6 +37,25 @@ func TestEvalBooleanExpression(l_test *testing.T){
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test to convert the '!' operator to boolean value and negate it
|
||||||
|
func TestBangOperator(l_test *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
input string
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
{"!true", false},
|
||||||
|
{"!false", true},
|
||||||
|
{"!5", false},
|
||||||
|
{"!!true", true},
|
||||||
|
{"!!false", false},
|
||||||
|
{"!!5", true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tt := range tests {
|
||||||
|
evaluated := test_eval(tt.input)
|
||||||
|
test_boolean_object(l_test, evaluated, tt.expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Helpers
|
// Helpers
|
||||||
func test_eval(input string) object.Object {
|
func test_eval(input string) object.Object {
|
||||||
|
@ -4,9 +4,9 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"monkey/evaluator"
|
||||||
"monkey/lexer"
|
"monkey/lexer"
|
||||||
"monkey/parser"
|
"monkey/parser"
|
||||||
"monkey/evaluator"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const MONKEY_FACE = ` __,__
|
const MONKEY_FACE = ` __,__
|
||||||
|
Loading…
Reference in New Issue
Block a user