Evaluate Integers

This commit is contained in:
Chuck Smith
2024-01-18 16:45:32 -05:00
parent 96dc5b448e
commit beed497ed5
4 changed files with 133 additions and 2 deletions

34
evaluator/evaluator.go Normal file
View 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
}

View 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
View 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"
}

View File

@@ -4,6 +4,7 @@ import (
"bufio" "bufio"
"fmt" "fmt"
"io" "io"
"monkey/evaluator"
"monkey/lexer" "monkey/lexer"
"monkey/parser" "monkey/parser"
) )
@@ -30,9 +31,12 @@ func Start(in io.Reader, out io.Writer) {
continue continue
} }
io.WriteString(out, program.String()) evaluated := evaluator.Eval(program)
if evaluated != nil {
io.WriteString(out, evaluated.Inspect())
io.WriteString(out, "\n") io.WriteString(out, "\n")
} }
}
} }
const MONKEY_FACE = ` __,__ const MONKEY_FACE = ` __,__