home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / PASCAL / C_TO_PAS.ZIP / C_TO_PAS.C
Encoding:
Text File  |  1985-02-09  |  2.9 KB  |  144 lines

  1.  
  2.  
  3. /* C to pascal - filter to replace C punctuation and certain key words 
  4.     with their Pascal equivalents.
  5.  
  6. C form        Pascal form
  7. ------        -----------
  8.   "        '
  9.   {        BEGIN
  10.   }        END;
  11. <tab>        <2 blank spaces>
  12.   ()        <nothing>
  13.   &&        AND
  14.   ||        OR
  15. comment start    {
  16. comment end    }
  17.   ==        =
  18.   !=        <>
  19.   =        :=
  20.   printf    writeln
  21.   scanf        readln
  22.   while        WHILE
  23.  
  24. Usage: ctp <infile >outfile 
  25.  
  26. Copyright 1984 Ted Carnevale.
  27. Permission granted for personal nonprofit use.
  28. All other rights reserved.
  29. */
  30.  
  31.  
  32. #define EOF -1
  33. #define EOS '\0'
  34.  
  35. main() 
  36. {
  37.     char c,*letter,word[100];
  38.     int wordlnth;
  39.  
  40.     letter=word;
  41.     wordlnth=0;
  42.     while ((c=getchar()) != EOF) {
  43.         if (isalpha(c)) letter[wordlnth++]=c;
  44.         else {
  45.             if (wordlnth>0) {    /* word ready to check */
  46.                 letter[wordlnth]='\0';
  47.                 wtest(word);    /* pass or replace it */
  48.                 wordlnth=0;    /* reset index */
  49.             }
  50.             ctest(c);    /* process following char */
  51.         }
  52.     }
  53. }    /* note:  the last word in the file will be missed if it is
  54. immediately followed by EOF with no intervening nonalpha character.
  55. This is not a problem for Pascal or C program sources.  However,
  56. a general purpose word filter would have to check for a nonzero 
  57. wordlength after EOF is reached. */
  58.  
  59. wtest(word)
  60. char *word;
  61. {
  62.     char *swapword;
  63.  
  64.     swapword=word;
  65.     switch (word[0]) {    /* test first letter, then rest of word */
  66.     case 'p':  if (strcmp(word,"printf\0")==0) swapword="writeln\0";
  67.            break;
  68.     case 's':  if (strcmp(word,"scanf\0")==0) swapword="readln\0";
  69.            break;
  70.     case 'w':  if (strcmp(word,"while\0")==0) swapword="WHILE\0";
  71.            break;
  72.     default:  break;    /* pass unchanged */
  73.     }
  74.     swap(swapword);
  75. }
  76.  
  77. ctest(c)
  78. char c;
  79. {
  80.     switch (c) {
  81.     case '"':  putchar('\'');
  82.            break;
  83.     case '{':  swap("BEGIN\0");
  84.            break;
  85.     case '}':  swap("END;\0");
  86.            break;
  87.     case '\t':  swap("  \0");
  88.            break;
  89.     case '&':  swapif('&','&'," AND \0");
  90.            break;
  91.     case '|':  swapif('|','|'," OR \0");
  92.            break;
  93.     case '(':  swapif('(',')',"\0");    /* () simply deleted */
  94.            break;
  95.     case '/':  swapif('/','*',"{\0");
  96.            break;
  97.     case '*':  swapif('*','/',"}\0");
  98.            break;
  99.     case '!':  swapif('!','=',"<>\0");    /* != -> <> */
  100.            break;
  101.     case '<':
  102.     case '>':  putchar(c);    /* <x and >x are passed unchanged */
  103.            c=getchar();
  104.            putchar(c);
  105.            break;
  106.     case '=':  identassign();    /* == -> = , = -> := */
  107.            break;
  108.     default:  putchar(c);
  109.            break;
  110.     }
  111. }
  112.  
  113. swap(s)
  114. char *s;
  115. {
  116.     while (*s!=EOS) putchar(*s++);
  117. }
  118.  
  119. swapif(first,second,replacement)
  120. char first,second,*replacement;
  121. {
  122.     char c;
  123.  
  124.     if ((c=getchar())==second) swap(replacement);
  125.     else {
  126.         putchar(first);
  127.         putchar(c);
  128.     }
  129. }
  130.  
  131. identassign()
  132. {
  133.     char c;
  134.  
  135.     if ((c=getchar())!= '=') {    /* assignment */
  136.         putchar(':');
  137.         putchar('=');
  138.     }
  139.     putchar(c);
  140. }
  141.  
  142. /* end of ctp.c */
  143.  
  144.