Thanks for all the replies! SICP is one of my favorite books actually,
I have read through it many times already.

My question is particularly concerning expanding macros during
compilation.

If your source has NO use of macros. Then it's trivial to compile.
Just compile each form one by one.

The problem arises when you use macros. When do you expand the macros?

Ideally you wish to expand the macros before the actual compilation.

So the straightforward steps should be:
1) step through each form
2) macroexpand the form
3) compile the expanded form

But what if the macro itself calls functions? Should we make scan-
ahead for function definitions? The code I posted is to demonstrate
how this could be a complicated process. I don't know how it's
resolved in commercial compilers, or Clojure. If someone knows, I
would really appreciate an explanation.

  -Patrick



On Sep 5, 6:48 pm, Michał Marczyk <michal.marc...@gmail.com> wrote:
> On 5 September 2010 18:46, CuppoJava <patrickli_2...@hotmail.com> wrote:
>
> > I'm writing a simple lisp for educational purposes, and I can't figure
> > out how to do compilation.
>
> The final chapter of SICP [1] deals with compilation of Scheme code
> down to a kind of assembly language (also introduced in the book).
> It's fantastically written and will likely be helpful.
>
> The general idea is that you have to establish some conventions for
> passing arguments to functions (they might all be passed on the stack
> or you might permit passing some through registers or as structs on
> the heap with a pointer on the stack etc.). Then you need to figure
> out a way to translate your Lisp code into executable code which,
> given a bunch of arguments in the appropriate places (as per the
> convention for argument passing), will compute the result of the
> function and put it in some fixed place (another convention). Then you
> need to have function invocation sites store the return address on the
> stack, pass arguments and jump to wherever the given function resides
> in memory. Jumping back to the return address is the job of a bit of
> "linkage" to be put at the end of the function's code in memory.
>
> Once again, I highly recommend the SICP chapter (don't skip the
> exercises!) -- it's not enough to give one the ability to write an
> industrial strength compiler, but it does give a level of
> understanding of the issues involved.
>
> Sincerely,
> Michał
>
> [1]http://mitpress.mit.edu/sicp/

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to