Merged Code, Semantic and Tests
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
Some checks are pending
Gitea Actions Demo / Explore-Gitea-Actions (push) Waiting to run
This commit is contained in:
commit
46154fbb01
@ -21,7 +21,6 @@ import ast.statementexpressions.AssignableNode;
|
|||||||
import ast.statementexpressions.NewDeclarationNode;
|
import ast.statementexpressions.NewDeclarationNode;
|
||||||
import ast.statementexpressions.crementexpressions.DecrementNode;
|
import ast.statementexpressions.crementexpressions.DecrementNode;
|
||||||
import ast.statementexpressions.crementexpressions.IncrementNode;
|
import ast.statementexpressions.crementexpressions.IncrementNode;
|
||||||
import ast.statementexpressions.methodcallstatementnexpressions.ChainedMethodNode;
|
|
||||||
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
||||||
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
|
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
|
||||||
import ast.statements.*;
|
import ast.statements.*;
|
||||||
@ -30,7 +29,6 @@ import ast.type.EnumAccessModifierNode;
|
|||||||
import ast.type.ValueNode;
|
import ast.type.ValueNode;
|
||||||
import ast.type.type.*;
|
import ast.type.type.*;
|
||||||
import com.sun.jdi.IntegerType;
|
import com.sun.jdi.IntegerType;
|
||||||
import semantic.context.ClassContext;
|
|
||||||
import semantic.context.Context;
|
import semantic.context.Context;
|
||||||
import semantic.exceptions.*;
|
import semantic.exceptions.*;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
@ -165,11 +163,6 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(FieldNode toCheck) {
|
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) {
|
if (currentFields.get(toCheck.identifier) != null) {
|
||||||
errors.add(new AlreadyDeclaredException("Already declared " + toCheck.identifier));
|
errors.add(new AlreadyDeclaredException("Already declared " + toCheck.identifier));
|
||||||
return new TypeCheckResult(false, null);
|
return new TypeCheckResult(false, null);
|
||||||
@ -177,8 +170,6 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
currentFields.put(toCheck.identifier, toCheck.type);
|
currentFields.put(toCheck.identifier, toCheck.type);
|
||||||
}
|
}
|
||||||
return new TypeCheckResult(true, null);
|
return new TypeCheckResult(true, null);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -284,6 +275,13 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
currentNullType = oldNullType;
|
currentNullType = oldNullType;
|
||||||
var valid = true;
|
var valid = true;
|
||||||
|
|
||||||
|
// This check currently handles things like :
|
||||||
|
/**
|
||||||
|
* private int i;
|
||||||
|
* void foo(int i){
|
||||||
|
* i = i;
|
||||||
|
* }
|
||||||
|
*/
|
||||||
if (assignable.equals(rExpression)) {
|
if (assignable.equals(rExpression)) {
|
||||||
errors.add(new TypeMismatchException("Cannot assign to self"));
|
errors.add(new TypeMismatchException("Cannot assign to self"));
|
||||||
valid = false;
|
valid = false;
|
||||||
@ -326,15 +324,6 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
targetType = currentFields.get(toCheck.target.identifier);
|
targetType = currentFields.get(toCheck.target.identifier);
|
||||||
}
|
}
|
||||||
if (targetType instanceof ReferenceType reference) {
|
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);
|
var type = getTypeFromMethod(toCheck, reference);
|
||||||
if (type != null) {
|
if (type != null) {
|
||||||
return new TypeCheckResult(true, type);
|
return new TypeCheckResult(true, type);
|
||||||
@ -631,37 +620,4 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
return null;
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
@ -21,10 +21,6 @@ public class Context {
|
|||||||
return classes.get(identifier);
|
return classes.get(identifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
public HashMap<String, ClassContext> getClasses() {
|
|
||||||
return classes;
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean containsClass(String identifier) {
|
public boolean containsClass(String identifier) {
|
||||||
return classes.containsKey(identifier);
|
return classes.containsKey(identifier);
|
||||||
}
|
}
|
||||||
|
@ -1,12 +0,0 @@
|
|||||||
// @expected: NotDeclaredException
|
|
||||||
public class Test {
|
|
||||||
|
|
||||||
public House1 h;
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public class House {
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
@ -1,24 +1,38 @@
|
|||||||
public class Test {
|
public class AllFeaturesClassExample {
|
||||||
|
int a;
|
||||||
|
boolean b;
|
||||||
|
char c;
|
||||||
|
|
||||||
public Car c;
|
public void controlStructures(int adf, boolean bool) {
|
||||||
|
if (a > (10 + 8)) {
|
||||||
public int test(boolean b, int x) {
|
|
||||||
if (b == true) {
|
|
||||||
return c.getSpeed();
|
|
||||||
} else {
|
} else {
|
||||||
return x;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
while (a > adf) {
|
||||||
|
a--;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// void logicalOperations() {
|
||||||
|
// Logische UND-Operation
|
||||||
|
// if (b && a > 5) {
|
||||||
|
// System.out.println("a ist größer als 5 und b ist wahr");
|
||||||
|
// }
|
||||||
|
|
||||||
|
// 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();
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class Car {
|
|
||||||
|
|
||||||
private int speed;
|
|
||||||
|
|
||||||
public int getSpeed() {
|
|
||||||
return speed;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user