-
Notifications
You must be signed in to change notification settings - Fork 0
/
mjsi.java
76 lines (68 loc) · 2.51 KB
/
mjsi.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
//Grupo: Erick Rocha e Gisele Oliveira
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import interpreter.Interpreter;
import interpreter.command.Command;
import lexical.LexicalAnalysis;
import syntatic.SyntaticAnalysis;
public class mjsi {
public static void main(String[] args) {
try {
switch (args.length) {
case 0:
runPrompt();
break;
case 1:
runFile(args[0]);
break;
default:
System.out.println("Usage: java mjsi [miniJScript file]");
break;
}
} catch (Exception e) {
System.err.println("Internal error: " + e.getMessage());
//e.printStackTrace();
}
}
private static void runPrompt() throws Exception {
InputStreamReader input = new InputStreamReader(System.in);
BufferedReader reader = new BufferedReader(input);
for (;;) {
System.out.print("> ");
String line = reader.readLine();
if (line == null) {
System.out.println();
break;
}
run(new ByteArrayInputStream(line.getBytes()));
}
}
private static void runFile(String filename) throws Exception {
run(new FileInputStream(filename));
}
private static void run(InputStream is) {
try (LexicalAnalysis l = new LexicalAnalysis(is)) {
// // O código a seguir é usado apenas para testar o analisador léxico.
// // TODO: depois de pronto, comentar o código abaixo.
// Token lex;
// do {
// lex = l.nextToken();
// System.out.printf("%02d: (\"%s\", %s, %s)\n", lex.line,
// lex.lexeme, lex.type, lex.literal);
// } while (lex.type != END_OF_FILE &&
// lex.type != INVALID_TOKEN &&
// lex.type != UNEXPECTED_EOF);
// O código a seguir é dado para testar o interpretador.
// TODO: descomentar depois que o analisador léxico estiver OK.
SyntaticAnalysis s = new SyntaticAnalysis(l);
Command cmd = s.process();
Interpreter.interpret(cmd);
} catch (Exception e) {
System.out.println(e.getMessage());
//e.printStackTrace();
}
}
}