Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
5.3 kB
2
Indexable
Never
int evaluateTree(BTNode *root) {
    int retval = 0, lv = 0, rv = 0;
    int i,j;
    int left,right;

    if (root != NULL) {
        switch (root->data) {
            case ID:
                retval = getval(root->lexeme);
                r[number]=retval;

                for(i=0;i<sbcount;i++)
                {
                    if( strcmp(table[i].name,root->lexeme)==0 )
                    {
                        printf("MOV r%d [%d]\n",number,i*4);

                        break;
                    }
                }

                number++;
                break;
            case INT:
                retval = atoi(root->lexeme);
                r[number]=retval;
                printf("MOV r%d %d\n",number,r[number]);
                number++;
                break;
            case ASSIGN:
                rv = evaluateTree(root->right);
                retval = setval(root->left->lexeme, rv);

                for(i=0;i<sbcount;i++)
                {
                    if( strcmp(table[i].name,root->left->lexeme)==0)
                    {
                        for(j=number-1;j>=0;j--)
                        {
                            if(rv==r[j])
                            {
                                printf("MOV [%d] r%d\n",i*4,j);
                                break;
                            }
                            
                        }
                        break;
                    }
                }

                break;
            case ADDSUB:
            case MULDIV:
            if (root -> left == NULL || root -> right == NULL) {
                    err(NOVALUE);
                }


                lv = evaluateTree(root->left);
                rv = evaluateTree(root->right);
                right = --number;
                left  = --number;
                number++;
                
                if (strcmp(root->lexeme, "+") == 0) {
                    retval = lv + rv;
                    r[left]=retval; r[right]=0;
                    printf("ADD r%d r%d\n",left,right);
                } else if (strcmp(root->lexeme, "-") == 0) {
                    retval = lv - rv;
                    r[left]=retval; r[right]=0;
                    printf("SUB r%d r%d\n",left,right);
                } else if (strcmp(root->lexeme, "*") == 0) {
                    retval = lv * rv;
                    r[left]=retval; r[right]=0;
                    printf("MUL r%d r%d\n",left,right);
                } else if (strcmp(root->lexeme, "/") == 0) {
                    if (rv == 0){
                        look_value=0;
                        look(root->right);
                        if(look_value)
                        retval = 0;                              
                        else
                        error(DIVZERO);
                    }
                    else retval = lv / rv;

                    r[left]=retval; r[right]=0;
                    printf("DIV r%d r%d\n",left,right);
                }
                break;
            case INCDEC:        
                if (root -> left == NULL || root -> right == NULL) {
                    err(NOVALUE);
                }
                lv = evaluateTree(root->left);                 
                rv = evaluateTree(root->right);
                right = --number;
                left  = --number;
                number++;
                number++;          

                if (strcmp(root->lexeme, "++") == 0) {
                    retval=lv+rv;
                    setval(root->right->lexeme, retval);
                    r[left]=1; r[right]=retval;
                    printf("ADD r%d r%d\n",right,left);
                } else if (strcmp(root->lexeme, "--") == 0) {
                    retval=rv-lv;
                    setval(root->right->lexeme, retval);
                    r[left]=1; r[right]=retval;
                    printf("SUB r%d r%d\n",right,left);
                }
                break;
            case AND:
            case OR:
            case XOR:
            if (root -> left == NULL || root -> right == NULL) {
                    err(NOVALUE);
                }

                lv = evaluateTree(root->left);                 
                rv = evaluateTree(root->right);
                right = --number;
                left  = --number;
                number++;
                
                if (strcmp(root->lexeme, "&") == 0) {
                    retval = lv & rv;
                    r[left]=retval; r[right]=0;
                    printf("AND r%d r%d\n",left,right);
                } else if (strcmp(root->lexeme, "|") == 0) {
                    retval = lv | rv;
                    r[left]=retval; r[right]=0;
                    printf("OR r%d r%d\n",left,right);
                } else if (strcmp(root->lexeme, "^") == 0) {
                    retval = lv ^ rv;
                    r[left]=retval; r[right]=0;
                    printf("XOR r%d r%d\n",left,right);
                }
                break;
            default:
                retval = 0;
        }
    }
    return retval;
}