Merge branch 'code-generator' into chainedMethodsSemanticCheck
Some checks failed
Gitea Actions Demo / Explore-Gitea-Actions (push) Has been cancelled

This commit is contained in:
Bruder John 2024-07-03 17:45:42 +02:00
commit 7d441116bd
5 changed files with 40 additions and 115 deletions

View File

@ -21,7 +21,6 @@ import ast.statementexpressions.AssignableNode;
import ast.statementexpressions.NewDeclarationNode;
import ast.statementexpressions.crementexpressions.DecrementNode;
import ast.statementexpressions.crementexpressions.IncrementNode;
import ast.statementexpressions.methodcallstatementnexpressions.ChainedMethodNode;
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
import ast.statements.*;
@ -30,7 +29,6 @@ import ast.type.EnumAccessModifierNode;
import ast.type.ValueNode;
import ast.type.type.*;
import com.sun.jdi.IntegerType;
import semantic.context.ClassContext;
import semantic.context.Context;
import semantic.exceptions.*;
import typechecker.TypeCheckResult;
@ -165,11 +163,6 @@ public class SemanticAnalyzer implements SemanticVisitor {
@Override
public TypeCheckResult analyze(FieldNode toCheck) {
if (toCheck.type instanceof ReferenceType referenceType) {
if(!context.containsClass(referenceType.getIdentifier())){
errors.add(new NotDeclaredException(referenceType.getIdentifier() + " not declared"));
}
}
if (currentFields.get(toCheck.identifier) != null) {
errors.add(new AlreadyDeclaredException("Already declared " + toCheck.identifier));
return new TypeCheckResult(false, null);
@ -177,8 +170,6 @@ public class SemanticAnalyzer implements SemanticVisitor {
currentFields.put(toCheck.identifier, toCheck.type);
}
return new TypeCheckResult(true, null);
}
@Override
@ -284,6 +275,13 @@ public class SemanticAnalyzer implements SemanticVisitor {
currentNullType = oldNullType;
var valid = true;
// This check currently handles things like :
/**
* private int i;
* void foo(int i){
* i = i;
* }
*/
if (assignable.equals(rExpression)) {
errors.add(new TypeMismatchException("Cannot assign to self"));
valid = false;
@ -326,15 +324,6 @@ public class SemanticAnalyzer implements SemanticVisitor {
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);
@ -631,37 +620,4 @@ public class SemanticAnalyzer implements SemanticVisitor {
return null;
}
private ITypeNode getTypeFromMethod(ChainedMethodNode toCheck, ReferenceType reference) {
var classContext = context.getClass(reference.getIdentifier());
var methods = classContext.getMethods();
for (var method : methods) {
if (toCheck.identifier.equals(method.getIdentifier())) {
if (method.getParameters().size() == toCheck.expressions.size() && !(method instanceof ConstructorNode)) {
boolean same = true;
for (int i = 0; i < method.getParameters().size(); i++) {
var result1 = method.getParameters().get(i).accept(this);
var result2 = toCheck.expressions.get(i).accept(this);
if (!Objects.equals(result1.getType(), result2.getType())) {
same = false;
}
}
if (same) {
if (method.accesModifier.accessType == EnumAccessModifierNode.PUBLIC) {
if (method.getType() == null) {
return new BaseType(TypeEnum.VOID);
}
return method.getType();
} else {
errors.add(new NotVisibleException("This Method is not Visible"));
}
}
}
}
}
return null;
}
}

View File

@ -21,10 +21,6 @@ public class Context {
return classes.get(identifier);
}
public HashMap<String, ClassContext> getClasses() {
return classes;
}
public boolean containsClass(String identifier) {
return classes.containsKey(identifier);
}

View File

@ -1,12 +0,0 @@
// @expected: NotDeclaredException
public class Test {
public House1 h;
}
public class House {
}

View File

@ -1,29 +0,0 @@
public class Test {
public House h;
public int test(House h){
return h.getW().getSize();
}
}
public class House {
private Window w;
public Window getW(){
return w;
}
}
public class Window{
private int size;
public int getSize() {
return size;
}
}

View File

@ -1,24 +1,38 @@
public class Test {
public class AllFeaturesClassExample {
int a;
boolean b;
char c;
public Car c;
public int test(boolean b, int x) {
if (b == true) {
return c.getSpeed();
public void controlStructures(int adf, boolean bool) {
if (a > (10 + 8)) {
} else {
return x;
}
while (a > adf) {
a--;
}
for (int i = 0; i < 5; i++) {
}
}
public class Car {
// void logicalOperations() {
// Logische UND-Operation
// if (b && a > 5) {
// System.out.println("a ist größer als 5 und b ist wahr");
// }
private int speed;
// Logische ODER-Operation
// if (b || a < 5) {
// System.out.println("b ist wahr oder a ist kleiner als 5");
// }
// }
public int getSpeed() {
return speed;
// public static void main(String[] args) {
// AllFeaturesClassExample obj = new AllFeaturesClassExample(12, true, 'a');
// obj.controlStructures();
// }
}
}