Added Class name to Member accesnode
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run

This commit is contained in:
Bruder John 2024-07-03 20:22:09 +02:00
parent ba08c6f6b0
commit 0e512161a0
11 changed files with 80 additions and 69 deletions

View File

@ -2,6 +2,7 @@ package ast.statementexpressions.methodcallstatementnexpressions;
import ast.expressions.IExpressionNode; import ast.expressions.IExpressionNode;
import ast.statements.IStatementNode; import ast.statements.IStatementNode;
import ast.type.type.ITypeNode;
import bytecode.visitor.MethodVisitor; import bytecode.visitor.MethodVisitor;
import semantic.SemanticVisitor; import semantic.SemanticVisitor;
import typechecker.TypeCheckResult; import typechecker.TypeCheckResult;
@ -11,6 +12,7 @@ import java.util.List;
public class MethodCallNode implements IStatementNode { public class MethodCallNode implements IStatementNode {
public TargetNode target; public TargetNode target;
public ITypeNode type;
public List<ChainedMethodNode> chainedMethods = new ArrayList<>(); public List<ChainedMethodNode> chainedMethods = new ArrayList<>();
public String identifier; public String identifier;
public List<IExpressionNode> parameters = new ArrayList<>(); public List<IExpressionNode> parameters = new ArrayList<>();

View File

@ -258,6 +258,10 @@ public class SemanticAnalyzer implements SemanticVisitor {
} else { } else {
if (currentFields.get(toCheck.identifier) != null) { if (currentFields.get(toCheck.identifier) != null) {
var type = currentFields.get(toCheck.identifier); var type = currentFields.get(toCheck.identifier);
MemberAccessNode memberAccessNode = new MemberAccessNode(false);
memberAccessNode.identifiers.add(currentClass.identifier);
memberAccessNode.identifiers.add(toCheck.identifier);
toCheck.memberAccess = memberAccessNode;
toCheck.setTypeNode(type); toCheck.setTypeNode(type);
return new TypeCheckResult(true, type); return new TypeCheckResult(true, type);
} else if (currentScope.getLocalVar(toCheck.identifier) != null) { } else if (currentScope.getLocalVar(toCheck.identifier) != null) {
@ -320,6 +324,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
@Override @Override
public TypeCheckResult analyze(MethodCallNode toCheck) { public TypeCheckResult analyze(MethodCallNode toCheck) {
if (toCheck.target != null) {
if (toCheck.target.identifier != null) { if (toCheck.target.identifier != null) {
var targetType = currentScope.getLocalVar(toCheck.target.identifier); var targetType = currentScope.getLocalVar(toCheck.target.identifier);
if (targetType == null) { if (targetType == null) {
@ -358,6 +363,27 @@ public class SemanticAnalyzer implements SemanticVisitor {
} }
} }
} }
} else {
ReferenceType reference = new ReferenceType(currentClass.identifier);
if (!toCheck.chainedMethods.isEmpty()) {
for (ChainedMethodNode chainedMethod : toCheck.chainedMethods) {
var type = getTypeFromMethod(chainedMethod, reference);
if (type instanceof ReferenceType referenceType)
reference = referenceType;
else
errors.add(new TypeMismatchException("Ein Basetyp hat keine funktionen"));
}
}
var type = getTypeFromMethod(toCheck, reference);
if (type != null) {
return new TypeCheckResult(true, type);
} else {
return new TypeCheckResult(false, null);
}
}
return new TypeCheckResult(false, null); return new TypeCheckResult(false, null);
} }
@ -508,6 +534,10 @@ public class SemanticAnalyzer implements SemanticVisitor {
if (currentScope.contains(unary.identifier)) { if (currentScope.contains(unary.identifier)) {
return new TypeCheckResult(valid, currentScope.getLocalVar(unary.identifier)); return new TypeCheckResult(valid, currentScope.getLocalVar(unary.identifier));
} else if (currentFields.get(unary.identifier) != null) { } else if (currentFields.get(unary.identifier) != null) {
MemberAccessNode memberAccessNode = new MemberAccessNode(false);
memberAccessNode.identifiers.add(currentClass.identifier);
memberAccessNode.identifiers.add(unary.identifier);
unary.memberAccess = memberAccessNode;
return new TypeCheckResult(valid, currentFields.get(unary.identifier)); return new TypeCheckResult(valid, currentFields.get(unary.identifier));
} else if (unary.statement != null) { } else if (unary.statement != null) {
var result = unary.statement.accept(this); var result = unary.statement.accept(this);
@ -537,8 +567,16 @@ public class SemanticAnalyzer implements SemanticVisitor {
public TypeCheckResult analyze(MemberAccessNode memberAccessNode) { public TypeCheckResult analyze(MemberAccessNode memberAccessNode) {
ITypeNode currentType = null; ITypeNode currentType = null;
int start = 0;
if(!memberAccessNode.identifiers.isEmpty()){
if(currentFields.get(memberAccessNode.identifiers.get(0)) != null){
memberAccessNode.identifiers.add(0, currentClass.identifier);
start = 1;
}
}
for (int i = start; i < memberAccessNode.identifiers.size(); i++) {
for (String s : memberAccessNode.identifiers) { String s = memberAccessNode.identifiers.get(i);
if (currentType == null) { if (currentType == null) {
if (currentScope.getLocalVar(s) != null) { if (currentScope.getLocalVar(s) != null) {
currentType = currentScope.getLocalVar(s); currentType = currentScope.getLocalVar(s);

View File

@ -26,6 +26,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import semantic.exceptions.NotDeclaredException;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
@ -108,6 +109,7 @@ public class EndToTypedAstTest {
} else { } else {
System.out.println("The provided path is not a directory."); System.out.println("The provided path is not a directory.");
} }
} }
@Test @Test

View File

@ -1,19 +0,0 @@
// @expected: WrongOverloading
public class Test{
public void test(int x){
if(this.get()){
} else {
}
}
public boolean b;
public boolean get(int c){
return b;
}
}

View File

@ -6,7 +6,7 @@ public class Test{
public int test(){ public int test(){
return this.test(i); return test(i);
} }

View File

@ -1,21 +1,9 @@
public class CorrectTest { public class CorrectTest {
int a; int a;
boolean b;
char c;
public void controlStructures(int adf, boolean bool) { public void test(){
if (a > (10 + 8)) { a = 10;
} else {
} }
while (a > adf) {
a--;
}
for (int i = 0; i < 5; i++) {
}
}
} }