Re: Can Clojure compile a source-file? How does it do it?
On 6 September 2010 02:01, CuppoJava wrote: > Ah that makes sense! Thanks Michal! Great! :-) > I have looked through Lisp in Small Pieces, and didn't find it very > well written. I think a lot was lost through the translation. Oh, that's unfortunate. Perhaps I should step up my French-learning efforts and read "Principes d'implantation de Scheme et Lisp" [1] then... > Besides > SICP, the other great lisp book I read was actually "The Scheme > Programming Language". The chapter on continuations is mind-blowing. Yes, Kent Dybvig often achieves that quality in his writings. :-) All the best, Michał [1] http://paracamplus.com/?CGIRunMode=book&urn=Cours/LiSP/4 -- 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
Re: Can Clojure compile a source-file? How does it do it?
Ah that makes sense! Thanks Michal! I have looked through Lisp in Small Pieces, and didn't find it very well written. I think a lot was lost through the translation. Besides SICP, the other great lisp book I read was actually "The Scheme Programming Language". The chapter on continuations is mind-blowing. -Patrick On Sep 5, 7:21 pm, Michał Marczyk wrote: > In the presence of macros, it's best to think "Lisp compilation = > evaluation of all forms". So, you do two things at the same time: (1) > accumulate object code to be output as the result of the compilation > and (2) actually execute the programme, so that you can call functions > and examine variables when doing macro expansion etc. The Clojure > compiler works this way -- for a demonstration, just have it compile a > file with a println at top level. > > That's the rough idea, anyway; I gather that Christian Queinnec's > "Lisp in Small Pieces" [1] is the definitive book on the subject, so > perhaps that would be more helpful to you. (Regrettably, I haven't yet > read that one, although it's definitely on my list as one of the > as-yet unread Lisp classics!) > > Sincerely, > Michał -- 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
Re: Can Clojure compile a source-file? How does it do it?
In the presence of macros, it's best to think "Lisp compilation = evaluation of all forms". So, you do two things at the same time: (1) accumulate object code to be output as the result of the compilation and (2) actually execute the programme, so that you can call functions and examine variables when doing macro expansion etc. The Clojure compiler works this way -- for a demonstration, just have it compile a file with a println at top level. That's the rough idea, anyway; I gather that Christian Queinnec's "Lisp in Small Pieces" [1] is the definitive book on the subject, so perhaps that would be more helpful to you. (Regrettably, I haven't yet read that one, although it's definitely on my list as one of the as-yet unread Lisp classics!) Sincerely, Michał -- 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
Re: Can Clojure compile a source-file? How does it do it?
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 wrote: > On 5 September 2010 18:46, CuppoJava 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
Re: Can Clojure compile a source-file? How does it do it?
On 5 September 2010 18:46, CuppoJava 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
Re: Can Clojure compile a source-file? How does it do it?
On Sun, Sep 5, 2010 at 5:11 PM, Stuart Sierra wrote: > Clojure compiles sources to Java ".class" files. To create a .class > file that can be run at the command line, you need a namespace with a > (:gen-class) directive and a function named "-main". Here's an > example: > I think the question was about how clojure works under the hood and how it can guarantee the same behaviour when compiled as when interactively typed. -- 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
Re: Can Clojure compile a source-file? How does it do it?
Clojure compiles sources to Java ".class" files. To create a .class file that can be run at the command line, you need a namespace with a (:gen-class) directive and a function named "-main". Here's an example: (ns com.example.myapp (:gen-class)) (defn -main [] (println "Hello, World!")) This would go in a file named src/com/example/myapp.clj To compile it, you would run the following commands: mkdir classes java -cp src:classes:clojure.jar clojure.lang.Compile com.example.myapp Then you can execute it with the command: java -cp classes:clojure.jar com.example.myapp In general, this process is made easier by build tools such as Leiningen and clojure-maven-plugin; consult those tools' documentation for more information. -S On Sep 5, 12:46 pm, CuppoJava wrote: > Hi everyone, > I'm writing a simple lisp for educational purposes, and I can't figure > out how to do compilation. In particular, I can't figure out how I can > get a compiled file to run in the same way as if it were loaded. I > read on the webpage that Clojure can do this. Does anyone know how > Clojure does it? > > eg. How can I compile this file, such that it does the same thing as > when it's loaded: > > (def macro-helper (atom nil)) > > (println "Setting Macrohelper!") > (swap! macro-helper > (constantly (fn [] > (println "Macro-Helper!") > `(println "Runtime!" > > (defmacro mymacro [] > (println "Macro Expansion Time!") > (@macro-helper)) > > (mymacro) > > Thanks a lot! > -Patrick -- 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
Can Clojure compile a source-file? How does it do it?
Hi everyone, I'm writing a simple lisp for educational purposes, and I can't figure out how to do compilation. In particular, I can't figure out how I can get a compiled file to run in the same way as if it were loaded. I read on the webpage that Clojure can do this. Does anyone know how Clojure does it? eg. How can I compile this file, such that it does the same thing as when it's loaded: (def macro-helper (atom nil)) (println "Setting Macrohelper!") (swap! macro-helper (constantly (fn [] (println "Macro-Helper!") `(println "Runtime!" (defmacro mymacro [] (println "Macro Expansion Time!") (@macro-helper)) (mymacro) Thanks a lot! -Patrick -- 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