/*****************************************************************************
 * FILE : emitter.c -  routines for output creation
 *
 * programmer - Pawel Cholewinski
 *
 * date 04/19/93
 * modified 19 Apr 1999 by Neil Moore <neil@cs.uky.edu>
 * modified 20 Aug 1999 by Neil Moore <neil@cs.uky.edu>
 * modified  5 Sep 1999 by Neil Moore <neil@cs.uky.edu>
 *
 * Part of sderes.  See COPYRIGHT for copyright information.
 *
 *****************************************************************************/

#include "default.h"
#include "global.h"
#include "token.h"

void printformula(int k);
int printliteral(int lit, char** lextab);
void printtoken(int tkn);

/*
 * =========================================================================
 * emitextnsn : display set of atoms stored in array Textnsn
 * =========================================================================
 */
void emitextnsn(int no, int GD[])
{
   int i, app=0;

   if(verbosity > -1) {
	   printf("Extension %d: Cn(W + {", no);
	   for (i=0 ; i<dno ; i++) 
		   if (GD[i]==1) {
			   if (app) printf(", ");
			   else app = 1;
			   printformula(conc[i]);
		   }
	   printf("%s"," })\n\n");
   }
}

/*
 * =========================================================================
 * printformula : decode and print formula starting at position k
 * =========================================================================
 */
void printformula(int k)
{
   int i, token;

	i = k;
	if (i<0) return;
	token = dttable[i];
	while (token != COLON && token != ENT && token != SEMI) {
	  if (token == VAR) {
	      printf("%s", defatomlex[dttable[i+1]]);
	      i = i+2;
	   }
	   else {
	      printtoken(token);
	      i = i+1;
	   }
	   token = dttable[i];
        }
}

int printliteral(int lit, char** lextab)
{
   if (lextab==NULL || lextab[abs(lit)]==NULL) {
	printf("%d",lit);
	return 1;
   }
   if (lit<0) {
	printtoken(NEG);
	lit = -lit;
   }
   printf("%s", lextab[lit]);
   return 0;
}
	
/*
 * =========================================================================
 * printtoken : decode and print token of code value tkn
 * =========================================================================
 */
void printtoken(int tkn)
{
        if (tkn==TOP) printf("1");
        else if (tkn==BOT) printf("0");
        else if (tkn==NEG) printf("!");
        else if (tkn==AND) printf(" && ");
        else if (tkn==COLON) printf(" : ");
        else if (tkn==COMMA) printf(", ");
        else if (tkn==ENT) printf(" -> ");
        else if (tkn==SEMI) printf(";");
        else if (tkn==BOW) printf("BOW");
        else if (tkn==EOW) printf("EOW");
        else if (tkn==BOD) printf("BOD");
        else if (tkn==EOD) printf("EOD");
        else if (tkn==ENDTH) printf("ENDTH");
	else if (tkn==EOI) printf("EOI");
}

