Skip to content

Commit

Permalink
evaluator: basic interface and implement the integer object
Browse files Browse the repository at this point in the history
- 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
mcapell committed Nov 4, 2024
1 parent e9de152 commit 268c04b
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 2 deletions.
34 changes: 34 additions & 0 deletions evaluator/evaluator.go
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
}
47 changes: 47 additions & 0 deletions evaluator/evaluator_test.go
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
}
35 changes: 35 additions & 0 deletions object/object.go
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" }
9 changes: 7 additions & 2 deletions repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"

"github.com/mcapell/go-monkey/evaluator"
"github.com/mcapell/go-monkey/lexer"
"github.com/mcapell/go-monkey/parser"
)
Expand Down Expand Up @@ -32,8 +33,12 @@ func Start(in io.Reader, out io.Writer) {
continue
}

io.WriteString(out, program.String()) // nolint: errcheck
io.WriteString(out, "\n") // nolint: errcheck
evaluated := evaluator.Eval(program)
if evaluated != nil {
io.WriteString(out, evaluated.Inspect()) // nolint: errcheck
io.WriteString(out, "\n") // nolint: errcheck
}

}
}

Expand Down

0 comments on commit 268c04b

Please sign in to comment.