-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from kamilturek/object-system-foundation
Object system & evaluator foundation
- Loading branch information
Showing
4 changed files
with
269 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package evaluator | ||
|
||
import ( | ||
"github.com/kamilturek/monkey/ast" | ||
"github.com/kamilturek/monkey/object" | ||
) | ||
|
||
var ( | ||
NULL = &object.Null{} | ||
TRUE = &object.Boolean{Value: true} | ||
FALSE = &object.Boolean{Value: false} | ||
) | ||
|
||
func Eval(node ast.Node) object.Object { | ||
switch node := node.(type) { | ||
// Statements | ||
case *ast.Program: | ||
return evalStatements(node.Statements) | ||
case *ast.ExpressionStatement: | ||
return Eval(node.Expression) | ||
|
||
// Expressions | ||
case *ast.IntegerLiteral: | ||
return &object.Integer{ | ||
Value: node.Value, | ||
} | ||
case *ast.BooleanLiteral: | ||
return nativeBoolToBooleanObject(node.Value) | ||
case *ast.PrefixExpression: | ||
right := Eval(node.Right) | ||
|
||
return evalPrefixExpression(node.Operator, right) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func evalStatements(stmts []ast.Statement) object.Object { | ||
var result object.Object | ||
|
||
for _, stmt := range stmts { | ||
result = Eval(stmt) | ||
} | ||
|
||
return result | ||
} | ||
|
||
func evalPrefixExpression(operator string, right object.Object) object.Object { | ||
switch operator { | ||
case "!": | ||
return evalBangOperatorExpression(right) | ||
case "-": | ||
return evalMinusPrefixOperatorExpression(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 | ||
} | ||
} | ||
|
||
func evalMinusPrefixOperatorExpression(right object.Object) object.Object { | ||
if right.Type() != object.INTEGER_OBJ { | ||
return NULL | ||
} | ||
|
||
integer, ok := right.(*object.Integer) | ||
if !ok { | ||
return NULL | ||
} | ||
|
||
return &object.Integer{ | ||
Value: -integer.Value, | ||
} | ||
} | ||
|
||
func nativeBoolToBooleanObject(input bool) *object.Boolean { | ||
if input { | ||
return TRUE | ||
} | ||
|
||
return FALSE | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
package evaluator_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/kamilturek/monkey/evaluator" | ||
"github.com/kamilturek/monkey/lexer" | ||
"github.com/kamilturek/monkey/object" | ||
"github.com/kamilturek/monkey/parser" | ||
) | ||
|
||
func testEval(input string) object.Object { | ||
l := lexer.NewLexer(input) | ||
p := parser.NewParser(l) | ||
program := p.ParseProgram() | ||
|
||
return evaluator.Eval(program) | ||
} | ||
|
||
func testIntegerObject(t *testing.T, obj object.Object, expected int64) bool { | ||
t.Helper() | ||
|
||
result, ok := obj.(*object.Integer) | ||
if !ok { | ||
t.Errorf("object is not Integer. got=%T (%+v)", obj, obj) | ||
|
||
return false | ||
} | ||
|
||
if result.Value != expected { | ||
t.Errorf("object has wrong value. got=%d, want=%d", expected, result.Value) | ||
|
||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
func testBooleanObject(t *testing.T, obj object.Object, expected bool) bool { //nolint:unparam | ||
t.Helper() | ||
|
||
result, ok := obj.(*object.Boolean) | ||
if !ok { | ||
t.Errorf("object is not Boolean. got=%T (%+v)", obj, obj) | ||
|
||
return false | ||
} | ||
|
||
if result.Value != expected { | ||
t.Errorf("object has wrong value. got=%t, want=%t", expected, result.Value) | ||
|
||
return false | ||
} | ||
|
||
return true | ||
} | ||
|
||
func TestEvalIntegerExpression(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
input string | ||
expected int64 | ||
}{ | ||
{"5", 5}, | ||
{"10", 10}, | ||
{"-5", -5}, | ||
{"-10", -10}, | ||
} | ||
|
||
for _, tt := range tests { | ||
evaluated := testEval(tt.input) | ||
testIntegerObject(t, evaluated, tt.expected) | ||
} | ||
} | ||
|
||
func TestEvalBooleanExpression(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
input string | ||
expected bool | ||
}{ | ||
{"true", true}, | ||
{"false", false}, | ||
} | ||
|
||
for _, tt := range tests { | ||
evaluated := testEval(tt.input) | ||
testBooleanObject(t, evaluated, tt.expected) | ||
} | ||
} | ||
|
||
func TestBangOperator(t *testing.T) { | ||
t.Parallel() | ||
|
||
tests := []struct { | ||
input string | ||
expected bool | ||
}{ | ||
{"!true", false}, | ||
{"!false", true}, | ||
{"!5", false}, | ||
{"!!true", true}, | ||
{"!!false", false}, | ||
{"!!5", true}, | ||
} | ||
|
||
for _, tt := range tests { | ||
evaluated := testEval(tt.input) | ||
testBooleanObject(t, evaluated, tt.expected) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package object | ||
|
||
import ( | ||
"strconv" | ||
) | ||
|
||
type ObjectType string | ||
|
||
const ( | ||
BOOLEAN_OBJ = "BOOLEAN" | ||
INTEGER_OBJ = "INTEGER" | ||
NULL_OBJ = "NULL" | ||
) | ||
|
||
type Object interface { | ||
Type() ObjectType | ||
Inspect() string | ||
} | ||
|
||
// Integer | ||
|
||
type Integer struct { | ||
Value int64 | ||
} | ||
|
||
func (i *Integer) Type() ObjectType { | ||
return INTEGER_OBJ | ||
} | ||
|
||
func (i *Integer) Inspect() string { | ||
return strconv.FormatInt(i.Value, 10) | ||
} | ||
|
||
// Boolean | ||
|
||
type Boolean struct { | ||
Value bool | ||
} | ||
|
||
func (b *Boolean) Type() ObjectType { | ||
return BOOLEAN_OBJ | ||
} | ||
|
||
func (b *Boolean) Inspect() string { | ||
return strconv.FormatBool(b.Value) | ||
} | ||
|
||
// Null | ||
|
||
type Null struct{} | ||
|
||
func (n *Null) Type() ObjectType { | ||
return NULL_OBJ | ||
} | ||
|
||
func (n *Null) Inspect() string { | ||
return "null" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters