implemented new methods in Compiler

This commit is contained in:
JonathanFleischmann 2024-05-08 11:27:54 +02:00
parent 388fddd466
commit ce711b3a27
5 changed files with 59 additions and 14 deletions

View File

@ -3,9 +3,12 @@ package de.maishai;
import de.maishai.antlr.DecafLexer;
import de.maishai.antlr.DecafParser;
import de.maishai.ast.records.Class;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import de.maishai.typedast.CodeGenUtils;
import de.maishai.typedast.typedclass.TypedClass;
import org.antlr.v4.runtime.*;
import java.io.FileOutputStream;
import java.io.IOException;
/**
* Decaf language Compiler
@ -22,5 +25,45 @@ public class Compiler {
return ast;
}
public static TypedClass generateTypedASTFromAst(Class ast) {
TypedClass typedAST = new TypedClass();
typedAST.startConversion(ast);
return typedAST;
}
public static byte[] generateByteCodeArrayFromTypedAst(TypedClass typedAST) {
return typedAST.codeGen();
}
public static byte[] generateByteCodeArray(String fromSource) {
Class ast = generateAST(fromSource);
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:");
}
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);
return generateByteCodeArrayFromTypedAst(typedAST);
}
public static void generateByteCodeFileFromFile(String sourcePath, String classname) {
byte[] bytes = generateByteCodeArrayFromFile(sourcePath);
CodeGenUtils.writeClassfile(bytes, classname);
}
public static void main(String[] args) {
generateByteCodeFileFromFile("src/main/resources/JavaTestfiles/PublicClass.java", "OnlyClass");
}
}

View File

@ -97,7 +97,6 @@ public class AbstractSyntax_ClassWithConstructorWithParameters {
),
new Assignment(
new Id("innerRepetitions"),
AssignSign.SUB_ASSIGN,
new Binary(new Id("innerRepetitions"),
Operator.SUB,
new IntLiteral(1))
@ -109,9 +108,9 @@ public class AbstractSyntax_ClassWithConstructorWithParameters {
new Assignment(
new Id("repetitions"),
new Binary(new Id("repetitions"),
new Op)
Operator.SUB,
new IntLiteral(1)
)
)
)
)

View File

@ -56,14 +56,14 @@ public class TypedAbstractSyntax_ClassWithConstructor {
TypedAssignment typedAssignment = new TypedAssignment();
typedAssignment.setLoc(new TypedId("x"));
typedAssignment.setAssignSign(AssignSign.ASSIGN);
// typedAssignment.setAssignSign(AssignSign.ASSIGN);
typedAssignment.setValue(new TypedIntLiteral(10));
TypedFor typedFor = new TypedFor();
TypedAssignment typedAssignmentFor = new TypedAssignment();
typedAssignmentFor.setLoc(new TypedId("i"));
typedAssignmentFor.setAssignSign(AssignSign.ASSIGN);
// typedAssignmentFor.setAssignSign(AssignSign.ASSIGN);
typedAssignmentFor.setValue(new TypedIntLiteral(0));
// typedFor.setAssign(typedAssignmentFor);
@ -82,7 +82,7 @@ public class TypedAbstractSyntax_ClassWithConstructor {
TypedAssignment typedAssignmentForIncr = new TypedAssignment();
typedAssignmentForIncr.setLoc(new TypedId("i"));
typedAssignmentForIncr.setAssignSign(AssignSign.ASSIGN);
// typedAssignmentForIncr.setAssignSign(AssignSign.ASSIGN);
typedAssignmentForIncr.setValue(typedBinaryForIncr);
// typedFor.setInc(typedAssignmentForIncr);
@ -99,7 +99,7 @@ public class TypedAbstractSyntax_ClassWithConstructor {
TypedAssignment typedAssignmentInnerFor = new TypedAssignment();
typedAssignmentInnerFor.setLoc(new TypedId("j"));
typedAssignmentInnerFor.setAssignSign(AssignSign.ASSIGN);
// typedAssignmentInnerFor.setAssignSign(AssignSign.ASSIGN);
typedAssignmentInnerFor.setValue(new TypedIntLiteral(0));
// typedInnerFor.setAssign(typedAssignmentInnerFor);
@ -113,7 +113,7 @@ public class TypedAbstractSyntax_ClassWithConstructor {
TypedAssignment typedAssignmentInnerForIncr = new TypedAssignment();
typedAssignmentInnerForIncr.setLoc(new TypedId("j"));
typedAssignmentInnerForIncr.setAssignSign(AssignSign.ADD_ASSIGN);
// typedAssignmentInnerForIncr.setAssignSign(AssignSign.ADD_ASSIGN);
typedAssignmentInnerForIncr.setValue(new TypedIntLiteral(1));
// typedInnerFor.setInc(typedAssignmentInnerForIncr);
@ -123,7 +123,7 @@ public class TypedAbstractSyntax_ClassWithConstructor {
TypedAssignment typedAssignmentInnerForBlock = new TypedAssignment();
typedAssignmentInnerForBlock.setLoc(new TypedId("x"));
typedAssignmentInnerForBlock.setAssignSign(AssignSign.ASSIGN);
// typedAssignmentInnerForBlock.setAssignSign(AssignSign.ASSIGN);
TypedBinary typedBinaryInnerForBlock = new TypedBinary();
typedBinaryInnerForBlock.setLeft(new TypedId("x"));

View File

@ -0,0 +1,5 @@
public class E2ETests {
public static void main(String[] args) {
}
}

View File

@ -1,2 +0,0 @@
public class Test {
}