fixed program instead of classes and did some renaming etc

This commit is contained in:
JonathanFleischmann 2024-05-14 15:23:12 +02:00
parent 131264d27e
commit f5cc94316e
8 changed files with 99 additions and 104 deletions

View File

@ -47,8 +47,7 @@ public class Compiler {
} }
public static TypedProgram generateTypedASTFromAst(Program ast) { public static TypedProgram generateTypedASTFromAst(Program ast) {
TypedProgram typedAST = new TypedProgram(ast); return new TypedProgram(ast);
return typedAST;
} }
public static byte[] generateByteCodeArrayFromTypedAst(TypedClass typedAST) { public static byte[] generateByteCodeArrayFromTypedAst(TypedClass typedAST) {
@ -99,6 +98,6 @@ public class Compiler {
} }
public static void main(String[] args) { 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"));
} }
} }

View File

@ -1,15 +1,14 @@
package de.maishai.typedast.typedclass; package de.maishai.typedast.typedclass;
import de.maishai.ast.records.Program; import de.maishai.ast.records.Program;
import de.maishai.typedast.ClassContext; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.Opcodes;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@Getter @Getter
@AllArgsConstructor
public class TypedProgram { public class TypedProgram {
private List<TypedClass> typedClasses = new ArrayList<>(); private List<TypedClass> typedClasses = new ArrayList<>();

View File

@ -1,20 +1,20 @@
public class ClassCanBeTyped { public class ComplexClass {
int x; int x;
int y; int y;
ClassCanBeTyped b; ComplexClass b;
ClassCanBeTyped c; ComplexClass c;
public ClassCanBeTyped(int x) { public ComplexClass(int x) {
this.x = x; this.x = x;
} }
public ClassCanBeTyped(int x, int y) { public ComplexClass(int x, int y) {
this.x = x; this.x = x;
this.y = y; this.y = y;
} }
public ClassCanBeTyped initClassCanBeTyped(int x) { public ComplexClass initComplexClass(int x) {
int a; int a;
a = 10; a = 10;
b = new ClassCanBeTyped(x); b = new ComplexClass(x);
b.x = 10 + a; b.x = 10 + a;
b.y = 20; b.y = 20;
b.c.x = 20 + a; b.c.x = 20 + a;
@ -22,17 +22,17 @@ public class ClassCanBeTyped {
return b; return b;
} }
public ClassCanBeTyped init(int x, int y) { public ComplexClass init(int x, int y) {
return new ClassCanBeTyped(x, y); return new ComplexClass(x, y);
} }
public ClassCanBeTyped(int x) { public ComplexClass(int x) {
this.x = x; this.x = x;
int i; int i;
b = b.getX().c.getC(); b = b.getX().c.getC();
i = b.getX().c.getX(); i = b.getX().c.getX();
} }
public ClassCanBeTyped() { public ComplexClass() {
this.x = 10; this.x = 10;
int i; int i;
for (i = 0; i < (x + 1); i = i + 1) { 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.x = x;
this.y = y; this.y = y;
} }
@ -65,7 +65,7 @@ public class ClassCanBeTyped {
return this.x; return this.x;
} }
public ClassCanBeTyped getC() { public ComplexClass getC() {
return c; return c;
} }

View File

@ -20,37 +20,24 @@ import de.maishai.typedast.typedclass.*;
import java.util.List; import java.util.List;
public class TypedAbstractSyntax_ClassWithConstructor { public class TypedAbstractSyntax_ClassWithConstructor {
public static TypedClass get() { public static TypedProgram get() {
return new TypedClass( return new TypedProgram(
"ClassWithConstructor",
List.of( List.of(
new TypedDeclaration( new TypedClass(
"x",
Type.INT
)
),
List.of(),
List.of(
new TypedConstructor(
"ClassWithConstructor", "ClassWithConstructor",
List.of(),
new TypedBlock(
List.of(),
List.of(),
Type.VOID
),
Type.VOID,
List.of( List.of(
new TypedLocalVariable( new TypedDeclaration(
"i", "x",
Type.INT Type.INT
) )
) ),
List.of(),
getConstructors(),
null,
null,
Type.REFERENCE("ClassWithField")
) )
), )
null,
null,
Type.REFERENCE("ClassWithField")
); );
} }

View File

@ -9,32 +9,36 @@ import de.maishai.typedast.typedclass.*;
import java.util.List; import java.util.List;
public class TypedAbstractSyntax_ClassWithField { public class TypedAbstractSyntax_ClassWithField {
public static TypedClass get() { public static TypedProgram get() {
return new TypedClass( return new TypedProgram(
"ClassWithField", List.of(
List.of( new TypedClass(
new TypedDeclaration( "ClassWithField",
"x", List.of(
Type.INT new TypedDeclaration(
) "x",
), Type.INT
List.of(), )
List.of( ),
new TypedConstructor( List.of(),
"ClassWithField", List.of(
List.of(), new TypedConstructor(
new TypedBlock( "ClassWithField",
List.of(), List.of(),
List.of(), new TypedBlock(
Type.VOID List.of(),
), List.of(),
Type.VOID, Type.VOID
List.of() ),
) Type.VOID,
), List.of()
null, )
null, ),
Type.REFERENCE("ClassWithField") null,
null,
Type.REFERENCE("ClassWithField")
)
)
); );
} }
} }

View File

@ -7,27 +7,31 @@ import de.maishai.typedast.typedclass.*;
import java.util.List; import java.util.List;
public class TypedAbstractSyntax_PublicClass { public class TypedAbstractSyntax_PublicClass {
public static TypedClass get() { public static TypedProgram get() {
return new TypedClass( return new TypedProgram(
"PublicClass",
List.of(),
List.of(),
List.of( List.of(
new TypedConstructor( new TypedClass(
"PublicClass", "PublicClass",
List.of(), List.of(),
new TypedBlock( List.of(),
List.of(), List.of(
List.of(), new TypedConstructor(
Type.VOID "PublicClass",
List.of(),
new TypedBlock(
List.of(),
List.of(),
Type.VOID
),
Type.VOID,
List.of()
)
), ),
Type.VOID, null,
List.of() null,
Type.REFERENCE("PublicClass")
) )
), )
null,
null,
Type.REFERENCE("PublicClass")
); );
} }
} }

View File

@ -1,62 +1,64 @@
import de.maishai.Compiler; import de.maishai.Compiler;
import de.maishai.ast.records.Class; import de.maishai.ast.records.Program;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
public class ScannerParserTests { public class ScannerParserTests {
@Test @Test
public void testPublicClass() { 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); assertEquals(AbstractSyntax_PublicClass.get(), resultAst);
} }
@Test @Test
public void testClassWithField() { 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); assertEquals(AbstractSyntax_ClassWithField.get(), resultAst);
} }
@Test @Test
public void testClassWithConstructor() { 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); assertEquals(AbstractSyntax_ClassWithConstructor.get(), resultAst);
} }
@Test @Test
public void testClassWithMethod() { 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); assertEquals(AbstractSyntax_ClassWithMethod.get(), resultAst);
} }
@Test @Test
public void testClassWithConstructorWithCodeInComments() { 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); assertEquals(AbstractSyntax_ClassWithConstructorWithCodeInComments.get(), resultAst);
} }
@Test @Test
public void testClassWithConstructorWithParameters() { 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); assertEquals(AbstractSyntax_ClassWithConstructorWithParameters.get(), resultAst);
} }
@Test @Test
public void testClassWithMethodAndField() { 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); assertEquals(AbstractSyntax_ClassWithMethodAndField.get(), resultAst);
} }
@Test @Test
public void testClassWithConstructorAndMethodCall() { 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); assertEquals(AbstractSyntax_ClassWithConstructorAndMethodCall.get(), resultAst);
} }
// @Test @Test
// public void testClassCanBeTyped() { public void testComplexClass() {
// Class resultAst = Compiler.generateASTFromFile("src/main/resources/JavaTestfiles/ClassCanBeTyped.java"); Program resultAst = Compiler.generateASTFromFile(List.of("src/main/resources/JavaTestfiles/ComplexClass.java"));
// assertEquals(AbstractSyntax_ClassWithConstructorAndMethodCall.get(), resultAst); assertEquals(AbstractSyntax_ClassWithConstructorAndMethodCall.get(), resultAst);
// } }
} }

View File

@ -1,5 +1,5 @@
import de.maishai.Compiler; import de.maishai.Compiler;
import de.maishai.typedast.typedclass.TypedClass; import de.maishai.typedast.typedclass.TypedProgram;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -8,19 +8,19 @@ public class TypingTests {
@Test @Test
public void testPublicClass() { public void testPublicClass() {
TypedClass resultTypedAst = Compiler.generateTypedASTFromAst(AbstractSyntax_PublicClass.get()); TypedProgram resultTypedAst = Compiler.generateTypedASTFromAst(AbstractSyntax_PublicClass.get());
assertEquals(TypedAbstractSyntax_PublicClass.get(), resultTypedAst); assertEquals(TypedAbstractSyntax_PublicClass.get(), resultTypedAst);
} }
@Test @Test
public void testClassWithField() { public void testClassWithField() {
TypedClass resultTypedAst = Compiler.generateTypedASTFromAst(AbstractSyntax_ClassWithField.get()); TypedProgram resultTypedAst = Compiler.generateTypedASTFromAst(AbstractSyntax_ClassWithField.get());
assertEquals(TypedAbstractSyntax_ClassWithField.get(), resultTypedAst); assertEquals(TypedAbstractSyntax_ClassWithField.get(), resultTypedAst);
} }
@Test @Test
public void testClassWithConstructor() { public void testClassWithConstructor() {
TypedClass resultTypedAst = Compiler.generateTypedASTFromAst(AbstractSyntax_ClassWithConstructor.get()); TypedProgram resultTypedAst = Compiler.generateTypedASTFromAst(AbstractSyntax_ClassWithConstructor.get());
assertEquals(TypedAbstractSyntax_ClassWithConstructor.get(), resultTypedAst); assertEquals(TypedAbstractSyntax_ClassWithConstructor.get(), resultTypedAst);
} }
} }