Merge branch 'master' of https://gitea.hb.dhbw-stuttgart.de/i22022/NichtHaskell
This commit is contained in:
commit
50215afb07
@ -7,7 +7,9 @@ class Example {
|
||||
class Example2 {
|
||||
boolean instVarBool;
|
||||
int m(int n){
|
||||
if(instVarBool){
|
||||
boolean localBool;
|
||||
localBool = true;
|
||||
if(localBool){
|
||||
return n;
|
||||
}
|
||||
return -1;
|
||||
|
@ -1,6 +1,12 @@
|
||||
package abstractSyntaxTree.Expression;
|
||||
|
||||
import TypeCheck.AbstractType;
|
||||
import TypeCheck.TypeCheckResult;
|
||||
import abstractSyntaxTree.Parameter.ParameterList;
|
||||
import org.objectweb.asm.MethodVisitor;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@ -11,6 +17,16 @@ public class IntConstantExpression extends AbstractType implements IExpression{
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void codeGen(MethodVisitor mv, LinkedHashMap<String, String> localVars, HashMap<String, HashMap<String, String>> typeContext) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
|
@ -25,7 +25,13 @@ public class LocalVarIdentifier implements IExpression{
|
||||
|
||||
@Override
|
||||
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception {
|
||||
return null;
|
||||
TypeCheckResult result = new TypeCheckResult();
|
||||
if (localVars.containsKey(identifier)) {
|
||||
result.type = localVars.get(identifier);
|
||||
} else {
|
||||
throw new Exception("TypeCheck Exception: local var does not exist");
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -36,6 +36,8 @@ public class BlockStatement extends AbstractType implements IStatement {
|
||||
}
|
||||
|
||||
for (IStatement statement : statements) {
|
||||
// todo remove later if there are no null statement any more
|
||||
if (statement != null) {
|
||||
TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars);
|
||||
|
||||
if (typeOfCurrentStatement.type.contains(",")) {
|
||||
@ -45,18 +47,18 @@ public class BlockStatement extends AbstractType implements IStatement {
|
||||
String firstType = substrings[0];
|
||||
String secondType = substrings[1];
|
||||
|
||||
if(!firstType.equals(this.returnType) || !firstType.equals(this.returnType)){
|
||||
if(!firstType.equals("void")){
|
||||
if (!firstType.equals(this.returnType) || !firstType.equals(this.returnType)) {
|
||||
if (!firstType.equals("void")) {
|
||||
throw new Exception("TypeCeck Exception: if paths return wrong type");
|
||||
}
|
||||
if(!secondType.equals("void")){
|
||||
if (!secondType.equals("void")) {
|
||||
throw new Exception("TypeCeck Exception: else paths return wrong type");
|
||||
}
|
||||
boolean firstIsVoid = firstType.equals("void");
|
||||
|
||||
if(!firstIsVoid){
|
||||
if (!firstIsVoid) {
|
||||
typeOfCurrentStatement.type = firstType;
|
||||
}else{
|
||||
} else {
|
||||
typeOfCurrentStatement.type = secondType;
|
||||
}
|
||||
}
|
||||
@ -67,6 +69,7 @@ public class BlockStatement extends AbstractType implements IStatement {
|
||||
throw new Exception("TypeCheck Exception: Block returns the wrong type.");
|
||||
}
|
||||
}
|
||||
}
|
||||
result.type = this.returnType;
|
||||
// todo check if the block returns the needed return type in every case
|
||||
// todo ignore unreachable statements?
|
||||
|
@ -3,10 +3,7 @@ package astGenerator;
|
||||
import abstractSyntaxTree.Class.FieldDecl;
|
||||
import abstractSyntaxTree.Class.MethodDecl;
|
||||
import abstractSyntaxTree.Class.RefType;
|
||||
import abstractSyntaxTree.Expression.BinaryExpression;
|
||||
import abstractSyntaxTree.Expression.IExpression;
|
||||
import abstractSyntaxTree.Expression.IntConstantExpression;
|
||||
import abstractSyntaxTree.Expression.LocalVarIdentifier;
|
||||
import abstractSyntaxTree.Expression.*;
|
||||
import abstractSyntaxTree.Node;
|
||||
import abstractSyntaxTree.Parameter.Parameter;
|
||||
import abstractSyntaxTree.Parameter.ParameterList;
|
||||
@ -176,9 +173,12 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
||||
|
||||
@Override
|
||||
public Node visitAssign(DecafParser.AssignContext ctx) {
|
||||
return new AssignStatementExpression("", null, null);
|
||||
Node right = visitExpression(ctx.expression());
|
||||
Node left = visitAssignableExpr(ctx.assignableExpr());
|
||||
return new AssignStatementExpression(ctx.Assign().getText(),(IExpression) left, (IExpression) right);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Node visitMethodCall(DecafParser.MethodCallContext ctx) {
|
||||
return super.visitMethodCall(ctx);
|
||||
@ -240,13 +240,14 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
||||
//todo
|
||||
@Override
|
||||
public Node visitDotSubExpr(DecafParser.DotSubExprContext ctx) {
|
||||
// if (ctx.IntValue() != null) {
|
||||
// int value = Integer.parseInt(ctx.IntValue().getText());
|
||||
// return new IntConstantExpression(value);
|
||||
// }
|
||||
if(ctx.Identifier() != null) {
|
||||
if (ctx.IntValue() != null) {
|
||||
int value = Integer.parseInt(ctx.IntValue().getText());
|
||||
return new IntConstantExpression(value);
|
||||
} else if(ctx.Identifier() != null) {
|
||||
String identifier = ctx.Identifier().getText();
|
||||
return new LocalVarIdentifier(identifier);
|
||||
} else if(ctx.instVar() != null) {
|
||||
return visitInstVar(ctx.instVar());
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@ -283,4 +284,20 @@ public class ASTGenerator extends DecafBaseVisitor<Node> {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Node visitInstVar(DecafParser.InstVarContext ctx) {
|
||||
return super.visitInstVar(ctx);
|
||||
}
|
||||
|
||||
// @Override
|
||||
// public Node visitArgumentList(DecafParser.ArgumentListContext ctx) {
|
||||
// if (ctx.expression().size() == 1) {
|
||||
// return visitExpression(ctx.expression(0));
|
||||
// } else if (ctx.expression().size() >= 2) {
|
||||
// for(DecafParser.ExpressionContext expressionContext: ctx.expression()) {
|
||||
//
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user