home *** CD-ROM | disk | FTP | other *** search
/ Turbo Toolbox / Turbo_Toolbox.iso / spezial / 20 / dos_txl / source / ppc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-19  |  2.9 KB  |  178 lines

  1. /******************************************/
  2. /********** Präprozessor für TXL **********/
  3. /******** (c) 1991 by Elmar Warken ********/
  4. /******************************************/
  5.  
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #include <stdio.h>
  9. #include "interp.h"
  10.  
  11. extern  char *LinePos;
  12. extern  int  Steuer;
  13. extern  char ScanBuffer[ZEILENLAENGE];
  14.  
  15. typedef struct constvar
  16. {
  17.   char Bez[20];
  18.   long val;
  19.   struct constvar *next;
  20. } ConstVar;
  21.  
  22. ConstVar *FirstConst;
  23.  
  24. void DelList(void)
  25. {
  26.   ConstVar *w,*v=FirstConst;
  27.   while (v)
  28.   {
  29.     w=v->next;
  30.     free(v);
  31.     v=w;
  32.   }
  33.   FirstConst=NULL;
  34. }
  35.  
  36. int FindConst(char *name, long *val)
  37. {
  38.   ConstVar *v=FirstConst;
  39.   while (v)
  40.   {
  41.     if (strcmp(v->Bez,name)==0)
  42.     { *val=v->val; return(OK);  }
  43.     v=v->next;
  44.   }
  45.   return(FALSE);
  46. }
  47.  
  48. void SetConst(char *name, long val)
  49. {
  50.   ConstVar *v=malloc(sizeof(ConstVar));
  51.  
  52.   strcpy(v->Bez,name);
  53.   v->val=val;
  54.   v->next=FirstConst;
  55.   FirstConst=v;
  56. }
  57.  
  58. void ScanLine(char *line, int linenumb)
  59. {
  60.   char *p;
  61.   long val;
  62.   char cname[20];
  63.  
  64. /** Leerzeichen am Ende und \n abschneiden **/
  65.  
  66.   for (p=line; *p; p++);
  67.   p--;
  68.   if (*p=='\n') *p=0;
  69.   for (; *p==' '; p--);
  70.   p++; *p=0;
  71.  
  72.   LinePos=line;
  73.   SkipChar();
  74.   Scan();
  75.   if (Steuer==ALPHA) 
  76.   {
  77.     if (strcmp(ScanBuffer,"label")==0)
  78.     {
  79.       Scan();
  80.       if (Steuer==ALPHA)
  81.     SetConst(ScanBuffer,linenumb+1);
  82.     } else
  83.     if (strcmp(ScanBuffer,"const")==0)
  84.     {
  85.       Scan();
  86.       if (Steuer==ALPHA)
  87.       {
  88.     strcpy(cname,ScanBuffer);
  89.     Scan();
  90.     if (Steuer==ZIFFER)
  91.       SetConst(cname,atol(ScanBuffer));
  92.       }
  93.     } 
  94.   }
  95. }
  96.  
  97. void Transform(char *line, int linenumb)
  98. {
  99.   char *p;
  100.   char newline[100];
  101.   char teil[100];
  102.   long val;
  103.  
  104. /** Leerzeichen am Ende und \n abschneiden **/
  105.  
  106.   for (p=line; *p; p++);
  107.   p--;
  108.   if (*p=='\n') *p=0;
  109.   for (; *p==' '; p--);
  110.   p++; *p=0;
  111.  
  112.   LinePos=line;
  113.   SkipChar();
  114.  
  115.   ltoa(linenumb,newline,10);
  116.   strcat(newline," ");
  117.   if (!*line)
  118.     strcat(newline,"merke");
  119.   else {
  120.     while (Scan()==SCANOK)
  121.     {
  122.       strcpy(teil,ScanBuffer);
  123.       strcat(teil," ");
  124.       if (Steuer==ALPHA)
  125.       {
  126.     if (FindConst(ScanBuffer,&val))
  127.     {
  128.       ltoa(val,teil,10);
  129.       strcat(teil," ");
  130.     }
  131.       } 
  132.       else if (Steuer==STRING)
  133.       {               
  134.     strcpy(teil,"\"");
  135.     strcat(teil,ScanBuffer);
  136.     strcat(teil,"\" ");
  137.       }
  138.       strcat(newline,teil);
  139.     }
  140.   }
  141.   strcpy(line,newline);
  142. }
  143.  
  144. void TransformFile(char *source, char *dest)
  145. {
  146.   char line[100];
  147.   FILE *f=fopen(source,"rt");
  148.   FILE *g=fopen(dest,"wt");
  149.  
  150.   int i=0;
  151.   while(!feof(f))
  152.   {
  153.     i++;
  154.     fgets(line,100,f);
  155.     ScanLine(line,i);
  156.   }
  157.   rewind(f);
  158.   i=0;
  159.   while(!feof(f))
  160.   {
  161.     i++;
  162.     if (fgets(line,100,f))
  163.     { 
  164.       Transform(line,i);
  165.       fputs(line,g);
  166.       fputs("\n",g);
  167.     }
  168.   }
  169.   fclose(f);
  170.   fclose(g);
  171.   DelList();
  172.   return;
  173. }
  174.  
  175. int cdummy(void)
  176. {
  177.   return(OK);
  178. }