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

View File

@ -166,7 +166,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
@Override
public TypeCheckResult analyze(FieldNode toCheck) {
if (toCheck.type instanceof ReferenceType referenceType) {
if(!context.containsClass(referenceType.getIdentifier())){
if (!context.containsClass(referenceType.getIdentifier())) {
errors.add(new NotDeclaredException(referenceType.getIdentifier() + " not declared"));
}
}
@ -233,7 +233,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();
@ -258,6 +258,10 @@ public class SemanticAnalyzer implements SemanticVisitor {
} else {
if (currentFields.get(toCheck.identifier) != null) {
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);
return new TypeCheckResult(true, type);
} else if (currentScope.getLocalVar(toCheck.identifier) != null) {
@ -320,44 +324,66 @@ public class SemanticAnalyzer implements SemanticVisitor {
@Override
public TypeCheckResult analyze(MethodCallNode toCheck) {
if (toCheck.target.identifier != null) {
var targetType = currentScope.getLocalVar(toCheck.target.identifier);
if (targetType == null) {
targetType = currentFields.get(toCheck.target.identifier);
}
if (targetType instanceof ReferenceType reference) {
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"));
if (toCheck.target != null) {
if (toCheck.target.identifier != null) {
var targetType = currentScope.getLocalVar(toCheck.target.identifier);
if (targetType == null) {
targetType = currentFields.get(toCheck.target.identifier);
}
if (targetType instanceof ReferenceType reference) {
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);
}
}
} else {
if (toCheck.target.thisTar != null) {
if (toCheck.target.thisTar) {
var type = getTypeFromMethod(toCheck, new ReferenceType(currentClass.identifier));
if (type != null) {
return new TypeCheckResult(true, type);
}
}
} else {
var result = toCheck.target.accept(this);
if (result.getType() instanceof ReferenceType reference) {
return new TypeCheckResult(true, getTypeFromMethod(toCheck, reference));
}
}
var type = getTypeFromMethod(toCheck, reference);
if (type != null) {
return new TypeCheckResult(true, type);
} else {
return new TypeCheckResult(false, null);
}
}
} else {
if (toCheck.target.thisTar != null) {
if (toCheck.target.thisTar) {
var type = getTypeFromMethod(toCheck, new ReferenceType(currentClass.identifier));
if (type != null) {
return new TypeCheckResult(true, type);
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 {
var result = toCheck.target.accept(this);
if (result.getType() instanceof ReferenceType reference) {
return new TypeCheckResult(true, getTypeFromMethod(toCheck, reference));
}
return new TypeCheckResult(false, null);
}
}
return new TypeCheckResult(false, null);
}
@ -508,6 +534,10 @@ public class SemanticAnalyzer implements SemanticVisitor {
if (currentScope.contains(unary.identifier)) {
return new TypeCheckResult(valid, currentScope.getLocalVar(unary.identifier));
} 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));
} else if (unary.statement != null) {
var result = unary.statement.accept(this);
@ -537,8 +567,16 @@ public class SemanticAnalyzer implements SemanticVisitor {
public TypeCheckResult analyze(MemberAccessNode memberAccessNode) {
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 (currentScope.getLocalVar(s) != null) {
currentType = currentScope.getLocalVar(s);

View File

@ -26,6 +26,7 @@ import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.jupiter.api.Test;
import semantic.exceptions.NotDeclaredException;
import static org.junit.jupiter.api.Assertions.*;
@ -108,6 +109,7 @@ public class EndToTypedAstTest {
} else {
System.out.println("The provided path is not a directory.");
}
}
@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(){
return this.test(i);
return test(i);
}
@ -19,4 +19,4 @@ public class Test{
return ret;
}
}
}

View File

@ -1,21 +1,9 @@
public class CorrectTest {
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 test(){
a = 10;
}
}