Figure 6 shows a simplified definition of write/1 to illustrate the described functions. This simplified version does not deal with operators. It is called display/1, because it mimics closely the behaviour of this Edinburgh predicate.
foreign_t
pl_display(term_t t)
{ functor_t functor;
int arity, len, n;
char *s;
switch( PL_term_type(t) )
{ case PL_VARIABLE:
case PL_ATOM:
case PL_INTEGER:
case PL_FLOAT:
PL_get_chars(t, &s, CVT_ALL);
if (! Sfprintf(Scurrent_output, "%s", s) )
PL_fail;
break;
case PL_STRING:
if ( !PL_get_string_chars(t, &s, &len) &&
!Sfprintf(Scurrent_output, "\"%s\"", s) )
PL_fail;
break;
case PL_TERM:
{ term_t a = PL_new_term_ref();
if ( !PL_get_name_arity(t, &name, &arity) &&
!Sfprintf(Scurrent_output, "%s(", PL_atom_chars(name)) )
PL_fail
for(n=1; n<=arity; n++)
{ if ( ! PL_get_arg(n, t, a) )
PL_fail;
if ( n > 1 )
if ( ! Sfprintf(Scurrent_output, ", ") )
PL_fail;
if ( !pl_display(a) )
PL_fail;
}
if ( !Sfprintf(Scurrent_output, ")") )
PL_fail;
break;
default:
PL_fail; /* should not happen */
}
}
PL_succeed;
}