How do you compile macros in a Lisp compiler?

前端 未结 4 1320
情歌与酒
情歌与酒 2021-01-30 23:14

In a Lisp interpreter, there can easily be a branch in eval that can expand a macro, and in the process of expanding it, call functions to build up the expanded exp

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-30 23:40

    There's nothing particularly magical about macros.

    At a high level, they're simply functions. Functions that return S-Exprs for Lisp forms. The "runtime" for the macro is available in the macroexpand function, which as you may already know, expands macros.

    So, you can look at it in terms that the compiler detects that a form is a macro, evaluates it, then compiles the subsequent form that is returned as a result of that macro.

    Normally, there is a lot of quoting and splicing and other list surgery within macros to make them easier to write, much like a templating system. But those constructs are not required. You can return an S-Expr built however you want. So, looked at that way, you can see they, at their core, they're just simple functions evaluated at compile time.

提交回复
热议问题