continued typecheck
This commit is contained in:
parent
a147512dcb
commit
abc4e3ff70
@ -7,7 +7,9 @@ class Example {
|
|||||||
class Example2 {
|
class Example2 {
|
||||||
boolean instVarBool;
|
boolean instVarBool;
|
||||||
int m(int n){
|
int m(int n){
|
||||||
if(instVarBool){
|
boolean localBool;
|
||||||
|
localBool = true;
|
||||||
|
if(localBool){
|
||||||
return n;
|
return n;
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
|
@ -1,6 +1,12 @@
|
|||||||
package abstractSyntaxTree.Expression;
|
package abstractSyntaxTree.Expression;
|
||||||
|
|
||||||
import TypeCheck.AbstractType;
|
import TypeCheck.AbstractType;
|
||||||
|
import TypeCheck.TypeCheckResult;
|
||||||
|
import abstractSyntaxTree.Parameter.ParameterList;
|
||||||
|
import org.objectweb.asm.MethodVisitor;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.LinkedHashMap;
|
||||||
|
|
||||||
public class IntConstantExpression extends AbstractType implements IExpression{
|
public class IntConstantExpression extends AbstractType implements IExpression{
|
||||||
public int value;
|
public int value;
|
||||||
@ -8,4 +14,14 @@ public class IntConstantExpression extends AbstractType implements IExpression{
|
|||||||
public IntConstantExpression(int value) {
|
public IntConstantExpression(int value) {
|
||||||
this.value = value;
|
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 {
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,13 @@ public class LocalVarIdentifier 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();
|
||||||
|
if (localVars.containsKey(identifier)) {
|
||||||
|
result.type = localVars.get(identifier);
|
||||||
|
} else {
|
||||||
|
throw new Exception("TypeCheck Exception: local var does not exist");
|
||||||
|
}
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -34,35 +34,38 @@ public class BlockStatement extends AbstractType implements IStatement {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for (IStatement statement : statements) {
|
for (IStatement statement : statements) {
|
||||||
TypeCheckResult typeOfCurrentStatement = statement.typeCheck(methodContext, typeContext, localVars);
|
// 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(",")) {
|
if (typeOfCurrentStatement.type.contains(",")) {
|
||||||
// else if has 2 returns, all code paths must retrun a value.
|
// else if has 2 returns, all code paths must retrun a value.
|
||||||
String[] substrings = typeOfCurrentStatement.type.split(",");
|
String[] substrings = typeOfCurrentStatement.type.split(",");
|
||||||
|
|
||||||
String firstType = substrings[0];
|
String firstType = substrings[0];
|
||||||
String secondType = substrings[1];
|
String secondType = substrings[1];
|
||||||
|
|
||||||
if(!firstType.equals(this.returnType) || !firstType.equals(this.returnType)){
|
if (!firstType.equals(this.returnType) || !firstType.equals(this.returnType)) {
|
||||||
if(!firstType.equals("void")){
|
if (!firstType.equals("void")) {
|
||||||
throw new Exception("TypeCeck Exception: if paths return wrong type");
|
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");
|
throw new Exception("TypeCeck Exception: else paths return wrong type");
|
||||||
}
|
}
|
||||||
boolean firstIsVoid = firstType.equals("void");
|
boolean firstIsVoid = firstType.equals("void");
|
||||||
|
|
||||||
if(!firstIsVoid){
|
if (!firstIsVoid) {
|
||||||
typeOfCurrentStatement.type = firstType;
|
typeOfCurrentStatement.type = firstType;
|
||||||
}else{
|
} else {
|
||||||
typeOfCurrentStatement.type = secondType;
|
typeOfCurrentStatement.type = secondType;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (!typeOfCurrentStatement.type.equals(this.returnType)) {
|
if (!typeOfCurrentStatement.type.equals(this.returnType)) {
|
||||||
if (!typeOfCurrentStatement.type.equals("void"))
|
if (!typeOfCurrentStatement.type.equals("void"))
|
||||||
throw new Exception("TypeCheck Exception: Block returns the wrong type.");
|
throw new Exception("TypeCheck Exception: Block returns the wrong type.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result.type = this.returnType;
|
result.type = this.returnType;
|
||||||
|
Loading…
Reference in New Issue
Block a user