next up previous
Next: 7 ガーベッジコレクション Up: 6 マクロの処理 Previous: 6.1 マクロの定義defmacro (xlcont.c)

6.2 macroexpand (xleval.c)

マクロ展開を行う手続きは,

/* xmacroexpand - expand a macro call repeatedly */
LVAL xmacroexpand()
{
  LVAL form;
  form = xlgetarg();
  xllastarg();
  return (xlexpandmacros(form));
}

/* xlexpandmacros - expand macros in a form */
LVAL xlexpandmacros(form)
     LVAL form;
{
  LVAL fun,args;
  
  /* protect some pointers */
  xlstkcheck(3);
  xlprotect(form);
  xlsave(fun);
  xlsave(args);
  
  /* expand until the form isn't a macro call */
  while (consp(form)) {
    fun = car(form);        /* get the macro name */
    args = cdr(form);        /* get the arguments */
    if (!symbolp(fun) || !fboundp(fun))
      break;
    /* get the expansion function */
    fun = xlgetfunction(fun);
    if (!macroexpand(fun,args,&form))
      break;
  }
  
  /* restore the stack and return the expansion */
  xlpopn(3);
  return (form);
}
マクロ展開を1度だけ行う関数として,MACROEXPAND-1があります. それに対応するCの関数は,x1macroexpandです.

/* x1macroexpand - expand a macro call */
LVAL x1macroexpand()
{
  LVAL form,fun,args;
  
  /* protect some pointers */
  xlstkcheck(2);
  xlsave(fun);
  xlsave(args);
  
  /* get the form */
  form = xlgetarg();
  xllastarg();
  
  /* expand until the form isn't a macro call */
  if (consp(form)) {
    fun = car(form);        /* get the macro name */
    args = cdr(form);        /* get the arguments */
    if (symbolp(fun) && fboundp(fun)) {
      /* get the expansion function */
      fun = xlgetfunction(fun);
      macroexpand(fun,args,&form);
    }
  }
  
  /* restore the stack and return the expansion */
  xlpopn(2);
  return (form);
}


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