-
Notifications
You must be signed in to change notification settings - Fork 0
/
ast_test.go
42 lines (33 loc) · 902 Bytes
/
ast_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package main
import (
"bytes"
"testing"
)
func TestAST(t *testing.T) {
lexer := NewLexer(bytes.NewReader([]byte("(1)+(2)")))
err := lexer.Analyze()
if err != nil {
t.Error("error while doing lexical analysis: ", err)
t.Fail()
return
}
t.Logf("Token List: %v", lexer.tokenList)
actual := parseTokens(lexer.tokenList, parseExpr)
t.Log("Actual: ", actual.String())
expected := newASTNode(NewToken(Plus, '+'),
newASTNode(NewToken(Number, '1'), nil, nil, nil),
newASTNode(NewToken(Number, '2'), nil, nil, nil), nil)
if !compareAST(expected, actual) {
t.Error("Expected AST didn't match with Actual AST")
t.Error("Expected: ", expected.String())
}
}
func compareAST(x, y *astNode) bool {
if x == nil && y == nil {
return true
}
if x != nil && y != nil {
return x.token.Equals(y.token) && compareAST(x.left, y.left) && compareAST(x.right, y.right)
}
return false
}