mirror of
https://github.com/JonathanFleischmann/CompilerULTIMATE.git
synced 2024-10-06 18:21: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 {
|
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) {
|
private void clearLocalVariable(TypedProgram typedProgram){
|
||||||
chekType = returnStmt.getType();
|
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) {
|
public void codeGen(MethodContext ctx) {
|
||||||
|
@ -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) {
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user