Follow Slashdot blog updates by subscribing to our blog RSS feed

 



Forgot your password?
typodupeerror
×
Programming

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

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?
This discussion has been archived. No new comments can be posted.

How Would You Generate C Code Using Common Lisp Macros?

Comments Filter:
  • by Anonymous Coward

    ... and had time to waste on idle thoughts.

  • Butterflies (Score:3, Funny)

    by rsilvergun ( 571051 ) on Saturday December 24, 2016 @05:11PM (#53549431)
    Oblig XKCD [xkcd.com]
  • I wouldn't (Score:2, Insightful)

    by Anonymous Coward

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

    • Re: (Score:2, Insightful)

      by Anonymous Coward

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

  • Sounds like an emacs junky trying to validate why his editor is the be all end all. Can atom.io or vi create c automatically? Well mine can

    • Theoretically, I can see it as a nice way to interact with C libraries.
    • by ogdenk ( 712300 )

      What the hell does this have to do with Emacs? This is about Common Lisp, a real language.... not the crippled Emacs Lisp.

  • Still my favorite syntax for programming and I believe will always be. I mostly use Clojure at the moment but definitely wanted something just like this to work closer to C.
  • Excellent (Score:5, Funny)

    by phantomfive ( 622387 ) on Saturday December 24, 2016 @05:33PM (#53549501) Journal
    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))
    • by dgatwood ( 11270 ) on Saturday December 24, 2016 @06:29PM (#53549673) Homepage Journal

      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."

    • Yeah, but being able to do a switch on strings is pretty cool, is it not?

      • QuickBasic wants its argument back.
      • by Anonymous Coward

        Not really that cool, it's been possible to do that since the 70s. Hint: a string constant is just a pointer to a unique memory location. You can cast the first two bytes as an integer, then the switch works on strings for all intents and purposes.

        • by Raenex ( 947668 )

          Sounds like the kind of non-portable hacks C is infamous for.

        • which only works as long as :

          - all of the strings are going to be at least longer than the integer type you're using
          (there is no 3-chars long string, when you cast them to uint32 for this hack)
          and/or you use proper padding everywhere.

          - the integer type you're using is long enough for the different string to be easily distinguishable by this point
          (i.e.: you're not having all of your string beginning with "keep..." when you're restricted to uint32)

          (and note that both previous point are more or less going in o

    • It looks ambiguous.
  • But, upon reading the summary, I shuddered - I felt a chill as if Satan himself had walked across my grave...

  • It is not the best illustration of an idea that's been running around my head for ages.

    In terms of what traditional textual C can represent, with the exception of C macros, one can construct a suitable data structure. Now consider how parsed C could then be programmatically manipulated, and produced from other source formats than the usual. And so on.

    Thinking more in a pythonic syntax, I have wondered about replacing textual source with a scripting language, so that, with something like C++, we could write

    n

    • Have you looked at rust and d?

    • by kruhft ( 323362 )

      newclass = cpp.Class()
      meth = newclass.addMethod(name="dosomething")
      meth.signature = cpp.Signature([cpp.int,cpp.float])

      Interesting idea and I like where it could go. Thank you.

    • newclass = cpp.Class()
      meth = newclass.addMethod(name="dosomething")
      meth.signature = cpp.Signature([cpp.int,cpp.float])

      Welcome to 1976. This is exactly how Smalltalk works. Unlike later class-based languages, it's a purely imperative system: you create a new class by sending a subclass: message to a class, with the name of the new class as the argument. The return value is the new subclass. You then add instance variables and methods to the class in a similar way.

    • by rkww ( 675767 )

      I have wondered about replacing textual source with a scripting language

      You're pretty much describing ici [wikipedia.org]

  • I would let LISP print out a s-expression representing the abstract syntax tree (usually called abstract parse tree). Next I would add a small piece of code to my IParse program [iwriteiam.nl] to read s-expression and use the unparse abilities with the C grammar to produce nicely formatted C code. The unparse function is smart enough to output a S-expression like '(times (add 3 4) 5) into (3+4)*5 based on the given grammar rules fed to IParse. Feel free to contact me, if you want any help with using IParse.
  • With great trepidation and a bottle of aquavit, and the obvious subconscious yearning for discipline ultimately resulting in years of medication augmented therapy.

  • How? (Score:4, Funny)

    by Gravis Zero ( 934156 ) on Saturday December 24, 2016 @06:16PM (#53549619)

    Weeping and at gunpoint. ;)

  • I wouldn't, because I'm not a masochist. Next question.

  • Lisp to C (Score:4, Interesting)

    by jbolden ( 176878 ) on Saturday December 24, 2016 @06:35PM (#53549691) Homepage

    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... [buildyourownlisp.com]
    http://howtowriteaprogram.blog... [blogspot.com]
    http://sbcl.sourceforge.net/ [sourceforge.net]
    http://clisp.sourceforge.net/ [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 [norvig.com]
    And finally also off topic but the original LISP in assembly: http://www.softwarepreservatio... [softwarepreservation.org]

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

    • If one is interested primarily in code generation, there's a very nice formatter for Scheme [synthcode.com] (think printf on steroids) written by Alex Shinn that has extensions for generating C-ish output.
    • by tgv ( 254536 )

      True. I wrote toy one 20 years ago with the assumption you wouldn't make circular data structures and it was pretty fast. You couldn't do things like (set (f ...) (g ...)) either, of course. But I couldn't and can't find good use for Lisp-to-C. There are good compilers out there, so why bother going via C?

      • by jbolden ( 176878 )

        I think the way you generally handle something like this is through void pointers. Part of the data structure becomes a void function pointer. That's perfectly legal in C. You just have to be careful that when you set out to run a step in the evaluation you have all your types in line.

  • ... arefully.

  • by ooloorie ( 4394035 ) on Saturday December 24, 2016 @07:58PM (#53549923)

    Kyoto Common Lisp [wikipedia.org] and its descendants compile into C.

  • Can you describe the business requirement behind the ask please?

  • We had a genuine Lisp->C transpiler (we just called it a converter) way, way back when (in the late 1980's, probably before a lot of you were born and maybe even in some cases before your parents were born) at Thinking Machines Corp. The system software for the Connection Machine CM-1/CM-2 (which was effectively a gigantic coprocessor) was originally written in Lisp for the Lisp Machine (Symbolics 36xx IIRC), but eventually we figured out that we needed it on *ahem* more conventional platforms. Rather

    • by kruhft ( 323362 )

      Thank you for your anecdote, I appreciate the comparison with on of my favourite supercomputer companies! sxc is not necessarily a 'Lisp to C' converter, but a C -> C converter with the optional metaprogramming facilities to generate code using Common Lisp. The syntax of the sxc language is Lisp like; the semantics are C. So really this is a C -> C translator with added meta facilities.

      The code that goes in and the code that comes out is strictly C. What you can do with the added ability of Common

  • by darkain ( 749283 ) on Saturday December 24, 2016 @10:12PM (#53550277) Homepage

    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

    • by kruhft ( 323362 )

      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 li

  • by kruhft ( 323362 ) on Saturday December 24, 2016 @11:47PM (#53550493) Homepage Journal

    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.

    • 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 origin

      • by kruhft ( 323362 )

        Thank you for the interesting response. Could you explain a bit more about what you mean by "using for example tree-pattern-matching replacement into a low-level form". What would you be replacing with what?

        My goto performance sensitive problem has always been raytracing, so I think that's the next logical project to implement with this tool and see where it goes.

        • For example, (map f (map g sequence)) might or might not be measurably faster than (map (lambda (x) (f (g x))) sequence). You won't know until you try. So this particular subtree of operations is a candidate for replacement. The process is rather generic and could be adapted to a wide range of problems: find possible transformations for the current tree, pick one of them, repeat until you hit a terminating condition. Do this non-deterministically so that you ultimately try as many diverse results as possibl

    • Btw shouldn't this article be in the "Ask slashdot" category since it comes from a reader, and not from some news..
  • by johannesg ( 664142 ) on Sunday December 25, 2016 @05:05AM (#53551085)

    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?

  • The state of the art for code generation is not Lisp but Pliant.
    Basically the main difficulty with code generation is not to make it powerful, but have it generate meaningful error messages rather than crazy code when some bad source is submitted. Through the definition of 'Expression' and 'Instruction' notions, Pliant defined a clean way to encode a program at various stages, and the rules for the code generator. S-expresions plus syntaxical substitution with a Turing machine equivalent substitution engine

    • I'm not sure that there's a limitation on the usefulness of error messages in a custom-written transformer. It can be as bad or as good as you like it. Lisp provides the hooks into the compiler, but you provide the code. The description on that page also very much sounds like what an application of OMeta could do.
  • With a program!

It is easier to write an incorrect program than understand a correct one.

Working...