-
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.
evaluator: basic interface and implement the integer object
- Implement the basic interface on object/object.go - Basic object types for integers, booleans, and null object. - Implement the integer object. - Use the new objects as outputs of the REPL.
- Loading branch information
Showing
4 changed files
with
123 additions
and
2 deletions.
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,34 @@ | ||
package evaluator | ||
|
||
import ( | ||
"github.com/mcapell/go-monkey/ast" | ||
"github.com/mcapell/go-monkey/object" | ||
) | ||
|
||
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} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func evalStatements(statements []ast.Statement) object.Object { | ||
var result object.Object | ||
|
||
for _, statement := range statements { | ||
result = Eval(statement) | ||
} | ||
|
||
return result | ||
} |
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,47 @@ | ||
package evaluator | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/mcapell/go-monkey/lexer" | ||
"github.com/mcapell/go-monkey/object" | ||
"github.com/mcapell/go-monkey/parser" | ||
) | ||
|
||
func TestEvalIntegerExpression(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expected int64 | ||
}{ | ||
{"5", 5}, | ||
{"10", 10}, | ||
} | ||
|
||
for _, tt := range tests { | ||
evaluated := testEval(tt.input) | ||
testIntegerObject(t, evaluated, tt.expected) | ||
} | ||
} | ||
|
||
func testEval(input string) object.Object { | ||
l := lexer.New(input) | ||
p := parser.New(l) | ||
program := p.ParseProgram() | ||
|
||
return Eval(program) | ||
} | ||
|
||
func testIntegerObject(t *testing.T, obj object.Object, expected int64) bool { | ||
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", result.Value, expected) | ||
return false | ||
} | ||
|
||
return true | ||
} |
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,35 @@ | ||
package object | ||
|
||
import "fmt" | ||
|
||
type ObjectType string | ||
|
||
const ( | ||
INTEGER_OBJ = "INTEGER" | ||
BOOLEAN_OBJ = "BOOLEAN" | ||
NULL_OBJ = "NULL" | ||
) | ||
|
||
type Object interface { | ||
Type() ObjectType | ||
Inspect() string | ||
} | ||
|
||
type Integer struct { | ||
Value int64 | ||
} | ||
|
||
func (i *Integer) Type() ObjectType { return INTEGER_OBJ } | ||
func (i *Integer) Inspect() string { return fmt.Sprintf("%d", i.Value) } | ||
|
||
type Boolean struct { | ||
Value bool | ||
} | ||
|
||
func (i *Boolean) Type() ObjectType { return BOOLEAN_OBJ } | ||
func (i *Boolean) Inspect() string { return fmt.Sprintf("%t", i.Value) } | ||
|
||
type Null struct{} | ||
|
||
func (i *Null) Type() ObjectType { return NULL_OBJ } | ||
func (i *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