mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-10-06 14:31:40 +00:00
Fixed Scope Issue, local variables not accessible outsidte their block
This commit is contained in:
parent
24c01a3f34
commit
72733f4612
@ -18,11 +18,12 @@ import java.util.List;
|
||||
public class TypedBlock implements TypedNode {
|
||||
private List<TypedLocalVariable> vars = new ArrayList<>();
|
||||
private List<TypedStatement> stmts = new ArrayList<>();
|
||||
private Type type;
|
||||
private Type type = Type.VOID;
|
||||
|
||||
|
||||
public TypedBlock(TypedProgram typedProgram, Block unTypedBlock) {
|
||||
convertToTypedBlock(typedProgram, unTypedBlock);
|
||||
clearLocalVariable(typedProgram);
|
||||
}
|
||||
|
||||
public TypedBlock(List<TypedLocalVariable> vars, List<TypedStatement> stmts) {
|
||||
@ -89,24 +90,26 @@ public class TypedBlock implements TypedNode {
|
||||
stmts.add(typedPrint);
|
||||
}
|
||||
}
|
||||
this.typeCheck(typedProgram);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
Type chekType = null;
|
||||
for (TypedStatement stmt : stmts) {
|
||||
stmt.typeCheck(typedProgram);
|
||||
if (stmt instanceof TypedReturn returnStmt) {
|
||||
chekType = returnStmt.getType();
|
||||
return type;
|
||||
}
|
||||
|
||||
private void clearLocalVariable(TypedProgram typedProgram){
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (chekType == null) {
|
||||
chekType = Type.VOID;
|
||||
}
|
||||
type = chekType;
|
||||
return type;
|
||||
}
|
||||
|
||||
public void codeGen(MethodContext ctx) {
|
||||
|
@ -71,6 +71,9 @@ public class TypedConstructor implements TypedNode {
|
||||
this.typedBlock = new TypedBlock(typedProgram, unTypedConstructor.block());
|
||||
typeCheck(typedProgram);
|
||||
}
|
||||
public void deleteLocalVariableInConstructor(String localVarName){
|
||||
localVariables.removeIf(localVariable -> localVariable.getName().equals(localVarName));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Type typeCheck(TypedProgram typedProgram) {
|
||||
|
@ -66,6 +66,9 @@ public class TypedMethod implements TypedNode {
|
||||
return isLocalVariablePresent(localVarName) || isParameterPresent(localVarName);
|
||||
}
|
||||
|
||||
public void deleteLocalVariableInMethod(String localVarName){
|
||||
localVariables.removeIf(localVariable -> localVariable.getName().equals(localVarName));
|
||||
}
|
||||
public Type getLocalVariableType(String localVarName) {
|
||||
return localVariables.stream().filter(localVariable -> localVariable.getName().equals(localVarName)).findFirst().get().getType();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user