-
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.
- Loading branch information
1 parent
3007fd6
commit 1775525
Showing
2 changed files
with
109 additions
and
0 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,55 @@ | ||
package evaluator | ||
|
||
import ( | ||
"solbot/ast" | ||
"solbot/object" | ||
) | ||
|
||
func Eval(node ast.Node) object.Object { | ||
switch node := node.(type) { | ||
// File | ||
|
||
case *ast.File: | ||
return evalDeclarations(node.Declarations) | ||
|
||
// Declarations | ||
|
||
case *ast.FunctionDeclaration: | ||
// TODO: Hacky way to eval other parts in tests, fix later. | ||
return Eval(node.Body) | ||
|
||
// Statements | ||
|
||
case *ast.ExpressionStatement: | ||
return Eval(node.Expression) | ||
case *ast.BlockStatement: | ||
return evalStatements(node.Statements) | ||
|
||
// Expressions | ||
|
||
case *ast.NumberLiteral: | ||
return &object.Integer{Value: node.Value} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func evalDeclarations(decls []ast.Declaration) object.Object { | ||
var result object.Object | ||
|
||
for _, decl := range decls { | ||
result = Eval(decl) | ||
} | ||
|
||
return result | ||
} | ||
|
||
func evalStatements(stmts []ast.Statement) object.Object { | ||
var result object.Object | ||
|
||
for _, stmt := range stmts { | ||
result = Eval(stmt) | ||
} | ||
|
||
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,54 @@ | ||
package evaluator | ||
|
||
import ( | ||
"math/big" | ||
"solbot/object" | ||
"solbot/parser" | ||
"solbot/token" | ||
"testing" | ||
) | ||
|
||
func TestEvalIntegerExpression(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expected *big.Int | ||
}{ | ||
{"1", big.NewInt(1)}, | ||
{"50", big.NewInt(50)}, | ||
} | ||
|
||
for _, tt := range tests { | ||
evaluated := testEval(tt.input, true) | ||
testIntegerObject(t, evaluated, tt.expected) | ||
} | ||
} | ||
|
||
func testEval(input string, boilerplate bool) object.Object { | ||
p := parser.Parser{} | ||
|
||
if boilerplate { | ||
input = "function test() { " + input + " }" | ||
} | ||
|
||
handle := token.NewFile("test.sol", input) | ||
p.Init(handle) | ||
|
||
file := p.ParseFile() | ||
|
||
return Eval(file) | ||
} | ||
|
||
func testIntegerObject(t *testing.T, obj object.Object, expected *big.Int) bool { | ||
result, ok := obj.(*object.Integer) | ||
if !ok { | ||
t.Errorf("Expected object.Integer, got %T (%+v)", obj, obj) | ||
return false | ||
} | ||
|
||
if result.Value.Cmp(expected) != 0 { | ||
t.Errorf("Expected %s, got %s", expected.String(), result.Value.String()) | ||
return false | ||
} | ||
|
||
return true | ||
} |