Deleted TypedId class not required in typedastg

This commit is contained in:
Ahmad 2024-05-08 11:24:34 +02:00
parent 6c1030f888
commit 591dee0d48
10 changed files with 47 additions and 76 deletions

View File

@ -10,30 +10,29 @@ import java.util.Map;
@Data @Data
public class TypedAssignment implements TypedStatement { public class TypedAssignment implements TypedStatement {
private TypedId loc; private String varName;
private TypedExpression value; private TypedExpression value;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if (!localVar.containsKey(loc.getName())) { if (!localVar.containsKey(varName)) {
throw new RuntimeException("Variable not declared"); throw new RuntimeException("Variable not declared");
} }
Type typeLeft = localVar.get(loc.getName()); Type typeLeft = localVar.get(varName);
Type typeRight = value.typeCheck(localVar, classes); Type typeRight = value.typeCheck(localVar, classes);
if (typeLeft.equals(typeRight) ) { if (typeLeft.equals(typeRight) ) {
return typeLeft; 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 @Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) { public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Assignment untyped = (Assignment) unTypedAST; Assignment untyped = (Assignment) unTypedAST;
TypedAssignment typedAssignment = new TypedAssignment(); TypedAssignment typedAssignment = new TypedAssignment();
typedAssignment.setLoc((TypedId) loc.convertToTypedAST(localVar, classes, untyped.loc())); typedAssignment.setVarName(untyped.loc().name());
// typedAssignment.setAssignSign(untyped.assignment());
typedAssignment.setValue((TypedExpression) value.convertToTypedAST(localVar, classes, untyped.value())); typedAssignment.setValue((TypedExpression) value.convertToTypedAST(localVar, classes, untyped.value()));
return typedAssignment; return typedAssignment;
} }

View File

@ -15,7 +15,7 @@ import java.util.Map;
@Data @Data
public class TypedClass implements TypedNode { public class TypedClass implements TypedNode {
private Boolean isPublic; private Boolean isPublic;
private TypedId typedId; private String className;
private List<TypedField> typedFields; private List<TypedField> typedFields;
private List<TypedMethod> typedMethods; private List<TypedMethod> typedMethods;
private TypedMainMethod typedMainMethod; private TypedMainMethod typedMainMethod;
@ -24,17 +24,20 @@ public class TypedClass implements TypedNode {
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if (classes.containsKey(typedId.getName())) { if (classes.containsKey(className)) {
throw new RuntimeException("Class " + typedId.getName() + " already exists"); throw new RuntimeException("Class " + className + " already exists");
} }
classes.put(typedId.getName(), this); classes.put(className, this);
for (TypedField field : typedFields) { for (TypedField field : typedFields) {
field.typeCheck(localVar, classes); field.typeCheck(localVar, classes);
} }
for(TypedConstructor constructor : typedConstructors) {
constructor.typeCheck(localVar, classes);
}
for (TypedMethod typedMethod : typedMethods) { for (TypedMethod typedMethod : typedMethods) {
typedMethod.typeCheck(localVar, classes); typedMethod.typeCheck(localVar, classes);
} }
return Type.REFERENCE(typedId.getName()); return Type.REFERENCE(className);
} }
public TypedNode startConversion(de.maishai.ast.records.Class c) { public TypedNode startConversion(de.maishai.ast.records.Class c) {
@ -65,7 +68,7 @@ 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, 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) { for (TypedField field : typedFields) {
field.codeGen(cw); field.codeGen(cw);
} }
@ -84,15 +87,15 @@ public class TypedClass implements TypedNode {
public static void main(String[] args) { public static void main(String[] args) {
TypedClass c = new TypedClass(); TypedClass c = new TypedClass();
c.typedId = new TypedId("SomeClass"); c.setClassName("SomeClass");
//Fields //Fields
TypedField f1 = new TypedField(new TypedId("someNumber"), Type.INT); TypedField f1 = new TypedField("someNumber", Type.INT);
TypedField f2 = new TypedField(new TypedId("someChar"), Type.CHAR); TypedField f2 = new TypedField("someChar", Type.CHAR);
c.typedFields = List.of(f1, f2); c.typedFields = List.of(f1, f2);
//Constructors //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); c.typedConstructors = List.of(constructor);
//Methods //Methods

View File

@ -23,17 +23,17 @@ import java.util.Map;
public class TypedConstructor implements TypedNode { public class TypedConstructor implements TypedNode {
private Boolean isPublic; private Boolean isPublic;
private TypedId typedId; private String name;
private List<TypedParameter> typedParameters; private List<TypedParameter> typedParameters;
private TypedBlock typedBlock; private TypedBlock typedBlock;
//constructor: PUBLIC? id '(' params? ')' block; //constructor: PUBLIC? id '(' params? ')' block;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if (localVar.containsKey(typedId.getName())) { if (localVar.containsKey(name)) {
throw new RuntimeException("constructor already declared"); throw new RuntimeException("constructor already declared");
} }
localVar.put(typedId.getName(), Type.VOID); localVar.put(name, Type.VOID);
if (typedParameters != null) { if (typedParameters != null) {
for (TypedParameter param : typedParameters) { for (TypedParameter param : typedParameters) {
param.typeCheck(localVar, classes); param.typeCheck(localVar, classes);
@ -47,7 +47,7 @@ public class TypedConstructor implements TypedNode {
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) { public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Constructor untyped = (Constructor) unTypedAST; Constructor untyped = (Constructor) unTypedAST;
TypedConstructor typedConstructor = new TypedConstructor(); TypedConstructor typedConstructor = new TypedConstructor();
typedConstructor.setTypedId((TypedId) typedId.convertToTypedAST(localVar, classes, untyped.id())); typedConstructor.setName(untyped.id().name());
if (untyped.params().isEmpty()) { if (untyped.params().isEmpty()) {
typedConstructor.setTypedParameters(null); typedConstructor.setTypedParameters(null);

View File

@ -16,15 +16,15 @@ import java.util.Map;
@RequiredArgsConstructor @RequiredArgsConstructor
@Data @Data
public class TypedField implements TypedNode { public class TypedField implements TypedNode {
private TypedId typedId; private String varName;
private Type type; private Type type;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if (localVar.containsKey(typedId.getName())) { if (localVar.containsKey(varName)) {
throw new RuntimeException("Variable " + typedId.getName() + " already declared"); throw new RuntimeException("Variable " + varName + " already declared");
} }
localVar.put(typedId.getName(), type); localVar.put(varName, type);
return type; return type;
} }
@ -32,7 +32,7 @@ public class TypedField implements TypedNode {
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) { public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Field untypedField = (Field) unTypedAST; Field untypedField = (Field) unTypedAST;
TypedField typedField = new TypedField(); TypedField typedField = new TypedField();
typedField.setTypedId((TypedId) typedId.convertToTypedAST(localVar, classes, untypedField.name())); typedField.setVarName(untypedField.name().name());
typedField.setType(TypeMapper.getType(untypedField.type())); typedField.setType(TypeMapper.getType(untypedField.type()));
return typedField; return typedField;
@ -40,6 +40,6 @@ public class TypedField implements TypedNode {
public void codeGen(ClassWriter cw) { public void codeGen(ClassWriter cw) {
int access = Opcodes.ACC_PUBLIC; // laut Andi ist es ok, dass alle Felder public sind 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();
} }
} }

View File

@ -11,7 +11,7 @@ import java.util.Map;
public class TypedFieldId implements TypedExpression { public class TypedFieldId implements TypedExpression {
private Boolean field; private Boolean field;
private TypedExpression recipient; private TypedExpression recipient;
private TypedId typedId; private String name;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
//TODO: Implement typeCheck for FieldId //TODO: Implement typeCheck for FieldId
@ -28,7 +28,7 @@ public class TypedFieldId implements TypedExpression {
TypedFieldId typedFieldId = new TypedFieldId(); TypedFieldId typedFieldId = new TypedFieldId();
typedFieldId.setField(untypedFieldId.field()); typedFieldId.setField(untypedFieldId.field());
typedFieldId.setRecipient((TypedExpression) recipient.convertToTypedAST(localVar, classes, untypedFieldId.recipient())); 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; return typedFieldId;
} }

View File

@ -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<String, Type> localVar, Map<String, TypedClass> classes) {
if (!localVar.containsKey(name)) {
throw new RuntimeException("Variable " + name + " not declared or not found");
}
return localVar.get(name);
}
@Override
public TypedNode convertToTypedAST(Map<String, Type> localVar, Map<String, TypedClass> classes, de.maishai.ast.records.Node unTypedAST) {
Id untypedId = (Id) unTypedAST;
return new TypedId(untypedId.name());
}
}

View File

@ -15,15 +15,15 @@ import java.util.Map;
@AllArgsConstructor @AllArgsConstructor
@NoArgsConstructor @NoArgsConstructor
public final class TypedLocalVariable implements TypedNode { public final class TypedLocalVariable implements TypedNode {
private TypedId typedId; private String name;
private Type type; private Type type;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if(localVar.containsKey(typedId.getName())) { if(localVar.containsKey(name)) {
throw new RuntimeException("Variable " + typedId.getName() + " already exists"); throw new RuntimeException("Variable " + name + " already exists");
} }
localVar.put(typedId.getName(), type); localVar.put(name, type);
return type; return type;
} }
@ -32,7 +32,7 @@ public final class TypedLocalVariable implements TypedNode {
LocalVariable untypedLocalVariable = (LocalVariable) unTypedAST; LocalVariable untypedLocalVariable = (LocalVariable) unTypedAST;
TypedLocalVariable typedLocalVariable = new TypedLocalVariable(); TypedLocalVariable typedLocalVariable = new TypedLocalVariable();
typedLocalVariable.setTypedId((TypedId) typedId.convertToTypedAST(localVar, classes, untypedLocalVariable.name())); typedLocalVariable.setName(untypedLocalVariable.name().name());
typedLocalVariable.setType(TypeMapper.getType(untypedLocalVariable.type())); typedLocalVariable.setType(TypeMapper.getType(untypedLocalVariable.type()));
return typedLocalVariable; return typedLocalVariable;

View File

@ -14,17 +14,17 @@ import java.util.Map;
@Data @Data
public class TypedMethod implements TypedNode { public class TypedMethod implements TypedNode {
private Boolean isPublic; private Boolean isPublic;
private TypedId typedId; private String name;
private Type returnType; private Type returnType;
private List<TypedParameter> typedParameters; private List<TypedParameter> typedParameters;
private TypedBlock typedBlock; private TypedBlock typedBlock;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if(localVar.containsKey(typedId.getName())) { if(localVar.containsKey(name)) {
throw new RuntimeException("Method " + typedId.getName() + " already exists"); throw new RuntimeException("Method " + name + " already exists");
} }
localVar.put(typedId.getName(), returnType); localVar.put(name, returnType);
for(TypedParameter typedParameter : typedParameters) { for(TypedParameter typedParameter : typedParameters) {
typedParameter.typeCheck(localVar, classes); typedParameter.typeCheck(localVar, classes);
} }
@ -36,7 +36,7 @@ public class TypedMethod implements TypedNode {
Method untypedMethod = (Method) unTypedAST; Method untypedMethod = (Method) unTypedAST;
TypedMethod typedMethod = new TypedMethod(); TypedMethod typedMethod = new TypedMethod();
typedMethod.setTypedId((TypedId) typedId.convertToTypedAST(localVar, classes, untypedMethod.id())); typedMethod.setName(untypedMethod.id().name());
typedMethod.setReturnType(TypeMapper.getReturnType(untypedMethod.type())); typedMethod.setReturnType(TypeMapper.getReturnType(untypedMethod.type()));
if(untypedMethod.params().isEmpty()){ if(untypedMethod.params().isEmpty()){
@ -56,7 +56,7 @@ public class TypedMethod implements TypedNode {
private TypedParameter getTypedParameter(Parameter parameter) { private TypedParameter getTypedParameter(Parameter parameter) {
TypedParameter typedParameter = new TypedParameter(); TypedParameter typedParameter = new TypedParameter();
typedParameter.setTypedId(new TypedId(parameter.name())); typedParameter.setParaName(parameter.name());
if(parameter.type() == de.maishai.ast.Type.CHAR){ if(parameter.type() == de.maishai.ast.Type.CHAR){
typedParameter.setType(Type.CHAR); typedParameter.setType(Type.CHAR);
@ -73,7 +73,7 @@ public class TypedMethod implements TypedNode {
public void codeGen(ClassWriter cw) { public void codeGen(ClassWriter cw) {
int accessModifier = Opcodes.ACC_PUBLIC; // ist laut Andi ok 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); CodeGenUtils.generateDescriptor(typedParameters, returnType), null, null);
mv.visitCode(); mv.visitCode();

View File

@ -11,7 +11,7 @@ import java.util.Map;
public class TypedMethodCall implements TypedExpression, TypedStatement { public class TypedMethodCall implements TypedExpression, TypedStatement {
private Boolean field; private Boolean field;
private TypedExpression recipient; private TypedExpression recipient;
private TypedId typedId; private String name;
private List<TypedExpression> args; private List<TypedExpression> args;
@Override @Override

View File

@ -12,15 +12,15 @@ import java.util.Map;
@RequiredArgsConstructor @RequiredArgsConstructor
@Data @Data
public class TypedParameter implements TypedNode { public class TypedParameter implements TypedNode {
private TypedId typedId; private String paraName;
private Type type; private Type type;
@Override @Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) { public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> classes) {
if (localVar.containsKey(typedId.getName())) { if (localVar.containsKey(paraName)) {
throw new RuntimeException("Parameter " + typedId.getName() + " already exists"); throw new RuntimeException("Parameter " + paraName + " already exists");
} }
localVar.put(typedId.getName(), type); localVar.put(paraName, type);
return type; return type;
} }