typecheck update for Example3

This commit is contained in:
Krauß, Josefine 2024-06-14 17:00:03 +02:00
parent 23f541ff7d
commit fa430301bb
8 changed files with 34 additions and 12 deletions

View File

@ -104,7 +104,7 @@ public class Compiler {
// } // }
abstractSyntaxTree.classes.get(2).methodDecls.get(0).codeBlock.returnType = "int"; //abstractSyntaxTree.classes.get(0).methodDecls.get(0).codeBlock.returnType = "char";
abstractSyntaxTree.typeCheck(); abstractSyntaxTree.typeCheck();
abstractSyntaxTree.codeGen(); abstractSyntaxTree.codeGen();

View File

@ -1,6 +1,6 @@
public class TestClass { public class TestClass {
public static void main(String[] args){ public static void main(String[] args){
new Example(); // new Example();
new Example2(); // new Example2();
} }
} }

View File

@ -5,6 +5,10 @@ import java.util.Objects;
public class TypeCheckHelper { public class TypeCheckHelper {
public String upperBound(String type1, String type2) throws Exception{ public String upperBound(String type1, String type2) throws Exception{
if(Objects.equals(type1, "boolean"))
type1 = "bool";
if (Objects.equals(type2, "boolean"))
type2 = "bool";
boolean type1Primitiv = Objects.equals(type1, "bool") || Objects.equals(type1, "int") || Objects.equals(type1, "char"); boolean type1Primitiv = Objects.equals(type1, "bool") || Objects.equals(type1, "int") || Objects.equals(type1, "char");
boolean type2Primitiv = Objects.equals(type2, "bool") || Objects.equals(type2, "int") || Objects.equals(type2, "char"); boolean type2Primitiv = Objects.equals(type2, "bool") || Objects.equals(type2, "int") || Objects.equals(type2, "char");

View File

@ -17,7 +17,10 @@ public class BooleanConstantExpression extends AbstractType implements IExpressi
@Override @Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception { 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();
result.type = "bool";
setTypeCheckResult(result);
return result;
} }
@Override @Override

View File

@ -17,7 +17,10 @@ public class CharConstantExpression extends AbstractType implements IExpression{
@Override @Override
public TypeCheckResult typeCheck(HashMap<String, HashMap<String, HashMap<String, ParameterList>>> methodContext, HashMap<String, HashMap<String, String>> typeContext, HashMap<String, String> localVars) throws Exception { 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();
result.type = "char";
setTypeCheckResult(result);
return result;
} }
@Override @Override

View File

@ -16,7 +16,7 @@ public class BlockStatement extends AbstractType implements IStatement {
//We will need a parameter which holds the symbol table //We will need a parameter which holds the symbol table
HashMap<String, String> localVars; HashMap<String, String> localVars;
public String returnType; public String returnType; // "not" --> not void
public List<IStatement> statements; public List<IStatement> statements;
// do we need expression, statementexpression // do we need expression, statementexpression
@ -31,16 +31,23 @@ public class BlockStatement extends AbstractType implements IStatement {
HashMap<String, String> localVars) throws Exception { HashMap<String, String> localVars) throws Exception {
TypeCheckResult result = new TypeCheckResult(); TypeCheckResult result = new TypeCheckResult();
this.localVars = localVars;
if (statements.size() == 0) { if (statements.size() == 0) {
result.type = "void"; result.type = "void";
} }
for (IStatement statement : statements) { for (IStatement statement : statements) {
System.out.println(statement.getClass()); //todo remove
// todo remove later if there are no null statement any more // todo remove later if there are no null statement any more
if (statement != null) { if (statement != null) {
TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars); TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars);
if(this.returnType == "not" && statement instanceof ReturnStatement){
ReturnStatement returnStatement = (ReturnStatement) statement;
this.returnType = returnStatement.getTypeCheckResult().type;
}
if(statement instanceof LocalVarDecl){ if(statement instanceof LocalVarDecl){
LocalVarDecl localVarDecl = (LocalVarDecl) statement; LocalVarDecl localVarDecl = (LocalVarDecl) statement;
localVars.put(localVarDecl.identifier, localVarDecl.type); localVars.put(localVarDecl.identifier, localVarDecl.type);

View File

@ -29,6 +29,7 @@ public class ReturnStatement extends AbstractType implements IStatement{
result.type = typedExpression.type; result.type = typedExpression.type;
} }
setTypeCheckResult(result);
return result; return result;
} }
@ -64,8 +65,8 @@ public class ReturnStatement extends AbstractType implements IStatement{
); );
} }
@Override // @Override
public TypeCheckResult getTypeCheckResult() { // public TypeCheckResult getTypeCheckResult() {
return getTypeCheckResult(); // return this.
} // }
} }

View File

@ -45,10 +45,14 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
TypeCheckResult rightType = right.typeCheck(methodContext, typeContext, localVars); TypeCheckResult rightType = right.typeCheck(methodContext, typeContext, localVars);
String upperbound = helper.upperBound(leftType.type, rightType.type); String upperbound = helper.upperBound(leftType.type, rightType.type);
if (Objects.equals(upperbound, leftType.type)) { if(Objects.equals(leftType.type, "boolean"))
result.type = leftType.type; leftType.type = "bool";
if (!Objects.equals(upperbound, leftType.type)) {
throw new Exception("TypeCheck Exception: upper bound of assignment is not left type");
} }
result.type = "void";
setTypeCheckResult(result); setTypeCheckResult(result);
return result; return result;
} }