mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-28 15:58:03 +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.DecafLexer;
|
||||||
import de.maishai.antlr.DecafParser;
|
import de.maishai.antlr.DecafParser;
|
||||||
import de.maishai.ast.records.Class;
|
import de.maishai.ast.records.Class;
|
||||||
|
import de.maishai.ast.records.Program;
|
||||||
import de.maishai.typedast.CodeGenUtils;
|
import de.maishai.typedast.CodeGenUtils;
|
||||||
import de.maishai.typedast.typedclass.TypedClass;
|
import de.maishai.typedast.typedclass.TypedClass;
|
||||||
import org.antlr.v4.runtime.*;
|
import org.antlr.v4.runtime.*;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decaf language Compiler
|
* Decaf language Compiler
|
||||||
@ -34,16 +37,22 @@ public class Compiler {
|
|||||||
""");
|
""");
|
||||||
} */
|
} */
|
||||||
|
|
||||||
public static Class generateAST(String fromSource) {
|
public static Program generateAST(List<String> fromSources) {
|
||||||
|
List<Class> classes = new ArrayList<>();
|
||||||
|
for (String fromSource : fromSources) {
|
||||||
CharStream input = CharStreams.fromString(fromSource);
|
CharStream input = CharStreams.fromString(fromSource);
|
||||||
DecafLexer lexer = new DecafLexer(input);
|
DecafLexer lexer = new DecafLexer(input);
|
||||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||||
DecafParser parser = new DecafParser(tokens);
|
DecafParser parser = new DecafParser(tokens);
|
||||||
DecafParser.ClassContext tree = parser.class_(); //Parsen
|
DecafParser.ClassContext tree = parser.class_(); //Parsen
|
||||||
return ASTGenerator.generateAST(tree);
|
classes.add(ASTGenerator.generateAST(tree));
|
||||||
|
}
|
||||||
|
return new Program(classes);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static Class generateASTFromFile(String sourcePath) {
|
public static Program generateASTFromFile(List<String> sourcePaths) {
|
||||||
|
List<String> sources = new ArrayList<>();
|
||||||
|
for (String sourcePath : sourcePaths) {
|
||||||
ANTLRInputStream antlrInputStream;
|
ANTLRInputStream antlrInputStream;
|
||||||
try {
|
try {
|
||||||
antlrInputStream = new ANTLRFileStream(sourcePath);
|
antlrInputStream = new ANTLRFileStream(sourcePath);
|
||||||
@ -51,10 +60,12 @@ public class Compiler {
|
|||||||
System.out.println("Ungültiger Dateipfad D:");
|
System.out.println("Ungültiger Dateipfad D:");
|
||||||
throw new RuntimeException("Ungültiger Dateipfad D:");
|
throw new RuntimeException("Ungültiger Dateipfad D:");
|
||||||
}
|
}
|
||||||
return generateAST(antlrInputStream.toString());
|
sources.add(antlrInputStream.toString());
|
||||||
|
}
|
||||||
|
return generateAST(sources);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static TypedClass generateTypedASTFromAst(Class ast) {
|
public static TypedClass generateTypedASTFromAst(Program ast) {
|
||||||
TypedClass typedAST = new TypedClass();
|
TypedClass typedAST = new TypedClass();
|
||||||
typedAST = (TypedClass) typedAST.startConversion(ast);
|
typedAST = (TypedClass) typedAST.startConversion(ast);
|
||||||
return typedAST;
|
return typedAST;
|
||||||
@ -64,13 +75,15 @@ public class Compiler {
|
|||||||
return typedAST.codeGen();
|
return typedAST.codeGen();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] generateByteCodeArray(String fromSource) {
|
public static byte[] generateByteCodeArray(List<String> fromSources) {
|
||||||
Class ast = generateAST(fromSource);
|
Program ast = generateAST(fromSources);
|
||||||
TypedClass typedAST = generateTypedASTFromAst(ast);
|
TypedClass typedAST = generateTypedASTFromAst(ast);
|
||||||
return generateByteCodeArrayFromTypedAst(typedAST);
|
return generateByteCodeArrayFromTypedAst(typedAST);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static byte[] generateByteCodeArrayFromFile(String sourcePath) {
|
public static byte[] generateByteCodeArrayFromFile(List<String> sourcePaths) {
|
||||||
|
List<Class> classes = new ArrayList<>();
|
||||||
|
for (String sourcePath : sourcePaths) {
|
||||||
ANTLRInputStream antlrInputStream;
|
ANTLRInputStream antlrInputStream;
|
||||||
try {
|
try {
|
||||||
antlrInputStream = new ANTLRFileStream(sourcePath);
|
antlrInputStream = new ANTLRFileStream(sourcePath);
|
||||||
@ -83,16 +96,21 @@ public class Compiler {
|
|||||||
DecafParser parser = new DecafParser(tokens);
|
DecafParser parser = new DecafParser(tokens);
|
||||||
DecafParser.ClassContext tree = parser.class_(); //Parsen
|
DecafParser.ClassContext tree = parser.class_(); //Parsen
|
||||||
Class ast = ASTGenerator.generateAST(tree);
|
Class ast = ASTGenerator.generateAST(tree);
|
||||||
TypedClass typedAST = generateTypedASTFromAst(ast);
|
classes.add(ast);
|
||||||
|
}
|
||||||
|
Program program = new Program(classes);
|
||||||
|
TypedClass typedAST = generateTypedASTFromAst(program);
|
||||||
|
//TODO: Für jede Klasse einzeln Bytecode generieren
|
||||||
return generateByteCodeArrayFromTypedAst(typedAST);
|
return generateByteCodeArrayFromTypedAst(typedAST);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void generateByteCodeFileFromFile(String sourcePath, String classname) {
|
public static void generateByteCodeFileFromFile(List<String> sourcePath, String classname) {
|
||||||
byte[] bytes = generateByteCodeArrayFromFile(sourcePath);
|
byte[] bytes = generateByteCodeArrayFromFile(sourcePath);
|
||||||
|
//TODO: Für jede Klasse einzeln Bytecode schreiben
|
||||||
CodeGenUtils.writeClassfile(bytes, classname);
|
CodeGenUtils.writeClassfile(bytes, classname);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
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;
|
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;
|
return type;
|
||||||
}
|
}
|
||||||
|
|
||||||
public TypedNode startConversion(Class c) {
|
public TypedNode startConversion(Program p) {
|
||||||
Map<String, Type> local = new HashMap<>();
|
Map<String, Type> local = new HashMap<>();
|
||||||
|
Class c = p.classes().get(0);
|
||||||
return new TypedClass(c);
|
return new TypedClass(c);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user