mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-28 16:28: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.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,27 +37,35 @@ public class Compiler {
|
|||||||
""");
|
""");
|
||||||
} */
|
} */
|
||||||
|
|
||||||
public static Class generateAST(String fromSource) {
|
public static Program generateAST(List<String> fromSources) {
|
||||||
CharStream input = CharStreams.fromString(fromSource);
|
List<Class> classes = new ArrayList<>();
|
||||||
DecafLexer lexer = new DecafLexer(input);
|
for (String fromSource : fromSources) {
|
||||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
CharStream input = CharStreams.fromString(fromSource);
|
||||||
DecafParser parser = new DecafParser(tokens);
|
DecafLexer lexer = new DecafLexer(input);
|
||||||
DecafParser.ClassContext tree = parser.class_(); //Parsen
|
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
||||||
return ASTGenerator.generateAST(tree);
|
DecafParser parser = new DecafParser(tokens);
|
||||||
}
|
DecafParser.ClassContext tree = parser.class_(); //Parsen
|
||||||
|
classes.add(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:");
|
|
||||||
}
|
}
|
||||||
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();
|
TypedClass typedAST = new TypedClass();
|
||||||
typedAST = (TypedClass) typedAST.startConversion(ast);
|
typedAST = (TypedClass) typedAST.startConversion(ast);
|
||||||
return typedAST;
|
return typedAST;
|
||||||
@ -64,35 +75,42 @@ 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) {
|
||||||
ANTLRInputStream antlrInputStream;
|
List<Class> classes = new ArrayList<>();
|
||||||
try {
|
for (String sourcePath : sourcePaths) {
|
||||||
antlrInputStream = new ANTLRFileStream(sourcePath);
|
ANTLRInputStream antlrInputStream;
|
||||||
} catch (IOException e) {
|
try {
|
||||||
System.out.println("Ungültiger Dateipfad D:");
|
antlrInputStream = new ANTLRFileStream(sourcePath);
|
||||||
throw new RuntimeException("Ungültiger Dateipfad D:");
|
} 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);
|
Program program = new Program(classes);
|
||||||
CommonTokenStream tokens = new CommonTokenStream(lexer);
|
TypedClass typedAST = generateTypedASTFromAst(program);
|
||||||
DecafParser parser = new DecafParser(tokens);
|
//TODO: Für jede Klasse einzeln Bytecode generieren
|
||||||
DecafParser.ClassContext tree = parser.class_(); //Parsen
|
|
||||||
Class ast = ASTGenerator.generateAST(tree);
|
|
||||||
TypedClass typedAST = generateTypedASTFromAst(ast);
|
|
||||||
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