typecheck update for Example3
This commit is contained in:
parent
23f541ff7d
commit
fa430301bb
@ -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.codeGen();
|
||||
|
@ -1,6 +1,6 @@
|
||||
public class TestClass {
|
||||
public static void main(String[] args){
|
||||
new Example();
|
||||
new Example2();
|
||||
// new Example();
|
||||
// new Example2();
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,10 @@ import java.util.Objects;
|
||||
|
||||
public class TypeCheckHelper {
|
||||
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 type2Primitiv = Objects.equals(type2, "bool") || Objects.equals(type2, "int") || Objects.equals(type2, "char");
|
||||
|
||||
|
@ -17,7 +17,10 @@ public class BooleanConstantExpression extends AbstractType implements IExpressi
|
||||
|
||||
@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();
|
||||
result.type = "bool";
|
||||
setTypeCheckResult(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -17,7 +17,10 @@ public class CharConstantExpression extends AbstractType 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();
|
||||
result.type = "char";
|
||||
setTypeCheckResult(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -16,7 +16,7 @@ public class BlockStatement extends AbstractType implements IStatement {
|
||||
|
||||
//We will need a parameter which holds the symbol table
|
||||
HashMap<String, String> localVars;
|
||||
public String returnType;
|
||||
public String returnType; // "not" --> not void
|
||||
public List<IStatement> statements;
|
||||
// do we need expression, statementexpression
|
||||
|
||||
@ -31,16 +31,23 @@ public class BlockStatement extends AbstractType implements IStatement {
|
||||
HashMap<String, String> localVars) throws Exception {
|
||||
TypeCheckResult result = new TypeCheckResult();
|
||||
|
||||
this.localVars = localVars;
|
||||
if (statements.size() == 0) {
|
||||
result.type = "void";
|
||||
}
|
||||
|
||||
for (IStatement statement : statements) {
|
||||
|
||||
System.out.println(statement.getClass()); //todo remove
|
||||
// todo remove later if there are no null statement any more
|
||||
if (statement != null) {
|
||||
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){
|
||||
LocalVarDecl localVarDecl = (LocalVarDecl) statement;
|
||||
localVars.put(localVarDecl.identifier, localVarDecl.type);
|
||||
|
@ -29,6 +29,7 @@ public class ReturnStatement extends AbstractType implements IStatement{
|
||||
result.type = typedExpression.type;
|
||||
}
|
||||
|
||||
setTypeCheckResult(result);
|
||||
return result;
|
||||
}
|
||||
|
||||
@ -64,8 +65,8 @@ public class ReturnStatement extends AbstractType implements IStatement{
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeCheckResult getTypeCheckResult() {
|
||||
return getTypeCheckResult();
|
||||
}
|
||||
// @Override
|
||||
// public TypeCheckResult getTypeCheckResult() {
|
||||
// return this.
|
||||
// }
|
||||
}
|
||||
|
@ -45,10 +45,14 @@ public class AssignStatementExpression extends AbstractType implements IExpressi
|
||||
TypeCheckResult rightType = right.typeCheck(methodContext, typeContext, localVars);
|
||||
|
||||
String upperbound = helper.upperBound(leftType.type, rightType.type);
|
||||
if (Objects.equals(upperbound, leftType.type)) {
|
||||
result.type = leftType.type;
|
||||
if(Objects.equals(leftType.type, "boolean"))
|
||||
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);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user