4 ms·
I don't know what's the proper way of implementing macros but I can tell you how I did it in my toy lisp implementation: I compiled defmacro as a normal functio
by EdiX 16y ago
I don't know what's the proper way of implementing macros but I can tell you how I did it in my toy lisp implementation:
I compiled defmacro as a normal function but I also added it to a macro table, then in my compile-form function I would use this table to decide if the form is
a) a special form
b) a function call
c) a macro
(a) and (b) are obvious, (c) means I would call the compiled macro "function" on the rest of the form and the execute compile-form on the result.
It's unhygenic and there's probably some fatal flaw I didn't catch.
- bitsai 16y agoQuestion about (a): How do you handle special forms? I wrote a toy Lisp interpreter in Clojure, but couldn't figure out how to implement ', which I assumed should be implemented as a special form.
- abecedarius 16y ago'X is expanded by the reader into (QUOTE X), a special form like any other.
- bitsai 16y agoRighto. I had a working implementation that could expand 'x to (quote a) and '(1 2 3) to (quote (1 2 3)), but the case of expanding '''a to (quote (quote (quote a))) stumped me. I realize that ' is a special form like any other... except I haven't figured out how to implement any other special forms either :)
- EdiX 16y agoI handled them by generating the assembly code to implement them and setting up the symbol tables as required, I implemented ' as abecedarius said and then quote was a special form that compiled into something that "returned" the requested symbol object. PS similar deal with ` it would be expanded into a bunch of normal forms by the parser ("reader" in lisp-speak) and from there it's implemented with macros and functions, I copied Guy Steele example implementation, you can find it somewhere.
- bitsai 16y agoThanks for the tips. It's probably time for me to stop messing around and start reading the Lambda Papers in earnest...
- abecedarius 16y agoThat's the standard way to do it, except DEFMACRO is not a function. So define a function called SET-MACROEXPANDER or something, and define DEFMACRO as a macro using it.
- EdiX 16y agoI think I did something like that, maybe defmacro was a special form, I don't remember.