void checkIfStmt(AST_NODE* ifNode)
{
  AST_NODE* exprNode = ifNode->child;
  AST_NODE* blockNode1 = exprNode->rightSibling;
  AST_NODE* blockNode2 = blockNode1->rightSibling;
  openScope();
  processBlockNode(blockNode1);
  closeScope();
  if (blockNode2->nodeType == BLOCK_NODE) {
    openScope();
    processBlockNode(blockNode2);
    closeScope();
  }
}
void checkForStmt(AST_NODE* forNode)
{
  AST_NODE *initNode = forNode->child;
  AST_NODE *condNode = initNode->rightSibling;
  AST_NODE *incrNode = condNode->rightSibling;
  AST_NODE *stmtNode = incrNode->rightSibling;
  initNode = initNode->child;
  while (initNode) {
    checkAssignmentStmt(initNode);
    initNode = initNode->rightSibling;
  }
  if (condNode->nodeType != NUL_NODE) {
    condNode = condNode->child;
    while (condNode) {
      processExprRelatedNode(condNode);
      condNode = condNode->rightSibling;
    }
  }
  incrNode = incrNode->child;
  while (incrNode) {
    checkAssignmentStmt(incrNode);
    incrNode = incrNode->rightSibling;
  }
  openScope();
  processBlockNode(stmtNode);
  closeScope();
}
void checkWhileStmt(AST_NODE* whileNode)
{
  AST_NODE *condNode = whileNode->child;
  AST_NODE *blockNode = condNode->rightSibling;
  processExprRelatedNode(condNode);
  openScope();
  processBlockNode(blockNode);
  closeScope();
}
Exemple #4
0
void define_routines(void)
{	
	SymbolEntry *se;
	const char *name;

	name = "READ_INT";
	se = newFunction(name);
	openScope();
    endFunctionHeader(se, typeInteger);
	closeScope();

	name = "READ_BOOL";
	se = newFunction(name);
	openScope();
    endFunctionHeader(se, typeBoolean);
	closeScope();
	
	name = "getchar";
	se = newFunction(name);
	openScope();
    endFunctionHeader(se, typeInteger);
	closeScope();

	name = "READ_REAL";
	se = newFunction(name);
	openScope();
    endFunctionHeader(se, typeReal);
	closeScope();

	name = "READ_STRING";
	se = newFunction(name);
	openScope();
	newParameter("par1", typeInteger, PASS_BY_VALUE, se);
	newParameter("par2", typeIArray(typeChar), PASS_BY_REFERENCE, se);
    endFunctionHeader(se, typeVoid);
	closeScope();

	name = "putchar";
	se = newFunction(name);
	openScope();
	newParameter("par", typeChar, PASS_BY_VALUE, se);
    endFunctionHeader(se, typeVoid);
	closeScope();

	name = "puts";
	se = newFunction(name);
	openScope();
	newParameter("par", typeIArray(typeChar), PASS_BY_REFERENCE, se);
    endFunctionHeader(se, typeVoid);
	closeScope();

	name = "writeInteger";
	se = newFunction(name);
	openScope();
	newParameter("par1", typeInteger, PASS_BY_VALUE, se);
	//newParameter("par2", typeInteger, PASS_BY_VALUE, se);
    endFunctionHeader(se, typeVoid);
	closeScope();

	name = "writeBoolean";
	se = newFunction(name);
	openScope();
	newParameter("par1", typeBoolean, PASS_BY_VALUE, se);
	//newParameter("par2", typeInteger, PASS_BY_VALUE, se);
    endFunctionHeader(se, typeVoid);
	closeScope();

	name = "writeChar";
	se = newFunction(name);
	openScope();
	newParameter("par1", typeChar, PASS_BY_VALUE, se);
	//newParameter("par2", typeInteger, PASS_BY_VALUE, se);
    endFunctionHeader(se, typeVoid);
	closeScope();

	name = "writeReal";
	se = newFunction(name);
	openScope();
	newParameter("par1", typeReal, PASS_BY_VALUE, se);
	newParameter("par2", typeInteger, PASS_BY_VALUE, se);
	//newParameter("par3", typeInteger, PASS_BY_VALUE, se);
    endFunctionHeader(se, typeVoid);
	closeScope();

	name = "writeString";
	se = newFunction(name);
	openScope();
	newParameter("par1", typeIArray(typeChar), PASS_BY_REFERENCE, se);
	//newParameter("par2", typeInteger, PASS_BY_VALUE, se);
    endFunctionHeader(se, typeVoid);
	closeScope();
	
	/*Math builtin functions*/
	name = "abs";
	se = newFunction(name);
	openScope();
	newParameter("par", typeInteger, PASS_BY_VALUE, se);
    endFunctionHeader(se, typeInteger);
	closeScope();

	name = "fabs";
	se = newFunction(name);
	openScope();
	newParameter("par", typeReal, PASS_BY_VALUE, se);
    endFunctionHeader(se, typeReal);
	closeScope();

	name = "sqrt";
	se = newFunction(name);
	openScope();
	newParameter("par", typeReal, PASS_BY_VALUE, se);
    endFunctionHeader(se, typeReal);
	closeScope();

	name = "sin";
	se = newFunction(name);
	openScope();
	newParameter("par", typeReal, PASS_BY_VALUE, se);
    endFunctionHeader(se, typeReal);
	closeScope();

	name = "cos";
	se = newFunction(name);
	openScope();
	newParameter("par", typeReal, PASS_BY_VALUE, se);
    endFunctionHeader(se, typeReal);
	closeScope();

	name = "tan";
	se = newFunction(name);
	openScope();
	newParameter("par", typeReal, PASS_BY_VALUE, se);
    endFunctionHeader(se, typeReal);
	closeScope();

	name = "arctan";
	se = newFunction(name);
	openScope();
	newParameter("par", typeReal, PASS_BY_VALUE, se);
    endFunctionHeader(se, typeReal);
	closeScope();

	name = "exp";
	se = newFunction(name);
	openScope();
	newParameter("par", typeReal, PASS_BY_VALUE, se);
    endFunctionHeader(se, typeReal);
	closeScope();
	
	name = "ln";
	se = newFunction(name);
	openScope();
	newParameter("par", typeReal, PASS_BY_VALUE, se);
    endFunctionHeader(se, typeReal);
	closeScope();
	
	name = "pi";
	se = newFunction(name);
	openScope();
    endFunctionHeader(se, typeReal);
	closeScope();

	name = "trunc";
	se = newFunction(name);
	openScope();
	newParameter("par", typeReal, PASS_BY_VALUE, se);
    endFunctionHeader(se, typeReal);
	closeScope();

	name = "round";
	se = newFunction(name);
	openScope();
	newParameter("par", typeReal, PASS_BY_VALUE, se);
    endFunctionHeader(se, typeReal);
	closeScope();

	name = "TRUNC";
	se = newFunction(name);
	openScope();
	newParameter("par", typeReal, PASS_BY_VALUE, se);
    endFunctionHeader(se, typeInteger);
	closeScope();
	
	name = "ROUND";
	se = newFunction(name);
	openScope();
	newParameter("par", typeReal, PASS_BY_VALUE, se);
    endFunctionHeader(se, typeInteger);
	closeScope();
	
	/*End Math builtin functions*/
	
	/*String handling builtin functions*/
	name = "strlen";
	se = newFunction(name);
	openScope();
	newParameter("par", typeIArray(typeChar), PASS_BY_REFERENCE, se);
    endFunctionHeader(se, typeInteger);
	closeScope();

	name = "strcmp";
	se = newFunction(name);
	openScope();
	newParameter("par1", typeIArray(typeChar), PASS_BY_REFERENCE, se);
	newParameter("par2", typeIArray(typeChar), PASS_BY_REFERENCE, se);
    endFunctionHeader(se, typeInteger);
	closeScope();

	name = "strcpy";
	se = newFunction(name);
	openScope();
	newParameter("par1", typeIArray(typeChar), PASS_BY_REFERENCE, se);
	newParameter("par2", typeIArray(typeChar), PASS_BY_REFERENCE, se);
    endFunctionHeader(se, typeVoid);
	closeScope();

	name = "strcat";
	se = newFunction(name);
	openScope();
	newParameter("par1", typeIArray(typeChar), PASS_BY_REFERENCE, se);
	newParameter("par2", typeIArray(typeChar), PASS_BY_REFERENCE, se);
    endFunctionHeader(se, typeVoid);
	closeScope();

	/*End String handling builtin functions*/
	

}
Exemple #5
0
SymbolTable::~SymbolTable() {
	while (!symtabStack_.empty())
		closeScope();
}
void declareFunction(AST_NODE* declarationNode)
{
  //get node
  AST_NODE* idNode = declarationNode->rightSibling;
  AST_NODE* paramListNode = idNode->rightSibling;
  AST_NODE* blockNode = paramListNode->rightSibling;

  SymbolAttribute *funcAttr = (SymbolAttribute*)malloc(sizeof(SymbolAttribute));
  FunctionSignature* funcSig = (FunctionSignature*)malloc(sizeof(FunctionSignature));
  Parameter *param = NULL;
  //generate attribute
  funcAttr->attributeKind = FUNCTION_SIGNATURE;
  funcAttr->attr.functionSignature = funcSig;

  //generate signature
  funcSig->parametersCount = 0;
  funcSig->parameterList = NULL;
  funcSig->returnType = retrieveType(declarationNode->semantic_value.identifierSemanticValue.identifierName);

  //printf("get function %s\n", idNode->semantic_value.identifierSemanticValue.identifierName);
  //printf("return type %d\n", retrieveType(declarationNode->semantic_value.identifierSemanticValue.identifierName));

  //parse parameter node
  AST_NODE *paramNode = paramListNode->child;
  while(paramNode) {
    //push into Parameter node
    AST_NODE *varTypeNode = paramNode->child;
    AST_NODE *varidNode = varTypeNode->rightSibling;
    Parameter *nextParam = param;
    param = (Parameter*)malloc(sizeof(Parameter));
    param->type = (TypeDescriptor*)malloc(sizeof(TypeDescriptor));
    param->parameterName = (char*)malloc(strlen(varidNode->semantic_value.identifierSemanticValue.identifierName+1));
    strcpy(param->parameterName, varidNode->semantic_value.identifierSemanticValue.identifierName);
    param->next = nextParam;
    ++(funcSig->parametersCount);
    param->type->properties.dataType
      = retrieveType(varTypeNode->semantic_value.identifierSemanticValue.identifierName);

    processDeclDimList(varidNode, param->type, True);

    paramNode = paramNode->rightSibling;
  }
  funcSig->parameterList = param;
  if (detectSymbol(idNode->semantic_value.identifierSemanticValue.identifierName)) {
    //function name redefine
    printErrorMsg(idNode, SYMBOL_REDECLARE);
  } else {
    //push into symbol table
    enterSymbol(idNode->semantic_value.identifierSemanticValue.identifierName, funcAttr);
  }
  openScope();
  //generate param symbol
  SymbolAttribute *paramAttr = NULL;
  TypeDescriptor *paramType = NULL;
  Parameter *paramVar = param;
  while(paramVar){
    SymbolAttribute *paramAttr = (SymbolAttribute*)malloc(sizeof(SymbolAttribute));
    TypeDescriptor *paramType = (TypeDescriptor*)malloc(sizeof(TypeDescriptor));
    paramAttr->attributeKind = VARIABLE_ATTRIBUTE;
    paramAttr->attr.typeDescriptor = paramType;

    memcpy(paramType, param->type, sizeof(TypeDescriptor));
    if (detectSymbol(paramVar->parameterName)) {
      //function name redefine
      printErrorMsg(paramListNode, SYMBOL_REDECLARE);
    } else {
      //push into symbol table
      enterSymbol(paramVar->parameterName, paramAttr);
    }

    paramVar = paramVar->next;
  }
  
  processBlockNode(blockNode);
  closeScope();
}
Exemple #7
0
void
ObjectDumper::closeStruct()
{
    closeScope();
    addLine("}");
}
Exemple #8
0
void symbolTableEnd(){
  while(symbolTable.tables != NULL){
    closeScope();
  }
}