Fixed Scope Issue, local variables not accessible outsidte their block

This commit is contained in:
Ahmad 2024-06-22 13:37:11 +02:00
parent 24c01a3f34
commit 72733f4612
3 changed files with 22 additions and 13 deletions

View File

@ -18,11 +18,12 @@ import java.util.List;
public class TypedBlock implements TypedNode { public class TypedBlock implements TypedNode {
private List<TypedLocalVariable> vars = new ArrayList<>(); private List<TypedLocalVariable> vars = new ArrayList<>();
private List<TypedStatement> stmts = new ArrayList<>(); private List<TypedStatement> stmts = new ArrayList<>();
private Type type; private Type type = Type.VOID;
public TypedBlock(TypedProgram typedProgram, Block unTypedBlock) { public TypedBlock(TypedProgram typedProgram, Block unTypedBlock) {
convertToTypedBlock(typedProgram, unTypedBlock); convertToTypedBlock(typedProgram, unTypedBlock);
clearLocalVariable(typedProgram);
} }
public TypedBlock(List<TypedLocalVariable> vars, List<TypedStatement> stmts) { public TypedBlock(List<TypedLocalVariable> vars, List<TypedStatement> stmts) {
@ -89,24 +90,26 @@ public class TypedBlock implements TypedNode {
stmts.add(typedPrint); stmts.add(typedPrint);
} }
} }
this.typeCheck(typedProgram);
} }
@Override @Override
public Type typeCheck(TypedProgram typedProgram) { public Type typeCheck(TypedProgram typedProgram) {
Type chekType = null; return type;
for (TypedStatement stmt : stmts) {
stmt.typeCheck(typedProgram);
if (stmt instanceof TypedReturn returnStmt) {
chekType = returnStmt.getType();
}
} }
if (chekType == null) { private void clearLocalVariable(TypedProgram typedProgram){
chekType = Type.VOID; for(var typedLocalVariable : vars){
if (typedProgram.getCurrentClass().isCurrentMethodPresent() && !typedProgram.getCurrentClass().isCurrentConstructorPresent()) {
if (typedProgram.getCurrentClass().getCurrentMethod().isLocalVariableInMethod(typedLocalVariable.getName())) {
typedProgram.getCurrentClass().getCurrentMethod().deleteLocalVariableInMethod(typedLocalVariable.getName());
}
}
if (!typedProgram.getCurrentClass().isCurrentMethodPresent() && typedProgram.getCurrentClass().isCurrentConstructorPresent()) {
if (typedProgram.getCurrentClass().getCurrentConstructor().isLocalVariableInConstructor(typedLocalVariable.getName())) {
typedProgram.getCurrentClass().getCurrentConstructor().deleteLocalVariableInConstructor(typedLocalVariable.getName());
}
}
} }
type = chekType;
return type;
} }
public void codeGen(MethodContext ctx) { public void codeGen(MethodContext ctx) {

View File

@ -71,6 +71,9 @@ public class TypedConstructor implements TypedNode {
this.typedBlock = new TypedBlock(typedProgram, unTypedConstructor.block()); this.typedBlock = new TypedBlock(typedProgram, unTypedConstructor.block());
typeCheck(typedProgram); typeCheck(typedProgram);
} }
public void deleteLocalVariableInConstructor(String localVarName){
localVariables.removeIf(localVariable -> localVariable.getName().equals(localVarName));
}
@Override @Override
public Type typeCheck(TypedProgram typedProgram) { public Type typeCheck(TypedProgram typedProgram) {

View File

@ -66,6 +66,9 @@ public class TypedMethod implements TypedNode {
return isLocalVariablePresent(localVarName) || isParameterPresent(localVarName); return isLocalVariablePresent(localVarName) || isParameterPresent(localVarName);
} }
public void deleteLocalVariableInMethod(String localVarName){
localVariables.removeIf(localVariable -> localVariable.getName().equals(localVarName));
}
public Type getLocalVariableType(String localVarName) { public Type getLocalVariableType(String localVarName) {
return localVariables.stream().filter(localVariable -> localVariable.getName().equals(localVarName)).findFirst().get().getType(); return localVariables.stream().filter(localVariable -> localVariable.getName().equals(localVarName)).findFirst().get().getType();
} }