diff --git a/src/main/java/de/maishai/Compiler.java b/src/main/java/de/maishai/Compiler.java index bb9ff7c..043d1cf 100644 --- a/src/main/java/de/maishai/Compiler.java +++ b/src/main/java/de/maishai/Compiler.java @@ -3,11 +3,14 @@ package de.maishai; import de.maishai.antlr.DecafLexer; import de.maishai.antlr.DecafParser; import de.maishai.ast.records.Class; +import de.maishai.ast.records.Program; import de.maishai.typedast.CodeGenUtils; import de.maishai.typedast.typedclass.TypedClass; import org.antlr.v4.runtime.*; import java.io.IOException; +import java.util.ArrayList; +import java.util.List; /** * Decaf language Compiler @@ -34,27 +37,35 @@ public class Compiler { """); } */ - public static Class generateAST(String fromSource) { - CharStream input = CharStreams.fromString(fromSource); - DecafLexer lexer = new DecafLexer(input); - CommonTokenStream tokens = new CommonTokenStream(lexer); - DecafParser parser = new DecafParser(tokens); - DecafParser.ClassContext tree = parser.class_(); //Parsen - return ASTGenerator.generateAST(tree); - } - - public static Class generateASTFromFile(String sourcePath) { - ANTLRInputStream antlrInputStream; - try { - antlrInputStream = new ANTLRFileStream(sourcePath); - } catch (IOException e) { - System.out.println("Ungültiger Dateipfad D:"); - throw new RuntimeException("Ungültiger Dateipfad D:"); + public static Program generateAST(List fromSources) { + List classes = new ArrayList<>(); + for (String fromSource : fromSources) { + CharStream input = CharStreams.fromString(fromSource); + DecafLexer lexer = new DecafLexer(input); + CommonTokenStream tokens = new CommonTokenStream(lexer); + DecafParser parser = new DecafParser(tokens); + DecafParser.ClassContext tree = parser.class_(); //Parsen + classes.add(ASTGenerator.generateAST(tree)); } - return generateAST(antlrInputStream.toString()); + return new Program(classes); } - public static TypedClass generateTypedASTFromAst(Class ast) { + public static Program generateASTFromFile(List sourcePaths) { + List sources = new ArrayList<>(); + for (String sourcePath : sourcePaths) { + ANTLRInputStream antlrInputStream; + try { + antlrInputStream = new ANTLRFileStream(sourcePath); + } catch (IOException e) { + System.out.println("Ungültiger Dateipfad D:"); + throw new RuntimeException("Ungültiger Dateipfad D:"); + } + sources.add(antlrInputStream.toString()); + } + return generateAST(sources); + } + + public static TypedClass generateTypedASTFromAst(Program ast) { TypedClass typedAST = new TypedClass(); typedAST = (TypedClass) typedAST.startConversion(ast); return typedAST; @@ -64,35 +75,42 @@ public class Compiler { return typedAST.codeGen(); } - public static byte[] generateByteCodeArray(String fromSource) { - Class ast = generateAST(fromSource); + public static byte[] generateByteCodeArray(List fromSources) { + Program ast = generateAST(fromSources); TypedClass typedAST = generateTypedASTFromAst(ast); return generateByteCodeArrayFromTypedAst(typedAST); } - public static byte[] generateByteCodeArrayFromFile(String sourcePath) { - ANTLRInputStream antlrInputStream; - try { - antlrInputStream = new ANTLRFileStream(sourcePath); - } catch (IOException e) { - System.out.println("Ungültiger Dateipfad D:"); - throw new RuntimeException("Ungültiger Dateipfad D:"); + public static byte[] generateByteCodeArrayFromFile(List sourcePaths) { + List classes = new ArrayList<>(); + for (String sourcePath : sourcePaths) { + ANTLRInputStream antlrInputStream; + try { + antlrInputStream = new ANTLRFileStream(sourcePath); + } catch (IOException e) { + System.out.println("Ungültiger Dateipfad D:"); + throw new RuntimeException("Ungültiger Dateipfad D:"); + } + DecafLexer lexer = new DecafLexer(antlrInputStream); + CommonTokenStream tokens = new CommonTokenStream(lexer); + DecafParser parser = new DecafParser(tokens); + DecafParser.ClassContext tree = parser.class_(); //Parsen + Class ast = ASTGenerator.generateAST(tree); + classes.add(ast); } - DecafLexer lexer = new DecafLexer(antlrInputStream); - CommonTokenStream tokens = new CommonTokenStream(lexer); - DecafParser parser = new DecafParser(tokens); - DecafParser.ClassContext tree = parser.class_(); //Parsen - Class ast = ASTGenerator.generateAST(tree); - TypedClass typedAST = generateTypedASTFromAst(ast); + Program program = new Program(classes); + TypedClass typedAST = generateTypedASTFromAst(program); + //TODO: Für jede Klasse einzeln Bytecode generieren return generateByteCodeArrayFromTypedAst(typedAST); } - public static void generateByteCodeFileFromFile(String sourcePath, String classname) { + public static void generateByteCodeFileFromFile(List sourcePath, String classname) { byte[] bytes = generateByteCodeArrayFromFile(sourcePath); + //TODO: Für jede Klasse einzeln Bytecode schreiben CodeGenUtils.writeClassfile(bytes, classname); } public static void main(String[] args) { - generateByteCodeFileFromFile("src/main/resources/JavaTestfiles/ClassCanBeTyped.java", "ClassCanBeTyped"); + generateByteCodeFileFromFile(List.of("src/main/resources/JavaTestfiles/ClassCanBeTyped.java"), "ClassCanBeTyped"); } } diff --git a/src/main/java/de/maishai/ast/records/Node.java b/src/main/java/de/maishai/ast/records/Node.java index 2e3cf76..0d3e782 100644 --- a/src/main/java/de/maishai/ast/records/Node.java +++ b/src/main/java/de/maishai/ast/records/Node.java @@ -1,4 +1,4 @@ package de.maishai.ast.records; -public sealed interface Node permits Block, Class, Constructor, Expression, Declaration, Method, Parameter, Statement { +public sealed interface Node permits Block, Class, Constructor, Declaration, Expression, Method, Parameter, Program, Statement { } diff --git a/src/main/java/de/maishai/ast/records/Program.java b/src/main/java/de/maishai/ast/records/Program.java new file mode 100644 index 0000000..11d85b8 --- /dev/null +++ b/src/main/java/de/maishai/ast/records/Program.java @@ -0,0 +1,6 @@ +package de.maishai.ast.records; + +import java.util.List; + +public record Program(List classes) implements Node { +} diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java index 2e9fab6..31a2989 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java @@ -123,9 +123,9 @@ public class TypedClass implements TypedNode { return type; } - public TypedNode startConversion(Class c) { + public TypedNode startConversion(Program p) { Map local = new HashMap<>(); - + Class c = p.classes().get(0); return new TypedClass(c); }