added some Semantic 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:
parent
576146c4fc
commit
10eb17497e
@ -1,11 +1,14 @@
|
|||||||
package ast.expressions.unaryexpressions;
|
package ast.expressions.unaryexpressions;
|
||||||
|
|
||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
|
import semantic.SemanticVisitor;
|
||||||
|
import typechecker.TypeCheckResult;
|
||||||
|
import visitor.Visitable;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
public class MemberAccessNode implements ASTNode {
|
public class MemberAccessNode implements ASTNode, Visitable {
|
||||||
public Boolean thisExpr;
|
public Boolean thisExpr;
|
||||||
public List<String> identifiers = new ArrayList<>();
|
public List<String> identifiers = new ArrayList<>();
|
||||||
|
|
||||||
@ -17,4 +20,9 @@ public class MemberAccessNode implements ASTNode {
|
|||||||
identifiers.add(identifier);
|
identifiers.add(identifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
|
return visitor.analyze(this);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -3,8 +3,11 @@ package ast.statementexpressions.methodcallstatementnexpressions;
|
|||||||
import ast.ASTNode;
|
import ast.ASTNode;
|
||||||
import ast.expressions.unaryexpressions.MemberAccessNode;
|
import ast.expressions.unaryexpressions.MemberAccessNode;
|
||||||
import ast.statementexpressions.NewDeclarationNode;
|
import ast.statementexpressions.NewDeclarationNode;
|
||||||
|
import semantic.SemanticVisitor;
|
||||||
|
import typechecker.TypeCheckResult;
|
||||||
|
import visitor.Visitable;
|
||||||
|
|
||||||
public class TargetNode implements ASTNode {
|
public class TargetNode implements ASTNode, Visitable {
|
||||||
public Boolean thisTar;
|
public Boolean thisTar;
|
||||||
public MemberAccessNode memberAccess;
|
public MemberAccessNode memberAccess;
|
||||||
public NewDeclarationNode newDeclaration;
|
public NewDeclarationNode newDeclaration;
|
||||||
@ -25,4 +28,11 @@ public class TargetNode implements ASTNode {
|
|||||||
public TargetNode(String identifier) {
|
public TargetNode(String identifier) {
|
||||||
this.identifier = identifier;
|
this.identifier = identifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeCheckResult accept(SemanticVisitor visitor) {
|
||||||
|
return visitor.analyze(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@ -9,6 +9,7 @@ import java.util.Objects;
|
|||||||
import ast.*;
|
import ast.*;
|
||||||
import ast.expressions.IExpressionNode;
|
import ast.expressions.IExpressionNode;
|
||||||
import ast.expressions.binaryexpressions.*;
|
import ast.expressions.binaryexpressions.*;
|
||||||
|
import ast.expressions.unaryexpressions.MemberAccessNode;
|
||||||
import ast.expressions.unaryexpressions.UnaryNode;
|
import ast.expressions.unaryexpressions.UnaryNode;
|
||||||
import ast.members.ConstructorNode;
|
import ast.members.ConstructorNode;
|
||||||
import ast.members.FieldNode;
|
import ast.members.FieldNode;
|
||||||
@ -21,7 +22,9 @@ 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.MethodCallNode;
|
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
||||||
|
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
|
||||||
import ast.statements.*;
|
import ast.statements.*;
|
||||||
|
import ast.type.EnumAccessModifierNode;
|
||||||
import ast.type.type.*;
|
import ast.type.type.*;
|
||||||
import semantic.context.Context;
|
import semantic.context.Context;
|
||||||
import semantic.exceptions.*;
|
import semantic.exceptions.*;
|
||||||
@ -268,15 +271,12 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
variable = currentFields.get(toCheck.assignable.identifier);
|
variable = currentFields.get(toCheck.assignable.identifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Objects.equals(variable, rExpression.getType())) {
|
if (!Objects.equals(variable, rResult.getType())) {
|
||||||
errors.add(new TypeMismatchException(
|
errors.add(new TypeMismatchException(
|
||||||
"Mismatch types in Assign-Statement: cannot convert from \"" + lResult.getType() + "\" to \""
|
"Mismatch types in Assign-Statement: cannot convert from \"" + lResult.getType() + "\" to \""
|
||||||
+ rResult.getType() + "\""));
|
+ rResult.getType() + "\""));
|
||||||
valid = false;
|
valid = false;
|
||||||
}
|
}
|
||||||
// else {
|
|
||||||
// toCheck.setType(assignable.getType());
|
|
||||||
// }
|
|
||||||
valid = valid && lResult.isValid() && rResult.isValid();
|
valid = valid && lResult.isValid() && rResult.isValid();
|
||||||
currentNullType = null;
|
currentNullType = null;
|
||||||
return new TypeCheckResult(valid, null); // return type is null to get the return type sufficently
|
return new TypeCheckResult(valid, null); // return type is null to get the return type sufficently
|
||||||
@ -295,25 +295,43 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(MethodCallNode toCheck) {
|
public TypeCheckResult analyze(MethodCallNode toCheck) {
|
||||||
|
|
||||||
|
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) {
|
||||||
targetType = currentFields.get(toCheck.target.identifier);
|
targetType = currentFields.get(toCheck.target.identifier);
|
||||||
}
|
}
|
||||||
if (targetType instanceof ReferenceType reference) {
|
if (targetType instanceof ReferenceType reference) {
|
||||||
|
return new TypeCheckResult(true, getTypeFromMethod(toCheck, reference));
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if(toCheck.target.thisTar){
|
||||||
|
return new TypeCheckResult(true, getTypeFromMethod(toCheck, new ReferenceType(currentClass.identifier)));
|
||||||
|
} else {
|
||||||
|
var result = toCheck.target.accept(this);
|
||||||
|
if (result.getType() instanceof ReferenceType reference) {
|
||||||
|
return new TypeCheckResult(true, getTypeFromMethod(toCheck, reference));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ITypeNode getTypeFromMethod(MethodCallNode toCheck, ReferenceType reference) {
|
||||||
var classContext = context.getClass(reference.getIdentifier());
|
var classContext = context.getClass(reference.getIdentifier());
|
||||||
|
|
||||||
if (classContext == null) {
|
if (classContext == null) {
|
||||||
errors.add(new NotDefinedException(toCheck.target.identifier + "is not Defined"));
|
errors.add(new NotDeclaredException(toCheck.target.identifier + "is not Defined"));
|
||||||
} else {
|
} else {
|
||||||
var methods = classContext.getMethods();
|
var methods = classContext.getMethods();
|
||||||
for (var method : methods) {
|
for (var method : methods) {
|
||||||
if (toCheck.identifier.equals(method.getIdentifier()) && method.getParameters().size() == toCheck.parameters.size()) {
|
if (toCheck.identifier.equals(method.getIdentifier()) && method.getParameters().size() == toCheck.parameters.size()) {
|
||||||
return new TypeCheckResult(true, method.getType());
|
if(method.accesModifier.accessType == EnumAccessModifierNode.PUBLIC){
|
||||||
}
|
return method.getType();
|
||||||
}
|
|
||||||
}
|
|
||||||
}else {
|
}else {
|
||||||
|
errors.add(new NotVisibleException("This Method is not Visible"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
@ -406,4 +424,46 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
return new TypeCheckResult(valid, null);
|
return new TypeCheckResult(valid, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeCheckResult analyze(MemberAccessNode memberAccessNode) {
|
||||||
|
|
||||||
|
ITypeNode currentType = null;
|
||||||
|
|
||||||
|
for (String s : memberAccessNode.identifiers) {
|
||||||
|
if (currentType == null) {
|
||||||
|
if (currentScope.getLocalVar(s) != null) {
|
||||||
|
currentType = currentScope.getLocalVar(s);
|
||||||
|
|
||||||
|
} else if (currentFields.get(s) != null) {
|
||||||
|
currentType = currentFields.get(s);
|
||||||
|
} else {
|
||||||
|
errors.add(new NotDeclaredException(s + "Not Declared"));
|
||||||
|
return new TypeCheckResult(false, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
if(currentType instanceof ReferenceType reference) {
|
||||||
|
var currentTypeClass = context.getClass(reference.getIdentifier());
|
||||||
|
|
||||||
|
var currentField = currentTypeClass.getField(s);
|
||||||
|
currentType = currentField.getType();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return new TypeCheckResult(true, currentType);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public TypeCheckResult analyze(TargetNode targetNode) {
|
||||||
|
|
||||||
|
if (targetNode.memberAccess != null) {
|
||||||
|
return targetNode.memberAccess.accept(this);
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
@ -2,6 +2,7 @@ package semantic;
|
|||||||
|
|
||||||
import ast.*;
|
import ast.*;
|
||||||
import ast.expressions.binaryexpressions.*;
|
import ast.expressions.binaryexpressions.*;
|
||||||
|
import ast.expressions.unaryexpressions.MemberAccessNode;
|
||||||
import ast.expressions.unaryexpressions.UnaryNode;
|
import ast.expressions.unaryexpressions.UnaryNode;
|
||||||
import ast.members.*;
|
import ast.members.*;
|
||||||
import ast.parameters.ParameterNode;
|
import ast.parameters.ParameterNode;
|
||||||
@ -11,6 +12,7 @@ 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.MethodCallNode;
|
import ast.statementexpressions.methodcallstatementnexpressions.MethodCallNode;
|
||||||
|
import ast.statementexpressions.methodcallstatementnexpressions.TargetNode;
|
||||||
import ast.statements.*;
|
import ast.statements.*;
|
||||||
import typechecker.TypeCheckResult;
|
import typechecker.TypeCheckResult;
|
||||||
|
|
||||||
@ -38,8 +40,6 @@ public interface SemanticVisitor {
|
|||||||
|
|
||||||
TypeCheckResult analyze(ElseNode toCheck);
|
TypeCheckResult analyze(ElseNode toCheck);
|
||||||
|
|
||||||
//TypeCheckResult analyze(ForNode toCheck);
|
|
||||||
|
|
||||||
TypeCheckResult analyze(AssignNode toCheck);
|
TypeCheckResult analyze(AssignNode toCheck);
|
||||||
|
|
||||||
TypeCheckResult analyze(DecrementNode toCheck);
|
TypeCheckResult analyze(DecrementNode toCheck);
|
||||||
@ -66,4 +66,8 @@ public interface SemanticVisitor {
|
|||||||
|
|
||||||
TypeCheckResult analyze(UnaryNode toCheck);
|
TypeCheckResult analyze(UnaryNode toCheck);
|
||||||
|
|
||||||
|
TypeCheckResult analyze(MemberAccessNode toCheck);
|
||||||
|
|
||||||
|
TypeCheckResult analyze(TargetNode toCheck);
|
||||||
|
|
||||||
}
|
}
|
@ -0,0 +1,9 @@
|
|||||||
|
package semantic.exceptions;
|
||||||
|
|
||||||
|
public class NotVisibleException extends RuntimeException {
|
||||||
|
|
||||||
|
public NotVisibleException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -26,11 +26,37 @@ 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 static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
public class EndToTypedAstTest {
|
public class EndToTypedAstTest {
|
||||||
private static final Map<String, Class<?>> exceptionMap = new HashMap<>();
|
private static final Map<String, Class<?>> exceptionMap = new HashMap<>();
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testOnlyOneFile() {
|
||||||
|
SemanticAnalyzer.clearAnalyzer();
|
||||||
|
|
||||||
|
CharStream codeCharStream = null;
|
||||||
|
try {
|
||||||
|
codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/input/typedAstFeaturesTests/CorrectTest.java"));
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
SimpleJavaLexer lexer = new SimpleJavaLexer(codeCharStream);
|
||||||
|
CommonTokenStream tokenStream = new CommonTokenStream(lexer);
|
||||||
|
|
||||||
|
SimpleJavaParser parser = new SimpleJavaParser(tokenStream);
|
||||||
|
ParseTree parseTree = parser.program(); // parse the input
|
||||||
|
|
||||||
|
/* ------------------------- AST builder -> AST ------------------------- */
|
||||||
|
ASTBuilder astBuilder = new ASTBuilder();
|
||||||
|
ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree);
|
||||||
|
|
||||||
|
var result = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||||
|
|
||||||
|
assertTrue(SemanticAnalyzer.errors.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void exceptionsTest() {
|
public void exceptionsTest() {
|
||||||
String directoryPath = "src/test/resources/input/typedAstExceptionsTests";
|
String directoryPath = "src/test/resources/input/typedAstExceptionsTests";
|
||||||
@ -127,9 +153,8 @@ public class EndToTypedAstTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ------------------ Helpers ------------------
|
// ------------------ Helpers ------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method is used to extract the expected exception from a given file.
|
* This method is used to extract the expected exception from a given file.
|
||||||
* It reads the file line by line and uses a regular expression to match the expected exception annotation.
|
* It reads the file line by line and uses a regular expression to match the expected exception annotation.
|
||||||
|
BIN
src/test/resources/input/featureTests/BooleanOperations.class
Normal file
BIN
src/test/resources/input/featureTests/BooleanOperations.class
Normal file
Binary file not shown.
BIN
src/test/resources/input/featureTests/CharManipulation.class
Normal file
BIN
src/test/resources/input/featureTests/CharManipulation.class
Normal file
Binary file not shown.
Binary file not shown.
BIN
src/test/resources/input/featureTests/EmptyClassExample.class
Normal file
BIN
src/test/resources/input/featureTests/EmptyClassExample.class
Normal file
Binary file not shown.
BIN
src/test/resources/input/featureTests/LoopExamples.class
Normal file
BIN
src/test/resources/input/featureTests/LoopExamples.class
Normal file
Binary file not shown.
BIN
src/test/resources/input/featureTests/MethodOverloading.class
Normal file
BIN
src/test/resources/input/featureTests/MethodOverloading.class
Normal file
Binary file not shown.
@ -0,0 +1,21 @@
|
|||||||
|
// @expected: NotVisibleException
|
||||||
|
public class Test {
|
||||||
|
|
||||||
|
public int firstInt;
|
||||||
|
public Car ca;
|
||||||
|
|
||||||
|
public int speed(){
|
||||||
|
return ca.getSpeed();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Car{
|
||||||
|
|
||||||
|
private int speed;
|
||||||
|
|
||||||
|
private int getSpeed(){
|
||||||
|
return speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,8 +0,0 @@
|
|||||||
// @expected: TypeMismatchException
|
|
||||||
public class Example {
|
|
||||||
|
|
||||||
public static void testMethod(int x){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@ -0,0 +1,20 @@
|
|||||||
|
public class Test {
|
||||||
|
|
||||||
|
public int firstInt;
|
||||||
|
public Car ca;
|
||||||
|
|
||||||
|
public int speed(){
|
||||||
|
return ca.getSpeed();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public class Car{
|
||||||
|
|
||||||
|
private int speed;
|
||||||
|
|
||||||
|
public int getSpeed(){
|
||||||
|
return speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,41 +1,8 @@
|
|||||||
public class Example {
|
|
||||||
|
|
||||||
public int a;
|
|
||||||
|
|
||||||
public Car c;
|
|
||||||
|
|
||||||
public void test(){
|
|
||||||
c = new Car();
|
|
||||||
int speed = c.getSpeed();
|
|
||||||
}
|
|
||||||
|
|
||||||
public static int testReturnMethod(int b, boolean bo){
|
|
||||||
a = b;
|
|
||||||
return a;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testOjektMethod(int testInt){
|
|
||||||
Test testClass = new Test();
|
|
||||||
int b = testClass.getFirstInt(testInt);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Test {
|
|
||||||
|
|
||||||
public int firstInt;
|
|
||||||
|
|
||||||
public int getFirstInt(int b){
|
|
||||||
return firstInt--;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
public class Car{
|
public class Car{
|
||||||
|
|
||||||
private int speed;
|
private int speed;
|
||||||
|
|
||||||
public int getSpeed(){
|
public int getSpeed(boolean boo){
|
||||||
return speed;
|
return speed;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,13 @@
|
|||||||
|
public class Car{
|
||||||
|
|
||||||
|
private int speed;
|
||||||
|
|
||||||
|
public int getSpeed(){
|
||||||
|
return speed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int test(){
|
||||||
|
return this.getSpeed();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user