From 9730455b50ab1a3d2d16f9dce89ecc2f9fa05646 Mon Sep 17 00:00:00 2001 From: JonathanFleischmann Date: Wed, 8 May 2024 11:11:51 +0200 Subject: [PATCH 1/3] adapted testfiles to new ast and commented code with issues out --- .../AbstractSyntax_ClassWithConstructor.java | 1 - ...tax_ClassWithConstructorAndMethodCall.java | 2 - ...ax_ClassWithConstructorWithParameters.java | 1 - .../AbstractSyntax_ClassWithField.java | 1 - .../AbstractSyntax_ClassWithMethod.java | 2 - ...bstractSyntax_ClassWithMethodAndField.java | 2 - ...tax_ClassWithMoreComplexMethodAndMain.java | 2 - .../AbstractSyntax_OnlyClass.java | 1 - .../AbstractSyntax_PublicClass.java | 1 - ...edAbstractSyntax_ClassWithConstructor.java | 154 ++++++++++++++++++ ...tax_ClassWithConstructorAndMethodCall.java | 134 +++++++++++++++ ...ax_ClassWithConstructorWithParameters.java | 130 +++++++++++++++ .../TypedAbstractSyntax_ClassWithField.java | 26 +++ .../TypedAbstractSyntax_ClassWithMethod.java | 46 ++++++ ...bstractSyntax_ClassWithMethodAndField.java | 86 ++++++++++ ...tax_ClassWithMoreComplexMethodAndMain.java | 137 ++++++++++++++++ .../TypedAbstractSyntax_OnlyClass.java | 19 +++ .../TypedAbstractSyntax_PublicClass.java | 19 +++ 18 files changed, 751 insertions(+), 13 deletions(-) create mode 100644 src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java create mode 100644 src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructorAndMethodCall.java create mode 100644 src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructorWithParameters.java create mode 100644 src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithField.java create mode 100644 src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithMethod.java create mode 100644 src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithMethodAndField.java create mode 100644 src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithMoreComplexMethodAndMain.java create mode 100644 src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_OnlyClass.java create mode 100644 src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_PublicClass.java diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructor.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructor.java index 692c764..849f9a8 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructor.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructor.java @@ -29,7 +29,6 @@ public class AbstractSyntax_ClassWithConstructor { List methods = List.of(); List constructors = getConstructors(); return new Class( - true, new Id("ClassWithConstructor"), fields, methods, diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorAndMethodCall.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorAndMethodCall.java index b16abb9..c2159ce 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorAndMethodCall.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorAndMethodCall.java @@ -38,7 +38,6 @@ public class AbstractSyntax_ClassWithConstructorAndMethodCall { List methodList = getMethods(); List constructorList = getConstructors(); return new Class( - true, new Id("ClassWithConstructorAndMethodCall"), fieldList, methodList, @@ -98,7 +97,6 @@ public class AbstractSyntax_ClassWithConstructorAndMethodCall { private static Method getMethod1() { return new Method( - true, ReturnType.BOOL, new Id("methodCall"), List.of(), diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java index 70e7fb0..50b959a 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java @@ -31,7 +31,6 @@ public class AbstractSyntax_ClassWithConstructorWithParameters { ); List constructors = getConstructors(); return new Class( - true, new Id("ClassWithConstructorWithParameters"), fields, List.of(), diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithField.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithField.java index a4ec849..7b763b1 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithField.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithField.java @@ -22,7 +22,6 @@ public class AbstractSyntax_ClassWithField { List methods = List.of(); List constructors = List.of(); return new Class( - true, new Id("ClassWithAssignment"), fields, methods, diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethod.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethod.java index 1466e08..c074149 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethod.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethod.java @@ -14,7 +14,6 @@ public class AbstractSyntax_ClassWithMethod { public static Class get() { List methods = getMethods(); return new Class( - true, new Id("ClassWithMethod"), List.of(), methods, @@ -29,7 +28,6 @@ public class AbstractSyntax_ClassWithMethod { private static Method getMethod1() { return new Method( - true, ReturnType.BOOL, new Id("method"), List.of(), diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethodAndField.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethodAndField.java index bd024fc..bdb4baf 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethodAndField.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMethodAndField.java @@ -27,7 +27,6 @@ class AbstractSyntax_ClassWithMethodAndField { List methods = getMethods(); List constructors = getConstructors(); return new Class( - false, new Id("ClassWithMethodAndField"), fields, methods, @@ -69,7 +68,6 @@ class AbstractSyntax_ClassWithMethodAndField { private static Method getMethod1() { return new Method( - true, ReturnType.BOOL, new Id("method"), List.of(), diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMoreComplexMethodAndMain.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMoreComplexMethodAndMain.java index 467e24d..5dd1db1 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMoreComplexMethodAndMain.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithMoreComplexMethodAndMain.java @@ -37,7 +37,6 @@ public class AbstractSyntax_ClassWithMoreComplexMethodAndMain { List methods = getMethods(); List constructors = List.of(); return new Class( - true, new Id("ClassWithMoreComplexMethodAndMain"), fields, methods, @@ -104,7 +103,6 @@ public class AbstractSyntax_ClassWithMoreComplexMethodAndMain { ) ); return new Method( - true, ReturnType.BOOL, new Id("moreComplexMethod"), List.of(), diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_OnlyClass.java b/src/main/resources/AbstractSyntax/AbstractSyntax_OnlyClass.java index ef0a552..2af596b 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_OnlyClass.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_OnlyClass.java @@ -9,7 +9,6 @@ import java.util.List; public class AbstractSyntax_OnlyClass { public static Class get() { return new Class( - false, new Id("OnlyClass"), List.of(), List.of(), diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_PublicClass.java b/src/main/resources/AbstractSyntax/AbstractSyntax_PublicClass.java index 820aaea..8f8165c 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_PublicClass.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_PublicClass.java @@ -9,7 +9,6 @@ import java.util.List; public class AbstractSyntax_PublicClass { public static Class get() { return new Class( - true, new Id("PublicClass"), List.of(), List.of(), diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java new file mode 100644 index 0000000..8563ee8 --- /dev/null +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java @@ -0,0 +1,154 @@ +//public class ClassWithConstructor { +// int x; +// public classWithConstructor() { +// this.x = 10; +// int i; +// for (i = 0; i < 6; i = i + 1) { +// int j; +// for (j = 0; j < this.x; j += 1) { +// this.x = this.x * this.x; +// } +// } +// } +//} + +import de.maishai.ast.AssignSign; +import de.maishai.ast.Operator; +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.*; + +import java.util.List; + +public class TypedAbstractSyntax_ClassWithConstructor { + public static TypedClass get() { + TypedClass typedClass = new TypedClass(); + typedClass.setIsPublic(true); + typedClass.setTypedId(new TypedId("ClassWithConstructor")); + + TypedField typedField = new TypedField(); + typedField.setTypedId(new TypedId("x")); + typedField.setType(Type.INT); + + typedClass.setTypedFields(List.of(typedField)); + typedClass.setTypedMethods(List.of()); + typedClass.setTypedMainMethod(null); + typedClass.setTypedConstructors(getConstructors()); + return typedClass; + } + + private static List getConstructors() { + return List.of(getConstructor1()); + } + + private static TypedConstructor getConstructor1() { + TypedConstructor typedConstructor = new TypedConstructor(); + typedConstructor.setIsPublic(true); + typedConstructor.setTypedId(new TypedId("ClassWithConstructor")); + typedConstructor.setTypedParameters(List.of()); + + TypedBlock typedBlock = new TypedBlock(); + + TypedLocalVariable typedLocalVariable = new TypedLocalVariable(); + typedLocalVariable.setTypedId(new TypedId("i")); + typedLocalVariable.setType(Type.INT); + + typedBlock.setVars(List.of(typedLocalVariable)); + + TypedAssignment typedAssignment = new TypedAssignment(); + typedAssignment.setLoc(new TypedId("x")); + 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.setValue(new TypedIntLiteral(0)); + +// typedFor.setAssign(typedAssignmentFor); + + TypedBinary typedBinaryFor = new TypedBinary(); + typedBinaryFor.setLeft(new TypedId("i")); + typedBinaryFor.setOp(Operator.LT); + typedBinaryFor.setRight(new TypedIntLiteral(6)); + + typedFor.setCond(typedBinaryFor); + + TypedBinary typedBinaryForIncr = new TypedBinary(); + typedBinaryForIncr.setLeft(new TypedId("i")); + typedBinaryForIncr.setOp(Operator.ADD); + typedBinaryForIncr.setRight(new TypedIntLiteral(1)); + + TypedAssignment typedAssignmentForIncr = new TypedAssignment(); + typedAssignmentForIncr.setLoc(new TypedId("i")); + typedAssignmentForIncr.setAssignSign(AssignSign.ASSIGN); + typedAssignmentForIncr.setValue(typedBinaryForIncr); + +// typedFor.setInc(typedAssignmentForIncr); + + TypedBlock typedBlockFor = new TypedBlock(); + + TypedLocalVariable typedLocalVariableFor = new TypedLocalVariable(); + typedLocalVariableFor.setTypedId(new TypedId("j")); + typedLocalVariableFor.setType(Type.INT); + + typedBlockFor.setVars(List.of(typedLocalVariableFor)); + + TypedFor typedInnerFor = new TypedFor(); + + TypedAssignment typedAssignmentInnerFor = new TypedAssignment(); + typedAssignmentInnerFor.setLoc(new TypedId("j")); + typedAssignmentInnerFor.setAssignSign(AssignSign.ASSIGN); + typedAssignmentInnerFor.setValue(new TypedIntLiteral(0)); + +// typedInnerFor.setAssign(typedAssignmentInnerFor); + + TypedBinary typedBinaryInnerFor = new TypedBinary(); + typedBinaryInnerFor.setLeft(new TypedId("j")); + typedBinaryInnerFor.setOp(Operator.LT); + typedBinaryInnerFor.setRight(new TypedId("x")); + + typedInnerFor.setCond(typedBinaryInnerFor); + + TypedAssignment typedAssignmentInnerForIncr = new TypedAssignment(); + typedAssignmentInnerForIncr.setLoc(new TypedId("j")); + typedAssignmentInnerForIncr.setAssignSign(AssignSign.ADD_ASSIGN); + typedAssignmentInnerForIncr.setValue(new TypedIntLiteral(1)); + +// typedInnerFor.setInc(typedAssignmentInnerForIncr); + + TypedBlock typedBlockInnerFor = new TypedBlock(); + typedBlockInnerFor.setVars(List.of()); + + TypedAssignment typedAssignmentInnerForBlock = new TypedAssignment(); + typedAssignmentInnerForBlock.setLoc(new TypedId("x")); + typedAssignmentInnerForBlock.setAssignSign(AssignSign.ASSIGN); + + TypedBinary typedBinaryInnerForBlock = new TypedBinary(); + typedBinaryInnerForBlock.setLeft(new TypedId("x")); + typedBinaryInnerForBlock.setOp(Operator.MUL); + typedBinaryInnerForBlock.setRight(new TypedId("x")); + + typedAssignmentInnerForBlock.setValue(typedBinaryInnerForBlock); + + typedBlockInnerFor.setStmts(List.of(typedAssignmentInnerForBlock)); + + typedInnerFor.setTypedBlock(typedBlockInnerFor); + + typedBlockFor.setStmts(List.of(typedInnerFor)); + + typedFor.setTypedBlock(typedBlockFor); + + typedBlock.setStmts( + List.of( + typedAssignment, + typedFor + ) + ); + + typedConstructor.setTypedBlock(typedBlock); + + return typedConstructor; + } +} \ No newline at end of file diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructorAndMethodCall.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructorAndMethodCall.java new file mode 100644 index 0000000..9d20841 --- /dev/null +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructorAndMethodCall.java @@ -0,0 +1,134 @@ +//public class ClassWithConstructorAndMethodCall { +// int x; +// +// public ClassWithConstructorAndMethodCall() { +// this.x = 10; +// while (methodCall()) { +// this.x = this.x * this.x; +// } +// } +// +// public boolean methodCall() { +// if (this.x > 100) { +// return false; +// } else { +// return true; +// } +// } +//} + + +import de.maishai.ast.AssignSign; +import de.maishai.ast.Operator; +import de.maishai.ast.ReturnType; +import de.maishai.ast.Type; +import de.maishai.ast.records.*; +import de.maishai.ast.records.Class; + +import java.util.List; + +public class TypedAbstractSyntax_ClassWithConstructorAndMethodCall { +// public static Class get() { +// List fieldList = List.of( +// new Field( +// new Id("x"), +// Type.INT +// ) +// ); +// List methodList = getMethods(); +// List constructorList = getConstructors(); +// return new Class( +// new Id("ClassWithConstructorAndMethodCall"), +// fieldList, +// methodList, +// null, +// constructorList +// ); +// } +// +// private static List getConstructors() { +// return List.of(getConstructor1()); +// } +// +// private static Constructor getConstructor1() { +// Block block = new Block( +// List.of(), +// List.of( +// new Assignment( +// new Id("x"), +// AssignSign.ASSIGN, +// new IntLiteral(10) +// ), +// new While( +// new MethodCall( +// false, +// null, +// new Id("methodCall"), +// List.of() +// ), +// new Block( +// List.of(), +// List.of( +// new Assignment( +// new Id("x"), +// AssignSign.ASSIGN, +// new Binary( +// new Id("x"), +// Operator.MUL, +// new Id("x") +// ) +// ) +// ) +// ) +// ) +// ) +// ); +// return new Constructor( +// true, +// new Id("ClassWithConstructorAndMethodCall"), +// List.of(), +// block +// ); +// } +// +// private static List getMethods() { +// return List.of(getMethod1()); +// } +// +// private static Method getMethod1() { +// return new Method( +// true, +// ReturnType.BOOL, +// new Id("methodCall"), +// List.of(), +// new Block( +// List.of(), +// List.of( +// new IfElse( +// new Binary( +// new Id("x"), +// Operator.GT, +// new IntLiteral(100) +// ), +// new Block( +// List.of(), +// List.of( +// new Return( +// new BoolLiteral(false) +// ) +// ) +// ), +// new Block( +// List.of(), +// List.of( +// new Return( +// new BoolLiteral(true) +// ) +// ) +// ) +// ) +// ) +// ) +// ); +// } +} \ No newline at end of file diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructorWithParameters.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructorWithParameters.java new file mode 100644 index 0000000..5f073d5 --- /dev/null +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructorWithParameters.java @@ -0,0 +1,130 @@ +//public class ClassWithConstructorWithParameters { +// int x; +// public classWithConstructorWithParameters(int startValue, int repetitions) { +// this.x = startValue; +// while (repetitions > 0) { +// int innerRepetitions; +// innerRepetitions = this.x; +// while (innerRepetitions > 0) { +// this.x = this.x * this.x; +// innerRepetitions -= 1; +// } +// repetitions -= 1; +// } +// } +//} +import de.maishai.ast.AssignSign; +import de.maishai.ast.Operator; +import de.maishai.ast.records.*; +import de.maishai.ast.Type; +import de.maishai.ast.records.Class; + +import java.util.List; + +public class TypedAbstractSyntax_ClassWithConstructorWithParameters { +// public static Class get() { +// List fields = List.of( +// new Field( +// new Id("x"), +// Type.INT +// ) +// ); +// List constructors = getConstructors(); +// return new Class( +// true, +// new Id("ClassWithConstructorWithParameters"), +// fields, +// List.of(), +// null, +// constructors +// ); +// } +// +// private static List getConstructors() { +// return List.of(getConstructor1()); +// } +// +// private static Constructor getConstructor1() { +// List parameters = List.of( +// new Parameter( +// "startValue", +// Type.INT +// ), +// new Parameter( +// "repetitions", +// Type.INT +// ) +// ); +// Block block = new Block( +// List.of(), +// List.of( +// new Assignment( +// new Id("x"), +// AssignSign.ASSIGN, +// new Id("startValue") +// ), +// new While( +// new Binary( +// new Id("repetitions"), +// Operator.GT, +// new IntLiteral(0) +// ), +// new Block( +// List.of( +// new LocalVariable( +// new Id("innerRepetitions"), +// Type.INT +// ) +// ), +// List.of( +// new Assignment( +// new Id("innerRepetitions"), +// AssignSign.ASSIGN, +// new Id("x") +// ), +// new While( +// new Binary( +// new Id("repetitions"), +// Operator.GT, +// new IntLiteral(0) +// ), +// new Block( +// List.of(), +// List.of( +// new Assignment( +// new Id("x"), +// AssignSign.ASSIGN, +// new Binary( +// new Id("x"), +// Operator.MUL, +// new Id("x") +// ) +// ), +// new Assignment( +// new Id("innerRepetitions"), +// AssignSign.SUB_ASSIGN, +// new IntLiteral(1) +// ) +// ) +// ) +// ), +// new Assignment( +// new Id("repetitions"), +// AssignSign.SUB_ASSIGN, +// new IntLiteral(1) +// +// ) +// ) +// ) +// ) +// ) +// ); +// return new Constructor( +// true, +// new Id("classWithConstructorWithParameters"), +// parameters, +// block +// ); +// } + +} \ No newline at end of file diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithField.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithField.java new file mode 100644 index 0000000..30cffbf --- /dev/null +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithField.java @@ -0,0 +1,26 @@ +//public class ClassWithField { +// int x; +//} + + +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.*; + +import java.util.List; + +public class TypedAbstractSyntax_ClassWithField { + public static TypedClass get() { + TypedClass typedClass = new TypedClass(); + typedClass.setIsPublic(true); + typedClass.setTypedId(new TypedId("ClassWithField")); + typedClass.setTypedFields( + List.of( + new TypedField(new TypedId("x"), Type.INT) + ) + ); + typedClass.setTypedMethods(List.of()); + typedClass.setTypedMainMethod(null); + typedClass.setTypedConstructors(List.of()); + return typedClass; + } +} \ No newline at end of file diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithMethod.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithMethod.java new file mode 100644 index 0000000..9129647 --- /dev/null +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithMethod.java @@ -0,0 +1,46 @@ +//public class ClassWithMethod { +// public boolean method() { +// return false; +// } +//} + +import de.maishai.typedast.Type; +import de.maishai.typedast.typedclass.*; + +import java.util.List; + +public class TypedAbstractSyntax_ClassWithMethod { + public static TypedClass get() { + TypedClass typedClass = new TypedClass(); + typedClass.setIsPublic(true); + typedClass.setTypedId(new TypedId("ClassWithMethod")); + typedClass.setTypedFields(List.of()); + typedClass.setTypedMethods(getMethods()); + return typedClass; + } + + private static List getMethods() { + return List.of(getMethod1()); + } + + private static TypedMethod getMethod1() { + TypedMethod typedMethod = new TypedMethod(); + typedMethod.setIsPublic(true); + typedMethod.setTypedId(new TypedId("method")); + typedMethod.setReturnType(Type.BOOL); + typedMethod.setTypedParameters(List.of()); + + TypedBoolLiteral typedBoolLiteral = new TypedBoolLiteral(); + typedBoolLiteral.setValue(false); + + TypedReturn typedReturn = new TypedReturn(); + typedReturn.setRet(typedBoolLiteral); + + TypedBlock typedBlock = new TypedBlock(); + typedBlock.setVars(List.of()); + typedBlock.setStmts(List.of(typedReturn)); + + typedMethod.setTypedBlock(typedBlock); + return typedMethod; + } +} \ No newline at end of file diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithMethodAndField.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithMethodAndField.java new file mode 100644 index 0000000..d74f623 --- /dev/null +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithMethodAndField.java @@ -0,0 +1,86 @@ +//public class ClassWithMethodAndField { +// char c; +// +// public ClassWithMethodAndField(char character) { +// this.c = character; +// } +// +// public boolean method() { +// return true; +// } +//} + +import de.maishai.ast.*; +import de.maishai.ast.records.*; +import de.maishai.ast.records.Class; + +import java.util.List; + +class TypedAbstractSyntax_ClassWithMethodAndField { +// public static Class get() { +// List fields = List.of( +// new Field( +// new Id("c"), +// Type.CHAR +// ) +// ); +// List methods = getMethods(); +// List constructors = getConstructors(); +// return new Class( +// false, +// new Id("ClassWithMethodAndField"), +// fields, +// methods, +// null, +// constructors +// ); +// } +// +// private static List getConstructors() { +// return List.of(getConstructor1()); +// } +// +// private static Constructor getConstructor1() { +// return new Constructor( +// true, +// new Id("ClassWithMethodAndField"), +// List.of( +// new Parameter( +// "character", +// Type.CHAR +// ) +// ), +// new Block( +// List.of(), +// List.of( +// new Assignment( +// new Id("c"), +// AssignSign.ASSIGN, +// new Id("character") +// ) +// ) +// ) +// ); +// } +// +// private static List getMethods() { +// return List.of(getMethod1()); +// } +// +// private static Method getMethod1() { +// return new Method( +// true, +// ReturnType.BOOL, +// new Id("method"), +// List.of(), +// new Block( +// List.of(), +// List.of( +// new Return( +// new BoolLiteral(true) +// ) +// ) +// ) +// ); +// } +} \ No newline at end of file diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithMoreComplexMethodAndMain.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithMoreComplexMethodAndMain.java new file mode 100644 index 0000000..d3682fd --- /dev/null +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithMoreComplexMethodAndMain.java @@ -0,0 +1,137 @@ +//public class ClassWithMoreComplexMethodAndMain { +// ClassWithMoreComplexMethodAndMain instance; +// +// public boolean moreComplexMethod() { +// int i; +// i = 11; +// boolean returnValue; +// returnValue = false; +// while (i > 0) { +// i -= 1; +// returnValue = !returnValue; +// } +// return returnValue; +// } +// +// public static void main(String[] args) { +// instance = new ClassWithMoreComplexMethodAndMain() +// } +//} + +import de.maishai.ast.*; +import de.maishai.ast.records.*; +import de.maishai.ast.records.Class; + +import java.util.List; + +public class TypedAbstractSyntax_ClassWithMoreComplexMethodAndMain { +// public static Class get() { +// Type TypeClassWithMoreComplexMethodAndMain = Type.OBJECT; +// TypeClassWithMoreComplexMethodAndMain.setObjectType(new Id("ClassWithMoreComplexMethodAndMain")); +// List fields = List.of( +// new Field( +// new Id("instance"), +// TypeClassWithMoreComplexMethodAndMain +// ) +// ); +// List methods = getMethods(); +// List constructors = List.of(); +// return new Class( +// true, +// new Id("ClassWithMoreComplexMethodAndMain"), +// fields, +// methods, +// getMainMethod(), +// constructors +// ); +// } +// +// private static List getMethods() { +// return List.of(getMethod1()); +// } +// +// private static Method getMethod1() { +// Block block = new Block( +// List.of( +// new LocalVariable( +// new Id("i"), +// Type.INT +// ), +// new LocalVariable( +// new Id("returnValue"), +// Type.BOOL +// ) +// ), +// List.of( +// new Assignment( +// new Id("i"), +// AssignSign.ASSIGN, +// new IntLiteral(11) +// ), +// new Assignment( +// new Id("returnValue"), +// AssignSign.ASSIGN, +// new BoolLiteral(false) +// ), +// new While( +// new Binary( +// new Id("i"), +// Operator.GT, +// new IntLiteral(0) +// ), +// new Block( +// List.of(), +// List.of( +// new Assignment( +// new Id("i"), +// AssignSign.SUB_ASSIGN, +// new IntLiteral(1) +// ), +// new Assignment( +// new Id("returnValue"), +// AssignSign.ASSIGN, +// new Unary( +// UnaryOperator.NOT, +// new Id("returnValue") +// ) +// ) +// ) +// ) +// ), +// new Return( +// new Id("returnValue") +// ) +// ) +// ); +// return new Method( +// true, +// ReturnType.BOOL, +// new Id("moreComplexMethod"), +// List.of(), +// block +// ); +// } +// +// +// private static MainMethod getMainMethod() { +// Type TypeClassWithMoreComplexMethodAndMain = Type.OBJECT; +// TypeClassWithMoreComplexMethodAndMain.setObjectType(new Id("ClassWithMoreComplexMethodAndMain")); +// List statementList = List.of( +// new Assignment( +// new Id("instance"), +// AssignSign.ASSIGN, +// new New( +// TypeClassWithMoreComplexMethodAndMain, +// List.of() +// ) +// ) +// ); +// Block block = new Block( +// List.of(), +// statementList +// ); +// return new MainMethod( +// block +// ); +// } +} \ No newline at end of file diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_OnlyClass.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_OnlyClass.java new file mode 100644 index 0000000..4ff1212 --- /dev/null +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_OnlyClass.java @@ -0,0 +1,19 @@ +//class OnlyClass { +//} + +import de.maishai.typedast.typedclass.*; + +import java.util.List; + +public class TypedAbstractSyntax_OnlyClass { + public static TypedClass get() { + TypedClass typedClass = new TypedClass(); + typedClass.setIsPublic(false); + typedClass.setTypedId(new TypedId("OnlyClass")); + typedClass.setTypedFields(List.of()); + typedClass.setTypedMethods(List.of()); + typedClass.setTypedMainMethod(null); + typedClass.setTypedConstructors(List.of()); + return typedClass; + } +} \ No newline at end of file diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_PublicClass.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_PublicClass.java new file mode 100644 index 0000000..80eaac0 --- /dev/null +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_PublicClass.java @@ -0,0 +1,19 @@ +//public class PublicClass { +//} + +import de.maishai.typedast.typedclass.*; + +import java.util.List; + +public class TypedAbstractSyntax_PublicClass { + public static TypedClass get() { + TypedClass typedClass = new TypedClass(); + typedClass.setIsPublic(true); + typedClass.setTypedId(new TypedId("PublicClass")); + typedClass.setTypedFields(List.of()); + typedClass.setTypedMethods(List.of()); + typedClass.setTypedMainMethod(null); + typedClass.setTypedConstructors(List.of()); + return typedClass; + } +} \ No newline at end of file From ce711b3a27f970b28f83253c716885eb88ad15e7 Mon Sep 17 00:00:00 2001 From: JonathanFleischmann Date: Wed, 8 May 2024 11:27:54 +0200 Subject: [PATCH 2/3] implemented new methods in Compiler --- src/main/java/de/maishai/Compiler.java | 49 +++++++++++++++++-- ...ax_ClassWithConstructorWithParameters.java | 5 +- ...edAbstractSyntax_ClassWithConstructor.java | 12 ++--- src/test/java/E2ETests.java | 5 ++ src/test/java/Test.java | 2 - 5 files changed, 59 insertions(+), 14 deletions(-) create mode 100644 src/test/java/E2ETests.java delete mode 100644 src/test/java/Test.java diff --git a/src/main/java/de/maishai/Compiler.java b/src/main/java/de/maishai/Compiler.java index 4567d94..92e75fc 100644 --- a/src/main/java/de/maishai/Compiler.java +++ b/src/main/java/de/maishai/Compiler.java @@ -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"); + } } diff --git a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java index 0e6fe5d..2652d37 100644 --- a/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java +++ b/src/main/resources/AbstractSyntax/AbstractSyntax_ClassWithConstructorWithParameters.java @@ -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) - + ) ) ) ) diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java index 8563ee8..9fd103f 100644 --- a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java @@ -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")); diff --git a/src/test/java/E2ETests.java b/src/test/java/E2ETests.java new file mode 100644 index 0000000..db3a256 --- /dev/null +++ b/src/test/java/E2ETests.java @@ -0,0 +1,5 @@ +public class E2ETests { + public static void main(String[] args) { + + } +} diff --git a/src/test/java/Test.java b/src/test/java/Test.java deleted file mode 100644 index a065407..0000000 --- a/src/test/java/Test.java +++ /dev/null @@ -1,2 +0,0 @@ -public class Test { -} From 8d58ac0a08547cf4e307d491e4798d8165b84141 Mon Sep 17 00:00:00 2001 From: Boolean-True Date: Wed, 8 May 2024 11:28:52 +0200 Subject: [PATCH 3/3] Fix numbers in variable names --- src/main/antlr/Decaf.g4 | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/antlr/Decaf.g4 b/src/main/antlr/Decaf.g4 index 0171f6e..f0e3a96 100644 --- a/src/main/antlr/Decaf.g4 +++ b/src/main/antlr/Decaf.g4 @@ -88,6 +88,6 @@ CHAR : 'char'; BOOLEANLITERAL : 'true' | 'false' ; CHARLITERAL : ['][a-zA-Z][']; -IDENTIFIER : [a-zA-Z]+; +IDENTIFIER : ([a-zA-Z]+[0-9]*)+; NUMBER : [0-9]+; WS : [ \t\r\n] -> skip;