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.typeCheck();
|
||||||
|
|
||||||
abstractSyntaxTree.codeGen();
|
abstractSyntaxTree.codeGen();
|
||||||
|
@ -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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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");
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
@ -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
|
||||||
|
@ -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);
|
||||||
|
@ -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.
|
||||||
}
|
// }
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user