NichtHaskell/Source/Compiler.java

65 lines
2.0 KiB
Java

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;
import java.io.CharArrayReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
public class Compiler {
public static void main(String[] args) throws Exception{
// 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 { }");
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.toString()).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);
Program abstractSyntaxTree = new Program(new ArrayList<>());
List<FieldDecl> emptyFieldDecl = new ArrayList<>();
List<MethodDecl> emptyMethodDecl = new ArrayList<>();
abstractSyntaxTree.classes.add(new RefType(emptyFieldDecl, emptyMethodDecl, null, null, "MyClass"));
abstractSyntaxTree.typeCheck();
abstractSyntaxTree.codeGen();
}
}