////////////////////////////////////////////////////////////////
// Grip 0.5s54 Designed and implemented by: Serhei Makarov.   //
// Special thanks to: Vladimir Makarov, for mentoring and     //
// moral support,                                             //
//                    Nadia Makarov, for moral support, and   //
//                    The person who invented ISPs, otherwise //
// I might not have gotten time to get such a head start in   //
// programming language design.                               //
////////////////////////////////////////////////////////////////

// GRIP 0.5 is a structured language roughly capable of performing
// algorhythmic calculations, e.g. a sieve for primes.
// WARNING : WE HAVE NOT IMPLEMENTED ANY INPUT DEVICES.

include "error"; // The error management package.
include "intrep"; // Interpretation package.
include "parser"; // Translation package.

// Command line options :
//  -v
//  --verbose       Turns on dumpings at runtime.
//
//  -t
//  --parse-results Output the results of parsing.
//
//  -d
//  --full-parse    Output information about how
//                  the parse went.
//
//  -h
//  --help          Print possible command line
//                  options and quit.
//
//  -n
//  --version       Print version number and quit.
//
//  filename        Interpret 'filename'.
var verbose_flag = 0, debug_level = 0, file_name = "",
    translator = grip_parser(), db_fl = 0;
// Db_fl is defined to find conflicting -td options and for -hn option usage.
// Find if we just want help.
if (#argv == 0) {
  putln ("No arguments found. Use 'grip --help' for help.");
  exit (0);
}
for (var argpos = 0; argpos < #argv; argpos++)
  if (argv[argpos] == "--help" ||
      argv[argpos] == "-h") {
    putln ("GRIP 0.5s54.");
    putln ("Usage: grip (option | filename)...");
    putln ("Options:");
    putln ("POSIX one character | GNU style long");
    putln (" -v                 | --verbose       : ",
           "Turn on dumpings at runtime.");
    putln (" -t                 | --parse-results : ",
           "Show the results of parsing.");
    putln (" -d                 | --full-parse    : ",
           "Show how the parse went.");
    putln (" -n                 | --version       : ",
           "Print version number and quit.");
    putln (" -h                 | --help          : ",
           "Show this screen.");
    putln ();
    putln ("Grip returns 0 on success, 2 on a non-fatal error, and");
    putln ("3 on a fatal error that prevents its functioning properly."); 
    exit (0);
  } else if (argv[argpos] == "-n" ||
             argv[argpos] == "--version") {
    putln ("GRIP 0.5s54 designed and implemented by Serhei V. Makarov.");
    putln ("Special thanks to:");
    putln ("                   Vladimir Makarov, for mentoring and");
    putln ("                   moral support, and");
    putln ("                   Nadia Makarov, for moral support.");
    exit (0);
  }
// Analyze argv[] to find out the filename needed.
for (argpos = 0; argpos < #argv; argpos++) {
  if (argv[argpos] == "-v" ||
      argv[argpos] == "--verbose") verbose_flag = 1;
  else if (argv[argpos] == "-t" ||
           argv[argpos] == "--parse-results") if (!db_fl) {
                debug_level = 2;
                db_fl++;
        } else error ("Conflicting '-t' and '-d' options.");
  else if (argv[argpos] == "-d" ||
           argv[argpos] == "--full-parse") if (!db_fl) {
                debug_level = 5;
                db_fl++;
        } else error ("Conflicting '-t' and '-d' options.");
  else if (argv[argpos] == "-" && file_name == "") file_name = "/dev/stdin";
  else if (file_name == "") file_name = argv[argpos];
  else error ("Too many arguments or arguments invalid.");
}
if (file_name == "") error ("No file name specified or file name invalid");
if (global_errfl) abend (" command line ");
// Translate and interpret.
var executable_program = 
translator.parse_file(file_name, debug_level, verbose_flag);
executable_program.execute (verbose_flag, verbose_flag, verbose_flag);
exit(0);