Evaluate Integers
This commit is contained in:
34
evaluator/evaluator.go
Normal file
34
evaluator/evaluator.go
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package evaluator
|
||||||
|
|
||||||
|
import (
|
||||||
|
"monkey/ast"
|
||||||
|
"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(stmts []ast.Statement) object.Object {
|
||||||
|
var result object.Object
|
||||||
|
|
||||||
|
for _, statement := range stmts {
|
||||||
|
result = Eval(statement)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
|
}
|
||||||
45
evaluator/evaluator_test.go
Normal file
45
evaluator/evaluator_test.go
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
package evaluator
|
||||||
|
|
||||||
|
import (
|
||||||
|
"monkey/lexer"
|
||||||
|
"monkey/object"
|
||||||
|
"monkey/parser"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
48
object/object.go
Normal file
48
object/object.go
Normal file
@@ -0,0 +1,48 @@
|
|||||||
|
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 (b *Boolean) Type() ObjectType {
|
||||||
|
return BOOLEAN_OBJ
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *Boolean) Inspect() string {
|
||||||
|
return fmt.Sprintf("%t", b.Value)
|
||||||
|
}
|
||||||
|
|
||||||
|
type Null struct{}
|
||||||
|
|
||||||
|
func (n *Null) Type() ObjectType {
|
||||||
|
return NULL_OBJ
|
||||||
|
}
|
||||||
|
func (n *Null) Inspect() string {
|
||||||
|
return "null"
|
||||||
|
}
|
||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"bufio"
|
"bufio"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"monkey/evaluator"
|
||||||
"monkey/lexer"
|
"monkey/lexer"
|
||||||
"monkey/parser"
|
"monkey/parser"
|
||||||
)
|
)
|
||||||
@@ -30,8 +31,11 @@ func Start(in io.Reader, out io.Writer) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
io.WriteString(out, program.String())
|
evaluated := evaluator.Eval(program)
|
||||||
io.WriteString(out, "\n")
|
if evaluated != nil {
|
||||||
|
io.WriteString(out, evaluated.Inspect())
|
||||||
|
io.WriteString(out, "\n")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user