void initializeSymbolTable()
{
    ARoffset = 0;
    symbolTable.currentLevel = 0;
    symbolTable.scopeDisplayElementCount = 10;
    symbolTable.scopeDisplay = (SymbolTableEntry**)malloc(symbolTable.scopeDisplayElementCount * sizeof(SymbolTableEntry*));
    int index = 0;
    for(index = 0; index != symbolTable.scopeDisplayElementCount; ++index)
    {
        symbolTable.scopeDisplay[index] = NULL;
    }
    for(index = 0; index != HASH_TABLE_SIZE; ++index)
    {
        symbolTable.hashTable[index] = NULL;
    }

    SymbolAttribute* intAttribute = (SymbolAttribute*)malloc(sizeof(SymbolAttribute));
    intAttribute->attributeKind = TYPE_ATTRIBUTE;
    intAttribute->attr.typeDescriptor = (TypeDescriptor*)malloc(sizeof(TypeDescriptor));
    intAttribute->attr.typeDescriptor->kind = SCALAR_TYPE_DESCRIPTOR;
    intAttribute->attr.typeDescriptor->properties.dataType = INT_TYPE;
    enterSymbol(SYMBOL_TABLE_INT_NAME, intAttribute);

    SymbolAttribute* floatAttribute = (SymbolAttribute*)malloc(sizeof(SymbolAttribute));
    floatAttribute->attributeKind = TYPE_ATTRIBUTE;
    floatAttribute->attr.typeDescriptor = (TypeDescriptor*)malloc(sizeof(TypeDescriptor));
    floatAttribute->attr.typeDescriptor->kind = SCALAR_TYPE_DESCRIPTOR;
    floatAttribute->attr.typeDescriptor->properties.dataType = FLOAT_TYPE;
    enterSymbol(SYMBOL_TABLE_FLOAT_NAME, floatAttribute);

    SymbolAttribute* voidAttribute = (SymbolAttribute*)malloc(sizeof(SymbolAttribute));
    voidAttribute->attributeKind = TYPE_ATTRIBUTE;
    voidAttribute->attr.typeDescriptor = (TypeDescriptor*)malloc(sizeof(TypeDescriptor));
    voidAttribute->attr.typeDescriptor->kind = SCALAR_TYPE_DESCRIPTOR;
    voidAttribute->attr.typeDescriptor->properties.dataType = VOID_TYPE;
    enterSymbol(SYMBOL_TABLE_VOID_NAME, voidAttribute);

    SymbolAttribute* readAttribute = NULL;
    readAttribute = (SymbolAttribute*)malloc(sizeof(SymbolAttribute));
    readAttribute->attributeKind = FUNCTION_SIGNATURE;
    readAttribute->attr.functionSignature = (FunctionSignature*)malloc(sizeof(FunctionSignature));
    readAttribute->attr.functionSignature->returnType = INT_TYPE;
    readAttribute->attr.functionSignature->parameterList = NULL;
    readAttribute->attr.functionSignature->parametersCount = 0;
    enterSymbol(SYMBOL_TABLE_SYS_LIB_READ, readAttribute);

    SymbolAttribute* freadAttribute = NULL;
    freadAttribute = (SymbolAttribute*)malloc(sizeof(SymbolAttribute));
    freadAttribute->attributeKind = FUNCTION_SIGNATURE;
    freadAttribute->attr.functionSignature = (FunctionSignature*)malloc(sizeof(FunctionSignature));
    freadAttribute->attr.functionSignature->returnType = FLOAT_TYPE;
    freadAttribute->attr.functionSignature->parameterList = NULL;
    freadAttribute->attr.functionSignature->parametersCount = 0;
    enterSymbol(SYMBOL_TABLE_SYS_LIB_FREAD, freadAttribute);
}
Esempio n. 2
0
void declareIdList(AST_NODE* declarationNode, SymbolAttributeKind isVariableOrTypeAttribute, int ignoreArrayFirstDimSize)
{
  AST_NODE *idNode = declarationNode->rightSibling;
  while (idNode) {
    if (detectSymbol(idNode->semantic_value.identifierSemanticValue.identifierName)) {
      //symbol name redefine
      printErrorMsg(idNode, SYMBOL_REDECLARE);
    } else {
      SymbolAttribute* varAttr = (SymbolAttribute*)malloc(sizeof(SymbolAttribute));
      TypeDescriptor* typeDesc = (TypeDescriptor*)malloc(sizeof(TypeDescriptor));
      varAttr->attributeKind = VARIABLE_ATTRIBUTE;
      varAttr->attr.typeDescriptor = typeDesc;
      typeDesc->properties.dataType 
        = retrieveType(declarationNode->semantic_value.identifierSemanticValue.identifierName);

      if (typeDesc->properties.dataType == VOID_TYPE) {
        printErrorMsg(idNode, VOID_VARIABLE);
      } else {
        //printf("get variable %s\n", idNode->semantic_value.identifierSemanticValue.identifierName);
        //printf("type %d\n", retrieveType(declarationNode->semantic_value.identifierSemanticValue.identifierName));

        processDeclDimList(idNode, typeDesc, False);

        enterSymbol(idNode->semantic_value.identifierSemanticValue.identifierName, varAttr);
      }
    }

    idNode = idNode->rightSibling;
  }
}
void initializeSymbolTable (void) {
    int i;
    symbolTable.currentLevel = 0;
    symbolTable.scopeDisplayElementCount = 256;
    symbolTable.scopeDisplay = (SymbolTableEntry **)malloc(sizeof(SymbolTableEntry *) * 256);
    symbolTable.scopeDisplay[0] = newSymbolTableEntry(0);
    for(i = 0; i < HASH_TABLE_SIZE; i++)
        symbolTable.hashTable[i] = newSymbolTableEntry(0);

    SymbolAttribute *int1, *float1, *void1, *read, *fread, *write;
    Parameter *w;
    int1 = (SymbolAttribute *)malloc(sizeof(SymbolAttribute));
    float1 = (SymbolAttribute *)malloc(sizeof(SymbolAttribute));
    void1 = (SymbolAttribute *)malloc(sizeof(SymbolAttribute));
    read = (SymbolAttribute *)malloc(sizeof(SymbolAttribute));
    fread = (SymbolAttribute *)malloc(sizeof(SymbolAttribute));
    write = (SymbolAttribute *)malloc(sizeof(SymbolAttribute));
    int1->attributeKind = TYPE_ATTRIBUTE;
    float1->attributeKind = TYPE_ATTRIBUTE;
    void1->attributeKind = TYPE_ATTRIBUTE;
    read->attributeKind = FUNCTION_SIGNATURE;
    fread->attributeKind = FUNCTION_SIGNATURE;
    write->attributeKind = FUNCTION_SIGNATURE;
    int1->attr.typeDescriptor = (TypeDescriptor *)malloc(sizeof(TypeDescriptor));
    float1->attr.typeDescriptor = (TypeDescriptor *)malloc(sizeof(TypeDescriptor));
    void1->attr.typeDescriptor = (TypeDescriptor *)malloc(sizeof(TypeDescriptor));
    read->attr.functionSignature = (FunctionSignature *)malloc(sizeof(FunctionSignature));
    fread->attr.functionSignature = (FunctionSignature *)malloc(sizeof(FunctionSignature));
    write->attr.functionSignature = (FunctionSignature *)malloc(sizeof(FunctionSignature));
    w = (Parameter *)malloc(sizeof(Parameter));
    w->type = (TypeDescriptor *)malloc(sizeof(TypeDescriptor));
    w->next = NULL;
    w->type->kind = SCALAR_TYPE_DESCRIPTOR;
    w->type->properties.dataType = VOID_TYPE;
    w->parameterName = "yooo";
    int1->attr.typeDescriptor->kind = SCALAR_TYPE_DESCRIPTOR;
    float1->attr.typeDescriptor->kind = SCALAR_TYPE_DESCRIPTOR;
    void1->attr.typeDescriptor->kind = SCALAR_TYPE_DESCRIPTOR;
    read->attr.functionSignature->parametersCount = 0;
    fread->attr.functionSignature->parametersCount = 0;
    write->attr.functionSignature->parametersCount = 1;
    read->attr.functionSignature->parameterList = NULL;
    fread->attr.functionSignature->parameterList = NULL;
    write->attr.functionSignature->parameterList = w;
    read->attr.functionSignature->returnType = INT_TYPE;
    fread->attr.functionSignature->returnType = FLOAT_TYPE;
    write->attr.functionSignature->returnType = VOID_TYPE;
    int1->attr.typeDescriptor->properties.dataType = INT_TYPE;
    float1->attr.typeDescriptor->properties.dataType = FLOAT_TYPE;
    void1->attr.typeDescriptor->properties.dataType = VOID_TYPE;

    enterSymbol(SYMBOL_TABLE_INT_NAME, int1);
    enterSymbol(SYMBOL_TABLE_FLOAT_NAME, float1);
    enterSymbol(SYMBOL_TABLE_VOID_NAME, void1);
    enterSymbol(SYMBOL_TABLE_SYS_LIB_READ, read);
    enterSymbol(SYMBOL_TABLE_SYS_LIB_FREAD, fread);
    enterSymbol(SYMBOL_TABLE_SYS_LIB_WRITE, write);
}
Esempio n. 4
0
void processTypeNode(AST_NODE* idNodeAsType)
{
  AST_NODE* idNode = idNodeAsType->rightSibling;
  SymbolAttribute* typeAttr = (SymbolAttribute*)malloc(sizeof(SymbolAttribute));
  typeAttr->attributeKind = TYPE_ATTRIBUTE;
  typeAttr->attr.typeDescriptor = (TypeDescriptor*)malloc(sizeof(TypeDescriptor));
  typeAttr->attr.typeDescriptor->properties.dataType 
    = retrieveType(idNodeAsType->semantic_value.identifierSemanticValue.identifierName);
  while (idNode) {
    if (detectSymbol(idNode->semantic_value.identifierSemanticValue.identifierName)) {
      //typename redefine
      printErrorMsg(idNode, SYMBOL_REDECLARE);
    } else {
      //push into symbol table
      enterSymbol(idNode->semantic_value.identifierSemanticValue.identifierName, typeAttr);
    }
    //rightSibling until NULL
    idNode = idNode->rightSibling;
  }
}
Esempio n. 5
0
void SymbolTable::initializeForCminus()
{
	enterSymbol("int", new TypeAttributes(new IntegerTypeDescriptor));
	enterSymbol("float", new TypeAttributes(new FloatTypeDescriptor));
	enterSymbol("void", new TypeAttributes(new VoidTypeDescriptor));
}
Esempio n. 6
0
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();
}