50 lines
1.6 KiB
Java
50 lines
1.6 KiB
Java
package abstractSyntaxTree.Statement;
|
|
|
|
import TypeCheck.TypeCheckResult;
|
|
import TypeCheck.AbstractType;
|
|
import org.objectweb.asm.*;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.List;
|
|
|
|
public class BlockStatement extends AbstractType implements IStatement{
|
|
|
|
//We will need a parameter which holds the symbol table
|
|
HashMap<String, String > localVars;
|
|
HashMap<String, String > typeIndentifierTable; // from program
|
|
List<IStatement> statements;
|
|
// do we need expression, statementexpression
|
|
|
|
public BlockStatement(List<IStatement> statements, HashMap<String, String> localVars, HashMap<String, String> typeIndentifierTable){
|
|
|
|
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;
|
|
}
|
|
|
|
@Override
|
|
public void codeGen(MethodVisitor mv) throws Exception {
|
|
for (IStatement statement : statements) {
|
|
statement.codeGen(mv); //TODO: I think we need to pass the symbol table here
|
|
}
|
|
}
|
|
}
|