/*
 *      Copyright (c) 1991 Paul Campbell
 *      All Rights Reserved
 *      THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF Paul Campbell
 *      The copyright notice above does not evidence any
 *      actual or intended publication of such source code.
 */
 
static char Copyright[] = "Copyright (c) 1991 Paul Campbell All Rights Reserved";

#include <stdio.h>
#include "lex.h"
extern int yyerrcount, yydebug;
static char *prog, *this_file;
FILE *fcode;
char code_file[256];
#ifdef MPW
char *prefix = ":";
#else
char *prefix = "./";
#endif
int nowarn = 0;
char *object_output = NULL;
int pass, valid, op_size;
unsigned short pc;
unsigned short text_pc;
unsigned short data_pc;
void  code_init();
int intel = 1, bin = 0, map = 0, text = 0, data = 0, brflg=0;

main(int argc, char **argv)
{
	int i, quit;
	char *cp;
	
	code_init();
	yyinit();
	prog = argv[0];
	this_file = NULL;
	for (i = 1;i < argc; i++) {
		if (argv[i][0] == '-') {
			cp = &argv[i][1];
			for (quit = 0;!quit;) {
				switch (*cp++) {
				case 0:
					quit = 1;
					break;
				
				case 'd':
					yydebug = 1 - yydebug;
					break;
					
				case 'w':
					nowarn = 1-nowarn;
					break;
					
				case 'i':
					intel = 1; bin = 0;
					break;
					
				case 'b':
					bin = 1; intel = 0;
					break;
					
				case 'B':
					brflg = 1;
					fprintf(stderr,"#!/usr/local/bin/perl\n");
					fprintf(stderr,"$line = 0;\n");
					fprintf(stderr,"while (<>) {\n");
					fprintf(stderr,"	$a = $_;\n");
					fprintf(stderr,"	$line++;\n");
					break;
					
				case 'm':
					map = 1;
					break;
					
				case 'o':
					i++;
					if (i >= argc) {
						fprintf(stderr, "%s: missing argument for -%c\n", prog, cp[-1]);
						exit(2);
					}
					object_output = argv[i];
					break;
					
				default:
					fprintf(stderr, "%s: unknown flag -%c\n", prog, cp[-1]);
					exit(2);
				}
			}
		} else {
			if (this_file) {
				fprintf(stderr, "%s: multiple input files '%s' and '%s'\n", prog, this_file, argv[i]);
				exit(2);
			}
			this_file = argv[i];
		}
	}
	if (this_file == NULL) {
		fprintf(stderr, "%s: no input file\n", prog);
		exit(2);
	}
	if (yyopen(this_file)) {
		fprintf(stderr, "%s: can't open '%s'\n", prog, this_file);
		exit(2);
	}
	if (object_output) {
		strcpy(code_file, object_output);
	} else {
		strcpy(code_file, prefix);
		strcat(code_file, this_file);
		i = strlen(code_file);
		if (i > 2 && code_file[i-1] == 's' && code_file[i-2] == '.') {
			code_file[i-1] = '5';
			code_file[i]   = '1';
			code_file[i+1] = 0;
		} else {
			code_file[i] = '.';
			code_file[i+1] = '5';
			code_file[i+2] = '1';
			code_file[i+3] = 0;
		}
	}
	pass = 1;
	text = 1;
	data = 0;
	data_pc = 0;
	text_pc = 0;
	pc = 0;
	if (yyparse() || yyerrcount > 0) {
		fprintf(stderr, "%s: %d error%s in '%s', compile failed\n", prog, yyerrcount, (yyerrcount==1?"":"s"), this_file);
		exit(2);
	}
	fcode = fopen(code_file, "w");
	if (fcode == NULL) {
		fprintf(stderr, "%s: can't create '%s'\n", prog, code_file);
		exit(2);
	}
	pass = 2;
	pc = 0;
	text = 1;
	data = 0;
	data_pc = 0;
	text_pc = 0;
	yyrewind();
	set_addr();
	if (yyparse() || yyerrcount > 0) {
		fprintf(stderr, "%s: %d error%s in '%s', compile failed\n", prog, yyerrcount, (yyerrcount==1?"":"s"), this_file);
		exit(2);
	}
	dump_done();
	yyclose();
	fclose(fcode);
	if (brflg) {
		fprintf(stderr,"	print $a;\n");
		fprintf(stderr,"}\n");
	}
	if (map)
		pmap(0);
}

void
int_error(char *s, char *a, char *b, char *c, char *d, char *e)
{
	fprintf(stderr, "internal compiler error: ");
	fprintf(stderr, s, a, b, c, d, e);
	fprintf(stderr, "\n");
	exit(2);
}

