2024-05-08 13:14:58 +00:00
|
|
|
import abstractSyntaxTree.Class.FieldDecl;
|
|
|
|
import abstractSyntaxTree.Class.MethodDecl;
|
|
|
|
import abstractSyntaxTree.Class.RefType;
|
|
|
|
import abstractSyntaxTree.Program;
|
2024-05-09 13:12:08 +00:00
|
|
|
import gen.DecafLexer;
|
|
|
|
import gen.DecafParser;
|
|
|
|
import org.antlr.v4.runtime.CharStream;
|
|
|
|
import org.antlr.v4.runtime.CharStreams;
|
|
|
|
import org.antlr.v4.runtime.CommonTokenStream;
|
|
|
|
import org.antlr.v4.runtime.Token;
|
|
|
|
import org.antlr.v4.runtime.tree.ParseTree;
|
2024-05-08 13:14:58 +00:00
|
|
|
|
2024-05-09 13:12:08 +00:00
|
|
|
import java.io.CharArrayReader;
|
|
|
|
import java.nio.file.Files;
|
|
|
|
import java.nio.file.Paths;
|
2024-05-08 13:14:58 +00:00
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
public class Compiler {
|
|
|
|
|
2024-05-09 12:20:55 +00:00
|
|
|
public static void main(String[] args) throws Exception{
|
2024-05-08 13:14:58 +00:00
|
|
|
|
|
|
|
// get file
|
2024-05-09 13:12:08 +00:00
|
|
|
// String filePath = "EmptyClass.java";
|
|
|
|
// String content = new String(Files.readAllBytes(Paths.get(filePath)));
|
|
|
|
// CharArrayReader charStream = new CharArrayReader(content.toCharArray());
|
|
|
|
|
2024-05-09 14:49:53 +00:00
|
|
|
CharStream codeCharStream = CharStreams.fromString("class Example { int i; }");
|
2024-05-09 13:12:08 +00:00
|
|
|
DecafLexer lexer = new DecafLexer(codeCharStream);
|
|
|
|
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
|
|
|
|
|
|
|
System.out.println("--- print tokens ---");
|
|
|
|
tokens.fill();
|
|
|
|
|
|
|
|
List<Token> tokenList = tokens.getTokens();
|
|
|
|
|
|
|
|
StringBuilder stringBuilder = new StringBuilder();
|
|
|
|
for (Token token : tokenList) {
|
2024-05-09 14:49:53 +00:00
|
|
|
stringBuilder.append(token.getText()).append(" ");
|
2024-05-09 13:12:08 +00:00
|
|
|
}
|
|
|
|
String readableTokens = stringBuilder.toString().trim();
|
|
|
|
|
|
|
|
System.out.println(readableTokens);
|
|
|
|
|
|
|
|
|
|
|
|
DecafParser parser = new DecafParser(tokens);
|
|
|
|
|
|
|
|
ParseTree tree = parser.program();
|
|
|
|
|
|
|
|
System.out.println("--- print tree ---");
|
|
|
|
System.out.println(tree);
|
|
|
|
|
|
|
|
|
2024-05-08 13:14:58 +00:00
|
|
|
|
2024-05-09 12:28:32 +00:00
|
|
|
Program abstractSyntaxTree = new Program(new ArrayList<>());
|
2024-05-08 13:14:58 +00:00
|
|
|
|
|
|
|
abstractSyntaxTree.typeCheck();
|
|
|
|
|
|
|
|
abstractSyntaxTree.codeGen();
|
|
|
|
}
|
|
|
|
}
|