aboutsummaryrefslogblamecommitdiff
path: root/ast.c
blob: 599b67da9ab9177b6137c838d15ae42a3a5b5c64 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13












                                                  





















                                         











                                                                          
                                


                                           
                  
                                     
      





                                

                                                    
     
                   

                    
                       













































































                                                               
                       




                                                           



                                      
                      


                             




                                                                                      





                                     
                          
                      
                     







                                                            



                                
                                  
                            






                                                                   



                                    
                        
                       






                                                                



                                    
                             
                       




                            





                                               


                                         

                                          



                                             

                                                  




                                                     



                                                   

                                            















                                                              
                                      





                               






























































































                                                     






                                              


                  
                                          



                                              
                                     



                




















                                                   

             
#include <stdlib.h>
#include <assert.h>
#include <stdarg.h>
#include <stdio.h>
#include "ast.h"
#include "cibic.tab.h"
#define NEW_CNODE ((CNode *)malloc(sizeof(CNode)))

CNode *ast_root;

void cnode_init() {
}

void cnode_reverse_chd(CNode *node) {
    static CNode *chdn[MAX_CHDN];
    CNode *p;
    int n = 0;
    for (p = node->chd; p; p = p->next)
        cnode_reverse_chd(p);
    for (p = node->chd; p; p = p->next)
        chdn[n++] = p;
    if (n)
    {
        node->chd = chdn[--n];
        for (; n; n--)
            chdn[n]->next = chdn[n - 1];
        chdn[0]->next = NULL;
    }
}

CNode *cnode_create_ast(CNode *wrapped) {
    cnode_reverse_chd(wrapped);
    return wrapped;
}

CNode *cnode_create_nop() {
    CNode *nop = NEW_CNODE;
    nop->type = NOP;
    nop->next = nop->chd = NULL;
    return nop;
}

CNode *cnode_create_general(int type, int subtype, int pnum, va_list ap) {
    int i;
    CNode *exp = NEW_CNODE;
    exp->type = type;
    exp->rec.subtype = subtype;
    exp->next = exp->chd = NULL;
    for (i = 0; i < pnum; i++)
    {
        CNode *subexp = va_arg(ap, CNode*);
#ifdef CNODE_DEBUG
        assert(subexp->next == NULL);
#endif
        subexp->next = exp->chd;
        exp->chd = subexp;
    }
    return exp;
}

CNode *cnode_list_append(CNode *list, CNode *tail) {
    if (list->type == NOP) 
    {
        free(list);
        return tail;
    }
    tail->next = list; 
    return tail;
}

CNode *cnode_create_identifier(char *val) {
    CNode *exp = NEW_CNODE;
    exp->type = ID;
    exp->chd = exp->next = NULL;
    exp->rec.strval = val;
    return exp;
}

CNode *cnode_create_int_const(int val) {
    /* TODO: overflow checking */
    CNode *exp = NEW_CNODE;
    exp->type = INT;
    exp->chd = exp->next = NULL;
    exp->rec.intval = val;
    return exp;
}

CNode *cnode_create_char_const(int val) {
    /* TODO: overflow checking */
    CNode *exp = NEW_CNODE;
    exp->type = CHAR;
    exp->chd = exp->next = NULL;
    exp->rec.intval = val;
    return exp;
}

CNode *cnode_create_str_const(char *val) {
    CNode *exp = NEW_CNODE;
    exp->type = STR;
    exp->chd = exp->next = NULL;
    exp->rec.strval = val;
    return exp;
}

CNode *cnode_create_exp(int exp_type, int