Refactored TypedAST and changed visibility of methods

This commit is contained in:
ahmad 2024-07-03 23:27:34 +02:00
parent 371480a790
commit a80a828dbf
22 changed files with 61 additions and 70 deletions

View File

@ -27,7 +27,7 @@ public class TypedAssignment implements TypedStatement {
typeCheck(typedProgram);
}
public void convertToTypedAssignment(TypedProgram typedProgram, Assignment untyped) {
private void convertToTypedAssignment(TypedProgram typedProgram, Assignment untyped) {
value = convertExpression(typedProgram, untyped.value());
location = new TypedFieldVarAccess(typedProgram, untyped.location());
}
@ -36,7 +36,7 @@ public class TypedAssignment implements TypedStatement {
public Type typeCheck(TypedProgram typedProgram) {
TypedClass currentClass = typedProgram.getCurrentClass();
if (currentClass.isCurrentMainMethodPresent() && location.getField() && location.getRecursiveOwnerChain() == null) {
if (Boolean.TRUE.equals(currentClass.isCurrentMainMethodPresent() && location.getField()) && location.getRecursiveOwnerChain() == null) {
throw new RuntimeException("Main Method, is not allowed to have fields, they are not static");
}

View File

@ -27,7 +27,7 @@ public class TypedBinary implements TypedExpression {
typeCheck(typedProgram);
}
public void convertToTypedBinary(TypedProgram typedProgram, Binary unTypedBinary) {
private void convertToTypedBinary(TypedProgram typedProgram, Binary unTypedBinary) {
left = convertExpression(typedProgram, unTypedBinary.left());
right = convertExpression(typedProgram, unTypedBinary.right());
op = unTypedBinary.op();

View File

@ -31,7 +31,7 @@ public class TypedBlock implements TypedNode {
this.stmts = stmts;
}
public void convertToTypedBlock(TypedProgram typedProgram, Block unTypedBlock) {
private void convertToTypedBlock(TypedProgram typedProgram, Block unTypedBlock) {
if (unTypedBlock == null) {
return;

View File

@ -21,7 +21,7 @@ public class TypedBoolLiteral implements TypedExpression {
convertToTypedBoolLiteral(unTypedBoolLiteral);
}
public void convertToTypedBoolLiteral(BoolLiteral unTypedBoolLiteral) {
private void convertToTypedBoolLiteral(BoolLiteral unTypedBoolLiteral) {
value = unTypedBoolLiteral.value();
type = Type.BOOL;
}

View File

@ -17,7 +17,7 @@ public class TypedCharLiteral implements TypedExpression {
convertToCharLiteral(unTypedCharLiteral);
}
public void convertToCharLiteral(CharLiteral unTypedCharLiteral) {
private void convertToCharLiteral(CharLiteral unTypedCharLiteral) {
value = unTypedCharLiteral.value();
type = Type.CHAR;
}

View File

@ -56,7 +56,7 @@ public class TypedClass implements TypedNode {
typedDeclarations.add(new TypedDeclaration(typedProgram, declaration));
}
for (Constructor constructor : c.constructors()) {
typedConstructors.add(new TypedConstructor(typedProgram, constructor, this.className));
typedConstructors.add(new TypedConstructor(constructor, this.className));
}
for (Method method : c.methods()) {

View File

@ -32,28 +32,33 @@ public class TypedConstructor implements TypedNode {
this.typedParameters = typedParameters;
this.typedBlock = typedBlock;
}
public TypedConstructor(TypedProgram typedProgram, Constructor unTypedConstructor, String className) {
convertToTypedConstructor(typedProgram, unTypedConstructor, className);
public TypedConstructor(Constructor unTypedConstructor, String className) {
convertToTypedConstructor(unTypedConstructor, className);
}
public void convertToTypedConstructor(TypedProgram typedProgram, Constructor unTypedConstructor, String className) {
private void convertToTypedConstructor(Constructor unTypedConstructor, String className) {
if(!unTypedConstructor.className().equals(className)) {
throw new RuntimeException("Constructor name "+ unTypedConstructor.className() +" must be the same as class name" + className);
}
name = unTypedConstructor.className();
convertToTypedParameter(typedProgram, unTypedConstructor.params());
convertToTypedParameter(unTypedConstructor.params());
type = Type.VOID;
}
private void convertToTypedParameter(TypedProgram typedProgram, List<Parameter> params) {
private void convertToTypedParameter(List<Parameter> params) {
for (Parameter param : params) {
TypedParameter typedParameter = new TypedParameter(typedProgram, param);
TypedParameter typedParameter = new TypedParameter(param);
checkIfParameterExists(typedParameter.getParaName());
typedParameters.add(typedParameter);
localVariables.add(new TypedLocalVariable(typedParameter.getParaName(), typedParameter.getType()));
}
}
public void convertToBlock(TypedProgram typedProgram, Constructor unTypedConstructor) {
this.typedBlock = new TypedBlock(typedProgram, unTypedConstructor.block());
typeCheck(typedProgram);
}
@Override
public Type typeCheck(TypedProgram typedProgram) {
type = typedBlock.typeCheck(typedProgram);
@ -64,8 +69,6 @@ public class TypedConstructor implements TypedNode {
return type;
}
public boolean isLocalVariablePresent(String localVarName) {
return localVariables.stream().anyMatch(localVariable -> localVariable.getName().equals(localVarName));
}
@ -88,11 +91,6 @@ public class TypedConstructor implements TypedNode {
}
}
public void convertToBlock(TypedProgram typedProgram, Constructor unTypedConstructor) {
this.typedBlock = new TypedBlock(typedProgram, unTypedConstructor.block());
typeCheck(typedProgram);
}
public void deleteLocalVariableInConstructor(String localVarName) {
localVariables.removeIf(localVariable -> localVariable.getName().equals(localVarName));
}

View File

@ -19,23 +19,21 @@ public final class TypedDeclaration implements TypedNode {
public TypedDeclaration(TypedProgram typedProgram, Declaration declaration) {
convertToTypedDeclaration(typedProgram, declaration);
typeCheck(typedProgram);
}
public void convertToTypedDeclaration(TypedProgram typedProgram, Declaration declaration) {
private void convertToTypedDeclaration(TypedProgram typedProgram, Declaration declaration) {
name = declaration.name();
type = declaration.type();
typeCheck(typedProgram);
}
@Override
public Type typeCheck(TypedProgram typedProgram) {
TypedClass currentClass = typedProgram.getCurrentClass();
if (type.getReference() != null && !currentClass.getClassName().equals(type.getReference())) {
if (!typedProgram.isTypedClassPresent(type.getReference())) {
if (type.getReference() != null && !currentClass.getClassName().equals(type.getReference()) && !typedProgram.isTypedClassPresent(type.getReference())) {
throw new RuntimeException("Type " + type.getReference() + " not found");
}
}
if (currentClass.isThereField(name)) {
throw new RuntimeException("Field " + name + " already declared");

View File

@ -21,7 +21,7 @@ public class TypedDoWhile implements TypedStatement {
typeCheck(typedProgram);
}
public void convertToTypedDoWhile(TypedProgram typedProgram, DoWhile unTypedDoWhile) {
private void convertToTypedDoWhile(TypedProgram typedProgram, DoWhile unTypedDoWhile) {
typedBlock = new TypedBlock(typedProgram, unTypedDoWhile.block());
cond = convertExpression(typedProgram, unTypedDoWhile.cond());
}

View File

@ -30,7 +30,7 @@ public class TypedFieldVarAccess implements TypedExpression {
typeCheck(typedProgram);
}
public void convertToTypedFieldVarAccess(TypedProgram typedProgram, FieldVarAccess unTypedFieldVarAccess) {
private void convertToTypedFieldVarAccess(TypedProgram typedProgram, FieldVarAccess unTypedFieldVarAccess) {
field = unTypedFieldVarAccess.field();
recursiveOwnerChain = convertExpression(typedProgram, unTypedFieldVarAccess.recursiveOwnerChain());
name = unTypedFieldVarAccess.id();

View File

@ -25,7 +25,7 @@ public class TypedFor implements TypedStatement {
typeCheck(typedProgram);
}
public void convertToTypedFor(TypedProgram typedProgram, For unTypedFor) {
private void convertToTypedFor(TypedProgram typedProgram, For unTypedFor) {
assign = new TypedAssignment(typedProgram, unTypedFor.assign());
cond = convertExpression(typedProgram, unTypedFor.cond());
inc = new TypedAssignment(typedProgram, unTypedFor.inc());

View File

@ -24,7 +24,7 @@ public class TypedIfElse implements TypedStatement {
typeCheck(typedProgram);
}
public void convertToTypedIfElse(TypedProgram typedProgram, IfElse unTypedIfElse) {
private void convertToTypedIfElse(TypedProgram typedProgram, IfElse unTypedIfElse) {
ifTypedBlock = new TypedBlock(typedProgram, unTypedIfElse.ifBlock());
elseTypedBlock = new TypedBlock(typedProgram, unTypedIfElse.elseBlock());
typedCon = convertExpression(typedProgram, unTypedIfElse.cond());

View File

@ -20,7 +20,7 @@ public class TypedIntLiteral implements TypedExpression {
convertToTypedIntLiteral(unTypedIntLiteral);
}
public void convertToTypedIntLiteral(IntLiteral unTypedIntLiteral) {
private void convertToTypedIntLiteral(IntLiteral unTypedIntLiteral) {
value = unTypedIntLiteral.value();
type = Type.INT;
}

View File

@ -17,13 +17,13 @@ public final class TypedLocalVariable implements TypedNode {
private Type type;
public TypedLocalVariable(TypedProgram typedProgram, Declaration declaration) {
convertToTypedLocalVariable(typedProgram, declaration);
convertToTypedLocalVariable(declaration);
typeCheck(typedProgram);
}
public void convertToTypedLocalVariable(TypedProgram typedProgram, Declaration declaration) {
private void convertToTypedLocalVariable(Declaration declaration) {
name = declaration.name();
type = declaration.type();
typeCheck(typedProgram);
}
@Override

View File

@ -26,18 +26,24 @@ public class TypedMethod implements TypedNode {
convertToTypedMethod(typedProgram, unTypedMethod);
}
public void convertToTypedMethod(TypedProgram typedProgram, Method unTypedMethod) {
private void convertToTypedMethod(TypedProgram typedProgram, Method unTypedMethod) {
TypedClass currentClass = typedProgram.getCurrentClass();
name = unTypedMethod.methodName();
returnType = unTypedMethod.type();
convertToTypedParameter(typedProgram, unTypedMethod.params());
convertToTypedParameter(unTypedMethod.params());
currentClass.checkMethodExists(this);
}
private void convertToTypedParameter(TypedProgram typedProgram, List<Parameter> params) {
public void convertToTypedBlock(TypedProgram typedProgram, Method unTypedMethod) {
typedBlock = new TypedBlock(typedProgram, unTypedMethod.block());
typeCheck(typedProgram);
localVariables.clear();
}
private void convertToTypedParameter(List<Parameter> params) {
for (Parameter param : params) {
TypedParameter typedParameter = new TypedParameter(typedProgram, param);
TypedParameter typedParameter = new TypedParameter(param);
checkIfParameterExists(typedParameter.getParaName());
typedParameters.add(typedParameter);
localVariables.add(new TypedLocalVariable(typedParameter.getParaName(), typedParameter.getType()));
@ -51,12 +57,6 @@ public class TypedMethod implements TypedNode {
}
public void convertToTypedBlock(TypedProgram typedProgram, Method unTypedMethod) {
typedBlock = new TypedBlock(typedProgram, unTypedMethod.block());
typeCheck(typedProgram);
localVariables.clear();
}
public boolean isLocalVariablePresent(String localVarName) {
return localVariables.stream().anyMatch(localVariable -> localVariable.getName().equals(localVarName));
}

View File

@ -28,9 +28,8 @@ public class TypedMethodCall implements TypedExpression, TypedStatement {
typeCheck(typedProgram);
}
public void convertToTypedMethodCall(TypedProgram typedProgram, MethodCall unTypedMethodCall) {
private void convertToTypedMethodCall(TypedProgram typedProgram, MethodCall unTypedMethodCall) {
recipient = new TypedFieldVarAccess(typedProgram, unTypedMethodCall.recipient());
//recipient.typeCheck(typedProgram);
for (Expression arg : unTypedMethodCall.args()) {
args.add(convertExpression(typedProgram, arg));
}
@ -40,6 +39,7 @@ public class TypedMethodCall implements TypedExpression, TypedStatement {
public Type typeCheck(TypedProgram typedProgram) {
TypedClass currentClass = typedProgram.getCurrentClass();
String ownerChainName = null;
if (currentClass.isCurrentMainMethodPresent() && recipient.getRecursiveOwnerChain() == null) {
throw new RuntimeException("Main Method, is not allowed to have methods, they are not static");
}

View File

@ -23,7 +23,7 @@ public class TypedNew implements TypedExpression, TypedStatement {
typeCheck(typedProgram);
}
public void convertToTypedNew(TypedProgram typedProgram, New unTypedNew) {
private void convertToTypedNew(TypedProgram typedProgram, New unTypedNew) {
type = unTypedNew.type();
for (Expression arg : unTypedNew.args()) {
args.add(convertExpression(typedProgram, arg));
@ -32,8 +32,6 @@ public class TypedNew implements TypedExpression, TypedStatement {
@Override
public Type typeCheck(TypedProgram typedProgram) {
TypedClass currentClass = typedProgram.getCurrentClass();
if (typedProgram.isTypedClassPresent(type.getReference())) {
return Type.REFERENCE(type.getReference());
}

View File

@ -12,25 +12,14 @@ public class TypedParameter implements TypedNode {
private String paraName;
private Type type;
public TypedParameter(TypedProgram typedProgram, Parameter unTypedParameter) {
convertToTypedParameter(typedProgram, unTypedParameter);
public TypedParameter(Parameter unTypedParameter) {
convertToTypedParameter(unTypedParameter);
}
public void convertToTypedParameter(TypedProgram typedProgram, Parameter unTypedParameter) {
private void convertToTypedParameter(Parameter unTypedParameter) {
paraName = unTypedParameter.name();
type = unTypedParameter.type();
}
@Override
public boolean equals(Object obj) {
if (obj instanceof TypedParameter) {
TypedParameter other = (TypedParameter) obj;
return paraName.equals(other.paraName) && type.equals(other.type);
}
return false;
}
@Override
public Type typeCheck(TypedProgram typedProgram) {
@ -47,5 +36,14 @@ public class TypedParameter implements TypedNode {
return type;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof TypedParameter) {
TypedParameter other = (TypedParameter) obj;
return paraName.equals(other.paraName) && type.equals(other.type);
}
return false;
}
}

View File

@ -60,11 +60,11 @@ public class TypedProgram {
return typedClasses.stream().anyMatch(clas -> clas.getClassName().equals(className));
}
public void enterCurrentClass(TypedClass clas) {
private void enterCurrentClass(TypedClass clas) {
currentClass = clas;
}
public void exitCurrentClass() {
private void exitCurrentClass() {
currentClass = null;
}

View File

@ -25,7 +25,7 @@ public class TypedReturn implements TypedStatement {
typeCheck(typedProgram);
}
public void convertToTypedReturn(TypedProgram typedProgram, Return unTypedReturn) {
private void convertToTypedReturn(TypedProgram typedProgram, Return unTypedReturn) {
ret = convertExpression(typedProgram, unTypedReturn.ret());
if (ret == null) {
type = Type.VOID;
@ -39,8 +39,7 @@ public class TypedReturn implements TypedStatement {
TypedClass currentClass = typedProgram.getCurrentClass();
TypedMethod currentMethod = currentClass.getCurrentMethod();
if (currentClass.isCurrentMethodPresent()) {
if (currentMethod.getReturnType().getKind() != this.type.getKind()) {
if (currentClass.isCurrentMethodPresent() && currentMethod.getReturnType().getKind() != this.type.getKind()) {
StringBuilder exp = new StringBuilder();
exp.append("\nMismatched return type: ");
exp.append("\nExpected: ").append(currentMethod.getReturnType().getKind());
@ -48,7 +47,7 @@ public class TypedReturn implements TypedStatement {
exp.append("\nMethod name: ").append(currentMethod.getName());
throw new RuntimeException(exp.toString());
}
}
return type;
}

View File

@ -23,7 +23,7 @@ public class TypedUnary implements TypedExpression {
typeCheck(typedProgram);
}
public void convertToTypedUnary(TypedProgram typedProgram, Unary unTypedUnary) {
private void convertToTypedUnary(TypedProgram typedProgram, Unary unTypedUnary) {
op = unTypedUnary.op();
right = convertExpression(typedProgram, unTypedUnary.right());
}

View File

@ -24,7 +24,7 @@ public class TypedWhile implements TypedStatement {
typeCheck(typedProgram);
}
public void convertToTypedWhile(TypedProgram typedProgram, While unTypedWhile) {
private void convertToTypedWhile(TypedProgram typedProgram, While unTypedWhile) {
cond = convertExpression(typedProgram, unTypedWhile.cond());
typedBlock = new TypedBlock(typedProgram, unTypedWhile.block());
}