From 591dee0d4873686aa5747b655320daea3923b43f Mon Sep 17 00:00:00 2001 From: Ahmad Date: Wed, 8 May 2024 11:24:34 +0200 Subject: [PATCH] Deleted TypedId class not required in typedastg --- .../typedast/typedclass/TypedAssignment.java | 11 +++---- .../typedast/typedclass/TypedClass.java | 23 ++++++++------ .../typedast/typedclass/TypedConstructor.java | 8 ++--- .../typedast/typedclass/TypedField.java | 12 +++---- .../typedast/typedclass/TypedFieldId.java | 4 +-- .../maishai/typedast/typedclass/TypedId.java | 31 ------------------- .../typedclass/TypedLocalVariable.java | 10 +++--- .../typedast/typedclass/TypedMethod.java | 14 ++++----- .../typedast/typedclass/TypedMethodCall.java | 2 +- .../typedast/typedclass/TypedParameter.java | 8 ++--- 10 files changed, 47 insertions(+), 76 deletions(-) delete mode 100644 src/main/java/de/maishai/typedast/typedclass/TypedId.java diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java index fd75937..44294eb 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedAssignment.java @@ -10,30 +10,29 @@ import java.util.Map; @Data public class TypedAssignment implements TypedStatement { - private TypedId loc; + private String varName; private TypedExpression value; @Override public Type typeCheck(Map localVar, Map classes) { - if (!localVar.containsKey(loc.getName())) { + if (!localVar.containsKey(varName)) { throw new RuntimeException("Variable not declared"); } - Type typeLeft = localVar.get(loc.getName()); + Type typeLeft = localVar.get(varName); Type typeRight = value.typeCheck(localVar, classes); if (typeLeft.equals(typeRight) ) { return typeLeft; } - throw new RuntimeException("type of left not equals with type right"); + throw new RuntimeException("type of left not equals with type of right"); } @Override public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { Assignment untyped = (Assignment) unTypedAST; TypedAssignment typedAssignment = new TypedAssignment(); - typedAssignment.setLoc((TypedId) loc.convertToTypedAST(localVar, classes, untyped.loc())); - // typedAssignment.setAssignSign(untyped.assignment()); + typedAssignment.setVarName(untyped.loc().name()); typedAssignment.setValue((TypedExpression) value.convertToTypedAST(localVar, classes, untyped.value())); return typedAssignment; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java index 9edaf06..6bb3477 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedClass.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedClass.java @@ -15,7 +15,7 @@ import java.util.Map; @Data public class TypedClass implements TypedNode { private Boolean isPublic; - private TypedId typedId; + private String className; private List typedFields; private List typedMethods; private TypedMainMethod typedMainMethod; @@ -24,17 +24,20 @@ public class TypedClass implements TypedNode { @Override public Type typeCheck(Map localVar, Map classes) { - if (classes.containsKey(typedId.getName())) { - throw new RuntimeException("Class " + typedId.getName() + " already exists"); + if (classes.containsKey(className)) { + throw new RuntimeException("Class " + className + " already exists"); } - classes.put(typedId.getName(), this); + classes.put(className, this); for (TypedField field : typedFields) { field.typeCheck(localVar, classes); } + for(TypedConstructor constructor : typedConstructors) { + constructor.typeCheck(localVar, classes); + } for (TypedMethod typedMethod : typedMethods) { typedMethod.typeCheck(localVar, classes); } - return Type.REFERENCE(typedId.getName()); + return Type.REFERENCE(className); } public TypedNode startConversion(de.maishai.ast.records.Class c) { @@ -65,7 +68,7 @@ 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, typedId.getName(), null, "java/lang/Object", null); + cw.visit(Opcodes.V1_8, Opcodes.ACC_PUBLIC, className, null, "java/lang/Object", null); for (TypedField field : typedFields) { field.codeGen(cw); } @@ -84,15 +87,15 @@ public class TypedClass implements TypedNode { public static void main(String[] args) { TypedClass c = new TypedClass(); - c.typedId = new TypedId("SomeClass"); + c.setClassName("SomeClass"); //Fields - TypedField f1 = new TypedField(new TypedId("someNumber"), Type.INT); - TypedField f2 = new TypedField(new TypedId("someChar"), Type.CHAR); + TypedField f1 = new TypedField("someNumber", Type.INT); + TypedField f2 = new TypedField("someChar", Type.CHAR); c.typedFields = List.of(f1, f2); //Constructors - TypedConstructor constructor = new TypedConstructor(false, new TypedId("SomeClass"), List.of(new TypedParameter(new TypedId("test"), Type.INT)), new TypedBlock()); + TypedConstructor constructor = new TypedConstructor(false, "SomeClass", List.of(new TypedParameter("test", Type.INT)), new TypedBlock()); c.typedConstructors = List.of(constructor); //Methods diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java index f86b55c..97289e2 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedConstructor.java @@ -23,17 +23,17 @@ import java.util.Map; public class TypedConstructor implements TypedNode { private Boolean isPublic; - private TypedId typedId; + private String name; private List typedParameters; private TypedBlock typedBlock; //constructor: PUBLIC? id '(' params? ')' block; @Override public Type typeCheck(Map localVar, Map classes) { - if (localVar.containsKey(typedId.getName())) { + if (localVar.containsKey(name)) { throw new RuntimeException("constructor already declared"); } - localVar.put(typedId.getName(), Type.VOID); + localVar.put(name, Type.VOID); if (typedParameters != null) { for (TypedParameter param : typedParameters) { param.typeCheck(localVar, classes); @@ -47,7 +47,7 @@ public class TypedConstructor implements TypedNode { public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { Constructor untyped = (Constructor) unTypedAST; TypedConstructor typedConstructor = new TypedConstructor(); - typedConstructor.setTypedId((TypedId) typedId.convertToTypedAST(localVar, classes, untyped.id())); + typedConstructor.setName(untyped.id().name()); if (untyped.params().isEmpty()) { typedConstructor.setTypedParameters(null); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedField.java b/src/main/java/de/maishai/typedast/typedclass/TypedField.java index 1cb0e31..93e3c7c 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedField.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedField.java @@ -16,15 +16,15 @@ import java.util.Map; @RequiredArgsConstructor @Data public class TypedField implements TypedNode { - private TypedId typedId; + private String varName; private Type type; @Override public Type typeCheck(Map localVar, Map classes) { - if (localVar.containsKey(typedId.getName())) { - throw new RuntimeException("Variable " + typedId.getName() + " already declared"); + if (localVar.containsKey(varName)) { + throw new RuntimeException("Variable " + varName + " already declared"); } - localVar.put(typedId.getName(), type); + localVar.put(varName, type); return type; } @@ -32,7 +32,7 @@ public class TypedField implements TypedNode { public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { Field untypedField = (Field) unTypedAST; TypedField typedField = new TypedField(); - typedField.setTypedId((TypedId) typedId.convertToTypedAST(localVar, classes, untypedField.name())); + typedField.setVarName(untypedField.name().name()); typedField.setType(TypeMapper.getType(untypedField.type())); return typedField; @@ -40,6 +40,6 @@ public class TypedField implements TypedNode { public void codeGen(ClassWriter cw) { int access = Opcodes.ACC_PUBLIC; // laut Andi ist es ok, dass alle Felder public sind - cw.visitField(access, typedId.getName(), type.getDescriptor(), null, null).visitEnd(); + cw.visitField(access, varName, type.getDescriptor(), null, null).visitEnd(); } } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedFieldId.java b/src/main/java/de/maishai/typedast/typedclass/TypedFieldId.java index 205b89b..595022a 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedFieldId.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedFieldId.java @@ -11,7 +11,7 @@ import java.util.Map; public class TypedFieldId implements TypedExpression { private Boolean field; private TypedExpression recipient; - private TypedId typedId; + private String name; @Override public Type typeCheck(Map localVar, Map classes) { //TODO: Implement typeCheck for FieldId @@ -28,7 +28,7 @@ public class TypedFieldId implements TypedExpression { TypedFieldId typedFieldId = new TypedFieldId(); typedFieldId.setField(untypedFieldId.field()); typedFieldId.setRecipient((TypedExpression) recipient.convertToTypedAST(localVar, classes, untypedFieldId.recipient())); - typedFieldId.setTypedId((TypedId) typedId.convertToTypedAST(localVar, classes, untypedFieldId.id())); + typedFieldId.setName(untypedFieldId.id().name()); return typedFieldId; } diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedId.java b/src/main/java/de/maishai/typedast/typedclass/TypedId.java deleted file mode 100644 index 32272e4..0000000 --- a/src/main/java/de/maishai/typedast/typedclass/TypedId.java +++ /dev/null @@ -1,31 +0,0 @@ -package de.maishai.typedast.typedclass; - -import de.maishai.ast.records.Id; -import de.maishai.typedast.TypedExpression; -import de.maishai.typedast.TypedNode; -import de.maishai.typedast.Type; -import lombok.AllArgsConstructor; -import lombok.Data; - -import java.util.Map; - -@Data -@AllArgsConstructor -public class TypedId implements TypedExpression { - private String name; - - @Override - public Type typeCheck(Map localVar, Map classes) { - if (!localVar.containsKey(name)) { - throw new RuntimeException("Variable " + name + " not declared or not found"); - } - - return localVar.get(name); - } - - @Override - public TypedNode convertToTypedAST(Map localVar, Map classes, de.maishai.ast.records.Node unTypedAST) { - Id untypedId = (Id) unTypedAST; - return new TypedId(untypedId.name()); - } -} diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java b/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java index 027426c..e201020 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedLocalVariable.java @@ -15,15 +15,15 @@ import java.util.Map; @AllArgsConstructor @NoArgsConstructor public final class TypedLocalVariable implements TypedNode { - private TypedId typedId; + private String name; private Type type; @Override public Type typeCheck(Map localVar, Map classes) { - if(localVar.containsKey(typedId.getName())) { - throw new RuntimeException("Variable " + typedId.getName() + " already exists"); + if(localVar.containsKey(name)) { + throw new RuntimeException("Variable " + name + " already exists"); } - localVar.put(typedId.getName(), type); + localVar.put(name, type); return type; } @@ -32,7 +32,7 @@ public final class TypedLocalVariable implements TypedNode { LocalVariable untypedLocalVariable = (LocalVariable) unTypedAST; TypedLocalVariable typedLocalVariable = new TypedLocalVariable(); - typedLocalVariable.setTypedId((TypedId) typedId.convertToTypedAST(localVar, classes, untypedLocalVariable.name())); + typedLocalVariable.setName(untypedLocalVariable.name().name()); typedLocalVariable.setType(TypeMapper.getType(untypedLocalVariable.type())); return typedLocalVariable; diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java index 5536350..167258d 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethod.java @@ -14,17 +14,17 @@ import java.util.Map; @Data public class TypedMethod implements TypedNode { private Boolean isPublic; - private TypedId typedId; + private String name; private Type returnType; private List typedParameters; private TypedBlock typedBlock; @Override public Type typeCheck(Map localVar, Map classes) { - if(localVar.containsKey(typedId.getName())) { - throw new RuntimeException("Method " + typedId.getName() + " already exists"); + if(localVar.containsKey(name)) { + throw new RuntimeException("Method " + name + " already exists"); } - localVar.put(typedId.getName(), returnType); + localVar.put(name, returnType); for(TypedParameter typedParameter : typedParameters) { typedParameter.typeCheck(localVar, classes); } @@ -36,7 +36,7 @@ public class TypedMethod implements TypedNode { Method untypedMethod = (Method) unTypedAST; TypedMethod typedMethod = new TypedMethod(); - typedMethod.setTypedId((TypedId) typedId.convertToTypedAST(localVar, classes, untypedMethod.id())); + typedMethod.setName(untypedMethod.id().name()); typedMethod.setReturnType(TypeMapper.getReturnType(untypedMethod.type())); if(untypedMethod.params().isEmpty()){ @@ -56,7 +56,7 @@ public class TypedMethod implements TypedNode { private TypedParameter getTypedParameter(Parameter parameter) { TypedParameter typedParameter = new TypedParameter(); - typedParameter.setTypedId(new TypedId(parameter.name())); + typedParameter.setParaName(parameter.name()); if(parameter.type() == de.maishai.ast.Type.CHAR){ typedParameter.setType(Type.CHAR); @@ -73,7 +73,7 @@ public class TypedMethod implements TypedNode { public void codeGen(ClassWriter cw) { int accessModifier = Opcodes.ACC_PUBLIC; // ist laut Andi ok - MethodVisitor mv = cw.visitMethod(accessModifier, typedId.getName(), + MethodVisitor mv = cw.visitMethod(accessModifier, name, CodeGenUtils.generateDescriptor(typedParameters, returnType), null, null); mv.visitCode(); diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java b/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java index b445c3c..035a5ab 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedMethodCall.java @@ -11,7 +11,7 @@ import java.util.Map; public class TypedMethodCall implements TypedExpression, TypedStatement { private Boolean field; private TypedExpression recipient; - private TypedId typedId; + private String name; private List args; @Override diff --git a/src/main/java/de/maishai/typedast/typedclass/TypedParameter.java b/src/main/java/de/maishai/typedast/typedclass/TypedParameter.java index 792e074..c385734 100644 --- a/src/main/java/de/maishai/typedast/typedclass/TypedParameter.java +++ b/src/main/java/de/maishai/typedast/typedclass/TypedParameter.java @@ -12,15 +12,15 @@ import java.util.Map; @RequiredArgsConstructor @Data public class TypedParameter implements TypedNode { - private TypedId typedId; + private String paraName; private Type type; @Override public Type typeCheck(Map localVar, Map classes) { - if (localVar.containsKey(typedId.getName())) { - throw new RuntimeException("Parameter " + typedId.getName() + " already exists"); + if (localVar.containsKey(paraName)) { + throw new RuntimeException("Parameter " + paraName + " already exists"); } - localVar.put(typedId.getName(), type); + localVar.put(paraName, type); return type; }