NichtHaskell/Source/Compiler.java

62 lines
1.8 KiB
Java
Raw Normal View History

2024-05-08 13:14:58 +00:00
import abstractSyntaxTree.Class.FieldDecl;
import abstractSyntaxTree.Class.MethodDecl;
import abstractSyntaxTree.Class.RefType;
import abstractSyntaxTree.Program;
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
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
// String filePath = "EmptyClass.java";
// String content = new String(Files.readAllBytes(Paths.get(filePath)));
// CharArrayReader charStream = new CharArrayReader(content.toCharArray());
CharStream codeCharStream = CharStreams.fromString("class Example { int i; }");
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) {
stringBuilder.append(token.getText()).append(" ");
}
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();
}
}