From f5cc94316ef834135bc1d35612c4be1ab8ca2696 Mon Sep 17 00:00:00 2001 From: JonathanFleischmann Date: Tue, 14 May 2024 15:23:12 +0200 Subject: [PATCH] fixed program instead of classes and did some renaming etc --- src/main/java/de/maishai/Compiler.java | 5 +- .../typedast/typedclass/TypedProgram.java | 5 +- ...ClassCanBeTyped.java => ComplexClass.java} | 26 ++++----- ...edAbstractSyntax_ClassWithConstructor.java | 37 ++++-------- .../TypedAbstractSyntax_ClassWithField.java | 56 ++++++++++--------- .../TypedAbstractSyntax_PublicClass.java | 36 ++++++------ src/test/java/ScannerParserTests.java | 30 +++++----- src/test/java/TypingTests.java | 8 +-- 8 files changed, 99 insertions(+), 104 deletions(-) rename src/main/resources/JavaTestfiles/{ClassCanBeTyped.java => ComplexClass.java} (68%) diff --git a/src/main/java/de/maishai/Compiler.java b/src/main/java/de/maishai/Compiler.java index 9b1bb68..e354407 100644 --- a/src/main/java/de/maishai/Compiler.java +++ b/src/main/java/de/maishai/Compiler.java @@ -47,8 +47,7 @@ public class Compiler { } public static TypedProgram generateTypedASTFromAst(Program ast) { - TypedProgram typedAST = new TypedProgram(ast); - return typedAST; + return new TypedProgram(ast); } public static byte[] generateByteCodeArrayFromTypedAst(TypedClass typedAST) { @@ -99,6 +98,6 @@ public class Compiler { } public static void main(String[] args) { - generateByteCodeFileFromFile(List.of("src/main/resources/JavaTestfiles/ClassCanBeTyped.java"), List.of("ClassCanBeTyped")); + generateByteCodeFileFromFile(List.of("src/main/resources/JavaTestfiles/ComplexClass.java"), List.of("ComplexClass")); } } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedProgram.java b/src/main/java/de/maishai/typedast/typedclass/TypedProgram.java index c52567a..9a4f5e9 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedProgram.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedProgram.java @@ -1,15 +1,14 @@ package de.maishai.typedast.typedclass; import de.maishai.ast.records.Program; -import de.maishai.typedast.ClassContext; +import lombok.AllArgsConstructor; import lombok.Getter; -import org.objectweb.asm.ClassWriter; -import org.objectweb.asm.Opcodes; import java.util.ArrayList; import java.util.List; @Getter +@AllArgsConstructor public class TypedProgram { private List typedClasses = new ArrayList<>(); diff --git a/src/main/resources/JavaTestfiles/ClassCanBeTyped.java b/src/main/resources/JavaTestfiles/ComplexClass.java similarity index 68% rename from src/main/resources/JavaTestfiles/ClassCanBeTyped.java rename to src/main/resources/JavaTestfiles/ComplexClass.java index b4d9fd9..e61f30d 100644 --- a/src/main/resources/JavaTestfiles/ClassCanBeTyped.java +++ b/src/main/resources/JavaTestfiles/ComplexClass.java @@ -1,20 +1,20 @@ -public class ClassCanBeTyped { +public class ComplexClass { int x; int y; - ClassCanBeTyped b; - ClassCanBeTyped c; - public ClassCanBeTyped(int x) { + ComplexClass b; + ComplexClass c; + public ComplexClass(int x) { this.x = x; } - public ClassCanBeTyped(int x, int y) { + public ComplexClass(int x, int y) { this.x = x; this.y = y; } - public ClassCanBeTyped initClassCanBeTyped(int x) { + public ComplexClass initComplexClass(int x) { int a; a = 10; - b = new ClassCanBeTyped(x); + b = new ComplexClass(x); b.x = 10 + a; b.y = 20; b.c.x = 20 + a; @@ -22,17 +22,17 @@ public class ClassCanBeTyped { return b; } - public ClassCanBeTyped init(int x, int y) { - return new ClassCanBeTyped(x, y); + public ComplexClass init(int x, int y) { + return new ComplexClass(x, y); } - public ClassCanBeTyped(int x) { + public ComplexClass(int x) { this.x = x; int i; b = b.getX().c.getC(); i = b.getX().c.getX(); } - public ClassCanBeTyped() { + public ComplexClass() { this.x = 10; int i; for (i = 0; i < (x + 1); i = i + 1) { @@ -56,7 +56,7 @@ public class ClassCanBeTyped { } - public ClassCanBeTyped(int x, int y, char z) { + public ComplexClass(int x, int y, char z) { this.x = x; this.y = y; } @@ -65,7 +65,7 @@ public class ClassCanBeTyped { return this.x; } - public ClassCanBeTyped getC() { + public ComplexClass getC() { return c; } diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java index 7c6a76d..b7dbc45 100644 --- a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithConstructor.java @@ -20,37 +20,24 @@ import de.maishai.typedast.typedclass.*; import java.util.List; public class TypedAbstractSyntax_ClassWithConstructor { - public static TypedClass get() { - return new TypedClass( - "ClassWithConstructor", + public static TypedProgram get() { + return new TypedProgram( List.of( - new TypedDeclaration( - "x", - Type.INT - ) - ), - List.of(), - List.of( - new TypedConstructor( + new TypedClass( "ClassWithConstructor", - List.of(), - new TypedBlock( - List.of(), - List.of(), - Type.VOID - ), - Type.VOID, List.of( - new TypedLocalVariable( - "i", + new TypedDeclaration( + "x", Type.INT ) - ) + ), + List.of(), + getConstructors(), + null, + null, + Type.REFERENCE("ClassWithField") ) - ), - null, - null, - Type.REFERENCE("ClassWithField") + ) ); } diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithField.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithField.java index 284b83d..9c39652 100644 --- a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithField.java +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_ClassWithField.java @@ -9,32 +9,36 @@ import de.maishai.typedast.typedclass.*; import java.util.List; public class TypedAbstractSyntax_ClassWithField { - public static TypedClass get() { - return new TypedClass( - "ClassWithField", - List.of( - new TypedDeclaration( - "x", - Type.INT - ) - ), - List.of(), - List.of( - new TypedConstructor( - "ClassWithField", - List.of(), - new TypedBlock( - List.of(), - List.of(), - Type.VOID - ), - Type.VOID, - List.of() - ) - ), - null, - null, - Type.REFERENCE("ClassWithField") + public static TypedProgram get() { + return new TypedProgram( + List.of( + new TypedClass( + "ClassWithField", + List.of( + new TypedDeclaration( + "x", + Type.INT + ) + ), + List.of(), + List.of( + new TypedConstructor( + "ClassWithField", + List.of(), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ), + Type.VOID, + List.of() + ) + ), + null, + null, + Type.REFERENCE("ClassWithField") + ) + ) ); } } \ No newline at end of file diff --git a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_PublicClass.java b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_PublicClass.java index cf1c85d..dba4b7e 100644 --- a/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_PublicClass.java +++ b/src/main/resources/TypedAbstractSyntax/TypedAbstractSyntax_PublicClass.java @@ -7,27 +7,31 @@ import de.maishai.typedast.typedclass.*; import java.util.List; public class TypedAbstractSyntax_PublicClass { - public static TypedClass get() { - return new TypedClass( - "PublicClass", - List.of(), - List.of(), + public static TypedProgram get() { + return new TypedProgram( List.of( - new TypedConstructor( + new TypedClass( "PublicClass", List.of(), - new TypedBlock( - List.of(), - List.of(), - Type.VOID + List.of(), + List.of( + new TypedConstructor( + "PublicClass", + List.of(), + new TypedBlock( + List.of(), + List.of(), + Type.VOID + ), + Type.VOID, + List.of() + ) ), - Type.VOID, - List.of() + null, + null, + Type.REFERENCE("PublicClass") ) - ), - null, - null, - Type.REFERENCE("PublicClass") + ) ); } } \ No newline at end of file diff --git a/src/test/java/ScannerParserTests.java b/src/test/java/ScannerParserTests.java index 6f32feb..4e27183 100644 --- a/src/test/java/ScannerParserTests.java +++ b/src/test/java/ScannerParserTests.java @@ -1,62 +1,64 @@ import de.maishai.Compiler; -import de.maishai.ast.records.Class; +import de.maishai.ast.records.Program; import org.junit.jupiter.api.Test; +import java.util.List; + import static org.junit.jupiter.api.Assertions.assertEquals; public class ScannerParserTests { @Test public void testPublicClass() { - Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/PublicClass.java"); + Program resultAst = Compiler.generateASTFromFile(List.of("src/main/resources/JavaTestfiles/PublicClass.java")); assertEquals(AbstractSyntax_PublicClass.get(), resultAst); } @Test public void testClassWithField() { - Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithField.java"); + Program resultAst = Compiler.generateASTFromFile(List.of("src/main/resources/JavaTestfiles/ClassWithField.java")); assertEquals(AbstractSyntax_ClassWithField.get(), resultAst); } @Test public void testClassWithConstructor() { - Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithConstructor.java"); + Program resultAst = Compiler.generateASTFromFile(List.of("src/main/resources/JavaTestfiles/ClassWithConstructor.java")); assertEquals(AbstractSyntax_ClassWithConstructor.get(), resultAst); } @Test public void testClassWithMethod() { - Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithMethod.java"); + Program resultAst = Compiler.generateASTFromFile(List.of("src/main/resources/JavaTestfiles/ClassWithMethod.java")); assertEquals(AbstractSyntax_ClassWithMethod.get(), resultAst); } @Test public void testClassWithConstructorWithCodeInComments() { - Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithConstructorWithCodeInComments.java"); + Program resultAst = Compiler.generateASTFromFile(List.of("src/main/resources/JavaTestfiles/ClassWithConstructorWithCodeInComments.java")); assertEquals(AbstractSyntax_ClassWithConstructorWithCodeInComments.get(), resultAst); } @Test public void testClassWithConstructorWithParameters() { - Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithConstructorWithParameters.java"); + Program resultAst = Compiler.generateASTFromFile(List.of("src/main/resources/JavaTestfiles/ClassWithConstructorWithParameters.java")); assertEquals(AbstractSyntax_ClassWithConstructorWithParameters.get(), resultAst); } @Test public void testClassWithMethodAndField() { - Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithMethodAndField.java"); + Program resultAst = Compiler.generateASTFromFile(List.of("src/main/resources/JavaTestfiles/ClassWithMethodAndField.java")); assertEquals(AbstractSyntax_ClassWithMethodAndField.get(), resultAst); } @Test public void testClassWithConstructorAndMethodCall() { - Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassWithConstructorAndMethodCall.java"); + Program resultAst = Compiler.generateASTFromFile(List.of("src/main/resources/JavaTestfiles/ClassWithConstructorAndMethodCall.java")); assertEquals(AbstractSyntax_ClassWithConstructorAndMethodCall.get(), resultAst); } -// @Test -// public void testClassCanBeTyped() { -// Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassCanBeTyped.java"); -// assertEquals(AbstractSyntax_ClassWithConstructorAndMethodCall.get(), resultAst); -// } + @Test + public void testComplexClass() { + Program resultAst = Compiler.generateASTFromFile(List.of("src/main/resources/JavaTestfiles/ComplexClass.java")); + assertEquals(AbstractSyntax_ClassWithConstructorAndMethodCall.get(), resultAst); + } } diff --git a/src/test/java/TypingTests.java b/src/test/java/TypingTests.java index 1c7d646..d81e9dd 100644 --- a/src/test/java/TypingTests.java +++ b/src/test/java/TypingTests.java @@ -1,5 +1,5 @@ import de.maishai.Compiler; -import de.maishai.typedast.typedclass.TypedClass; +import de.maishai.typedast.typedclass.TypedProgram; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -8,19 +8,19 @@ public class TypingTests { @Test public void testPublicClass() { - TypedClass resultTypedAst = Compiler.generateTypedASTFromAst(AbstractSyntax_PublicClass.get()); + TypedProgram resultTypedAst = Compiler.generateTypedASTFromAst(AbstractSyntax_PublicClass.get()); assertEquals(TypedAbstractSyntax_PublicClass.get(), resultTypedAst); } @Test public void testClassWithField() { - TypedClass resultTypedAst = Compiler.generateTypedASTFromAst(AbstractSyntax_ClassWithField.get()); + TypedProgram resultTypedAst = Compiler.generateTypedASTFromAst(AbstractSyntax_ClassWithField.get()); assertEquals(TypedAbstractSyntax_ClassWithField.get(), resultTypedAst); } @Test public void testClassWithConstructor() { - TypedClass resultTypedAst = Compiler.generateTypedASTFromAst(AbstractSyntax_ClassWithConstructor.get()); + TypedProgram resultTypedAst = Compiler.generateTypedASTFromAst(AbstractSyntax_ClassWithConstructor.get()); assertEquals(TypedAbstractSyntax_ClassWithConstructor.get(), resultTypedAst); } }