aboutsummaryrefslogblamecommitdiff
path: root/semantics.h
blob: 84dc9dc13d1f247ffa233931793786c8818069bc (plain) (tree)
1
2
3
4
5
6
7
8
9
10

                   
                  
 
                           
                                



                           

                               

                         
 
             
                     

                                                 
               
                 
               
  
 
                                                               
                           
 
              
          





                

             
           
                     
           
                                                    
                                           
                
                         

                                                
                


                          
                        
                                                  
          
                                      
               
  
 
                                                             
                                   
 
                                                  


                                            
 

                             
                    
              
                 
            
  

                       
                                 
                     


                      


         


                                                            
                                         
      
                                 
                                                  

                                                                    
                                     
 



                             



                             
                    





                             
            

                    
                 


                                                  
  
 
                        
                 


             


                

             



                     
                   


                                      

                                         






                                                               
                         
                                                                   
                                                         
                                                         
                                                            

                               
                                     
 
                                        
                                         

                                 









                                    
      
#ifndef SEMANTICS_H
#define SEMANTICS_H
#include "const.h"

typedef struct CNode CNode;
typedef struct CTable *CTable_t;
typedef struct CType CType;
typedef CType *CType_t;
typedef struct CVar CVar;
typedef CVar *CVar_t;
typedef struct CSymbol CSymbol;
typedef CSymbol *CSymbol_t;
typedef struct CDef CDef;
typedef CDef *CDef_t;

struct CVar {
    const char *name;
    CVar_t next;    /* next in the linked list */
    CType_t type;
    int offset;
    int is_const;
    CNode *ast;
};

CVar_t cvar_create(const char *name, CType_t type, CNode *ast);
void cvar_print(CVar_t cv);

struct CType {
    enum {
        CINT,
        CCHAR,
        CVOID,
        CSTRUCT,
        CUNION,
        CARR,
        CPTR,
        CFUNC
    } type;
    const char *name;
    union {
        CTable_t fields; /* for a struct or union */
        CType_t ref;    /* for a pointer */
        struct {
            CType_t elem;
            int len;
        } arr;                /* for an array */
        struct {
            CVar_t params;
            CVar_t local;
            CType_t ret;
            CNode *body;
        } func;               /* for a function */
    } rec;
    int size;   /* memory footprint */
    CNode *ast;
};

CType_t ctype_create(const char *name, int type, CNode *ast);
void ctype_debug_print(CType_t ct);

typedef unsigned int (*Hashfunc_t) (const char *);
#ifdef CIBIC_DEBUG
typedef const char *(*Printfunc_t) (void *);
#endif

typedef struct CTNode CTNode;
struct CTNode {
    const char *key;
    void *val;
    CTNode *next;
    int lvl;
};

typedef struct CTable {
    CTNode *head[MAX_TABLE_SIZE];
    Hashfunc_t hfunc;
#ifdef CIBIC_DEBUG
    Printfunc_t pfunc;
#endif
} CTable;


#ifdef CIBIC_DEBUG
CTable_t ctable_create(Hashfunc_t hfunc, Printfunc_t pfunc);
#else
CTable_t ctable_create(Hashfunc_t hfunc);
#endif