NichtHaskell/Source/abstractSyntaxTree/Statement/BlockStatement.java

35 lines
1.1 KiB
Java

package abstractSyntaxTree.Statement;
import TypeCheck.TypeCheckResult;
import TypeCheck.AbstractType;
import abstractSyntaxTree.Expression.IExpression;
import java.util.List;
public class BlockStatement extends AbstractType implements IStatement{
List<IStatement> statements;
public BlockStatement(List<IStatement> statements){
this.statements = statements;
}
@Override
public TypeCheckResult typeCheck() throws Exception {
TypeCheckResult result = new TypeCheckResult();
if(statements.size() == 0){
result.type = "void";
}
TypeCheckResult blockType = null;
for (IStatement statement : statements) {
TypeCheckResult typeOfCurrentStatement = statement.typeCheck();
if (blockType == null) {
blockType = typeOfCurrentStatement;
} else if (!typeOfCurrentStatement.equals(blockType) && !blockType.equals("void")) {
throw new IllegalArgumentException("different statement types");
}
}
return result;
}
}