ASTFunctionDeclaration.java
package edu.udel.cis.vsl.tass.front.minimp.ast.declaration;
import edu.udel.cis.vsl.tass.front.minimp.ast.misc.ASTIdentifier;
import edu.udel.cis.vsl.tass.front.minimp.ast.misc.ASTFunction;
import edu.udel.cis.vsl.tass.front.minimp.ast.statement.ASTCompoundStatement;
import edu.udel.cis.vsl.tass.front.minimp.ast.statement.ASTStatementIF;
import edu.udel.cis.vsl.tass.front.minimp.ast.statement.ASTWhileStatement;
import edu.udel.cis.vsl.tass.front.minimp.ast.type.ASTTypeIF;
public class ASTFunctionDeclaration extends ASTDeclaration {
private ASTIdentifier functionName;
private ASTTypeIF returnType;
private ASTDeclarationIF[] formalList;
private ASTFunction functionBody;
public ASTFunctionDeclaration(ASTTypeIF type, ASTIdentifier name,
ASTDeclarationIF[] args, ASTFunction body) {
returnType = type;
functionName = name;
formalList = args;
functionBody = body;
}
public ASTFunctionDeclaration(){}
public void setReturnType(ASTTypeIF type){
returnType = type;
}
public void setName(ASTIdentifier name){
functionName = name;
}
public void setFormals(ASTDeclarationIF[] formals){
formalList = formals;
}
public ASTIdentifier getName() {
return functionName;
}
public ASTTypeIF getType() {
return returnType;
}
public ASTDeclarationIF[] getFormalList() {
return formalList;
}
public int numFormalParameter() {
return this.formalList.length;
}
public ASTDeclarationIF getFormalParameter(int index) {
return formalList[index];
}
public ASTFunction getBody() {
return functionBody;
}
public String toString() {
String result = "begin function declaration:\n";
result += source.text() + " ";
result += "line:" + source.firstLine() + ":" + source.lastLine()
+ ", column:" + source.firstColumn() + ":" + source.lastColumn() + "\n";
if (this.formalList != null) {
for (int i = 0; i < this.formalList.length; i++) {
result += this.formalList[i].toString();
}
}
result += this.functionBody.toString();
result += "end function declaration\n";
return result;
}
public void setBody(ASTFunction body) {
this.functionBody = body;
}
public ASTStatementIF getStatement(String label) {
for (ASTStatementIF stmt : this.functionBody.getStatementList()) {
if (stmt instanceof ASTWhileStatement) {
if (((ASTWhileStatement) stmt).getInvariant() != null) {
if (((ASTWhileStatement) stmt).getInvariant().getLabel().equals(label)) {
return stmt;
}
}
} else if (stmt instanceof ASTCompoundStatement) {
return ((ASTCompoundStatement) stmt).getStatement(label);
}
}
return null;
}
}