fixed constructor node parameter decleration
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run

This commit is contained in:
Bruder John 2024-07-02 08:48:37 +02:00
parent 71ffb8bb83
commit 561eafbf4c
4 changed files with 42 additions and 76 deletions

View File

@ -9,13 +9,10 @@ import java.util.List;
import java.util.Objects;
public class ConstructorNode extends MethodNode {
public AccessModifierNode accessType;
public String identifier;
public List<ParameterNode> parameters = new ArrayList<>();
public BlockNode block;
public ConstructorNode(String accessType, String identifier, BlockNode block) {
this.accessType = new AccessModifierNode(accessType);
this.accesModifier = new AccessModifierNode(accessType);
this.identifier = identifier;
this.block = block;
}

View File

@ -16,7 +16,7 @@ public class Scope {
public void addLocalVar(String name, ITypeNode type) {
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);
}

View File

@ -112,53 +112,49 @@ public class SemanticAnalyzer implements SemanticVisitor {
@Override
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()) {
if (Objects.equals(otherMethod, methodNode))
break;
if (otherMethod.isSame(methodNode)) {
errors.add(new AlreadyDeclaredException(
"Method " + methodNode.getIdentifier() + " is already defined in class "
+ currentClass.identifier));
valid = false;
}
for (var otherMethod : currentClass.getMethods()) {
if (Objects.equals(otherMethod, methodNode))
break;
if (otherMethod.isSame(methodNode)) {
errors.add(new AlreadyDeclaredException(
"Method " + methodNode.getIdentifier() + " is already defined in class "
+ currentClass.identifier));
valid = false;
}
}
currentScope.pushScope();
for (var parameter : methodNode.getParameters()) {
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);
currentScope.pushScope();
for (var parameter : methodNode.getParameters()) {
var result = parameter.accept(this);
valid = valid && result.isValid();
currentScope.popScope();
ITypeNode resultType = result.getType();
if (resultType == null) {
resultType = new BaseType(TypeEnum.VOID);
try {
currentScope.addLocalVar(parameter.identifier, parameter.type);
} catch (AlreadyDeclaredException e) {
errors.add(new AlreadyDeclaredException(parameter.identifier));
}
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
@ -224,7 +220,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
}
for (IStatementNode statementNode : blockNode.statements) {
var result = statementNode.accept(this);
if(!(statementNode instanceof IncrementNode) && !(statementNode instanceof DecrementNode)){
if (!(statementNode instanceof IncrementNode) && !(statementNode instanceof DecrementNode)) {
if (result.getType() != null) {
if (blockReturnType == null) {
blockReturnType = result.getType();

View File

@ -1,38 +1,11 @@
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() {
// Logische UND-Operation
// if (b && a > 5) {
// System.out.println("a ist größer als 5 und b ist wahr");
// }
public AllFeaturesClassExample(boolean b){
}
// 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();
// }
}