next up previous
Next: 4.3 Car, Cdr Up: 4 Xlispの組込み関数の定義 Previous: 4.1 リスト処理 xllist.c

4.2 Atom, symbolp etc

データの型を調べる述語は以下のようになります.

LVAL xatom()
{
  LVAL arg;
  arg = xlgetarg();
  xllastarg();
  return (atom(arg) ? true : NIL);
}

/* xsymbolp - is this an symbol? */
LVAL xsymbolp()
{
    LVAL arg;
    arg = xlgetarg();
    xllastarg();
    return (arg == NIL || symbolp(arg) ? true : NIL);
}

/* xnumberp - is this a number? */
LVAL xnumberp()
{
    LVAL arg;
    arg = xlgetarg();
    xllastarg();
    return (fixp(arg) || floatp(arg) ? true : NIL);
}
型の比較,引数のチェックなどはxlisp.hなどにマクロとして定義されています.

/* type predicates */                               
#define atom(x)          ((x) == NIL || ntype(x) != CONS)
#define null(x)          ((x) == NIL)
#define listp(x)  ((x) == NIL || ntype(x) == CONS)
#define consp(x)  ((x) && ntype(x) == CONS)
#define subrp(x)  ((x) && ntype(x) == SUBR)
#define fsubrp(x)  ((x) && ntype(x) == FSUBR)
#define stringp(x) ((x) && ntype(x) == STRING)
#define symbolp(x) ((x) && ntype(x) == SYMBOL)
#define streamp(x) ((x) && ntype(x) == STREAM)
#define objectp(x) ((x) && ntype(x) == OBJECT)
#define fixp(x)           ((x) && ntype(x) == FIXNUM)
#define floatp(x)  ((x) && ntype(x) == FLONUM)
#define vectorp(x) ((x) && ntype(x) == VECTOR)
#define closurep(x) ((x) && ntype(x) == CLOSURE)
#define charp(x)    ((x) && ntype(x) == CHAR)
#define ustreamp(x) ((x) && ntype(x) == USTREAM)
#define structp(x)  ((x) && ntype(x) == STRUCT)
#define boundp(x)   (getvalue(x) != s_unbound)
#define fboundp(x)  (getfunction(x) != s_unbound)

/* argument list parsing macros */
#define xlgetarg() (testarg(nextarg()))
#define xllastarg() {if (xlargc != 0) xltoomany();}
#define testarg(e)  (moreargs() ? (e) : xltoofew())
#define typearg(tp)
   (tp(*xlargv) ? nextarg() : xlbadtype(*xlargv))
#define nextarg()   (--xlargc, *xlargv++)
#define moreargs()  (xlargc > 0)


generated through LaTeX2HTML. M.Inaba 平成18年5月6日