Example #1
0
/*-------------------------------------------------------------------------*
 * PL_THROW_2                                                              *
 *                                                                         *
 *-------------------------------------------------------------------------*/
void
Pl_Throw_2(WamWord ball_word, WamWord b_word)
{
  WamWord word, tag_mask;
  WamWord *b;
  StmInf *pstm;

  DEREF(b_word, word, tag_mask);
  b = From_WamWord_To_B(word);

  if (b <= pl_query_top_b && pl_query_top_b != NULL)
    {
      Assign_B(pl_query_top_b);
      pl_query_exception = ball_word;
      Pl_Exit_With_Exception();
    }

  if (b == LSSA)
    {
      pstm = pl_stm_tbl[pl_stm_top_level_output];

      Pl_Stream_Printf(pstm, "\nsystem_error(cannot_catch_throw(");
      Pl_Write_Term(pstm, -1, MAX_PREC,
		    WRITE_NUMBER_VARS | WRITE_NAME_VARS | WRITE_QUOTED, NULL,
		    ball_word);
      Pl_Stream_Printf(pstm, "))\n");
      return;
    }

  Pl_Cut(b_word);
}
Example #2
0
/*-------------------------------------------------------------------------*
 * PL_WRITE_SIMPLE                                                         *
 *                                                                         *
 *-------------------------------------------------------------------------*/
void
Pl_Write(WamWord term_word)
{
  StmInf *pstm = pl_stm_tbl[pl_stm_output];

  Pl_Write_Term(pstm, -1, MAX_PREC, WRITE_NUMBER_VARS | WRITE_NAME_VARS, NULL,
		term_word);
  /* like write/1 */
}
Example #3
0
/*-------------------------------------------------------------------------*
 * FORMAT                                                                  *
 *                                                                         *
 *-------------------------------------------------------------------------*/
static void
Format(StmInf *pstm, char *format, WamWord *lst_adr)

#define IMPOSS -12345678

{
  WamWord word;
  Bool has_n;
  long generic;
  long n, n1;
  char *p;
  long x;
  double d;
  int lg, stop;
  int i, k;
  char *format_stack[256];
  char **top_stack = format_stack;
  char buff[2048];


  *top_stack++ = format;
  do
    {
      format = *--top_stack;

      while (*format)
	{
	  if (*format == '%')	/* C printf format */
	    {
	      if (format[1] == '%')
		{
		  Pl_Stream_Putc('%', pstm);
		  format += 2;
		  continue;
		}

	      p = buff;
	      n = n1 = IMPOSS;

	      do
		if ((*p++ = *format++) == '*')
		  {
		    if (n == IMPOSS)
		      n = Arg_Integer(&lst_adr);
		    else
		      n1 = Arg_Integer(&lst_adr);
		  }
	      while ((char *) strchr("diouxXpnceEfgGs", p[-1]) == NULL);

	      *p = '\0';
	      if (strchr("eEfgG", p[-1]) == NULL)
		{
		  generic = (p[-1] == 's') ? (long) Arg_Atom(&lst_adr)
		    : Arg_Integer(&lst_adr);
		  if (n != IMPOSS)
		    {
		      if (n1 != IMPOSS)
			Pl_Stream_Printf(pstm, buff, n, n1, generic);
		      else
			Pl_Stream_Printf(pstm, buff, n, generic);
		    }
		  else
		    Pl_Stream_Printf(pstm, buff, generic);
		}
	      else
		{
		  d = Arg_Float(&lst_adr);
		  if (n != IMPOSS)
		    {
		      if (n1 != IMPOSS)
			Pl_Stream_Printf(pstm, buff, n, n1, d);
		      else
			Pl_Stream_Printf(pstm, buff, n, d);
		    }
		  else
		    Pl_Stream_Printf(pstm, buff, d);
		}
	      continue;
	    }

	  if (*format != '~')
	    {
	      Pl_Stream_Putc(*format, pstm);
	      format++;
	      continue;
	    }

	  if (*++format == '*')
	    {
	      n = Arg_Integer(&lst_adr);
	      format++;
	      has_n = TRUE;
	    }
	  else
	    {
	      p = format;
	      n = strtol(format, &format, 10);
	      has_n = (format != p);
	    }



	  switch (*format)
	    {
	    case 'a':
	      p = Arg_Atom(&lst_adr);
	      if (has_n)
		Pl_Stream_Printf(pstm, "%*s", -n, p);
	      else
		Pl_Stream_Puts(p, pstm);
	      break;

	    case 'c':
	      x = Arg_Integer(&lst_adr);
	      if (!Is_Valid_Code(x))
		Pl_Err_Representation(pl_representation_character_code);

	      do
		Pl_Stream_Putc(x, pstm);
	      while (--n > 0);
	      break;

	    case 'e':
	    case 'E':
	    case 'f':
	    case 'g':
	    case 'G':
	      x = *format;
	      d = Arg_Float(&lst_adr);

	      if (has_n)
		sprintf(buff, "%%.%ld%c", n, (char) x);
	      else
		sprintf(buff, "%%%c", (char) x);

	      Pl_Stream_Printf(pstm, buff, d);
	      break;

	    case 'd':
	    case 'D':
	      x = Arg_Integer(&lst_adr);

	      if (n == 0 && *format == 'd')
		{
		  Pl_Stream_Printf(pstm, "%ld", x);
		  break;
		}

	      if (x < 0)
		{
		  Pl_Stream_Putc('-', pstm);
		  x = -x;
		}

	      sprintf(buff, "%ld", x);
	      lg = strlen(buff) - n;
	      if (lg <= 0)
		{
		  Pl_Stream_Puts("0.", pstm);
		  for (i = 0; i < -lg; i++)
		    Pl_Stream_Putc('0', pstm);
		  Pl_Stream_Printf(pstm, "%ld", x);
		  break;
		}


	      stop = (*format == 'D') ? lg % 3 : -1;

	      if (stop == 0)
		stop = 3;

	      for (p = buff, i = 0; *p; p++, i++)
		{
		  if (i == lg)
		    Pl_Stream_Putc('.', pstm), stop = -1;

		  if (i == stop)
		    Pl_Stream_Putc(',', pstm), stop += 3;
		  Pl_Stream_Putc(*p, pstm);
		}
	      break;

	    case 'r':
	    case 'R':
	      x = Arg_Integer(&lst_adr);

	      if (!has_n || n < 2 || n > 36)
		n = 8;

	      k = ((*format == 'r') ? 'a' : 'A') - 10;

	      if (x < 0)
		{
		  Pl_Stream_Putc('-', pstm);
		  x = -x;
		}

	      p = buff + sizeof(buff) - 1;
	      *p = '\0';
	      do
		{
		  i = x % n;
		  x = x / n;
		  --p;
		  *p = (i < 10) ? i + '0' : i + k;
		}
	      while (x);
	      Pl_Stream_Puts(p, pstm);
	      break;

	    case 's':
	    case 'S':
	      word = Read_Arg(&lst_adr);
	      if (*format == 's')
		p = Pl_Rd_Codes_Check(word);
	      else
		p = Pl_Rd_Chars_Check(word);

	      if (has_n)
		Pl_Stream_Printf(pstm, "%-*.*s", n, n, p);
	      else
		Pl_Stream_Printf(pstm, "%s", p);
	      break;

	    case 'i':
	      do
		Read_Arg(&lst_adr);
	      while (--n > 0);
	      break;

	    case 'k':
	      word = Read_Arg(&lst_adr);
	      Pl_Write_Term(pstm, -1, MAX_PREC,
			 WRITE_IGNORE_OP | WRITE_QUOTED, word);
	      break;

	    case 'q':
	      word = Read_Arg(&lst_adr);
	      Pl_Write_Term(pstm, -1, MAX_PREC, WRITE_NUMBER_VARS |
			 WRITE_NAME_VARS | WRITE_QUOTED, word);
	      break;

	    case 'p':		/* only work if print.pl is linked */
	      word = Read_Arg(&lst_adr);
	      Pl_Write_Term(pstm, -1, MAX_PREC, WRITE_NUMBER_VARS |
			 WRITE_NAME_VARS | WRITE_PORTRAYED, word);
	      break;

	    case 'w':
	      word = Read_Arg(&lst_adr);
	      Pl_Write_Term(pstm, -1, MAX_PREC, WRITE_NUMBER_VARS |
			 WRITE_NAME_VARS, word);
	      break;

	    case '~':
	      Pl_Stream_Putc('~', pstm);
	      break;

	    case 'N':
	      if (pstm->line_pos == 0)
		break;
	    case 'n':
	      do
		Pl_Stream_Putc('\n', pstm);
	      while (--n > 0);
	      break;

	    case '?':
	      if (format[1])
		*top_stack++ = format + 1;

	      format = Arg_Atom(&lst_adr);
	      continue;

	    default:
	      Pl_Err_Domain(pl_domain_format_control_sequence,
			    Tag_ATM(ATOM_CHAR(*format)));
	    }
	  format++;
	}
    }
  while (top_stack > format_stack);
}