mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-12-28 16:48:03 +00:00
implemented new methods in Compiler
This commit is contained in:
parent
388fddd466
commit
ce711b3a27
@ -3,9 +3,12 @@ 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 org.antlr.v4.runtime.CharStream;
|
import de.maishai.typedast.CodeGenUtils;
|
||||||
import org.antlr.v4.runtime.CharStreams;
|
import de.maishai.typedast.typedclass.TypedClass;
|
||||||
import org.antlr.v4.runtime.CommonTokenStream;
|
import org.antlr.v4.runtime.*;
|
||||||
|
|
||||||
|
import java.io.FileOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Decaf language Compiler
|
* Decaf language Compiler
|
||||||
@ -22,5 +25,45 @@ public class Compiler {
|
|||||||
return ast;
|
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");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -97,7 +97,6 @@ public class AbstractSyntax_ClassWithConstructorWithParameters {
|
|||||||
),
|
),
|
||||||
new Assignment(
|
new Assignment(
|
||||||
new Id("innerRepetitions"),
|
new Id("innerRepetitions"),
|
||||||
AssignSign.SUB_ASSIGN,
|
|
||||||
new Binary(new Id("innerRepetitions"),
|
new Binary(new Id("innerRepetitions"),
|
||||||
Operator.SUB,
|
Operator.SUB,
|
||||||
new IntLiteral(1))
|
new IntLiteral(1))
|
||||||
@ -109,9 +108,9 @@ public class AbstractSyntax_ClassWithConstructorWithParameters {
|
|||||||
new Assignment(
|
new Assignment(
|
||||||
new Id("repetitions"),
|
new Id("repetitions"),
|
||||||
new Binary(new Id("repetitions"),
|
new Binary(new Id("repetitions"),
|
||||||
new Op)
|
Operator.SUB,
|
||||||
new IntLiteral(1)
|
new IntLiteral(1)
|
||||||
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -56,14 +56,14 @@ public class TypedAbstractSyntax_ClassWithConstructor {
|
|||||||
|
|
||||||
TypedAssignment typedAssignment = new TypedAssignment();
|
TypedAssignment typedAssignment = new TypedAssignment();
|
||||||
typedAssignment.setLoc(new TypedId("x"));
|
typedAssignment.setLoc(new TypedId("x"));
|
||||||
typedAssignment.setAssignSign(AssignSign.ASSIGN);
|
// typedAssignment.setAssignSign(AssignSign.ASSIGN);
|
||||||
typedAssignment.setValue(new TypedIntLiteral(10));
|
typedAssignment.setValue(new TypedIntLiteral(10));
|
||||||
|
|
||||||
TypedFor typedFor = new TypedFor();
|
TypedFor typedFor = new TypedFor();
|
||||||
|
|
||||||
TypedAssignment typedAssignmentFor = new TypedAssignment();
|
TypedAssignment typedAssignmentFor = new TypedAssignment();
|
||||||
typedAssignmentFor.setLoc(new TypedId("i"));
|
typedAssignmentFor.setLoc(new TypedId("i"));
|
||||||
typedAssignmentFor.setAssignSign(AssignSign.ASSIGN);
|
// typedAssignmentFor.setAssignSign(AssignSign.ASSIGN);
|
||||||
typedAssignmentFor.setValue(new TypedIntLiteral(0));
|
typedAssignmentFor.setValue(new TypedIntLiteral(0));
|
||||||
|
|
||||||
// typedFor.setAssign(typedAssignmentFor);
|
// typedFor.setAssign(typedAssignmentFor);
|
||||||
@ -82,7 +82,7 @@ public class TypedAbstractSyntax_ClassWithConstructor {
|
|||||||
|
|
||||||
TypedAssignment typedAssignmentForIncr = new TypedAssignment();
|
TypedAssignment typedAssignmentForIncr = new TypedAssignment();
|
||||||
typedAssignmentForIncr.setLoc(new TypedId("i"));
|
typedAssignmentForIncr.setLoc(new TypedId("i"));
|
||||||
typedAssignmentForIncr.setAssignSign(AssignSign.ASSIGN);
|
// typedAssignmentForIncr.setAssignSign(AssignSign.ASSIGN);
|
||||||
typedAssignmentForIncr.setValue(typedBinaryForIncr);
|
typedAssignmentForIncr.setValue(typedBinaryForIncr);
|
||||||
|
|
||||||
// typedFor.setInc(typedAssignmentForIncr);
|
// typedFor.setInc(typedAssignmentForIncr);
|
||||||
@ -99,7 +99,7 @@ public class TypedAbstractSyntax_ClassWithConstructor {
|
|||||||
|
|
||||||
TypedAssignment typedAssignmentInnerFor = new TypedAssignment();
|
TypedAssignment typedAssignmentInnerFor = new TypedAssignment();
|
||||||
typedAssignmentInnerFor.setLoc(new TypedId("j"));
|
typedAssignmentInnerFor.setLoc(new TypedId("j"));
|
||||||
typedAssignmentInnerFor.setAssignSign(AssignSign.ASSIGN);
|
// typedAssignmentInnerFor.setAssignSign(AssignSign.ASSIGN);
|
||||||
typedAssignmentInnerFor.setValue(new TypedIntLiteral(0));
|
typedAssignmentInnerFor.setValue(new TypedIntLiteral(0));
|
||||||
|
|
||||||
// typedInnerFor.setAssign(typedAssignmentInnerFor);
|
// typedInnerFor.setAssign(typedAssignmentInnerFor);
|
||||||
@ -113,7 +113,7 @@ public class TypedAbstractSyntax_ClassWithConstructor {
|
|||||||
|
|
||||||
TypedAssignment typedAssignmentInnerForIncr = new TypedAssignment();
|
TypedAssignment typedAssignmentInnerForIncr = new TypedAssignment();
|
||||||
typedAssignmentInnerForIncr.setLoc(new TypedId("j"));
|
typedAssignmentInnerForIncr.setLoc(new TypedId("j"));
|
||||||
typedAssignmentInnerForIncr.setAssignSign(AssignSign.ADD_ASSIGN);
|
// typedAssignmentInnerForIncr.setAssignSign(AssignSign.ADD_ASSIGN);
|
||||||
typedAssignmentInnerForIncr.setValue(new TypedIntLiteral(1));
|
typedAssignmentInnerForIncr.setValue(new TypedIntLiteral(1));
|
||||||
|
|
||||||
// typedInnerFor.setInc(typedAssignmentInnerForIncr);
|
// typedInnerFor.setInc(typedAssignmentInnerForIncr);
|
||||||
@ -123,7 +123,7 @@ public class TypedAbstractSyntax_ClassWithConstructor {
|
|||||||
|
|
||||||
TypedAssignment typedAssignmentInnerForBlock = new TypedAssignment();
|
TypedAssignment typedAssignmentInnerForBlock = new TypedAssignment();
|
||||||
typedAssignmentInnerForBlock.setLoc(new TypedId("x"));
|
typedAssignmentInnerForBlock.setLoc(new TypedId("x"));
|
||||||
typedAssignmentInnerForBlock.setAssignSign(AssignSign.ASSIGN);
|
// typedAssignmentInnerForBlock.setAssignSign(AssignSign.ASSIGN);
|
||||||
|
|
||||||
TypedBinary typedBinaryInnerForBlock = new TypedBinary();
|
TypedBinary typedBinaryInnerForBlock = new TypedBinary();
|
||||||
typedBinaryInnerForBlock.setLeft(new TypedId("x"));
|
typedBinaryInnerForBlock.setLeft(new TypedId("x"));
|
||||||
|
5
src/test/java/E2ETests.java
Normal file
5
src/test/java/E2ETests.java
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
public class E2ETests {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
@ -1,2 +0,0 @@
|
|||||||
public class Test {
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user