Added Some Semantice Checks
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
8163d0b61e
commit
35b3e9ee46
@ -150,6 +150,9 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
if (resultType == null) {
|
if (resultType == null) {
|
||||||
resultType = new BaseType(TypeEnum.VOID);
|
resultType = new BaseType(TypeEnum.VOID);
|
||||||
}
|
}
|
||||||
|
if(methodNode.getType() == null){
|
||||||
|
methodNode.setType(new BaseType(TypeEnum.VOID));
|
||||||
|
}
|
||||||
if (!resultType.equals(methodNode.getType())) {
|
if (!resultType.equals(methodNode.getType())) {
|
||||||
errors.add(new TypeMismatchException("Method-Declaration " + methodNode.getIdentifier() + " with type "
|
errors.add(new TypeMismatchException("Method-Declaration " + methodNode.getIdentifier() + " with type "
|
||||||
+ methodNode.getType() + " has at least one Mismatching return Type:"));
|
+ methodNode.getType() + " has at least one Mismatching return Type:"));
|
||||||
@ -179,13 +182,13 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(ReturnNode toCheck) {
|
public TypeCheckResult analyze(ReturnNode toCheck) {
|
||||||
|
|
||||||
if (toCheck.expression != null) {
|
if (toCheck.expression != null) {
|
||||||
var result = toCheck.expression.accept(this);
|
var result = toCheck.expression.accept(this);
|
||||||
return new TypeCheckResult(true, result.getType());
|
return new TypeCheckResult(true, result.getType());
|
||||||
} else {
|
} else if(toCheck.voidReturn){
|
||||||
return new TypeCheckResult(false, null);
|
return new TypeCheckResult(false, new BaseType(TypeEnum.VOID));
|
||||||
}
|
}
|
||||||
|
return new TypeCheckResult(true, null);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -219,7 +222,14 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(AssignableNode toCheck) {
|
public TypeCheckResult analyze(AssignableNode toCheck) {
|
||||||
|
|
||||||
|
if ( currentFields.get(toCheck.identifier) != null){
|
||||||
return new TypeCheckResult(true, currentFields.get(toCheck.identifier));
|
return new TypeCheckResult(true, currentFields.get(toCheck.identifier));
|
||||||
|
} else if (currentScope.getLocalVar(toCheck.identifier) != null) {
|
||||||
|
return new TypeCheckResult(true, currentScope.getLocalVar(toCheck.identifier));
|
||||||
|
}
|
||||||
|
|
||||||
|
return new TypeCheckResult(true, null);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -228,11 +238,6 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*@Override
|
|
||||||
public TypeCheckResult analyze(ForNode toCheck) {
|
|
||||||
return null;
|
|
||||||
}*/
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(AssignNode toCheck) {
|
public TypeCheckResult analyze(AssignNode toCheck) {
|
||||||
AssignableNode assignable = toCheck.assignable;
|
AssignableNode assignable = toCheck.assignable;
|
||||||
@ -258,7 +263,12 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
currentNullType = lResult.getType();
|
currentNullType = lResult.getType();
|
||||||
var rResult = rExpression.accept(this);
|
var rResult = rExpression.accept(this);
|
||||||
|
|
||||||
if (!Objects.equals(currentScope.getLocalVar(toCheck.assignable.identifier), rExpression.getType())) {
|
var variable = currentScope.getLocalVar(toCheck.assignable.identifier);
|
||||||
|
if(variable == null){
|
||||||
|
variable = currentFields.get(toCheck.assignable.identifier);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Objects.equals(variable, rExpression.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() + "\""));
|
||||||
@ -273,8 +283,8 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(DecrementNode toCheck) {
|
public TypeCheckResult analyze(DecrementNode decrementNode) {
|
||||||
return null;
|
return decrementNode.assignableExpression.accept(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -284,6 +294,27 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(MethodCallNode toCheck) {
|
public TypeCheckResult analyze(MethodCallNode toCheck) {
|
||||||
|
|
||||||
|
var targetType = currentScope.getLocalVar(toCheck.target.identifier);
|
||||||
|
if(targetType == null){
|
||||||
|
targetType = currentFields.get(toCheck.target.identifier);
|
||||||
|
}
|
||||||
|
if(targetType instanceof ReferenceType reference){
|
||||||
|
var classContext = context.getClass(reference.getIdentifier());
|
||||||
|
|
||||||
|
if(classContext == null){
|
||||||
|
errors.add(new NotDefinedException(toCheck.target.identifier + "is not Defined"));
|
||||||
|
}else {
|
||||||
|
var methods = classContext.getMethods();
|
||||||
|
for (var method : methods) {
|
||||||
|
if(toCheck.identifier.equals(method.getIdentifier()) && method.getParameters().size() == toCheck.parameters.size()){
|
||||||
|
return new TypeCheckResult(true, method.getType());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
|
||||||
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -316,12 +347,17 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(NewDeclarationNode toCheck) {
|
public TypeCheckResult analyze(NewDeclarationNode toCheck) {
|
||||||
|
|
||||||
|
if(context.containsClass(toCheck.identifier)) {
|
||||||
|
return new TypeCheckResult(true, new ReferenceType(toCheck.identifier));
|
||||||
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public TypeCheckResult analyze(IncrementNode toCheck) {
|
public TypeCheckResult analyze(IncrementNode toCheck) {
|
||||||
return null;
|
return toCheck.assignableExpression.accept(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -360,6 +396,10 @@ public class SemanticAnalyzer implements SemanticVisitor {
|
|||||||
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) {
|
||||||
return new TypeCheckResult(valid, currentFields.get(unary.identifier));
|
return new TypeCheckResult(valid, currentFields.get(unary.identifier));
|
||||||
|
} else if (unary.statement != null) {
|
||||||
|
var result = unary.statement.accept(this);
|
||||||
|
unary.setType(result.getType());
|
||||||
|
return result;
|
||||||
} else {
|
} else {
|
||||||
errors.add(new NotDeclearedException("Var is not Decleared"));
|
errors.add(new NotDeclearedException("Var is not Decleared"));
|
||||||
}
|
}
|
||||||
|
@ -2,11 +2,15 @@ package semantic.context;
|
|||||||
|
|
||||||
import ast.ClassNode;
|
import ast.ClassNode;
|
||||||
import ast.members.FieldNode;
|
import ast.members.FieldNode;
|
||||||
|
import ast.members.MethodNode;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
|
||||||
public class ClassContext {
|
public class ClassContext {
|
||||||
|
|
||||||
private HashMap<String, FieldContext> fields;
|
private HashMap<String, FieldContext> fields;
|
||||||
|
private ArrayList<MethodNode> methods = new ArrayList<>();
|
||||||
|
|
||||||
public ClassContext(ClassNode classNode) {
|
public ClassContext(ClassNode classNode) {
|
||||||
|
|
||||||
@ -15,6 +19,8 @@ public class ClassContext {
|
|||||||
classNode.members.forEach(member -> {
|
classNode.members.forEach(member -> {
|
||||||
if(member instanceof FieldNode fieldNode) {
|
if(member instanceof FieldNode fieldNode) {
|
||||||
fields.put(fieldNode.identifier, new FieldContext(fieldNode));
|
fields.put(fieldNode.identifier, new FieldContext(fieldNode));
|
||||||
|
}else if(member instanceof MethodNode methodNode) {
|
||||||
|
methods.add(methodNode);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -24,4 +30,8 @@ public class ClassContext {
|
|||||||
return fields.get(name);
|
return fields.get(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ArrayList<MethodNode> getMethods() {
|
||||||
|
return methods;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -21,4 +21,8 @@ public class Context {
|
|||||||
return classes.get(identifier);
|
return classes.get(identifier);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean containsClass(String identifier) {
|
||||||
|
return classes.containsKey(identifier);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,9 @@
|
|||||||
|
package semantic.exeptions;
|
||||||
|
|
||||||
|
public class NotDefinedException extends RuntimeException {
|
||||||
|
|
||||||
|
public NotDefinedException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -50,7 +50,7 @@ public class EndToTAST {
|
|||||||
ASTNode tast = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
ASTNode tast = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
||||||
|
|
||||||
|
|
||||||
assertEquals(SemanticAnalyzer.errors.size(), 0);
|
assertEquals(0, SemanticAnalyzer.errors.size());
|
||||||
assertNotNull(tast);
|
assertNotNull(tast);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -255,28 +255,4 @@ public class EndToTAST {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void wrongTypeInIfClause(){
|
|
||||||
|
|
||||||
CharStream codeCharStream = null;
|
|
||||||
try {
|
|
||||||
codeCharStream = CharStreams.fromPath(Paths.get("src/test/resources/semantic/endToTAST/WrongIfClause.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();
|
|
||||||
|
|
||||||
ASTBuilder astBuilder = new ASTBuilder();
|
|
||||||
ProgramNode abstractSyntaxTree = (ProgramNode) astBuilder.visit(parseTree);
|
|
||||||
|
|
||||||
ASTNode tast = SemanticAnalyzer.generateTast(abstractSyntaxTree);
|
|
||||||
|
|
||||||
assertFalse(SemanticAnalyzer.errors.isEmpty());
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,41 @@ public class Example {
|
|||||||
|
|
||||||
public int a;
|
public int a;
|
||||||
|
|
||||||
public static int testMethod(int b, boolean bo){
|
public Car c;
|
||||||
|
|
||||||
|
public void test(){
|
||||||
|
c = new Car();
|
||||||
|
int speed = c.getSpeed();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static int testReturnMethod(int b, boolean bo){
|
||||||
a = b;
|
a = b;
|
||||||
return a;
|
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{
|
||||||
|
|
||||||
|
private int speed;
|
||||||
|
|
||||||
|
public int getSpeed(){
|
||||||
|
return speed;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -1,7 +0,0 @@
|
|||||||
public class Example {
|
|
||||||
|
|
||||||
public static void testMethod(int x){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user