/* ---------------------------------------------------------------------- */
/*                   Copyright (C) 1991 by Natrlich!                     */
/*                      This file is copyrighted!                         */
/*                Refer to the documentation for details.                 */
/* ---------------------------------------------------------------------- */
#include <stdio.h>
#include "defines.h"
#include "nasm.h"
#include "y_tab.h"
#include "buffer.h"
#include "ops.h"

extern char huge     *yylval;       /* ho ho */
extern buffer huge   *bp;
extern _ops          ops[];
extern char          buf[];


void h_print()
{
   buffer huge *p = bp;
   int         flag = 0;

   if( ! p)
      return;
   while( p->before)
      p = p->before;
   do
      if( (p->type & 0xF) <= BUF_MACRO)     /* MACRO or FILE */
      {
         if( flag++)
            putc( ',', ESTREAM);
         fprintf( ESTREAM, "%s[%d]",  p->name, p->line);
      }
   while( p = p->next);

   if( flag)
   {
      putc(':', ESTREAM);
      putc(' ', ESTREAM);
   }
}


char *cnv ( no)
{
   switch( no)
   {
      case 0          : return("EOF");
      case T_EOL      : return( "End of line");
      case T_NUMBER   : sprintf( buf, "$%lX", yylval); break;
      case T_STRING   : sprintf( buf, "\"%s\"", yylval + 2); break;
      case T_FILE     :
      case T_IDENT    :
      case T_LABEL    : return( yylval);
      case T_CHAR     : sprintf( buf, "'%c", (unsigned char) yylval); break;
      case T_INSTR    : return( get_mnemo( (unsigned int) yylval));
      case T_EXPR     : return( "<expression>");
      case T_DOTEQ    : return( ".=");
      case T_IF       : return( ".IF");
      case T_ELSE     : return( ".ELSE" );
      case T_ENDIF    : return( ".ENDIF");
      case T_ORG      : return( ".ORG");
      case T_DS       : return( ".DS"   );
      case T_WORD     : return( ".WORD");
      case T_BYTE     : return( ".BYTE");
      case T_SBYTE    : return( ".SBYTE");
      case T_OPT      : return( ".OPT");
      case T_MACRO    : return( ".MACRO");
      case T_ENDM     : return( ".ENDM");
      case T_END      : return( ".END");
      case T_NUM      : return( "NUM");
      case T_ACCU     : return( "A");
      case T_AND      : return( ".AND");
      case T_OR       : return( ".OR");
      case T_NEQ      : return( "<>");
      case T_GEQ      : return( ">=");
      case T_LEQ      : return( "<=");
      case T_NOT      : return( ".NOT");
      case T_DEF      : return( ".DEF");
      case T_NO       : return( "NO");
      case T_OBJ      : return( "OBJ");
      case T_MLIST    : return( "MLIST");
      case T_CLIST    : return( "CLIST");
      case T_EJECT    : return( "EJECT");
      case T_XREF     : return( "XREF");
      case T_ERR      : return( "ERR");
      case T_PAGE     : return( ".PAGE");
      case T_ERROR    : return( ".ERROR");
      case T_TITLE    : return( ".TITLE");
      case T_DBYTE    : return( ".DBYTE");
      case T_TAB      : return( ".TAB");
      case T_FLOAT    : return( ".FLOAT");
      case T_CBYTE    : return( ".CBYTE");
      case T_LOCAL    : return( ".LOCAL");
      case T_UNDEF    : return( ".UNDEF");
      case T_SET      : return( ".SET");
      case T_REF      : return( ".REF");
      case T_LIST     : return( ".LIST");
      case T_PARA     : return( "macro parameter");
      case T_MPARA    : sprintf( buf, "%%%ld", yylval);   break;
      case T_MSPARA   : sprintf( buf, "%%$%ld", yylval);  break;
      case T_MLPARA   : sprintf( buf, "%%(%s)", yylval);  break;
      case T_MLSPARA  : sprintf( buf, "%%$(%s)", yylval); break;
      case T_INCLUDE  : return( ".INCLUDE");
      default         :
         switch( no)
         {
            case ' '  : return( "[SPACE]");
            case '\t' : return( "[TAB]");
            case '\n' : return( "[LF]");
            case '\r' : return( "[CR]");
            case '\f' : return( "[FF]");
            default   :
               if( no >= ' ' && no <= 126)
                  sprintf( buf, "'%c'", no);
               else
                  sprintf( buf, "#$%04X", no);
         }
   }
   return( buf);
}

void nterror( err, tok)
char   *err;
{
   nserror( err, cnv( tok));
}

void nberror( err, line)
char        *err;
unsigned    line;
{
   sprintf( buf, "%s, Fwd ref (probably) in line %u", err, line);
   nerror( buf);
}
void finalshit() {}
