home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / magazine / drdobbs / 1991 / 04 / morrow / exec.c < prev    next >
C/C++ Source or Header  |  1989-09-03  |  4KB  |  249 lines

  1.  
  2. #include <stdio.h>
  3. #include <ctype.h>
  4. #include "ga.h"
  5. #include "table.h"
  6. #include "param.h"
  7. #include "util.h"
  8.  
  9. /***
  10. *       GASystem
  11. *       Mike Morrow
  12. *       September 1989
  13. ***/
  14.  
  15. static void exevset();
  16. static void exevshow();
  17.  
  18.  
  19. static int param_exe_gen = 0;        /* generation count */
  20.  
  21. static CONST PARAMSPEC exeparams[] =
  22. {
  23.     {"GEN", (void *) ¶m_exe_gen,    MODEINT,    FLAG_RO},    
  24.  
  25.  
  26.     {(char *) 0,     (void *) 0, (MODE) 0,    0}
  27. };
  28.  
  29.  
  30. static void exerun(), exequit(), exedump();
  31.  
  32. static CONST PARAMSPEC execmds[] =
  33. {
  34.     {"GO",     (void *) exerun,     0,        0},
  35.     {"QUIT", (void *) exequit,    0,        0},
  36.     {"DUMP", (void *) exedump,    0,        0},
  37.  
  38.  
  39.  
  40.     {(char *) 0,     (void *) 0, (MODE) 0,    0}
  41. };
  42.  
  43.  
  44. /**
  45. *    This routine inserts information about the parameter vars
  46. *    and commands into the symbol table.
  47. **/
  48. exepinit()
  49. {
  50.     CONST PARAMSPEC *p;
  51.     
  52.     for(p = execmds; p->param_name; p++)
  53.         paramins(p, SYMCMD);
  54.         
  55.     for(p = exeparams; p->param_name; p++)
  56.         paramins(p, SYMVAR);
  57. }
  58.  
  59.  
  60.  
  61. /**
  62. *       This is the command interpreter.  s is a string containing the
  63. *       user input.
  64. **/
  65.  
  66. void exec(s)
  67. char *s;
  68. {
  69.     TBL_PTR t;
  70.     char *cmd, remainder[80], *tail;
  71.     
  72.     s = eatblanks(s);
  73.     strupper(s);
  74.     
  75.                     /* get first part of line -- command or var name */
  76.     cmd = s;
  77.     if(! *cmd)
  78.         return ;    /* null command */
  79.     
  80.                     /* remainder of line are arguments to command */
  81.     while(isalnum(*s++)) /* blank */ ;
  82.  
  83.     s--;    
  84.     strcpy(remainder, s);
  85.     *s = '\0';
  86.     tail = eatblanks(remainder);
  87.  
  88.  
  89.     /**
  90.     *    Look up this word in the symbol table.  We'll learn if it's a
  91.     *    variable name, a command, or undefined.
  92.     **/
  93.     t = tbl_find(cmd);
  94.     if(! t)
  95.     {
  96.         warning("No such command/variable");
  97.         return ;
  98.     }
  99.     
  100.     if(t->symtype == SYMCMD)
  101.     {
  102.         void (*fcn)();
  103.  
  104.         fcn = t->symdata;
  105.         fcn(tail);
  106.     }
  107.     else if(t->symtype == SYMVAR && *tail == '=')
  108.     {
  109.         tail++;
  110.         tail = eatblanks(tail);
  111.         exevset(t, tail);
  112.     }
  113.     else if(t->symtype == SYMVAR)
  114.     {
  115.         exevshow(t);
  116.     }
  117. }
  118.         
  119.  
  120. /**
  121. *       Set the variable referenced by t to the value implied
  122. *       in the "tail" parameter.
  123. **/
  124.  
  125. static void exevset(t, tail)
  126. TBL_PTR t;
  127. char *tail;
  128. {
  129.     int n;
  130.     FIT_TYPE fit;
  131.  
  132.     if(t->symflags & FLAG_RO)
  133.     {
  134.         warning("Can't set that variable");
  135.         return ;
  136.     }
  137.  
  138.     if(t->symflags & FLAG_INIT)
  139.         poplfree();
  140.         
  141.     switch(t->symmode)
  142.     {
  143.         case MODEINT:
  144.             if(sscanf(tail, "%d", &n) != 1)
  145.             {
  146.                 warning("error in scanning RHS of '='");
  147.                 return ;
  148.             }
  149.             * ((int *) t->symdata) = n;
  150.             break;
  151.             
  152.         case MODEFIT:        /* OOPS, we assume MODEFIT is long int -- bad stuff */
  153.             if(sscanf(tail, "%ld", &fit) != 1)
  154.             {
  155.                 warning("error in scanning RHS of '='");
  156.                 return ;
  157.             }
  158.             * ((FIT_TYPE *) t->symdata) = fit;
  159.             break;
  160.             
  161.         default:
  162.             fatal("strange mode in exevset");
  163.     }
  164.                 
  165.     if(t->symflags & FLAG_INIT)
  166.     {
  167.         param_exe_gen = 0;
  168.         poplinit();
  169.         popleval();
  170.     }
  171. }
  172.  
  173.  
  174. /**
  175. *       Show the value of the variable implied by t.
  176. **/
  177.  
  178. static void exevshow(t)
  179. TBL_PTR t;
  180. {
  181.     switch(t->symmode)
  182.     {
  183.         case MODEINT:
  184.             printf("%d\n", * ((int *) t->symdata));
  185.             break;
  186.             
  187.         case MODEFIT:
  188.             printf("%ld\n", * ((FIT_TYPE *) t->symdata));
  189.             break;
  190.             
  191.         default:
  192.             fatal("strange mode in exerun");
  193.             
  194.     }
  195. }
  196.  
  197.  
  198. /**
  199. *       Do s generations
  200. **/
  201. static void exerun(s)
  202. char *s;
  203. {
  204.     unsigned int i, n;
  205.     
  206.  
  207.     if(*s)
  208.         n = atoi(s);
  209.     else
  210.         n = 1;
  211.         
  212.  
  213.     for(i = 0; i < n; i++, param_exe_gen++)
  214.     {
  215.         printf("Generation %d\n", param_exe_gen);
  216.         fflush(stdout);
  217.         poplrepro();
  218.         poplcross();
  219.         poplmutate();
  220.         popleval();
  221.     }
  222. }
  223.  
  224. /**
  225. *       Dump s members of the population.
  226. **/
  227. static void exedump(s)
  228. char *s;
  229. {
  230.     int n;
  231.     
  232.     if(s)
  233.         n = atoi(s);
  234.     else
  235.         n = 0;
  236.     
  237.     popldump(n);
  238. }
  239.  
  240.  
  241. /**
  242. *       Normal exit.
  243. **/
  244. static void exequit(s)
  245. char *s;
  246. {
  247.     exit(0);
  248. }
  249.