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
public class TypedAssignment implements TypedStatement {
private TypedId loc;
private String varName;
private TypedExpression value;
@Override
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");
}
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<String, Type> localVar, Map<String, TypedClass> 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;
}

View File

@ -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<TypedField> typedFields;
private List<TypedMethod> typedMethods;
private TypedMainMethod typedMainMethod;
@ -24,17 +24,20 @@ public class TypedClass implements TypedNode {
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> 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

View File

@ -23,17 +23,17 @@ import java.util.Map;
public class TypedConstructor implements TypedNode {
private Boolean isPublic;
private TypedId typedId;
private String name;
private List<TypedParameter> typedParameters;
private TypedBlock typedBlock;
//constructor: PUBLIC? id '(' params? ')' block;
@Override
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");
}
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<String, Type> localVar, Map<String, TypedClass> 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);

View File

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

View File

@ -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<String, Type> localVar, Map<String, TypedClass> 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;
}

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
@NoArgsConstructor
public final class TypedLocalVariable implements TypedNode {
private TypedId typedId;
private String name;
private Type type;
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> 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;

View File

@ -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<TypedParameter> typedParameters;
private TypedBlock typedBlock;
@Override
public Type typeCheck(Map<String, Type> localVar, Map<String, TypedClass> 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();

View File

@ -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<TypedExpression> args;
@Override

View File

@ -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<String, Type> localVar, Map<String, TypedClass> 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;
}