@@ -9,6 +9,20 @@ import (
99 "github.com/cedrickchee/hou/object"
1010)
1111
12+ var (
13+ // TRUE is a cached Boolean object holding the `true` value.
14+ TRUE = & object.Boolean {Value : true }
15+
16+ // FALSE is a cached Boolean object holding the `false` value.
17+ FALSE = & object.Boolean {Value : false }
18+
19+ // NULL is a cached Null object. There should only be one reference to a
20+ // null value, just as there's only one 'true' and one 'false'.
21+ // No kinda-but-not-quite-null, no half-null and no
22+ // basically-thesame-as-the-other-null.
23+ NULL = & object.Null {}
24+ )
25+
1226// Eval evaluates the node and returns an object.
1327func Eval (node ast.Node ) object.Object {
1428 // Traverse the AST by starting at the top of the tree, receiving an
@@ -30,6 +44,9 @@ func Eval(node ast.Node) object.Object {
3044 // Expressions
3145 case * ast.IntegerLiteral :
3246 return & object.Integer {Value : node .Value }
47+
48+ case * ast.Boolean :
49+ return nativeBoolToBooleanObject (node .Value )
3350 }
3451
3552 return nil
@@ -44,3 +61,19 @@ func evalStatements(stmts []ast.Statement) object.Object {
4461
4562 return result
4663}
64+
65+ // Helper function to reference true or false to only two instances of
66+ // object.Boolean: TRUE and FALSE.
67+ func nativeBoolToBooleanObject (input bool ) * object.Boolean {
68+ // We shouldn't create a new object.Boolean every time we encounter a true
69+ // or false. There is no difference between two trues. The same goes for
70+ // false. We shouldn't use new instances every time. There are only two
71+ // possible values, so let's reference them instead of allocating new
72+ // object.Booleans (creating new ones). That is a small performance
73+ // improvement we get without a lot of work.
74+
75+ if input {
76+ return TRUE
77+ }
78+ return FALSE
79+ }
0 commit comments