-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeCheckTest.java
More file actions
119 lines (107 loc) · 3.37 KB
/
Copy pathTypeCheckTest.java
File metadata and controls
119 lines (107 loc) · 3.37 KB
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
package cop5556fa17;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import static org.junit.Assert.*;
import cop5556fa17.AST.ASTNode;
import cop5556fa17.AST.ASTVisitor;
import cop5556fa17.AST.Declaration_Image;
import cop5556fa17.AST.Declaration_SourceSink;
import cop5556fa17.AST.Declaration_Variable;
import cop5556fa17.AST.Expression;
import cop5556fa17.AST.Expression_FunctionAppWithExprArg;
import cop5556fa17.AST.Expression_IntLit;
import cop5556fa17.AST.Expression_PixelSelector;
import cop5556fa17.AST.Expression_PredefinedName;
import cop5556fa17.AST.Expression_Unary;
import cop5556fa17.AST.Index;
import cop5556fa17.AST.LHS;
import cop5556fa17.AST.Program;
import cop5556fa17.AST.Source_CommandLineParam;
import cop5556fa17.AST.Source_StringLiteral;
import cop5556fa17.AST.Statement_Out;
import cop5556fa17.AST.Statement_Assign;
import cop5556fa17.Parser.SyntaxException;
import cop5556fa17.Scanner.Kind;
import cop5556fa17.Scanner.LexicalException;
import cop5556fa17.Scanner.Token;
import cop5556fa17.TypeCheckVisitor.SemanticException;
import static cop5556fa17.Scanner.Kind.*;
public class TypeCheckTest {
// set Junit to be able to catch exceptions
@Rule
public ExpectedException thrown = ExpectedException.none();
// To make it easy to print objects and turn this output on and off
static final boolean doPrint = true;
private void show(Object input) {
if (doPrint) {
System.out.println(input.toString());
}
}
/**
* Scans, parses, and type checks given input String.
*
* Catches, prints, and then rethrows any exceptions that occur.
*
* @param input
* @throws Exception
*/
void typeCheck(String input) throws Exception {
show(input);
try {
Scanner scanner = new Scanner(input).scan();
ASTNode ast = new Parser(scanner).parse();
show(ast);
ASTVisitor v = new TypeCheckVisitor();
ast.visit(v, null);
} catch (Exception e) {
show(e);
throw e;
}
}
/**
* Simple test case with an almost empty program.
*
* @throws Exception
*/
@Test
public void testSmallest() throws Exception {
String input = "n"; //Smallest legal program, only has a name
show(input); // Display the input
Scanner scanner = new Scanner(input).scan(); // Create a Scanner and
// initialize it
show(scanner); // Display the Scanner
Parser parser = new Parser(scanner); // Create a parser
ASTNode ast = parser.parse(); // Parse the program
TypeCheckVisitor v = new TypeCheckVisitor();
String name = (String) ast.visit(v, null);
show("AST for program " + name);
show(ast);
}
/**
* This test should pass with a fully implemented assignment
* @throws Exception
*/
@Test
public void testDec1() throws Exception {
String input = "prog int k = 42;";
typeCheck(input);
}
/**
* This program does not declare k. The TypeCheckVisitor should
* throw a SemanticException in a fully implemented assignment.
* @throws Exception
*/
@Test
public void testUndec() throws Exception {
String input = "prog k = 42;";
thrown.expect(SemanticException.class);
typeCheck(input);
}
@Test
public void test1() throws Exception {
String input = "prog boolean s=((false<=true) & (true>=false) );";
thrown.expect(SemanticException.class);
typeCheck(input);
}
}