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

@ -166,7 +166,7 @@ public class SemanticAnalyzer implements SemanticVisitor {
@Override @Override
public TypeCheckResult analyze(FieldNode toCheck) { public TypeCheckResult analyze(FieldNode toCheck) {
if (toCheck.type instanceof ReferenceType referenceType) { if (toCheck.type instanceof ReferenceType referenceType) {
if(!context.containsClass(referenceType.getIdentifier())){ if (!context.containsClass(referenceType.getIdentifier())) {
errors.add(new NotDeclaredException(referenceType.getIdentifier() + " not declared")); errors.add(new NotDeclaredException(referenceType.getIdentifier() + " not declared"));
} }
} }
@ -233,7 +233,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();
@ -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,44 +324,66 @@ public class SemanticAnalyzer implements SemanticVisitor {
@Override @Override
public TypeCheckResult analyze(MethodCallNode toCheck) { public TypeCheckResult analyze(MethodCallNode toCheck) {
if (toCheck.target.identifier != null) { if (toCheck.target != null) {
var targetType = currentScope.getLocalVar(toCheck.target.identifier); if (toCheck.target.identifier != null) {
if (targetType == null) { var targetType = currentScope.getLocalVar(toCheck.target.identifier);
targetType = currentFields.get(toCheck.target.identifier); if (targetType == null) {
} targetType = currentFields.get(toCheck.target.identifier);
if (targetType instanceof ReferenceType reference) { }
if (!toCheck.chainedMethods.isEmpty()) { if (targetType instanceof ReferenceType reference) {
for (ChainedMethodNode chainedMethod : toCheck.chainedMethods) { if (!toCheck.chainedMethods.isEmpty()) {
var type = getTypeFromMethod(chainedMethod, reference); for (ChainedMethodNode chainedMethod : toCheck.chainedMethods) {
if (type instanceof ReferenceType referenceType) var type = getTypeFromMethod(chainedMethod, reference);
reference = referenceType; if (type instanceof ReferenceType referenceType)
else reference = referenceType;
errors.add(new TypeMismatchException("Ein Basetyp hat keine funktionen")); 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 { } else {
if (toCheck.target.thisTar != null) {
if (toCheck.target.thisTar) { ReferenceType reference = new ReferenceType(currentClass.identifier);
var type = getTypeFromMethod(toCheck, new ReferenceType(currentClass.identifier)); if (!toCheck.chainedMethods.isEmpty()) {
if (type != null) { for (ChainedMethodNode chainedMethod : toCheck.chainedMethods) {
return new TypeCheckResult(true, type); 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 { } else {
var result = toCheck.target.accept(this); return new TypeCheckResult(false, null);
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)) { 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);
} }
@ -19,4 +19,4 @@ public class Test{
return ret; return ret;
} }
} }

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) {
if (a > (10 + 8)) {
} else {
}
while (a > adf) {
a--;
}
for (int i = 0; i < 5; i++) {
}
public void test(){
a = 10;
} }
} }