Removed the TypedField class and use instand of that teh TypedDeclaration

This commit is contained in:
ahmad 2024-05-12 11:33:15 +02:00
parent a05a4c8744
commit 938e92f679
5 changed files with 37 additions and 76 deletions

View File

@ -37,11 +37,11 @@ public class TypedBlock implements TypedNode {
if (unTypedBlock == null) { if (unTypedBlock == null) {
return; return;
} }
/*
for (Declaration var : unTypedBlock.localVariables()) { for (Declaration var : unTypedBlock.localVariables()) {
TypedDeclaration typedVar = new TypedDeclaration(localVar, clas, var); TypedDeclaration typedVar = new TypedDeclaration(localVar, clas, var);
vars.add(typedVar); vars.add(typedVar);
} } */
for (var stmt : unTypedBlock.stmts()) { for (var stmt : unTypedBlock.stmts()) {
if (stmt instanceof Assignment assignment) { if (stmt instanceof Assignment assignment) {

View File

@ -22,7 +22,7 @@ import java.util.Map;
@NoArgsConstructor @NoArgsConstructor
public class TypedClass implements TypedNode { public class TypedClass implements TypedNode {
private String className; private String className;
private List<TypedField> typedFields = new ArrayList<>(); private List<TypedDeclaration> typedDeclarations = new ArrayList<>();
private List<TypedMethod> typedMethods = new ArrayList<>(); private List<TypedMethod> typedMethods = new ArrayList<>();
private List<TypedConstructor> typedConstructors = new ArrayList<>(); private List<TypedConstructor> typedConstructors = new ArrayList<>();
private TypedMethod currentMethod; private TypedMethod currentMethod;
@ -85,8 +85,8 @@ public class TypedClass implements TypedNode {
className = c.classname(); className = c.classname();
type = Type.REFERENCE(className); type = Type.REFERENCE(className);
for (Declaration field : c.fieldDeclarations()) { for (Declaration declaration : c.fieldDeclarations()) {
typedFields.add(new TypedField(localVar, this, field)); typedDeclarations.add(new TypedDeclaration(localVar, this, declaration));
} }
// Am Anfang werden die Konstruktoren und Methoden in die Listen eingefügt // Am Anfang werden die Konstruktoren und Methoden in die Listen eingefügt
// Methoden können verwendet werden, bevor deren Blöcke ausgeführt werden // Methoden können verwendet werden, bevor deren Blöcke ausgeführt werden
@ -115,7 +115,6 @@ public class TypedClass implements TypedNode {
j++; j++;
} }
System.out.println("TypedClass: " + this.toString());
} }
@Override @Override
@ -172,8 +171,8 @@ public class TypedClass implements TypedNode {
} }
public boolean isThereField(String fieldName) { public boolean isThereField(String fieldName) {
for (TypedField f : typedFields) { for (TypedDeclaration f : typedDeclarations) {
if (f.getVarName().equals(fieldName)) { if (f.getName().equals(fieldName)) {
return true; return true;
} }
} }
@ -181,8 +180,8 @@ public class TypedClass implements TypedNode {
} }
public Type getFieldType(String fieldName) { public Type getFieldType(String fieldName) {
for (TypedField f : typedFields) { for (TypedDeclaration f : typedDeclarations) {
if (f.getVarName().equals(fieldName)) { if (f.getName().equals(fieldName)) {
return f.getType(); return f.getType();
} }
} }
@ -202,8 +201,8 @@ public class TypedClass implements TypedNode {
public byte[] codeGen() { public byte[] codeGen() {
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS); ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES | ClassWriter.COMPUTE_MAXS);
cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", null); cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", null);
for (TypedField field : typedFields) { for (TypedDeclaration declaration : typedDeclarations) {
field.codeGen(cw); declaration.codeGen(cw);
} }
for (TypedConstructor constructor : typedConstructors) { for (TypedConstructor constructor : typedConstructors) {
@ -223,9 +222,9 @@ public class TypedClass implements TypedNode {
c.setClassName("SomeClass"); c.setClassName("SomeClass");
//Fields //Fields
TypedField f1 = new TypedField("someNumber", Type.INT); TypedDeclaration f1 = new TypedDeclaration("someNumber", Type.INT);
TypedField f2 = new TypedField("someChar", Type.CHAR); TypedDeclaration f2 = new TypedDeclaration("someChar", Type.CHAR);
c.typedFields = List.of(f1, f2); c.typedDeclarations = List.of(f1, f2);
//Constructors //Constructors
TypedConstructor constructor = new TypedConstructor("SomeClass", List.of(new TypedParameter("test", Type.INT)), new TypedBlock(new ArrayList<>(), new ArrayList<>())); TypedConstructor constructor = new TypedConstructor("SomeClass", List.of(new TypedParameter("test", Type.INT)), new TypedBlock(new ArrayList<>(), new ArrayList<>()));

View File

@ -73,4 +73,5 @@ public class TypedConstructor implements TypedNode {
context.wrapUp(0, 0); context.wrapUp(0, 0);
} }
} }

View File

@ -7,10 +7,14 @@ import de.maishai.typedast.Type;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import java.util.Map; import java.util.Map;
import static de.maishai.typedast.Type.Kind.REFERENCE;
@Data @Data
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
@ -28,22 +32,34 @@ public final class TypedDeclaration implements TypedNode {
} }
name = declaration.name(); name = declaration.name();
type = declaration.type(); type = declaration.type();
localVar.put(this.name, this.type); //localVar.put(this.name, this.type);
} }
@Override @Override
public Type typeCheck(Map<String, Type> localVar, TypedClass clas) { public Type typeCheck(Map<String, Type> localVar, TypedClass clas) {
Type type = localVar.get(name); if (clas.isThereField(name)) {
if (type == this.type) { throw new RuntimeException("Field " + name + " already declared");
return type;
} }
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) { public void codeGen(MethodVisitor mv, MethodContext ctx) {
System.out.println("Generating code for local variable " + name); System.out.println("Generating code for local variable " + name);
int index = ctx.addVariable(name); int index = ctx.addVariable(name);
mv.visitLocalVariable(name, type.getDescriptor(), null, ctx.getStartLabel(), ctx.getEndLabel(), index); 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();
} }
} }

View File

@ -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<String, Type> localVar, TypedClass clas, Declaration declaration) {
convertToTypedField(localVar, clas, declaration);
}
public void convertToTypedField(Map<String, Type> localVar, TypedClass clas, Declaration declaration) {
this.type = declaration.type();
varName = declaration.name();
this.typeCheck(localVar, clas);
}
@Override
public Type typeCheck(Map<String, Type> 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();
}
}