mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-27 09:08:04 +00:00
Start Multiple Classes
This commit is contained in:
parent
71560cf163
commit
d99dbd4f64
@ -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<String> fromSources) {
|
||||
List<Class> 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<String> sourcePaths) {
|
||||
List<String> 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<String> 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<String> sourcePaths) {
|
||||
List<Class> 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<String> 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");
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
}
|
||||
|
6
src/main/java/de/maishai/ast/records/Program.java
Normal file
6
src/main/java/de/maishai/ast/records/Program.java
Normal file
@ -0,0 +1,6 @@
|
||||
package de.maishai.ast.records;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public record Program(List<Class> classes) implements Node {
|
||||
}
|
@ -123,9 +123,9 @@ public class TypedClass implements TypedNode {
|
||||
return type;
|
||||
}
|
||||
|
||||
public TypedNode startConversion(Class c) {
|
||||
public TypedNode startConversion(Program p) {
|
||||
Map<String, Type> local = new HashMap<>();
|
||||
|
||||
Class c = p.classes().get(0);
|
||||
return new TypedClass(c);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user