diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java b/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java index 24aee1a..e77c7e4 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedBlock.java @@ -37,11 +37,11 @@ public class TypedBlock implements TypedNode { if (unTypedBlock == null) { return; } - + /* for (Declaration var : unTypedBlock.localVariables()) { TypedDeclaration typedVar = new TypedDeclaration(localVar, clas, var); vars.add(typedVar); - } + } */ for (var stmt : unTypedBlock.stmts()) { if (stmt instanceof Assignment assignment) { diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java index 060e819..247fd15 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java @@ -22,7 +22,7 @@ import java.util.Map; @NoArgsConstructor public class TypedClass implements TypedNode { private String className; - private List typedFields = new ArrayList<>(); + private List typedDeclarations = new ArrayList<>(); private List typedMethods = new ArrayList<>(); private List typedConstructors = new ArrayList<>(); private TypedMethod currentMethod; @@ -85,8 +85,8 @@ public class TypedClass implements TypedNode { className = c.classname(); type = Type.REFERENCE(className); - for (Declaration field : c.fieldDeclarations()) { - typedFields.add(new TypedField(localVar, this, field)); + for (Declaration declaration : c.fieldDeclarations()) { + typedDeclarations.add(new TypedDeclaration(localVar, this, declaration)); } // Am Anfang werden die Konstruktoren und Methoden in die Listen eingefügt // Methoden können verwendet werden, bevor deren Blöcke ausgeführt werden @@ -115,7 +115,6 @@ public class TypedClass implements TypedNode { j++; } - System.out.println("TypedClass: " + this.toString()); } @Override @@ -172,8 +171,8 @@ public class TypedClass implements TypedNode { } public boolean isThereField(String fieldName) { - for (TypedField f : typedFields) { - if (f.getVarName().equals(fieldName)) { + for (TypedDeclaration f : typedDeclarations) { + if (f.getName().equals(fieldName)) { return true; } } @@ -181,8 +180,8 @@ public class TypedClass implements TypedNode { } public Type getFieldType(String fieldName) { - for (TypedField f : typedFields) { - if (f.getVarName().equals(fieldName)) { + for (TypedDeclaration f : typedDeclarations) { + if (f.getName().equals(fieldName)) { return f.getType(); } } @@ -202,8 +201,8 @@ public class TypedClass implements TypedNode { public byte[] codeGen() { ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", null); - for (TypedField field : typedFields) { - field.codeGen(cw); + for (TypedDeclaration declaration : typedDeclarations) { + declaration.codeGen(cw); } for (TypedConstructor constructor : typedConstructors) { @@ -223,9 +222,9 @@ public class TypedClass implements TypedNode { c.setClassName("SomeClass"); //Fields - TypedField f1 = new TypedField("someNumber", Type.INT); - TypedField f2 = new TypedField("someChar", Type.CHAR); - c.typedFields = List.of(f1, f2); + TypedDeclaration f1 = new TypedDeclaration("someNumber", Type.INT); + TypedDeclaration f2 = new TypedDeclaration("someChar", Type.CHAR); + c.typedDeclarations = List.of(f1, f2); //Constructors TypedConstructor constructor = new TypedConstructor("SomeClass", List.of(new TypedParameter("test", Type.INT)), new TypedBlock(new ArrayList<>(), new ArrayList<>())); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java index cb2c140..fbf868f 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java @@ -73,4 +73,5 @@ public class TypedConstructor implements TypedNode { context.wrapUp(0, 0); } + } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedDeclaration.java b/src/main/java/de/maishai/typedast/typedclass/TypedDeclaration.java index 8ba500b..b33175e 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedDeclaration.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedDeclaration.java @@ -7,10 +7,14 @@ import de.maishai.typedast.Type; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; +import org.objectweb.asm.ClassWriter; import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Opcodes; import java.util.Map; +import static de.maishai.typedast.Type.Kind.REFERENCE; + @Data @AllArgsConstructor @NoArgsConstructor @@ -28,22 +32,34 @@ public final class TypedDeclaration implements TypedNode { } name = declaration.name(); type = declaration.type(); - localVar.put(this.name, this.type); + //localVar.put(this.name, this.type); } @Override public Type typeCheck(Map localVar, TypedClass clas) { - Type type = localVar.get(name); - if (type == this.type) { - return type; + if (clas.isThereField(name)) { + throw new RuntimeException("Field " + name + " already declared"); } - throw new RuntimeException("type of left not equals with type of right"); + + if (type.getKind() == REFERENCE) { + if (!type.getReference().equals(clas.getClassName())) { + throw new RuntimeException("Field " + name + " has wrong type"); + } + + } + + return type; } + /* public void codeGen(MethodVisitor mv, MethodContext ctx) { System.out.println("Generating code for local variable " + name); int index = ctx.addVariable(name); mv.visitLocalVariable(name, type.getDescriptor(), null, ctx.getStartLabel(), ctx.getEndLabel(), index); - + } + */ + public void codeGen(ClassWriter cw) { + int access = Opcodes.ACC_PUBLIC; // laut Andi ist es ok, dass alle Felder public sind + cw.visitField(access, name, type.getDescriptor(), null, null).visitEnd(); } } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedField.java b/src/main/java/de/maishai/typedast/typedclass/TypedField.java deleted file mode 100644 index b607f23..0000000 --- a/src/main/java/de/maishai/typedast/typedclass/TypedField.java +++ /dev/null @@ -1,55 +0,0 @@ -package de.maishai.typedast.typedclass; - -import de.maishai.ast.records.Declaration; -import de.maishai.ast.records.Node; -import de.maishai.typedast.TypedNode; -import de.maishai.typedast.Type; -import lombok.AllArgsConstructor; -import lombok.Data; -import lombok.RequiredArgsConstructor; -import org.objectweb.asm.ClassWriter; -import org.objectweb.asm.Opcodes; - -import java.util.Map; - -import static de.maishai.typedast.Type.Kind.REFERENCE; - -@AllArgsConstructor -@RequiredArgsConstructor -@Data -public class TypedField implements TypedNode { - private String varName; - private Type type; - - //TODO: remove TypedField - public TypedField(Map localVar, TypedClass clas, Declaration declaration) { - convertToTypedField(localVar, clas, declaration); - } - - public void convertToTypedField(Map localVar, TypedClass clas, Declaration declaration) { - this.type = declaration.type(); - varName = declaration.name(); - this.typeCheck(localVar, clas); - } - - @Override - public Type typeCheck(Map localVar, TypedClass clas) { - - if (clas.isThereField(varName)) { - throw new RuntimeException("Field " + varName + " already declared"); - } - if (type.getKind() == REFERENCE) { - if (!type.getReference().equals(clas.getClassName())) { - throw new RuntimeException("Field " + varName + " has wrong type"); - } - - } - - return type; - } - - public void codeGen(ClassWriter cw) { - int access = Opcodes.ACC_PUBLIC; // laut Andi ist es ok, dass alle Felder public sind - cw.visitField(access, varName, type.getDescriptor(), null, null).visitEnd(); - } -}