home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / oxcc1433.zip / SRC / TOXCC.C < prev   
C/C++ Source or Header  |  1995-11-06  |  4KB  |  151 lines

  1. /* TOXCC.C -- test program for running oxcc while being compiled */
  2. /* 
  3.     try:    oxcc -r toxcc.c -TRACE=1 >trace
  4.           and
  5.             oxcc -Gb toxcc.c
  6.             bterp toxcc -TRACE=1 >trace
  7. */
  8.  
  9. #include <stdlib.h>
  10. #include <stdio.h>
  11. #include "oxcc.h"
  12.  
  13. typedef void *object;
  14.  
  15. extern object Compiler;
  16. extern object String;
  17. extern object File;
  18. extern object stdoutStream;
  19.  
  20. extern int gSeek();
  21. extern int gPosition();
  22. extern int gGets();
  23. extern void *gNew();
  24. extern void gDispose();
  25. extern int gPrintf();
  26. extern int gPuts();
  27.  
  28. extern void *oxlink_find_bare_symb();
  29. extern void *oxlink_load_bare_symb();
  30. extern void oxlink_use_library();
  31.  
  32. static char *testcode =
  33.  
  34.     "static inline int addfunc(int a, int b)"
  35.     "{"
  36.     "int q;"
  37.         "return a + b;"
  38.     "}"
  39.     "int func(void)"
  40.     "{"
  41.     "int x;"
  42.         "x = addfunc(1,2);"
  43.         "return x;"
  44.     "}";
  45.  
  46.  
  47. int toxcc(int argc, char **argv)
  48. {
  49. void *instance;
  50. void *is;        /* input stream */
  51. void *os;        /* output stream */
  52. void *es;        /* error stream */
  53. char buf[200];
  54. int err;
  55.  
  56.     printf("toxcc: Running\n");
  57.  
  58.     /* Dynamic link `oxcc' if it is not already there */
  59.     if(!oxlink_find_bare_symb("_oxcc"))
  60.     {
  61.       oxlink_use_library("oxcc.cff");
  62.       if(!oxlink_load_bare_symb("_oxcc", 1))
  63.         {printf("toxcc:ERROR: oxcc load failed\n"); return 1;}
  64.     }
  65.     /* METHOD 1 -- GET AN INSTANCE OF OXCC USING C CALLS */
  66.     printf("toxcc: Method 1, expect a warning for line:1 col:46\n");
  67.  
  68.     /* 
  69.         NOTE: The global calls to `oxcc_' below may have been unresolved until
  70.         the dynamic linker loaded it above.
  71.     */
  72.     instance = oxcc_open_instance();
  73.  
  74.     is = fopen(testcode, "x");    /* open string as a stream (readonly) */
  75.     os = fopen("", "w+UTM");        /* create a Unique Temp file in Memory */
  76.     es = fopen("", "w+UTM");        /* create a Unique Temp file in Memory */
  77.  
  78.     /* check the input file */
  79.     if(!(err = oxcc_preproc_file(instance, is, os, es, 0, 0)))
  80.     {
  81.       rewind(os);
  82.       if((err = oxcc_parse_file(instance, os, es, "testcode")))
  83.       {
  84.         oxcc_print_parse_errors(instance, es);
  85.       }
  86.       else
  87.       {
  88.         err = oxcc_check_ast_tree(instance, es, "testcode");
  89.       }
  90.     }
  91.     oxcc_cleanup_parse(instance);
  92.  
  93.     /* dump accumulated errors and warnings */
  94.     if(ftell(es) > 0)
  95.     {
  96.         rewind(es);
  97.         while(fgets(buf, 199, es))
  98.           fputs(buf, stdout);
  99.     }
  100.     fclose(is);
  101.     fclose(os);
  102.     fclose(es);
  103.     /* could loop here on a new file */
  104.  
  105.     oxcc_close_instance(instance);
  106.  
  107.     /*
  108.         NOTE: Using the class method, the `gNew' function would handle the
  109.         dynamic linking of `oxcc' if it hadn't been loaded above.
  110.     */
  111.     /* METHOD 2 -- GET AN INSTANCE OF OXCC USING THE CLASS METHOD */
  112.     gPrintf(stdoutStream, "\ntoxcc: Method 2, expect a warning for line:1 col:46\n");
  113.  
  114.     if(!(instance = gNew(Compiler, "cc")))
  115.       {gPrintf(stdoutStream, "toxcc:ERROR:oxcc class failed\n"); return 1;}
  116.  
  117.     is = gNew(String, testcode); /* or could use gNew(File, testcode, "x") */
  118.     os = gNew(File, "", "w+UTM");
  119.     es = gNew(File, "", "w+UTM");
  120.  
  121.     /* check the input file */
  122.     if(!(err = gPreProc(instance, is, os, es, 0, 0)))
  123.     {
  124.       gSeek(os, 0);
  125.       if((err = gParse(instance, os, es, "testcode")))
  126.       {
  127.         gPerror(instance, es);
  128.       }
  129.       else
  130.       {
  131.         err = gCheckTree(instance, es, "testcode");
  132.       }
  133.     }
  134.     gCleanup(instance);
  135.  
  136.     /* dump accumulated errors and warnings */
  137.     if(gPosition(es) > 0)
  138.     {
  139.         gSeek(es, 0);
  140.         while(gGets(es, buf, 199))
  141.           gPuts(stdoutStream, buf);
  142.     }
  143.     gDispose(is);
  144.     gDispose(os);
  145.     gDispose(es);
  146.     /* could loop here on a new file */
  147.  
  148.     gDispose(instance);
  149.     return 0;
  150. }
  151.