Skip to content

Commit

Permalink
Merge pull request #6 from kamilturek/object-system-foundation
Browse files Browse the repository at this point in the history
Object system & evaluator foundation
  • Loading branch information
kamilturek authored Jun 23, 2024
2 parents d4576f5 + 1d56d73 commit 1ce28af
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 1 deletion.
93 changes: 93 additions & 0 deletions evaluator/evaluator.go
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
}
113 changes: 113 additions & 0 deletions evaluator/evaluator_test.go
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)
}
}
58 changes: 58 additions & 0 deletions object/object.go
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"
}
6 changes: 5 additions & 1 deletion repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io"

"github.com/kamilturek/monkey/evaluator"
"github.com/kamilturek/monkey/lexer"
"github.com/kamilturek/monkey/parser"
)
Expand Down Expand Up @@ -33,7 +34,10 @@ func Start(in io.Reader, out io.Writer) {
continue
}

fmt.Fprint(out, program.String(), "\n")
evaluated := evaluator.Eval(program)
if evaluated != nil {
fmt.Fprintf(out, "%s\n", evaluated.Inspect())
}
}
}

Expand Down

0 comments on commit 1ce28af

Please sign in to comment.