// main5.cpp

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "eval5.h"

bool GetLine (FILE *input, char *line, int MaxLineLen) {
   bool success;  // 'success' is always assigned a value, so it doesn't need initializing.

   printf (":");  // Prompt for input.
   if (!fgets (line, MaxLineLen, input)) {
      if (!feof (input)) {
         perror ("GetLine");
      }
      success = false;
   } else {
       success = line[strlen (line) - 1] == '\n';
   }
   return success;
}

void pause (void) {
	printf ("Press 'enter' to continue\n");
	while (getchar() != '\n') ;
}

int main (int argc, char *argv[]) {
   int         ReturnStatus = 0;          // main() return status.
   char        *filename = "eval5.dat";   // Default file name.
   FILE        *input;
   char        line[100];
   bool        done;
   proglet_t   prog;

   if (argc > 1) {
      filename = argv[1];
   }
   if (strcmp ("-", filename) == 0) {
      input = stdin;
   } else {
      input = fopen (filename, "r");
   }

   if (input == 0) {
      printf ("Couldn't open '%s'.\n", filename);
      ReturnStatus = 1;
   } else {
      srand (1234);
      do {
         prog.reinit();
         do {
            if (GetLine (input, line, sizeof (line))) {
               done = prog.parse (line);
            } else {
               done = true;
            }
         } while (!done);

         printf ("--------------------\n");
         prog.dump();
		 pause ();
         printf ("--------------------\n");
         prog.run (0.1);   // Different time values.
		 pause ();
         printf ("--------------------\n");
         prog.run (1.0);   // Different time values.
         printf ("--------------------\n");
      } while (!feof (input));

      if (input != stdin) {
         fclose (input);
      }
   }

   return ReturnStatus;
}