-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathGrammarTests.java
141 lines (112 loc) · 6.47 KB
/
GrammarTests.java
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import norswap.autumn.AutumnTestFixture;
import norswap.sigh.SighGrammar;
import norswap.sigh.ast.*;
import org.testng.annotations.Test;
import static java.util.Arrays.asList;
import static norswap.sigh.ast.BinaryOperator.*;
public class GrammarTests extends AutumnTestFixture {
// ---------------------------------------------------------------------------------------------
private final SighGrammar grammar = new SighGrammar();
private final Class<?> grammarClass = grammar.getClass();
// ---------------------------------------------------------------------------------------------
private static IntLiteralNode intlit (long i) {
return new IntLiteralNode(null, i);
}
private static FloatLiteralNode floatlit (double d) {
return new FloatLiteralNode(null, d);
}
// ---------------------------------------------------------------------------------------------
@Test
public void testLiteralsAndUnary () {
rule = grammar.expression;
successExpect("42", intlit(42));
successExpect("42.0", floatlit(42d));
successExpect("\"hello\"", new StringLiteralNode(null, "hello"));
successExpect("(42)", new ParenthesizedNode(null, intlit(42)));
successExpect("[1, 2, 3]", new ArrayLiteralNode(null, asList(intlit(1), intlit(2), intlit(3))));
successExpect("true", new ReferenceNode(null, "true"));
successExpect("false", new ReferenceNode(null, "false"));
successExpect("null", new ReferenceNode(null, "null"));
successExpect("!false", new UnaryExpressionNode(null, UnaryOperator.NOT, new ReferenceNode(null, "false")));
}
// ---------------------------------------------------------------------------------------------
@Test
public void testNumericBinary () {
successExpect("1 + 2", new BinaryExpressionNode(null, intlit(1), ADD, intlit(2)));
successExpect("2 - 1", new BinaryExpressionNode(null, intlit(2), SUBTRACT, intlit(1)));
successExpect("2 * 3", new BinaryExpressionNode(null, intlit(2), MULTIPLY, intlit(3)));
successExpect("2 / 3", new BinaryExpressionNode(null, intlit(2), DIVIDE, intlit(3)));
successExpect("2 % 3", new BinaryExpressionNode(null, intlit(2), REMAINDER, intlit(3)));
successExpect("1.0 + 2.0", new BinaryExpressionNode(null, floatlit(1), ADD, floatlit(2)));
successExpect("2.0 - 1.0", new BinaryExpressionNode(null, floatlit(2), SUBTRACT, floatlit(1)));
successExpect("2.0 * 3.0", new BinaryExpressionNode(null, floatlit(2), MULTIPLY, floatlit(3)));
successExpect("2.0 / 3.0", new BinaryExpressionNode(null, floatlit(2), DIVIDE, floatlit(3)));
successExpect("2.0 % 3.0", new BinaryExpressionNode(null, floatlit(2), REMAINDER, floatlit(3)));
successExpect("2 * (4-1) * 4.0 / 6 % (2+1)", new BinaryExpressionNode(null,
new BinaryExpressionNode(null,
new BinaryExpressionNode(null,
new BinaryExpressionNode(null,
intlit(2),
MULTIPLY,
new ParenthesizedNode(null, new BinaryExpressionNode(null,
intlit(4),
SUBTRACT,
intlit(1)))),
MULTIPLY,
floatlit(4d)),
DIVIDE,
intlit(6)),
REMAINDER,
new ParenthesizedNode(null, new BinaryExpressionNode(null,
intlit(2),
ADD,
intlit(1)))));
}
// ---------------------------------------------------------------------------------------------
@Test public void testArrayStructAccess () {
rule = grammar.expression;
successExpect("[1][0]", new ArrayAccessNode(null,
new ArrayLiteralNode(null, asList(intlit(1))), intlit(0)));
successExpect("[1].length", new FieldAccessNode(null,
new ArrayLiteralNode(null, asList(intlit(1))), "length"));
successExpect("p.x", new FieldAccessNode(null, new ReferenceNode(null, "p"), "x"));
}
// ---------------------------------------------------------------------------------------------
@Test public void testDeclarations() {
rule = grammar.statement;
successExpect("var x: Int = 1", new VarDeclarationNode(null,
"x", new SimpleTypeNode(null, "Int"), intlit(1)));
successExpect("struct P {}", new StructDeclarationNode(null, "P", asList()));
successExpect("struct P { var x: Int; var y: Int }",
new StructDeclarationNode(null, "P", asList(
new FieldDeclarationNode(null, "x", new SimpleTypeNode(null, "Int")),
new FieldDeclarationNode(null, "y", new SimpleTypeNode(null, "Int")))));
successExpect("fun f (x: Int): Int { return 1 }",
new FunDeclarationNode(null, "f",
asList(new ParameterNode(null, "x", new SimpleTypeNode(null, "Int"))),
new SimpleTypeNode(null, "Int"),
new BlockNode(null, asList(new ReturnNode(null, intlit(1))))));
}
// ---------------------------------------------------------------------------------------------
@Test public void testStatements() {
rule = grammar.statement;
successExpect("return", new ReturnNode(null, null));
successExpect("return 1", new ReturnNode(null, intlit(1)));
successExpect("print(1)", new ExpressionStatementNode(null,
new FunCallNode(null, new ReferenceNode(null, "print"), asList(intlit(1)))));
successExpect("{ return }", new BlockNode(null, asList(new ReturnNode(null, null))));
successExpect("if true return 1 else return 2", new IfNode(null, new ReferenceNode(null, "true"),
new ReturnNode(null, intlit(1)),
new ReturnNode(null, intlit(2))));
successExpect("if false return 1 else if true return 2 else return 3 ",
new IfNode(null, new ReferenceNode(null, "false"),
new ReturnNode(null, intlit(1)),
new IfNode(null, new ReferenceNode(null, "true"),
new ReturnNode(null, intlit(2)),
new ReturnNode(null, intlit(3)))));
successExpect("while 1 < 2 { return } ", new WhileNode(null,
new BinaryExpressionNode(null, intlit(1), LOWER, intlit(2)),
new BlockNode(null, asList(new ReturnNode(null, null)))));
}
// ---------------------------------------------------------------------------------------------
}