fixed constructor node parameter decleration
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
This commit is contained in:
parent
71ffb8bb83
commit
561eafbf4c
@ -9,13 +9,10 @@ import java.util.List;
|
|||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
|
||||||
public class ConstructorNode extends MethodNode {
|
public class ConstructorNode extends MethodNode {
|
||||||
public AccessModifierNode accessType;
|
|
||||||
public String identifier;
|
public String identifier;
|
||||||
public List<ParameterNode> parameters = new ArrayList<>();
|
|
||||||
public BlockNode block;
|
|
||||||
|
|
||||||
public ConstructorNode(String accessType, String identifier, BlockNode block) {
|
public ConstructorNode(String accessType, String identifier, BlockNode block) {
|
||||||
this.accessType = new AccessModifierNode(accessType);
|
this.accesModifier = new AccessModifierNode(accessType);
|
||||||
this.identifier = identifier;
|
this.identifier = identifier;
|
||||||
this.block = block;
|
this.block = block;
|
||||||
}
|
}
|
||||||
|
@ -16,7 +16,7 @@ public class Scope {
|
|||||||
|
|
||||||
public void addLocalVar(String name, ITypeNode type) {
|
public void addLocalVar(String name, ITypeNode type) {
|
||||||
if (this.contains(name)) {
|
if (this.contains(name)) {
|
||||||
throw new AlreadyDeclaredException("Variable " + name + " already exists in this scope");
|
SemanticAnalyzer.errors.add(new AlreadyDeclaredException("Duplicate local variable " + name));
|
||||||
}
|
}
|
||||||
localVars.peek().put(name, type);
|
localVars.peek().put(name, type);
|
||||||
}
|
}
|
||||||
|
@ -112,53 +112,49 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(MethodNode methodNode) {
|
public TypeCheckResult analyze(MethodNode methodNode) {
|
||||||
if (methodNode instanceof ConstructorNode) {
|
|
||||||
return new TypeCheckResult(true, new BaseType(TypeEnum.VOID));
|
|
||||||
} else {
|
|
||||||
|
|
||||||
var valid = true;
|
var valid = true;
|
||||||
|
|
||||||
for (var otherMethod : currentClass.getMethods()) {
|
for (var otherMethod : currentClass.getMethods()) {
|
||||||
if (Objects.equals(otherMethod, methodNode))
|
if (Objects.equals(otherMethod, methodNode))
|
||||||
break;
|
break;
|
||||||
if (otherMethod.isSame(methodNode)) {
|
if (otherMethod.isSame(methodNode)) {
|
||||||
errors.add(new AlreadyDeclaredException(
|
errors.add(new AlreadyDeclaredException(
|
||||||
"Method " + methodNode.getIdentifier() + " is already defined in class "
|
"Method " + methodNode.getIdentifier() + " is already defined in class "
|
||||||
+ currentClass.identifier));
|
+ currentClass.identifier));
|
||||||
valid = false;
|
valid = false;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
currentScope.pushScope();
|
currentScope.pushScope();
|
||||||
for (var parameter : methodNode.getParameters()) {
|
for (var parameter : methodNode.getParameters()) {
|
||||||
var result = parameter.accept(this);
|
var result = parameter.accept(this);
|
||||||
valid = valid && result.isValid();
|
|
||||||
try {
|
|
||||||
currentScope.addLocalVar(parameter.identifier, parameter.type);
|
|
||||||
} catch (AlreadyDeclaredException e) {
|
|
||||||
errors.add(new AlreadyDeclaredException(parameter.identifier));
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
currentMethodReturnType = methodNode.getType();
|
|
||||||
currentNullType = currentMethodReturnType;
|
|
||||||
|
|
||||||
var result = methodNode.block.accept(this);
|
|
||||||
valid = valid && result.isValid();
|
valid = valid && result.isValid();
|
||||||
currentScope.popScope();
|
try {
|
||||||
ITypeNode resultType = result.getType();
|
currentScope.addLocalVar(parameter.identifier, parameter.type);
|
||||||
|
} catch (AlreadyDeclaredException e) {
|
||||||
if (resultType == null) {
|
errors.add(new AlreadyDeclaredException(parameter.identifier));
|
||||||
resultType = new BaseType(TypeEnum.VOID);
|
|
||||||
}
|
}
|
||||||
if (methodNode.getType() == null) {
|
|
||||||
methodNode.setType(new BaseType(TypeEnum.VOID));
|
|
||||||
}
|
|
||||||
|
|
||||||
return new TypeCheckResult(valid, resultType);
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
currentMethodReturnType = methodNode.getType();
|
||||||
|
currentNullType = currentMethodReturnType;
|
||||||
|
|
||||||
|
var result = methodNode.block.accept(this);
|
||||||
|
valid = valid && result.isValid();
|
||||||
|
currentScope.popScope();
|
||||||
|
ITypeNode resultType = result.getType();
|
||||||
|
|
||||||
|
if (resultType == null) {
|
||||||
|
resultType = new BaseType(TypeEnum.VOID);
|
||||||
|
}
|
||||||
|
if (methodNode.getType() == null) {
|
||||||
|
methodNode.setType(new BaseType(TypeEnum.VOID));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new TypeCheckResult(valid, resultType);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -224,7 +220,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
}
|
}
|
||||||
for (IStatementNode statementNode : blockNode.statements) {
|
for (IStatementNode statementNode : blockNode.statements) {
|
||||||
var result = statementNode.accept(this);
|
var result = statementNode.accept(this);
|
||||||
if(!(statementNode instanceof IncrementNode) && !(statementNode instanceof DecrementNode)){
|
if (!(statementNode instanceof IncrementNode) && !(statementNode instanceof DecrementNode)) {
|
||||||
if (result.getType() != null) {
|
if (result.getType() != null) {
|
||||||
if (blockReturnType == null) {
|
if (blockReturnType == null) {
|
||||||
blockReturnType = result.getType();
|
blockReturnType = result.getType();
|
||||||
|
@ -1,38 +1,11 @@
|
|||||||
public class AllFeaturesClassExample {
|
public class AllFeaturesClassExample {
|
||||||
int a;
|
|
||||||
boolean b;
|
|
||||||
char c;
|
|
||||||
|
|
||||||
public void controlStructures(int adf, boolean bool) {
|
|
||||||
if (a > (10 + 8)) {
|
|
||||||
} else {
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
while (a > adf) {
|
|
||||||
a--;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < 5; i++) {
|
|
||||||
}
|
|
||||||
|
|
||||||
|
public void controlStructures(int a, boolean b) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// void logicalOperations() {
|
public AllFeaturesClassExample(boolean b){
|
||||||
// Logische UND-Operation
|
|
||||||
// if (b && a > 5) {
|
}
|
||||||
// System.out.println("a ist größer als 5 und b ist wahr");
|
|
||||||
// }
|
|
||||||
|
|
||||||
// Logische ODER-Operation
|
|
||||||
// if (b || a < 5) {
|
|
||||||
// System.out.println("b ist wahr oder a ist kleiner als 5");
|
|
||||||
// }
|
|
||||||
// }
|
|
||||||
|
|
||||||
// public static void main(String[] args) {
|
|
||||||
// AllFeaturesClassExample obj = new AllFeaturesClassExample(12, true, 'a');
|
|
||||||
// obj.controlStructures();
|
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user