Slashdot Mirror


How Would You Generate C Code Using Common Lisp Macros? (github.com)

Long-time Slashdot reader kruhft brings news about a new S-Expression based language transpiler that has the feel of C. This structure allows for the creation of code generation macros using the full power of the host Common Lisp environment, a language designed for operating on S-Expressions, also known as Lists. It is unknown exactly what power might come about from this combination of low level processing with high level code generation.
This has prompted some discussion online about other attempts to convert Lisp to C -- raising several more questions. How (and why) would you convert your Lisp code into C, and what would then be the best uses for this capability?

20 of 108 comments (clear)

  1. Butterflies by rsilvergun · · Score: 3, Funny

    Oblig XKCD

    --
    Hi! I make Firefox Plug-ins. Check 'em out @ https://addons.mozilla.org/en-US/firefox/addon/youtube-mp3-podcaster/
    1. Re:Butterflies by K.+S.+Kyosuke · · Score: 4, Funny

      To honor Carrie Fisher, you should have posted this.

      --
      Ezekiel 23:20
    2. Re: Butterflies by bsDaemon · · Score: 4, Funny

      No, that would be this one:
      https://xkcd.com/224/

  2. I wouldn't by Anonymous Coward · · Score: 2, Insightful

    What kind of dumb article is this? Stop living in the past Grandpa.

    1. Re:I wouldn't by Anonymous Coward · · Score: 2, Insightful

      Stop chasing the fad of the moment and learn to actually hone a mature skill set, son.

    2. Re:I wouldn't by ShanghaiBill · · Score: 2

      you can keep your javascript constructed hell..

      Javascript is actually used in the real world.
      Lisp is not.
      And Lisp had a 40 year head start.

      Javascript is far from perfect, but at least I can look at code written by someone else, and understand it.
      That can't be done with Lisp.

      Did you look at the code samples in TFA?
      What possible real-world application could there be for that gibberish?
      If The Onion published tech articles, I would assume this was a joke.

    3. Re:I wouldn't by hey! · · Score: 2

      I would argue that Javascript until fairly recently was barely used in the real world. As someone who learned to program at MIT in the early 80s, Javascript feels a lot like Scheme -- a dialect of Lisp. But until Node came along people were using it like Visual Basic.

      As for Lisp in the real world, it definitely *is* used, but largely for things companies like to keep close to their vests -- defense, finance, that kind of thing. You can search for lisp jobs -- there are plenty out there, but they're not code monkey positions.

      --
      Post may contain irony: discontinue use if experiencing mood swings, nausea or elevated blood pressure.
  3. Excellent by phantomfive · · Score: 4, Funny

    All the beauty of C syntax with all the conciseness of Lisp:

    (int main ((int argc) (char (** argv)))
         (sswitch ([] argv 1)
              (case ("a" "c")
            (printf "The value is \"a\" or \"c\"\n"))
              (case "d"
            (goto e-label))
              (case "b"
            (printf "The value is \"b\"\n"))
              (case "e"
            (: e-label)
            (printf "The value is \"d\" or \"e\"\n"))
              (default
              (printf "The value is neither \"a\", \"b\", \"c\", \"d\", or \"e\"\n")))
         (return 0))

    --
    "First they came for the slanderers and i said nothing."
    1. Re:Excellent by dgatwood · · Score: 5, Funny

      I'm suddenly reminded of the Russian spy who tried to steal the source code for the American missile defense system. When he got home, he discovered that he had nothing but three pages filled with close parentheses. "The bad news," the spy said, "is that we only got the last three pages, and they contain no useful code. The good news is that we now know what language it was written in."

      --

      Check out my sci-fi/humor trilogy at PatriotsBooks.

  4. How? by Gravis+Zero · · Score: 3, Funny

    Weeping and at gunpoint. ;)

    --
    Anons need not reply. Questions end with a question mark.
  5. Lisp to C by jbolden · · Score: 3, Interesting

    How (and why) would you convert your Lisp code into C

    Well that's pretty easy. There are tons of simple Lisp compilers. Heck building a LISP used to be an exercise for many years included (full source) with Microsoft's C compiler.

    A few examples:
    http://www.buildyourownlisp.co...
    http://howtowriteaprogram.blog...
    http://sbcl.sourceforge.net/
    http://clisp.sourceforge.net/

      A little off topic but a Lisp in Java by a master of the programming craft which is worth looking at: http://norvig.com/jscheme.html
    And finally also off topic but the original LISP in assembly: http://www.softwarepreservatio...

    In short the way you convert LISP to C is writing a LISP interpreter or compiler.

  6. Re:Why? Just why? by RightwingNutjob · · Score: 2

    Yes and no. Yes in that functional programming isn't nearly all it's cracked up to be by its evangelists. No in that there are some applications (mainly in things involving control systems, physics modelling, and a few other niche applications) where Lisp is a much more elegant (read: less spaghetti-prone) way of writing the code, and having a good way to make your academic Lisp usable in real software is a way to make your life easier if you happen to work in one of those domains.

  7. Re:I don't know about you by Anonymous Coward · · Score: 3, Funny

    Who's providing internet access to your grave?

  8. Kyoto Common Lisp by ooloorie · · Score: 2

    Kyoto Common Lisp and its descendants compile into C.

  9. Facebook by darkain · · Score: 2

    Facebook already did this with PHP. It was called HipHop. But it sucked. Too many issues, and it didnt solve enough issues. Luckily, Facebook started over from scratch, and just built of PHP virtual machine that does JIT compilation instead of needing to compile PHP into C then into machine language, and the result is actually a faster product, which supports more of the PHP language, and is a hell of a lot more stable (still not perfect though)

    So, why bother cross-compiling to another language? Just build a LISP JIT VM system. Solve all your problems! :D

    1. Re:Facebook by kruhft · · Score: 2

      Unfortunately the editors made a slight mistake with the description of this tool. It's not to 're-write your lisp code in C', it's to write new C code using Lisp as the macro system.

      HipHop was interesting in that it would compile PHP and make it run faster, but it's not similar to this in any way.

      sxc is a dialect of Lisp that has the semantics of C. There's not cross compilation from anything to anything; the syntax of sxc is that of C, but you're editing a representation of the parse tree. This is a list, and the meta-programming facilities allow you to generate code using Common Lisp, the most powerful language for generating that type of data structure; the list.

      The homoiconicity that this provides to C gives me suspect to say that it is a more powerful language overall than cc + cpp.

  10. Re:Not gonna do your homework by kruhft · · Score: 2

    It's not a senior project; I graduated 20 years ago[1]. It's a tool I've created recently from some ideas I've seen and had in hopes of realizing something that is more than the sum of it's parts.

    [1] Here's my senior project: http://github.com/burtonsamogr... ;-)

  11. Editing changed the question, unfortunately by kruhft · · Score: 2

    I'm glad this story got posted and sxc is getting some press, but the real question I asked was:

    It is unknown exactly what power might come about from this combination of low level processing with high level code generation. Can you think of any possible uses?

    The idea of using pre-processors to generate C code is not new to programming; but the usage of the full power of Common Lisp in such a natural way is up to the question.

    1. Re:Editing changed the question, unfortunately by K.+S.+Kyosuke · · Score: 2

      Can you think of any possible uses?

      Yes. Formulate a performance-sensitive problem (such as FFT, or linear algebra, or some other numerical problem, but even stream processing or other data reduction might be interesting areas) in a high-level form, then translate it non-deterministically (and perhaps also parametrically) using for example tree-pattern-matching replacement into a low-level form, say, in C (but low-level Lisp works, too). Do this a few million times and benchmark the results. You may have found a faster solution to your original problem than what you might be able to write on your own with a substantial time investment.

      --
      Ezekiel 23:20
  12. The slashdot of Christmas' Past... by johannesg · · Score: 2

    It's like the old slashdot rose from the grave one more time... Don't worry kids, tomorrow we'll be back to stories about windmills and mobile phones, I'm sure.

    Apparently there is a kind of magic in LISP that is not in other languages. I studied LISP for a while to see if I could get a sense of this magic, but failed miserably - it never clicked, there was never any sense of enlightenment. Is LISP elegant? Not in my eyes. It's a mess of brackets, weird syntax, and strange symbols.

    At the same time, the attempts to write software with "C-like words, but with LISP syntax" is almost endearing. "Look, now you can actually have a switch-statement with strings in it!" they proudly exclaim, failing to note that this new statement only exists in their personal version of C, rather than in standard C. Who exactly is the target market for this new type of switch statement? Neither the C community nor the LISP community is likely to want it, since neither side recognizes the syntax they are using in the first place.

    According to one of the links, the goal is this: "The syntax is more like C as this is more of a tool that is meant to bring C programmers into the Lisp world rather than pulling Lisp programmers into the C world." To that I say, "good luck with that". But hey, maybe they can get Linus on board and rewrite the entire kernel in LISP-in-C-like-form?