Advertisement
Guest User

Jesus_GOOD_Style_from_eee2_master

a guest
Dec 13th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.74 KB | None | 0 0
  1. #include "miscellaneous.h"
  2. #include "tokenizer.h"
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <stdio.h>
  6.  
  7. /* allocate Stoken and prepend
  8.    to ll tokenizer->token
  9.    NOTE tokenizer.h totoken */
  10. void to_new_token(Stokenizer *tokenizer){
  11.   Stoken *token = malloc(sizeof(Stoken));
  12.   token->nxt = tokenizer->token;
  13.   tokenizer->token = token;
  14. }
  15.  
  16. /* what does this routine do?
  17.    allocate enough memory
  18.    copy string at tokenizer->p
  19.    null terminate and return (tokenizer->p remain unmodified) */
  20. char *to_get_match(Stokenizer *tokenizer){
  21.   char *s = malloc(tokenizer->length + 1);
  22.   strncpy(s,tokenizer->p,tokenizer->length);
  23.   s[tokenizer->length] = '\0';
  24.   return s;
  25. }
  26.  
  27. void sort(Stok_dclr *tok_dclr,int len_tok_dclr)
  28. {
  29.   int j, k;
  30.   for(j = 0; j < len_tok_dclr; ++j)
  31.     for(k = 0; k < len_tok_dclr; ++k)
  32.     {
  33.       //#define rtd(N) tok_dclr[N].tok_dclr
  34. #define rtd(N) tok_dclr[N]
  35.       int J = rtd(j).length != 0 ? rtd(j).length : (rtd(j).length = strlen(rtd(j).regex));
  36.       int K = rtd(k).length != 0 ? rtd(k).length : (rtd(k).length = strlen(rtd(k).regex));
  37. #undef  rtd
  38.       if(J > K)
  39.       {
  40.         Stok_dclr tdj = tok_dclr[j];
  41.         tok_dclr[j] = tok_dclr[k];
  42.         tok_dclr[k] = tdj;
  43.       }
  44.     }
  45. }
  46.  
  47. /*<#!
  48. ' tokenize the input string
  49. ' p, q is strlen(p) + 1
  50. \\*/
  51. Stoken *tokenize(char *p,char *q,Stok_dclr *tok_dclr,int len_tok_dclr)
  52. {
  53.   /*
  54.     sort() is required for tok_dclr so
  55.    */
  56.   sort(tok_dclr,len_tok_dclr);
  57.  
  58.   Stokenizer tokenizer = { .p = p, .q = q };
  59.  
  60.   int j;
  61.   int maxLevel;
  62.   for(j = 0, maxLevel = 0; j < len_tok_dclr; ++j)
  63.     if(maxLevel < tok_dclr[j].level)
  64.       maxLevel = tok_dclr[j].level;
  65.   maxLevel += 1; // highest possible level + 1
  66.  
  67.   Stoken *token = NULL; //TODO X,Y
  68.  
  69.   /*
  70.     go through p to q and tokenize using tok_dclr as base
  71.    */
  72.   int nevermatch;
  73.   nevermatch = 1; //comparison will never be true on any identifier
  74.   while(tokenizer.p < tokenizer.q)
  75.   {
  76.     int level;
  77.     for(level = 0; level < maxLevel; ++level)
  78.     {
  79.       for(j = 0; j < len_tok_dclr; ++j)
  80.       {
  81.     //#define td(N) tok_dclr[N]
  82.         if(level == tok_dclr[j].level)
  83.         {
  84.           char *P; uintptr_t typecount;
  85.       //skepticism that typecount can > 0 if regex did not compare true 100% (miscellaneous.c)
  86.           typecount = regex_cmp(tokenizer.p,tok_dclr[j].regex,&P,NULL);
  87.           if(typecount)
  88.           {
  89.             tokenizer.length = P - tokenizer.p;
  90.             P = tokenizer.p;
  91.             tokenizer.tok_dclr = &(tok_dclr[j]);
  92.             tok_dclr[j].tokenizer(&tokenizer);
  93.         /* check if tokenizer.token was modified
  94.            and if then set its X and Y */
  95.         //if(tokenizer.token != token)
  96.         //  {
  97.         //  token->x = tox;
  98.         //  token->y = toy;
  99.         //  token = tokenizer.token;
  100.         //  }
  101.             /* check if tokenizer.p.p was not modified
  102.            else set it to P which is where upto regex compared true */
  103.             if(tokenizer.p == P)
  104.               tokenizer.p += tokenizer.length;
  105.  
  106.             nevermatch = 0;
  107.             level = maxLevel;
  108.             break; //break to level < maxLevel for loop
  109.                    //so it can exit and go to the outermost loop
  110.                    //so we then can repeat everything (unless p >= q)
  111.                    //which is tested by the outermost loop
  112.                    //goto'd may be clearer to use here
  113.  
  114.             //if we do not break here we won't restart
  115.             //the level stuff to 0 and we need to set level
  116.             //to maxLevel so not level < maxLevel
  117.             //level = maxLevel;
  118.             //break;
  119.           }
  120.           else
  121.             ; //do nothing?
  122.         }
  123.     //#undef td
  124.       }
  125.     }
  126.  
  127.     if(nevermatch)//none of the levels did compare true on p
  128.     {
  129.       fprintf(stderr,"<-error;tokenize; inrecognizable character/string '%c' (typo?)...\n",*tokenizer.p);
  130.      
  131.       //      printf(
  132.       //parse_error(&tokenizer);
  133.       return NULL;
  134.     }
  135.     nevermatch = 1;
  136.   }
  137.   if(!(tokenizer.token))
  138.   {
  139.     fprintf(stderr,"<-error;tokenize; ll of token is void (inexecutable)...\n");
  140.     //parse_error(&tokenizer);
  141.     return NULL;
  142.   }
  143.   /*--------------------------------------------------------*\
  144.   '[OK] reverse singly linked list to get chronological order
  145.   \*--------------------------------------------------------*/
  146.   tokenizer.token = rsll(tokenizer.token,&((tokenizer.token)->nxt));
  147.   /*-------------------- *\
  148.   ' [OK] convert to doubly
  149.   ' d -> c -> b -> a
  150.   ' a -> b -> c -> d
  151.   ' a <- b <- c <- d
  152.   ' a <-> b <-> c <-> d
  153.   \*-------------------- */
  154.   //Stoken *token = tokenizer.token;
  155.   token = tokenizer.token;
  156.   token->prv = NULL;
  157.   while(token->nxt)
  158.   {
  159.     token->nxt->prv = token;
  160.     token = token->nxt;
  161.   }
  162.   return tokenizer.token;
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement