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) {
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) {

View File

@ -22,7 +22,7 @@ import java.util.Map;
@NoArgsConstructor
public class TypedClass implements TypedNode {
private String className;
private List<TypedField> typedFields = new ArrayList<>();
private List<TypedDeclaration> typedDeclarations = new ArrayList<>();
private List<TypedMethod> typedMethods = new ArrayList<>();
private List<TypedConstructor> 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<>()));

View File

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

View File

@ -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<String, Type> 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();
}
}

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();
}
}