absyntax/absyntax.cc
changeset 625 c0bda77b37a0
parent 612 c062ff18d04f
child 654 7421cb63defa
equal deleted inserted replaced
412:aad38592bdde 625:c0bda77b37a0
    38 #include <string.h>
    38 #include <string.h>
    39 
    39 
    40 #include "absyntax.hh"
    40 #include "absyntax.hh"
    41 //#include "../stage1_2/iec.hh" /* required for BOGUS_TOKEN_ID, etc... */
    41 //#include "../stage1_2/iec.hh" /* required for BOGUS_TOKEN_ID, etc... */
    42 #include "visitor.hh"
    42 #include "visitor.hh"
    43 
    43 #include "../main.hh" // required for ERROR() and ERROR_MSG() macros.
    44 #define ERROR error_exit(__FILE__,__LINE__)
       
    45 /* function defined in main.cc */
       
    46 extern void error_exit(const char *file_name, int line_no);
       
    47 
       
    48 #define ABORT(str) {printf("ERROR: %s\n", str); ERROR;}
       
    49 
    44 
    50 
    45 
    51 
    46 
    52 /* The base class of all symbols */
    47 /* The base class of all symbols */
    53 symbol_c::symbol_c(
    48 symbol_c::symbol_c(
    59   this->first_order  = first_order;
    54   this->first_order  = first_order;
    60   this->last_file    = lfile,
    55   this->last_file    = lfile,
    61   this->last_line    = last_line;
    56   this->last_line    = last_line;
    62   this->last_column  = last_column;
    57   this->last_column  = last_column;
    63   this->last_order   = last_order;
    58   this->last_order   = last_order;
       
    59   this->datatype     = NULL;
       
    60   this->const_value._real64.status   = cs_undefined;
       
    61   this->const_value._int64.status    = cs_undefined;
       
    62   this->const_value._uint64.status   = cs_undefined;
       
    63   this->const_value._bool.status     = cs_undefined;
    64 }
    64 }
    65 
    65 
    66 
    66 
    67 
    67 
    68 token_c::token_c(const char *value, 
    68 token_c::token_c(const char *value, 
    99 void list_c::add_element(symbol_c *elem) {
    99 void list_c::add_element(symbol_c *elem) {
   100 //printf("list_c::add_element()\n");
   100 //printf("list_c::add_element()\n");
   101   n++;
   101   n++;
   102   elements = (symbol_c **)realloc(elements, n * sizeof(symbol_c *));
   102   elements = (symbol_c **)realloc(elements, n * sizeof(symbol_c *));
   103   if (elements == NULL)
   103   if (elements == NULL)
   104     ABORT("Out of memory");
   104     ERROR_MSG("Out of memory");
   105   elements[n - 1] = elem;
   105   elements[n - 1] = elem;
   106  
   106  
   107   if (elem == NULL)
   107   if (elem == NULL)
   108     return;
   108     return;
   109 
   109 
   128 
   128 
   129 /* insert a new element before position pos. */
   129 /* insert a new element before position pos. */
   130 /* To insert into the begining of list, call with pos=0  */
   130 /* To insert into the begining of list, call with pos=0  */
   131 /* To insert into the end of list, call with pos=list->n */
   131 /* To insert into the end of list, call with pos=list->n */
   132 void list_c::insert_element(symbol_c *elem, int pos) {
   132 void list_c::insert_element(symbol_c *elem, int pos) {
   133   int i;
       
   134   if (pos > n) ERROR;
   133   if (pos > n) ERROR;
   135   
   134   
   136   /* add new element to end of list. Basically alocate required memory... */
   135   /* add new element to end of list. Basically alocate required memory... */
   137   /* will also increment n by 1 ! */
   136   /* will also increment n by 1 ! */
   138   add_element(elem);
   137   add_element(elem);
   139   /* if not inserting into end position, shift all elements up one position, to open up a slot in pos for new element */
   138   /* if not inserting into end position, shift all elements up one position, to open up a slot in pos for new element */
   140   if (pos < (n-1)) for (i = n-2; i >= pos; i--) elements[i+1] = elements[i];
   139   if (pos < (n-1)) for (int i = n-2; i >= pos; i--) elements[i+1] = elements[i];
   141   elements[pos] = elem;
   140   elements[pos] = elem;
   142 }
   141 }
   143 
   142 
   144 
   143 
       
   144 /* remove element at position pos. */
       
   145 void list_c::remove_element(int pos) {
       
   146   if (pos > n) ERROR;
       
   147   
       
   148   /* Shift all elements down one position, starting at the entry to delete. */
       
   149   for (int i = pos; i < n-1; i++) elements[i] = elements[i+1];
       
   150   /* corrent the new size, and free unused memory */
       
   151   n--;
       
   152   elements = (symbol_c **)realloc(elements, n * sizeof(symbol_c *));
       
   153 }
   145 
   154 
   146 #define SYM_LIST(class_name_c, ...)								\
   155 #define SYM_LIST(class_name_c, ...)								\
   147 class_name_c::class_name_c(									\
   156 class_name_c::class_name_c(									\
   148                            int fl, int fc, const char *ffile, long int forder,			\
   157                            int fl, int fc, const char *ffile, long int forder,			\
   149                            int ll, int lc, const char *lfile, long int lorder)			\
   158                            int ll, int lc, const char *lfile, long int lorder)			\