/* error.c */
#include "codegen.h"
#include "error.h"
#include "stdlib.h"
#include "vars.h"

extern void abort();

/*	Report errors for user	*/
void errorsummary()
{
	/* see if anything is left hanging... */
	if (ncmp)
		error("missing closing bracket");
	
	/* open compound statement ... */
	nl();
	outstr("There were ");
	outdec(errcnt);						/* total # errors */
	outstr(" errors in compilation.");
	nl();
}

/** Error Notification **/
void error(char ptr[])
{
	int k;
	char junk[81];

	toconsole();
	bell();
	
	outstr("Line ");
	outdec(lineno);
	outstr(", ");
	
	if (infunc == 0)
		foutput_chr('(');
	if (currfn == NULL)
		outstr("start of file");
	else
		outstr(currfn + NAME);
	
	if (infunc == 0)
		foutput_chr(')');
	
	outdec(lineno - fnstart);
	outstr(": ");
	outstr(ptr);
	nl();
	
	outstr(line);
	nl();
	
	/* skip to error position */
	k = 0;
	while (k < lptr)
	{
		if (line[k++] == 9)
			tab();
		else
			foutput_chr(' ');
	}
	
	foutput_chr('^');
	nl();
	++errcnt;

	if (errstop)
	{
		pl("Continue (Y,n,g) ? ");
		gets(junk);		
		k = junk[0];
		if ((k == 'N') | (k == 'n'))
			abort();
		if ((k == 'G') | (k == 'g'))
			errstop = 0;
	}
	
	tofile();
}	/* end error */