absyntax/absyntax.cc
changeset 1043 4165b7189c32
parent 1041 56ebe2a31b5b
child 1046 292b7dcf14f6
equal deleted inserted replaced
1042:6d1cdb7da363 1043:4165b7189c32
    56   this->last_file    = lfile,
    56   this->last_file    = lfile,
    57   this->last_line    = last_line;
    57   this->last_line    = last_line;
    58   this->last_column  = last_column;
    58   this->last_column  = last_column;
    59   this->last_order   = last_order;
    59   this->last_order   = last_order;
    60   this->parent       = NULL;
    60   this->parent       = NULL;
       
    61   this->token        = NULL;
    61   this->datatype     = NULL;
    62   this->datatype     = NULL;
    62   this->scope        = NULL;
    63   this->scope        = NULL;
    63 }
    64 }
    64 
    65 
    65 
    66 
    67 token_c::token_c(const char *value, 
    68 token_c::token_c(const char *value, 
    68                  int fl, int fc, const char *ffile, long int forder,
    69                  int fl, int fc, const char *ffile, long int forder,
    69                  int ll, int lc, const char *lfile, long int lorder)
    70                  int ll, int lc, const char *lfile, long int lorder)
    70   :symbol_c(fl, fc, ffile, forder, ll, lc, lfile, lorder) {
    71   :symbol_c(fl, fc, ffile, forder, ll, lc, lfile, lorder) {
    71   this->value = value;
    72   this->value = value;
       
    73   this->token = this; // every token is its own reference token.
    72 //  printf("New token: %s\n", value);
    74 //  printf("New token: %s\n", value);
    73 }
    75 }
    74 
    76 
    75 
    77 
    76 
    78 
    81 list_c::list_c(
    83 list_c::list_c(
    82                int fl, int fc, const char *ffile, long int forder,
    84                int fl, int fc, const char *ffile, long int forder,
    83                int ll, int lc, const char *lfile, long int lorder)
    85                int ll, int lc, const char *lfile, long int lorder)
    84   :symbol_c(fl, fc, ffile, forder, ll, lc, lfile, lorder),c(LIST_CAP_INIT) {
    86   :symbol_c(fl, fc, ffile, forder, ll, lc, lfile, lorder),c(LIST_CAP_INIT) {
    85   n = 0;
    87   n = 0;
    86   elements = (symbol_c**)malloc(LIST_CAP_INIT*sizeof(symbol_c*));
    88   elements = (element_entry_t*)malloc(LIST_CAP_INIT*sizeof(element_entry_t));
    87   if (NULL == elements) ERROR_MSG("out of memory");
    89   if (NULL == elements) ERROR_MSG("out of memory");
    88 }
    90 }
    89 
    91 
    90 
    92 
    91 list_c::list_c(symbol_c *elem, 
    93 list_c::list_c(symbol_c *elem, 
    92                int fl, int fc, const char *ffile, long int forder,
    94                int fl, int fc, const char *ffile, long int forder,
    93                int ll, int lc, const char *lfile, long int lorder)
    95                int ll, int lc, const char *lfile, long int lorder)
    94   :symbol_c(fl, fc, ffile, forder, ll, lc, lfile, lorder),c(LIST_CAP_INIT) { 
    96   :symbol_c(fl, fc, ffile, forder, ll, lc, lfile, lorder),c(LIST_CAP_INIT) { 
    95   n = 0;
    97   n = 0;
    96   elements = (symbol_c**)malloc(LIST_CAP_INIT*sizeof(symbol_c*));
    98   elements = (element_entry_t*)malloc(LIST_CAP_INIT*sizeof(element_entry_t));
    97   if (NULL == elements) ERROR_MSG("out of memory");
    99   if (NULL == elements) ERROR_MSG("out of memory");
    98   add_element(elem); 
   100   add_element(elem); 
    99 }
   101 }
   100 
   102 
   101 
   103 
       
   104 /*******************************************/    
   102 /* get element in position pos of the list */
   105 /* get element in position pos of the list */
   103 symbol_c *list_c::get_element(int pos) {return elements[pos];}
   106 /*******************************************/    
   104 
   107 symbol_c *list_c::get_element(int pos) {return elements[pos].symbol;}
       
   108 
       
   109 
       
   110 
       
   111 /******************************************/    
       
   112 /* find element associated to token value */
       
   113 /******************************************/    
       
   114 symbol_c *list_c::find_element(symbol_c *token) {
       
   115   token_c *t = dynamic_cast<token_c *>(token);
       
   116   if (t == NULL) ERROR;
       
   117   return find_element((const char *)t->value);  
       
   118 }
       
   119 
       
   120 symbol_c *list_c::find_element(const char *token_value) {
       
   121   // We could use strcasecmp(), but it's best to always use the same 
       
   122   // method of string comparison throughout matiec
       
   123   nocasecmp_c ncc; 
       
   124   for (int i = 0; i < n; i++) 
       
   125     if (ncc(elements[i].token_value, token_value))
       
   126       return elements[i].symbol;
       
   127   return NULL; // not found
       
   128 }
       
   129 
       
   130     
       
   131 /***********************************************/    
   105 /* append a new element to the end of the list */
   132 /* append a new element to the end of the list */
   106 void list_c::add_element(symbol_c *elem) {
   133 /***********************************************/    
   107   // printf("list_c::add_element()\n");
   134 void list_c::add_element(symbol_c *elem) {add_element(elem, elem);}
       
   135 
       
   136 void list_c::add_element(symbol_c *elem, symbol_c *token) {
       
   137   token_c *t =  (token == NULL)? NULL : token->token;
       
   138   add_element(elem, (t == NULL)? NULL : t->value);
       
   139 }
       
   140 
       
   141 void list_c::add_element(symbol_c *elem, const char *token_value) {
   108   if (c <= n)
   142   if (c <= n)
   109     if (!(elements=(symbol_c**)realloc(elements,(c+=LIST_CAP_INCR)*sizeof(symbol_c *))))
   143     if (!(elements=(element_entry_t*)realloc(elements,(c+=LIST_CAP_INCR)*sizeof(element_entry_t))))
   110       ERROR_MSG("out of memory");
   144       ERROR_MSG("out of memory");
   111   elements[n++] = elem;
   145   //elements[n++] = {token_value, elem};  // only available from C++11 onwards, best not use it for now.
   112  
   146   elements[n].symbol      = elem;
       
   147   elements[n].token_value = token_value;
       
   148   n++;
       
   149   
   113   if (NULL == elem) return;
   150   if (NULL == elem) return;
   114   /* Sometimes add_element() is called in stage3 or stage4 to temporarily add an AST symbol to the list.
   151   /* Sometimes add_element() is called in stage3 or stage4 to temporarily add an AST symbol to the list.
   115    * Since this symbol already belongs in some other place in the aST, it will have the 'parent' pointer set, 
   152    * Since this symbol already belongs in some other place in the aST, it will have the 'parent' pointer set, 
   116    * and so we must not overwrite it. We only set the 'parent' pointer on new symbols that have the 'parent'
   153    * and so we must not overwrite it. We only set the 'parent' pointer on new symbols that have the 'parent'
   117    * pointer still set to NULL.
   154    * pointer still set to NULL.
   144     last_line = elem->last_line;
   181     last_line = elem->last_line;
   145     last_column = elem->last_column;
   182     last_column = elem->last_column;
   146   }
   183   }
   147 }
   184 }
   148 
   185 
       
   186 
       
   187 /*********************************************/    
   149 /* insert a new element before position pos. */
   188 /* insert a new element before position pos. */
       
   189 /*********************************************/    
   150 /* To insert into the begining of list, call with pos=0  */
   190 /* To insert into the begining of list, call with pos=0  */
   151 /* To insert into the end of list, call with pos=list->n */
   191 /* To insert into the end of list, call with pos=list->n */
   152 void list_c::insert_element(symbol_c *elem, int pos) {
   192 
       
   193 void list_c::insert_element(symbol_c *elem, int pos) {insert_element(elem, elem, pos);}
       
   194 
       
   195 void list_c::insert_element(symbol_c *elem, symbol_c *token, int pos) {
       
   196   token_c *t    =  (token == NULL)? NULL : token->token;
       
   197   insert_element(elem, (t == NULL)? NULL : t->value, pos);
       
   198 }
       
   199 
       
   200 void list_c::insert_element(symbol_c *elem, const char *token_value, int pos) {
   153   if((pos<0) || (n<pos)) ERROR;
   201   if((pos<0) || (n<pos)) ERROR;
   154   
   202   
   155   /* add new element to end of list. Basically alocate required memory... */
   203   /* add new element to end of list. Basically alocate required memory... */
   156   /* will also increment n by 1 ! */
   204   /* will also increment n by 1 ! */
   157   add_element(elem);
   205   add_element(elem);
   158   /* if not inserting into end position, shift all elements up one position, to open up a slot in pos for new element */
   206   /* if not inserting into end position, shift all elements up one position, to open up a slot in pos for new element */
   159   if(pos < (n-1)){ 
   207   if(pos < (n-1)){ 
   160     for(int i=n-2 ; i>=pos ; --i) elements[i+1] = elements[i];
   208     for(int i=n-2 ; i>=pos ; --i) elements[i+1] = elements[i];
   161     elements[pos] = elem;
   209     elements[pos].symbol      = elem;
   162   }
   210     elements[pos].token_value = token_value;
   163 }
   211   }
   164 
   212 }
   165 
   213 
       
   214 
       
   215 /***********************************/    
   166 /* remove element at position pos. */
   216 /* remove element at position pos. */
       
   217 /***********************************/    
   167 void list_c::remove_element(int pos) {
   218 void list_c::remove_element(int pos) {
   168   if((pos<0) || (n<=pos)) ERROR;
   219   if((pos<0) || (n<=pos)) ERROR;
   169   
   220   
   170   /* Shift all elements down one position, starting at the entry to delete. */
   221   /* Shift all elements down one position, starting at the entry to delete. */
   171   for (int i = pos; i < n-1; i++) elements[i] = elements[i+1];
   222   for (int i = pos; i < n-1; i++) elements[i] = elements[i+1];
   172   /* corrent the new size */
   223   /* corrent the new size */
   173   n--;
   224   n--;
   174   /* elements = (symbol_c **)realloc(elements, n * sizeof(symbol_c *)); */
   225   /* elements = (symbol_c **)realloc(elements, n * sizeof(element_entry_t)); */
   175   /* TODO: adjust the location parameters, taking into account the removed element. */
   226   /* TODO: adjust the location parameters, taking into account the removed element. */
   176 }
   227 }
   177 
   228 
   178 
   229 
   179 /* remove element at position pos. */
   230 /**********************************/    
       
   231 /* Remove all elements from list. */
       
   232 /**********************************/    
   180 void list_c::clear(void) {
   233 void list_c::clear(void) {
   181   n = 0;
   234   n = 0;
   182   /* TODO: adjust the location parameters, taking into account the removed element. */
   235   /* TODO: adjust the location parameters, taking into account the removed element. */
   183 }
   236 }
   184 
   237