2024-04-25 11:27:38 +00:00
|
|
|
package abstractSyntaxTree.Statement;
|
|
|
|
|
2024-05-02 11:12:39 +00:00
|
|
|
import TypeCheck.TypeCheckResult;
|
|
|
|
import TypeCheck.AbstractType;
|
2024-05-08 10:56:40 +00:00
|
|
|
import org.objectweb.asm.*;
|
2024-05-02 12:31:37 +00:00
|
|
|
|
2024-05-08 09:22:12 +00:00
|
|
|
import java.util.HashMap;
|
2024-05-02 12:31:37 +00:00
|
|
|
import java.util.List;
|
2024-05-02 11:12:39 +00:00
|
|
|
|
|
|
|
public class BlockStatement extends AbstractType implements IStatement{
|
2024-05-07 11:50:51 +00:00
|
|
|
|
|
|
|
//We will need a parameter which holds the symbol table
|
2024-05-08 09:22:12 +00:00
|
|
|
HashMap<String, String > localVars;
|
|
|
|
HashMap<String, String > typeIndentifierTable; // from program
|
2024-05-02 12:31:37 +00:00
|
|
|
List<IStatement> statements;
|
2024-05-08 10:48:56 +00:00
|
|
|
// do we need expression, statementexpression
|
|
|
|
|
2024-05-08 09:22:12 +00:00
|
|
|
public BlockStatement(List<IStatement> statements, HashMap<String, String> localVars, HashMap<String, String> typeIndentifierTable){
|
|
|
|
|
2024-05-02 12:31:37 +00:00
|
|
|
this.statements = statements;
|
|
|
|
}
|
2024-05-02 11:12:39 +00:00
|
|
|
@Override
|
|
|
|
public TypeCheckResult typeCheck() throws Exception {
|
2024-05-02 12:31:37 +00:00
|
|
|
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;
|
2024-05-02 11:12:39 +00:00
|
|
|
}
|
2024-05-07 11:50:51 +00:00
|
|
|
|
|
|
|
@Override
|
2024-05-08 10:56:40 +00:00
|
|
|
public void codeGen(MethodVisitor mv) throws Exception {
|
2024-05-07 11:50:51 +00:00
|
|
|
for (IStatement statement : statements) {
|
2024-05-08 10:56:40 +00:00
|
|
|
statement.codeGen(mv); //TODO: I think we need to pass the symbol table here
|
2024-05-07 11:50:51 +00:00
|
|
|
}
|
|
|
|
}
|
2024-04-25 11:27:38 +00:00
|
|
|
}
|