-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExecuter.java
More file actions
142 lines (118 loc) · 3.27 KB
/
Executer.java
File metadata and controls
142 lines (118 loc) · 3.27 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
137
138
139
140
141
142
package translator;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import translator.Parser.Type;
public class Executer {
private BufferedReader br;
private List<List<Token>> listOfLexems = new ArrayList<>();
private List<String> strings = new ArrayList<>();
private Map<String, Type> varMap;
private List<String> codelist;
private List<String> functionlist;
public static void main(String[] args) {
if (args.length < 1)
throw new IllegalArgumentException("No arguments error: need a filename");
File f = new File(args[0]);
if (!f.exists())
throw new IllegalArgumentException("File '" + args[0] + "' doesn't exist");
try {
Executer e = new Executer();
e.br = new BufferedReader(new FileReader(f));
e.execute();
} catch (FileNotFoundException e1) {
System.err.println("File not found");
}
}
public void execute() {
try {
lex(); // çàïóñê ëåêñè÷åñêîãî àíàëèçà
parse(); // çàïóñê ñèíòàêñè÷åñêîãî àíàëèçà
} catch (ParseException e) {
System.err.println(e.getMessage());
} catch (LexException e) {
System.err.println(e.getMessage());
}
}
/**
* Äëÿ êàæäîãî ñïèñêà òîêåíîâ (ïî ñòðî÷êàì) çàïóñêàåò ñèíòàêñ. àíàëèçàòîð
*
* Ïîñëå áåðåò èç àíàëèçàòîðà:
* - êàðòó âñåõ ïåðåìåííûõ
* - êîä ìåòîäà ìýéí
* - ñïèñîê ìåòîäîâ ãåíåðèðóåìîãî êëàññà
* @throws ParseException
*/
public void parse() throws ParseException {
Parser p = new Parser();
for (List<Token> li : listOfLexems) {
if (!li.isEmpty()){
p.expr(li);
}
}
varMap = p.getVarMap();
codelist = p.getCodelist();
functionlist = p.getFunctionlist();
}
/**
* Ëåêñ. àíàëèç ïî ñòðîêàì, äëÿ êàæäîé ñòðîêè âîçâð. ñïèñîê òîêåíîâ è
* äîáàâëÿåò åãî â îáùèé ñïèñîê
* @throws LexException
*/
public void lex() throws LexException {
Lexer l = new Lexer();
try {
String str;
while ((str = br.readLine()) != null) {
try {
List<Token> tokens = l.read(str);
strings.add(str);
listOfLexems.add(tokens);
} catch (LexException e) {
throw new LexException(e.getMessage() + "\nin " + str);
}
}
br.close();
} catch (IOException e) {
System.err.println("Can't read input stream");
}
}
public void setText(String str) {
clearAll();
InputStream is = new ByteArrayInputStream(str.getBytes());
br = new BufferedReader(new InputStreamReader(is));
}
public void clearAll() {
listOfLexems.clear();
// functionlist.clear();
// codelist.clear();
strings.clear();
}
public List<List<Token>> getLexems() {
return listOfLexems;
}
public String getString(int i) {
return strings.get(i);
}
public void setFile(File input) throws FileNotFoundException {
clearAll();
br = new BufferedReader(new FileReader(input));
}
public Map<String, Type> getVarMap(){
return varMap;
}
public List<String> getCodelist() {
return codelist;
}
public List<String> getFunctionList() {
return functionlist;
}
}