Refactored typed classes

This commit is contained in:
ahmad 2024-05-15 18:50:43 +02:00
parent 0215d38aa1
commit 5fe0098062
12 changed files with 22 additions and 21 deletions

View File

@ -103,12 +103,12 @@ public class TypedBlock implements TypedNode {
Type chekType = null; Type chekType = null;
for (TypedStatement stmt : stmts) { for (TypedStatement stmt : stmts) {
stmt.typeCheck(typedProgram); stmt.typeCheck(typedProgram);
if(stmt instanceof TypedReturn returnStmt) { if (stmt instanceof TypedReturn returnStmt) {
chekType = returnStmt.getType(); chekType = returnStmt.getType();
} }
} }
if(chekType == null) { if (chekType == null) {
chekType = Type.VOID; chekType = Type.VOID;
} }
type = chekType; type = chekType;

View File

@ -32,9 +32,9 @@ public class TypedBoolLiteral implements TypedExpression {
@Override @Override
public void codeGen(MethodContext ctx) { public void codeGen(MethodContext ctx) {
if(value){ if (value) {
ctx.getMv().visitInsn(Opcodes.ICONST_1); ctx.getMv().visitInsn(Opcodes.ICONST_1);
}else{ } else {
ctx.getMv().visitInsn(Opcodes.ICONST_0); ctx.getMv().visitInsn(Opcodes.ICONST_0);
} }
} }

View File

@ -10,10 +10,6 @@ import lombok.Data;
public class TypedBreak implements TypedStatement { public class TypedBreak implements TypedStatement {
private Type type = Type.VOID; private Type type = Type.VOID;
public TypedBreak convertToTypedBreak(TypedClass clas, Break unTypedBreak) {
return this;
}
@Override @Override
public Type typeCheck(TypedProgram typedProgram) { public Type typeCheck(TypedProgram typedProgram) {
return type; return type;

View File

@ -125,6 +125,7 @@ public class TypedClass implements TypedNode {
} }
return false; return false;
} }
public Type getMethodType(String methodName) { public Type getMethodType(String methodName) {
for (TypedMethod m : typedMethods) { for (TypedMethod m : typedMethods) {
if (m.getName().equals(methodName)) { if (m.getName().equals(methodName)) {

View File

@ -66,6 +66,7 @@ public class TypedConstructor implements TypedNode {
throw new RuntimeException("Parameter " + paraName + " already exists"); throw new RuntimeException("Parameter " + paraName + " already exists");
} }
} }
public void convertToBlock(TypedProgram typedProgram, Constructor unTypedConstructor) { public void convertToBlock(TypedProgram typedProgram, Constructor unTypedConstructor) {
this.typedBlock = new TypedBlock(typedProgram, unTypedConstructor.block()); this.typedBlock = new TypedBlock(typedProgram, unTypedConstructor.block());
typeCheck(typedProgram); typeCheck(typedProgram);

View File

@ -29,7 +29,7 @@ public final class TypedDeclaration implements TypedNode {
@Override @Override
public Type typeCheck(TypedProgram typedProgram) { public Type typeCheck(TypedProgram typedProgram) {
if(type.getReference() != null && !typedProgram.getCurrentClass().getClassName().equals(type.getReference())) { if (type.getReference() != null && !typedProgram.getCurrentClass().getClassName().equals(type.getReference())) {
if (!typedProgram.isTypedClassPresent(type.getReference())) { if (!typedProgram.isTypedClassPresent(type.getReference())) {
throw new RuntimeException("Type " + type.getReference() + " not found"); throw new RuntimeException("Type " + type.getReference() + " not found");
} }

View File

@ -39,11 +39,12 @@ public class TypedFieldVarAccess implements TypedExpression {
return type; return type;
} }
} }
private Type checkFieldOrMethodType(TypedProgram typedProgram) { private Type checkFieldOrMethodType(TypedProgram typedProgram) {
if (typedProgram.getCurrentClass().isThereField(name)) { if (typedProgram.getCurrentClass().isThereField(name)) {
type = typedProgram.getCurrentClass().getFieldType(name); type = typedProgram.getCurrentClass().getFieldType(name);
return type; return type;
} else if(typedProgram.getCurrentClass().isMethodOfCurrentClass(name)) { } else if (typedProgram.getCurrentClass().isMethodOfCurrentClass(name)) {
type = typedProgram.getCurrentClass().getMethodType(name); type = typedProgram.getCurrentClass().getMethodType(name);
return type; return type;
} else if (recursiveOwnerChain instanceof TypedFieldVarAccess typedFieldVarAccess) { } else if (recursiveOwnerChain instanceof TypedFieldVarAccess typedFieldVarAccess) {
@ -53,6 +54,7 @@ public class TypedFieldVarAccess implements TypedExpression {
throw new RuntimeException("Field " + name + " not declared"); throw new RuntimeException("Field " + name + " not declared");
} }
} }
private Type checkVariableType(TypedProgram typedProgram) { private Type checkVariableType(TypedProgram typedProgram) {
if (typedProgram.getCurrentClass().isCurrentConstructorPresent()) { if (typedProgram.getCurrentClass().isCurrentConstructorPresent()) {
return checkConstructorVariableType(typedProgram); return checkConstructorVariableType(typedProgram);
@ -88,10 +90,9 @@ public class TypedFieldVarAccess implements TypedExpression {
private Type checkFieldOrMethodOrRecursiveType(TypedProgram typedProgram) { private Type checkFieldOrMethodOrRecursiveType(TypedProgram typedProgram) {
if (typedProgram.getCurrentClass().isThereField(name)) { if (typedProgram.getCurrentClass().isThereField(name)) {
type = typedProgram.getCurrentClass().getFieldType(name); type = typedProgram.getCurrentClass().getFieldType(name);
}else if(typedProgram.getCurrentClass().isMethodOfCurrentClass(name)) { } else if (typedProgram.getCurrentClass().isMethodOfCurrentClass(name)) {
type = typedProgram.getCurrentClass().getMethodType(name); type = typedProgram.getCurrentClass().getMethodType(name);
} } else if (recursiveOwnerChain instanceof TypedFieldVarAccess typedFieldVarAccess) {
else if (recursiveOwnerChain instanceof TypedFieldVarAccess typedFieldVarAccess) {
type = typedProgram.getCurrentClass().getFieldType(typedFieldVarAccess.getName()); type = typedProgram.getCurrentClass().getFieldType(typedFieldVarAccess.getName());
} else { } else {
throw new RuntimeException("Variable " + name + " not declared"); throw new RuntimeException("Variable " + name + " not declared");

View File

@ -40,6 +40,7 @@ public class TypedMethod implements TypedNode {
}, () -> { }, () -> {
}); });
} }
public void checkIfParameterExists(String parameterName) { public void checkIfParameterExists(String parameterName) {
if (typedParameters.stream().anyMatch(parameter -> parameter.getParaName().equals(parameterName))) { if (typedParameters.stream().anyMatch(parameter -> parameter.getParaName().equals(parameterName))) {
throw new RuntimeException("Parameter " + parameterName + " already exists"); throw new RuntimeException("Parameter " + parameterName + " already exists");

View File

@ -29,7 +29,7 @@ public class TypedNew implements TypedExpression, TypedStatement {
@Override @Override
public Type typeCheck(TypedProgram typedProgram) { public Type typeCheck(TypedProgram typedProgram) {
if(typedProgram.isTypedClassPresent(type.getReference())){ if (typedProgram.isTypedClassPresent(type.getReference())) {
return Type.REFERENCE(type.getReference()); return Type.REFERENCE(type.getReference());
} }

View File

@ -19,7 +19,7 @@ public class TypedProgram {
public void startConversion(Program program) { public void startConversion(Program program) {
for(var clas : program.classes()){ for (var clas : program.classes()) {
typedClasses.add(new TypedClass(clas)); typedClasses.add(new TypedClass(clas));
} }
@ -32,7 +32,7 @@ public class TypedProgram {
} }
int i = 0; int i = 0;
for(var clas : program.classes()){ for (var clas : program.classes()) {
enterCurrentClass(typedClasses.get(i)); enterCurrentClass(typedClasses.get(i));
typedClasses.get(i).covertBlocksOfConstructorsAndMethods(this, clas); typedClasses.get(i).covertBlocksOfConstructorsAndMethods(this, clas);
exitCurrentClass(); exitCurrentClass();
@ -43,6 +43,7 @@ public class TypedProgram {
public TypedClass getTypedClass(String className) { public TypedClass getTypedClass(String className) {
return typedClasses.stream().filter(clas -> clas.getClassName().equals(className)).findFirst().get(); return typedClasses.stream().filter(clas -> clas.getClassName().equals(className)).findFirst().get();
} }
public boolean isTypedClassPresent(String className) { public boolean isTypedClassPresent(String className) {
return typedClasses.stream().anyMatch(clas -> clas.getClassName().equals(className)); return typedClasses.stream().anyMatch(clas -> clas.getClassName().equals(className));
} }

View File

@ -21,9 +21,9 @@ public class TypedReturn implements TypedStatement {
public void convertToTypedReturn(TypedProgram typedProgram, Return unTypedReturn) { public void convertToTypedReturn(TypedProgram typedProgram, Return unTypedReturn) {
ret = convertExpression(typedProgram, unTypedReturn.ret()); ret = convertExpression(typedProgram, unTypedReturn.ret());
if(ret == null){ if (ret == null) {
type = Type.VOID; type = Type.VOID;
}else{ } else {
type = ret.getType(); type = ret.getType();
} }
} }

View File

@ -47,11 +47,11 @@ public class TypedUnary implements TypedExpression {
@Override @Override
public void codeGen(MethodContext ctx) { public void codeGen(MethodContext ctx) {
right.codeGen(ctx); right.codeGen(ctx);
if(op == UnaryOperator.NOT){ if (op == UnaryOperator.NOT) {
ctx.getMv().visitInsn(Opcodes.ICONST_1); ctx.getMv().visitInsn(Opcodes.ICONST_1);
ctx.getMv().visitInsn(Opcodes.IXOR); ctx.getMv().visitInsn(Opcodes.IXOR);
} }
if(op == UnaryOperator.SUB){ if (op == UnaryOperator.SUB) {
ctx.getMv().visitInsn(Opcodes.INEG); ctx.getMv().visitInsn(Opcodes.INEG);
} }
} }