engine/source/preprocessorlib/pp_expr.c
/**
 * Rearranges a parsed tree so that the operations will be done in order of
 * precedence.
 */
Rearranges a parsed tree so that the operations will be done in order of
precedence.
Rearranges a parsed tree so that the operations will be done in order of precedence .
void pp_expr_fix_precedence(pp_expr *self)
{
#if FIXTREE_DEBUG
    static int iteration = 0;
    ++iteration;
    printf("Iteration %i\n", iteration);
    tree_display2(self);
    printf("\n");
#endif
    if(self->info->type == LEAF)
    {
        assert(!self->left && !self->right);
    }
    else if(self->info->type == UNARY)
    {
        assert(self->left && !self->right);
        pp_expr_fix_precedence(self->left);
    }
    else
    {
        assert(self->info->type == BINARY);
        assert(self->left && self->right);
        pp_expr_fix_precedence(self->right);
        if(self->right->info->type == BINARY &&
                precedence(self->info->theToken.theType) >= precedence(self->right->info->theToken.theType))
        {
            pp_expr *ll = self->left->left;
            pp_expr *r = self->right;
#if FIXTREE_DEBUG
            printf("Swap %s with %s (iteration %i)\n", tokendisplay(self->info->theToken), tokendisplay(self->right->info->theToken), iteration);
#endif
            self->left->left = malloc(sizeof(pp_expr));
            pp_expr_init(self->left->left, self->left->info, ll ? ll : NULL, self->left->right);
            pp_expr_init(self->left, self->info, self->left->left, self->right->left);
            pp_expr_init(self, self->right->info, self->left, self->right->right);
            free(r);
        }
        pp_expr_fix_precedence(self->left);
    }
#if FIXTREE_DEBUG
    printf("return from %i\n", iteration);
    --iteration;
#endif
}
void pp_expr_fix_precedence ( pp_expr * self ) { #if FIXTREE_DEBUG static int iteration = 0 ; ++ iteration ; printf ( " \n " , iteration ) ; tree_display2 ( self ) ; printf ( " \n " ) ; #endif if ( self -> info -> type == LEAF ) { assert ( ! self -> left && ! self -> right ) ; } else if ( self -> info -> type == UNARY ) { assert ( self -> left && ! self -> right ) ; pp_expr_fix_precedence ( self -> left ) ; } else { assert ( self -> info -> type == BINARY ) ; assert ( self -> left && self -> right ) ; pp_expr_fix_precedence ( self -> right ) ; if ( self -> right -> info -> type == BINARY && precedence ( self -> info -> theToken . theType ) >= precedence ( self -> right -> info -> theToken . theType ) ) { pp_expr * ll = self -> left -> left ; pp_expr * r = self -> right ; #if FIXTREE_DEBUG printf ( " \n " , tokendisplay ( self -> info -> theToken ) , tokendisplay ( self -> right -> info -> theToken ) , iteration ) ; #endif self -> left -> left = malloc ( sizeof ( pp_expr ) ) ; pp_expr_init ( self -> left -> left , self -> left -> info , ll ? ll : NULL , self -> left -> right ) ; pp_expr_init ( self -> left , self -> info , self -> left -> left , self -> right -> left ) ; pp_expr_init ( self , self -> right -> info , self -> left , self -> right -> right ) ; free ( r ) ; } pp_expr_fix_precedence ( self -> left ) ; } #if FIXTREE_DEBUG printf ( " \n " , iteration ) ; -- iteration ; #endif }
Rearranges a parsed tree so that the operations will be done in order of
 precedence.
Rearranges a parsed tree so that the operations will be done in order of precedence .