/* Copyright 1989, 1990 by James Aspnes, David Applegate, and Bennet Yee */
/* See the file COPYING for distribution information */
#include "os.h"

#include "globals.h"
#include "db.h"
#include "bytecode.h"

extern byte *compile (const char *);

datum me = NOTHING;

int main (int argc, char **argv)
{
  FILE *f;
  char buf[2048];
  byte *code;
  int i;
  const char *s;

  if (argc < 2) {
    fprintf (stderr, "Usage: %s db-file\n", argv[0]);
    return (1);
  }

  if ((f = fopen (argv[1], "rb")) == NULL) {
    perror ("Couldn't open database.\n");
    return (2);
  }

  if (db_read (f) < 0) {
    fprintf (stderr, "Couldn't read database from %s\n", argv[1]);
    return (3);
  } else {
    fclose (f);
  }

  while (gets (buf)) {
    if ((code = compile (buf)) == 0) {
      puts (compile_error);
    } else {
      for (i = 0; code[i - 1] == LITERAL_OP || code[i] != RETURN_OP; i++) {
        printf ("%ld ", code[i]);
        if (isascii (code[i]) && isprint (code[i])) {
          putchar (code[i]);
        }
        if ((s = string (code[i])) != NULL) {
          printf (" %s", s);
        }
        putchar ('\n');
      }
    }
    full_gc ();
  }

  db_write (stdout);
  return 0;
}