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

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

extern byte *compile(const char *);

datum me = NOTHING;

void 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]);
	exit(1);
    }

    if((f = fopen(argv[1], "r")) == NULL) {
	perror(f);
	exit(2);
    }

    if(db_read(f) < 0) {
	fprintf(stderr, "Couldn't read database from %s\n", argv[1]);
	exit(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("%d ", 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);
}