Changed the retrieving of fields is allowed only with this

This commit is contained in:
Ahmad 2024-06-26 19:01:53 +02:00
parent eee1c139a7
commit 4532d220d3
2 changed files with 2 additions and 4 deletions

View File

@ -70,7 +70,7 @@ public class TypedAssignment implements TypedStatement {
if (currentMethod.isLocalVariableInMethod(name)) { if (currentMethod.isLocalVariableInMethod(name)) {
return currentMethod.getLocalVariableType(name); return currentMethod.getLocalVariableType(name);
} else if (currentClass.isThereField(name)) { } else if (currentClass.isThereField(name)) {
return currentClass.getFieldType(name); throw new RuntimeException("Field Variable "+ name + " should be used with `this´");
} else { } else {
throw new RuntimeException("Variable " + name + " not declared in method"); throw new RuntimeException("Variable " + name + " not declared in method");
} }
@ -80,7 +80,7 @@ public class TypedAssignment implements TypedStatement {
if (currentConstructor.isLocalVariableInConstructor(name)) { if (currentConstructor.isLocalVariableInConstructor(name)) {
return currentConstructor.getLocalVariableType(name); return currentConstructor.getLocalVariableType(name);
} else if (currentClass.isThereField(name)) { } else if (currentClass.isThereField(name)) {
return currentClass.getFieldType(name); throw new RuntimeException("Field Variable "+ name+ " should be used with `this´");
} else { } else {
throw new RuntimeException("Variable " + name + " not declared in constructor"); throw new RuntimeException("Variable " + name + " not declared in constructor");
} }

View File

@ -32,14 +32,12 @@ public final class TypedLocalVariable implements TypedNode {
if (typedProgram.getCurrentClass().getCurrentMethod().isLocalVariableInMethod(name)) { if (typedProgram.getCurrentClass().getCurrentMethod().isLocalVariableInMethod(name)) {
throw new RuntimeException("Variable " + name + " already declared"); throw new RuntimeException("Variable " + name + " already declared");
} }
typedProgram.getCurrentClass().getCurrentMethod().getLocalVariables().add(this);
return type; return type;
} }
if (!typedProgram.getCurrentClass().isCurrentMethodPresent() && typedProgram.getCurrentClass().isCurrentConstructorPresent()) { if (!typedProgram.getCurrentClass().isCurrentMethodPresent() && typedProgram.getCurrentClass().isCurrentConstructorPresent()) {
if (typedProgram.getCurrentClass().getCurrentConstructor().isLocalVariableInConstructor(name)) { if (typedProgram.getCurrentClass().getCurrentConstructor().isLocalVariableInConstructor(name)) {
throw new RuntimeException("Variable " + name + " already declared"); throw new RuntimeException("Variable " + name + " already declared");
} }
typedProgram.getCurrentClass().getCurrentConstructor().getLocalVariables().add(this);
return type; return type;
} }
throw new RuntimeException("not found method or constructor in class"); throw new RuntimeException("not found method or constructor in class");