struct PType *copyPType( struct PType *src ) { if( src == NULL ) { return NULL; } else { struct PType *dest = (struct PType *)malloc(sizeof(struct PType)); dest->isError = src->isError; dest->isArray = src->isArray; dest->hasValue = src->hasValue; dest->type = src->type; dest->dimNum = src->dimNum; dest->dim = copyArrayDimList( src->dim ); if(src->hasValue == __TRUE) { switch(src->type) { case INTEGER_t: dest->value.integerVal = src->value.integerVal;break; case REAL_t: dest->value.realVal = src->value.realVal;break; case BOOLEAN_t: dest->value.booleanVal = src->value.booleanVal;break; } // dest->value = src->value; } return dest; } }
__BOOLEAN verifyExistence( struct SymTable *table, struct expr_sem *expr, int scope, __BOOLEAN isAssignmentLHS ) { __BOOLEAN result = __TRUE; struct SymNode *node = 0; node = lookupSymbol( table, expr->varRef->id, scope, __FALSE ); // if not found, check normal symbol if( node == 0 ) { // symbol not found fprintf( stdout, "########## Error at Line#%d: '%s' is not declared ##########\n", linenum, expr->varRef->id ); semError = __TRUE; expr->pType = createPType( ERROR_t ); result = __FALSE; } else { // deference and verify, if pass, setup PType field in expr_sem // expr is dereferenced... expr->isDeref = __TRUE; if( node->category == FUNCTION_t ) { fprintf( stdout, "########## Error at Line#%d: '%s' is function ##########\n", linenum, node->name ); semError = __TRUE; expr->pType = createPType( ERROR_t ); result = __FALSE; } else if( node->category==CONSTANT_t && isAssignmentLHS==__TRUE ) { fprintf( stdout, "########## Error at Line#%d: constant '%s' cannot be assigned ##########\n", linenum, node->name ); semError = __TRUE; expr->pType = createPType( ERROR_t ); result = __FALSE; } else { if( expr->varRef->dimNum == 0 ) { expr->pType = copyPType( node->type ); } else { // dereference dimension if( node->type->dimNum < expr->varRef->dimNum ) { fprintf( stdout, "########## Error at Line#%d: '%s' is %d dimension(s), but reference in %d dimension(s) ##########\n", linenum, node->name, node->type->dimNum, expr->varRef->dimNum ); semError = __TRUE; expr->pType = createPType( ERROR_t ); result = __FALSE; } else if( node->type->dimNum == expr->varRef->dimNum ) { // result in scalar! expr->pType = createPType( node->type->type ); } else { // result in array type expr->pType = (struct PType *)malloc(sizeof(struct PType)); expr->pType->isError = __FALSE; expr->pType->isArray = __TRUE; expr->pType->type = node->type->type; expr->pType->dimNum = (node->type->dimNum)-(expr->varRef->dimNum); int i; struct ArrayDimNode *arrPtr; for( i=0, arrPtr=(node->type->dim) ; i<(expr->varRef->dimNum) ; i++, arrPtr=(arrPtr->next) ); expr->pType->dim = copyArrayDimList( arrPtr ); } } } } return result; }
struct PType *copyPType( struct PType *src ) { if( src == 0 ) { return 0; } else { struct PType *dest = (struct PType *)malloc(sizeof(struct PType)); dest->isError = src->isError; dest->isArray = src->isArray; dest->type = src->type; dest->dimNum = src->dimNum; dest->dim = copyArrayDimList( src->dim ); return dest; } }