-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCheck.java
More file actions
136 lines (122 loc) · 3.14 KB
/
Check.java
File metadata and controls
136 lines (122 loc) · 3.14 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.stringtemplate.*;
import java.io.*;
import java.util.Vector;
import java.util.HashMap;
import javax.json.JsonValue;
import javax.json.Json;
public class Check
{
public static void main(String[] args)
{
parseParameters(args);
CommonTokenStream tokens = new CommonTokenStream(createLexer());
MiniParser parser = new MiniParser(tokens);
CommonTree tree = parse(parser);
if (_displayAST && tree != null)
{
DOTTreeGenerator gen = new DOTTreeGenerator();
StringTemplate st = gen.toDOT(tree);
System.out.println(st);
}
else if (!parser.hasErrors())
{
try
{
translate(tree, tokens);
System.out.println("No type errors.");
}
catch (SyntaxException e)
{
System.out.println("Syntax error: " + e.getMessage());
}
}
}
private static final String DISPLAYAST = "-displayAST";
private static String _inputFile = null;
private static boolean _displayAST = false;
private static void parseParameters(String [] args)
{
for (int i = 0; i < args.length; i++)
{
if (args[i].equals(DISPLAYAST))
{
_displayAST = true;
}
else if (args[i].charAt(0) == '-')
{
System.err.println("unexpected option: " + args[i]);
System.exit(1);
}
else if (_inputFile != null)
{
System.err.println("too many files specified");
System.exit(1);
}
else
{
_inputFile = args[i];
}
}
}
private static CommonTree parse(MiniParser parser)
{
try
{
MiniParser.program_return ret = parser.program();
return (CommonTree)ret.getTree();
}
catch (org.antlr.runtime.RecognitionException e)
{
error(e.toString());
}
catch (Exception e)
{
System.exit(-1);
}
return null;
}
private static void translate(CommonTree tree, CommonTokenStream tokens) throws SyntaxException
{
try
{
CommonTreeNodeStream nodes = new CommonTreeNodeStream(tree);
nodes.setTokenStream(tokens);
TypeCheck tparser = new TypeCheck(nodes);
tparser.translate();
}
catch (org.antlr.runtime.RecognitionException e)
{
error(e.toString());
}
}
private static void error(String msg)
{
System.err.println(msg);
System.exit(1);
}
private static MiniLexer createLexer()
{
try
{
ANTLRInputStream input;
if (_inputFile == null)
{
input = new ANTLRInputStream(System.in);
}
else
{
input = new ANTLRInputStream(
new BufferedInputStream(new FileInputStream(_inputFile)));
}
return new MiniLexer(input);
}
catch (java.io.IOException e)
{
System.err.println("file not found: " + _inputFile);
System.exit(1);
return null;
}
}
}