home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / progm / dpl.zip / DPL.C < prev    next >
Text File  |  1988-07-12  |  11KB  |  425 lines

  1.  
  2. /*    Dougs macro assembler pre processor   */
  3.  
  4. #define  VERSION    "2.22"
  5.  
  6. #include "stdio.h"
  7. #include "string.h"
  8. #include "ctype.h"
  9.  
  10.    FILE *in, *out;
  11.    char work[132], *wk, *wkend;
  12.    char work1[132], *wk10, *wk11, *wk12, *wk13;
  13.    char work2[10];
  14.    char text[132];
  15.    int labcnt, linecnt, errcnt;
  16.  
  17. main(argc,argv)
  18. int argc;
  19. char *argv[];
  20.  
  21. {
  22.  
  23. /*******************************/
  24. /*  Give the logo to the user  */
  25. /*******************************/
  26.  
  27.    printf("\nDoug's Programming Language -- DPL, Version %s\n",VERSION);
  28.    printf("Copyright (c) 1988 Douglas S. Cody, All rights reserved.\n");
  29.  
  30. /********************************/
  31. /* fetch the file name from     */
  32. /* the command line/operator    */
  33. /********************************/
  34.  
  35.    if(argc!=2) {
  36.      /* prompt the operator for a file name */
  37.      printf ("\nENTER THE FILE NAME [.D]  ");
  38.      gets (work);
  39.     }
  40.    else {
  41.       strcpy (work,argv[1]);
  42.     }
  43.    if(strstr(work,".") == NULL) {
  44.       strcpy (work1,work);
  45.       strcat (work1,".asm");
  46.       strcat (work,".d");
  47.    }
  48.    else {
  49.       strcpy(work1,work);
  50.       strcpy(work1,(strtok(work1,".")));
  51.       strcat(work1,".asm");
  52.     }
  53.  
  54. /********************************/
  55. /*      open the text file      */
  56. /********************************/
  57.  
  58.    if ((in = fopen (work,"rt"))==NULL) {
  59.       printf("\nCAN'T OPEN INPUT FILE\n");
  60.       exit (1);
  61.     }
  62.     if ((out = fopen (work1,"w"))==NULL) {
  63.       printf("\nCAN'T OPEN OUTPUT FILE\n");
  64.       exit (1);
  65.      }
  66.  
  67.    printf("\nCOMPILING %s\n",work);
  68.    fputs ("\tINCLUDE\tDPL.MAC\n",out);
  69.  
  70. /************************************/
  71. /* process the contents of the file */
  72. /************************************/
  73.  
  74.    labcnt  = 0;    /* clear the label count          */
  75.    linecnt = 0;    /* clear the input line count     */
  76.    errcnt  = 0;    /* clear the DOS ERRORLEVEL count */
  77.  
  78.    get_string();
  79.  
  80.    while (!feof(in)) {
  81.       compile();
  82.       get_string();
  83.     }
  84.     fclose(in);
  85.     exit (errcnt);
  86.  
  87. }
  88.  
  89. /****************************************************************************/
  90. /*                         1st level routines                               */
  91. /****************************************************************************/
  92.  
  93.  
  94. /*******************************/
  95. /*    compile the code         */
  96. /*******************************/
  97.  
  98. compile() {
  99.  
  100.    strcpy (work1,work);
  101.    strtok (work1,"\n");
  102.    wk10 = strtok(work1,"\t ");             /* point to the first token */
  103.  
  104.    if (strcmp(wk10,"CALL") == 0) {
  105.       perform_call();
  106.       return(255);
  107.     }
  108.  
  109.    if (strcmp(wk10,"IF") == 0) {
  110.       perform_if();
  111.       return(255);
  112.     }
  113.  
  114.    if (strcmp(wk10,"RECORD") == 0) {
  115.       perform_rec();
  116.       return(255);
  117.     }
  118.  
  119.    wk11 = strtok('\0',"\t ");
  120.  
  121.    if (strcmp(wk11,"=") == 0) {
  122.       perform_arith();
  123.       return(255);
  124.     }
  125.  
  126.    if (strcmp(wk11,"$=") == 0) {
  127.       perform_scat();
  128.       return(255);
  129.     }
  130.  
  131.    fputs (work,out);
  132.    return(0);
  133. }
  134.  
  135. /****************************/
  136. /*   fetch a string from    */
  137. /*   the input file         */
  138. /****************************/
  139.  
  140. get_string() {
  141.       fgets (text, 132, in);
  142.       linecnt++;
  143.       strcpy (work,text);
  144. }
  145.  
  146. /****************************************************************************/
  147. /*                         2nd level routines                               */
  148. /****************************************************************************/
  149.  
  150. /********************************/
  151. /*    parse the MATH statment   */
  152. /********************************/
  153.  
  154. perform_arith() {
  155.  
  156.     fputs ("\t@LOAD\t",out);
  157.     fputs (strtok('\0',"\t "),out);
  158.     fputs ("\n",out);
  159.  
  160.     wk11 = strtok('\0',"\t ");
  161.  
  162.     while (wk11 && (wk11 != (strchr(wk11,59)))) {
  163.        if (strcmp(wk11,"+") == 0) strcpy (work2,"\t@ADD\t");
  164.        else if (strcmp(wk11,"-") == 0) strcpy (work2, "\t@SUB\t");
  165.        else if (strcmp(wk11,"*") == 0) strcpy (work2, "\t@MULT\t");
  166.        else if (strcmp(wk11,"/") == 0) strcpy (work2, "\t@DIV\t");
  167.        else if (strcmp(wk11,">>") == 0) strcpy (work2,"\t@SHR\t");
  168.        else if (strcmp(wk11,"<<") == 0) strcpy (work2,"\t@SHL\t");
  169.        else if (strcmp(wk11,"AND") == 0) strcpy (work2,"\t@AND\t");
  170.        else if (strcmp(wk11,"OR") == 0) strcpy (work2,"\t@OR\t");
  171.        else if (strcmp(wk11,"XOR") == 0) strcpy (work2,"\t@XOR\t");
  172.        else if (strcmp(wk11,"MOD") == 0) strcpy (work2,"\t@MOD\t");
  173.        else give_err ("ERROR -- incorrect math operator");
  174.  
  175.        fputs(work2,out);
  176.        fputs(strtok('\0',"\t "),out);
  177.        fputs ("\n",out);
  178.  
  179.        wk11 = strtok('\0',"\t ");
  180.      }
  181.  
  182.     fputs("\t@STOR\t",out);
  183.     fputs(wk10,out);
  184.     fputs ("\n",out);
  185.  
  186. }
  187.  
  188. /*******************************/
  189. /*   parse the CALL statement  */
  190. /*******************************/
  191.  
  192. perform_call() {
  193.    wk11 =  strtok('\0',"\t ");
  194.  
  195.    if (strcmp(wk11,"DWORD") && strcmp(wk11,"WORD")
  196.       && strcmp(wk11,"FAR") && strcmp(wk11,"NEAR"))
  197.    {
  198.       wk12 = strtok('\0',"\t ");
  199.       if (wk12 && *wk12!=';') {
  200.          fputs ("\tMOV\tAX,",out);
  201.          if (!isdigit(*wk12)) fputs ("OFFSET ",out);
  202.          fputs (wk12,out);
  203.          fputs ("\n",out);
  204.  
  205.          wk12 = strtok('\0',"\t ");
  206.          if (wk12 && *wk12!=';') {
  207.             fputs ("\tMOV\tBX,",out);
  208.             if (!isdigit(*wk12)) fputs ("OFFSET ",out);
  209.             fputs (wk12,out);
  210.             fputs ("\n",out);
  211.  
  212.             wk12 = strtok('\0',"\t ");
  213.             if (wk12 && *wk12!=';') {
  214.                fputs ("\tMOV\tCX,",out);
  215.                if (!isdigit(*wk12)) fputs ("OFFSET ",out);
  216.                fputs (wk12,out);
  217.                fputs ("\n",out);
  218.  
  219.                wk12 = strtok('\0',"\t ");
  220.                if (wk12 && *wk12!=';') {
  221.                   fputs ("\tMOV\tDX,",out);
  222.                   if (!isdigit(*wk12)) fputs ("OFFSET ",out);
  223.                   fputs (wk12,out);
  224.                   fputs ("\n",out);
  225.                }
  226.             }
  227.          }
  228.       }
  229.       fputs ("\tCALL\t",out);
  230.       fputs (wk11,out);
  231.       fputs ("\n",out);
  232.    }
  233.  
  234.    else {
  235.       fputs ("\tCALL\t",out);
  236.       do {
  237.      fputs (wk11,out);
  238.          fputs (" ",out);
  239.      wk11 = strtok('\0',"\t ");
  240.       } while (wk11);
  241.       fputs ("\n",out);
  242.    }
  243.  
  244. }
  245.  
  246. /*******************************/
  247. /*    parse the IF statement   */
  248. /*******************************/
  249.  
  250. perform_if() {
  251.    wk11 = strtok('\0',"\t ");
  252.    if (strcmp(wk11,"BYTE") == 0) do_ifn ("AL","BYTE");
  253.    else if (strcmp(wk11,"CHAR") == 0) do_ifn ("AL","BYTE");
  254.    else if (strcmp(wk11,"INT")  == 0) do_ifn ("AX","WORD");
  255.    else if (strcmp(wk11,"WORD") == 0) do_ifn ("AX","WORD");
  256.    else if (strcmp(wk11,"STRING") == 0) do_ifs();
  257.    else give_err ("ERROR -- No BYTE/WORD/STRING operator given");
  258.  
  259. }
  260.  
  261. /***********************************/
  262. /*    parse the RECORD statement   */
  263. /***********************************/
  264.  
  265. perform_rec() {
  266.    fputs ("REC\t",out);
  267.    fputs ((strtok('\0',",")),out);
  268.    fputs (",",out);
  269.    fputs ((strtok('\0'," ")),out);
  270.    fputs ("\n",out);
  271. }
  272.  
  273. /***************************************/
  274. /*    parse the string concatenation   */
  275. /***************************************/
  276.  
  277. perform_scat() {
  278.    wk11 = strtok('\0',"\t ");
  279.    if (strcmp(wk10,wk11) == 0) {
  280.       fputs ("\t@SCATSME\t",out);
  281.       fputs (wk11,out);
  282.       fputs ("\n",out);
  283.     }
  284.    else {
  285.       fputs ("\t@SMOV\t",out);
  286.       fputs (wk10,out);
  287.       fputs ("\n",out);
  288.       fputs ("\t@SCNCT\t",out);
  289.       fputs (wk11,out);
  290.       fputs ("\n",out);
  291.     }
  292.  
  293.    do {
  294.      wk11 = strtok('\0',"\t ");
  295.       if (wk11 && (strcmp(wk11,"+") == 0)) {
  296.          wk11 = strtok('\0',"\t ");
  297.          fputs ("\t@SCNCT\t",out);
  298.          fputs (wk11,out);
  299.          fputs ("\n",out);
  300.        }
  301.     } while (wk11 && (wk11 != (strchr(wk11,59))));
  302.  
  303. }
  304.  
  305. /****************************************************************************/
  306. /*                         3rd level routines                               */
  307. /****************************************************************************/
  308.  
  309. /************************************/
  310. /*     parse the IF xxx statement   */
  311. /************************************/
  312.  
  313. do_ifs() {
  314.    fputs ("\t@IFSTR\n",out);
  315.  
  316.    fputs ("\tMOV\tSI,OFFSET ",out);
  317.    fputs (strtok('\0',"\t "),out);
  318.    fputs ("\n",out);
  319.  
  320.    wk12 = strtok('\0',"\t ");
  321.  
  322.    fputs ("\tMOV\tDI,OFFSET ",out);
  323.    fputs (strtok('\0',"\t "),out);
  324.    fputs ("\n",out);
  325.  
  326.    if (strcmp(wk12,"EQ") == 0) fputs ("\tCALL\t_IFSEQ\n",out);
  327.       else if (strcmp(wk12,"NE") == 0) fputs ("\tCALL\t_IFSNE\n",out);
  328.          else fputs ("\tCALL\n",out);
  329.  
  330.    itoa(labcnt++,work2,10);
  331.    fputs ("\tJNC\t@@@XXX",out);
  332.    fputs (work2,out);
  333.    fputs ("\n",out);
  334.  
  335.    do_true();
  336.  
  337.    fputs ("@@@XXX",out);
  338.    fputs (work2,out);
  339.    fputs (":\n",out);
  340.  
  341. }
  342.  
  343. /************************************/
  344. /*   parse the IF xxxxx statement   */
  345. /************************************/
  346.  
  347. do_ifn(reg,siz)
  348.   char *reg, *siz;
  349.  
  350. {
  351.    fputs ("\tMOV\t",out);
  352.    fputs (reg,out);
  353.    fputs (",",out);
  354.    fputs (siz,out);
  355.    fputs (" PTR ",out);
  356.    fputs (strtok('\0',"\t "),out);
  357.    fputs ("\n",out);
  358.  
  359.    wk12 = strtok('\0',"\t ");
  360.  
  361.    fputs ("\tCMP\t",out);
  362.    fputs (reg,out);
  363.    fputs (",",out);
  364.    fputs (siz,out);
  365.    fputs (" PTR ",out);
  366.    fputs (strtok('\0',"\t "),out);
  367.    fputs ("\n",out);
  368.  
  369.    if (strcmp(wk12,"LT") == 0) strcpy(work2,"\tJGE\t");
  370.    else if (strcmp(wk12,"LE") == 0) strcpy (work2,"\tJG\t");
  371.    else if (strcmp(wk12,"EQ") == 0) strcpy (work2,"\tJNE\t");
  372.    else if (strcmp(wk12,"GE") == 0) strcpy (work2,"\tJL\t");
  373.    else if (strcmp(wk12,"GT") == 0) strcpy (work2,"\tJLE\t");
  374.    else if (strcmp(wk12,"NE") == 0) strcpy (work2,"\tJE\t");
  375.    else give_err ("ERROR -- incorrect comparison operator");
  376.  
  377.    fputs (work2,out);
  378.  
  379.    itoa(labcnt++,work2,10);
  380.    fputs ("@@@XXX",out);
  381.    fputs (work2,out);
  382.    fputs ("\n",out);
  383.  
  384.    do_true();
  385.  
  386.    fputs ("@@@XXX",out);
  387.    fputs (work2,out);
  388.    fputs (":\n",out);
  389.  
  390. }
  391.  
  392. /****************************************************************************/
  393. /*                         4th level routines                               */
  394. /****************************************************************************/
  395.  
  396. /*********************************/
  397. /* do_true PARSE of if statement */
  398. /*********************************/
  399.  
  400. do_true() {
  401.     wk13 = strtok('\0',"\t ");
  402.     if (strcmp(wk13,"GOTO") == 0) {
  403.        fputs ("\tGOTO\t",out);
  404.        fputs (strtok('\0',"\t "),out);
  405.        fputs ("\n",out);
  406.      }
  407.     else if (strcmp(wk13,"RETURN") == 0) fputs("\tRETURN\n",out);
  408.     else if (strcmp(wk13,"CALL") == 0) perform_call();
  409.     else give_err ("ERROR -- Missing GOTO/CALL/RETURN in 'IF' statement");
  410.  
  411. }
  412.  
  413. /****************************************************************************/
  414. /*                       Bottom level routines                              */
  415. /****************************************************************************/
  416.  
  417. give_err(str)
  418. char *str;
  419. {
  420.     printf ("%s\n",str);
  421.     printf ("\tline #%d: %s\n",linecnt,text);
  422.     errcnt++;
  423.  
  424. }
  425.