Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
89 lines (80 loc) · 3.1 KB
/
Copy pathMain.java
File metadata and controls
89 lines (80 loc) · 3.1 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
import egg.*;
import fr.n7.stl.tam.ast.Fragment;
import fr.n7.stl.tam.ast.Library;
import fr.n7.stl.tam.ast.Register;
import fr.n7.stl.tam.ast.TAMFactory;
import fr.n7.stl.tam.ast.impl.TAMFactoryImpl;
import mg.egg.eggc.runtime.libjava.ISourceUnit;
import mg.egg.eggc.runtime.libjava.SourceUnit;
import mg.egg.eggc.runtime.libjava.problem.IProblem;
import mg.egg.eggc.runtime.libjava.problem.ProblemReporter;
import mg.egg.eggc.runtime.libjava.problem.ProblemRequestor;
import java.io.*;
public class Main{
private static String ident(String s) {
StringBuilder _builder = new StringBuilder(0);
int nident = -1;
for (String _s : s.split("\n")) {
int variation = 0;
for (char i : _s.toCharArray()) {
if (i == '}') variation--;
if (i == '{') variation++;
}
if (variation < 0) nident += variation;
for (int i = 0; i < nident; i++)
_builder.append("\t");
_builder.append(_s).append("\n");
if (variation > 0) nident += variation;
}
return _builder.toString();
}
public static void main (String[] args){
try {
ISourceUnit cu = new SourceUnit(args[0]);
System.out.println("Fichier analysé : "+ args[0]);
ProblemReporter prp = new ProblemReporter(cu);
ProblemRequestor prq = new ProblemRequestor(true);
MiniJava bloc = new MiniJava(prp);
prq.beginReporting();
bloc.set_eval(true);
bloc.compile(cu);
for(IProblem problem : prp.getAllProblems()) {
prq.acceptProblem(problem);
}
prq.endReporting();
System.out.println(ident("AST :"+bloc.get_ast()));
if (prp.getAllProblems().isEmpty()) {
System.out.println("Format: OK");
} else {
for(IProblem problem : prp.getAllProblems()) {
System.out.println(problem);
}
System.out.println("Format: ERROR");
}
if (bloc.get_ast().checkType()) {
try{
String filename = args.length == 1 ?
args[0].substring(0, args[0].lastIndexOf('.')).concat(".tam") :
args[1];
PrintWriter writer = new PrintWriter(filename, "UTF-8");
bloc.get_ast().allocateMemory(Register.SB, 0);
TAMFactory factory = new TAMFactoryImpl();
Fragment code = bloc.get_ast().getCode(factory);
writer.println(code);
writer.close();
System.out.println( "Typing: OK" );
} catch (IOException e) {
e.printStackTrace();
System.out.println( "Typing: ERROR" );
}
} else {
System.out.println( "Typing: ERROR" );
}
System.exit(0);
}
catch(Exception e){
e.printStackTrace();
System.exit(1);
}
}
}