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 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?
I remember when I was young... (Score:1)
... and had time to waste on idle thoughts.
Butterflies (Score:3, Funny)
Re:Butterflies (Score:5, Funny)
Re: (Score:2)
Dunno why this was modded troll. At worst it's not very funny, but it is somewhat relevant.
I learned LISP once. I think I used emacs at the time too.
My first and only thought upon seeing this headline was "Why would you do that?"
But I've been told over and over again that LISP is a great language and you can....do things in it....artificially intelligent things even. LISP was kind of considered cool when I was in college, but I was instead swept up by the practicality and usefulness of C - and I switched
Re: (Score:2)
the practicality and usefulness of C
Such as the lack of generic arithmetics and type checking of heterogeneous data or the presence or pervasive manual memory management and error handling? ;)
Re: (Score:1)
You... say that like it's a bad thing?
Re: (Score:2)
Re: Butterflies (Score:5, Funny)
No, that would be this one:
https://xkcd.com/224/ [xkcd.com]
I wouldn't (Score:2, Insightful)
What kind of dumb article is this? Stop living in the past Grandpa.
Re: (Score:2, Insightful)
Stop chasing the fad of the moment and learn to actually hone a mature skill set, son.
Re: (Score:3)
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.
Re: (Score:3)
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
Re: (Score:2)
Interestingly, Javascript's heritage is from Self, which descends from Smalltalk, and dispenses with classes.
Re: (Score:2)
Re: (Score:2)
If you go back far enough you have a choice of Lisp or COBOL for inspiration.
Re: (Score:2)
Re: (Score:2)
As someone who learned to program at MIT in the early 80s, Javascript feels a lot like Scheme -- a dialect of Lisp.
That's because it was initially supposed to be Scheme, before some fashion-conscious suits ordered Eich to turn it into something that looks like Java.
Re: (Score:2)
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.
Fortunately, the statement in question included "I" instead of "one", otherwise Lisp would be doomed!
hmm (Score:2)
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
Re: (Score:2)
Re: (Score:2)
What the hell does this have to do with Emacs? This is about Common Lisp, a real language.... not the crippled Emacs Lisp.
My favorite (Score:1)
Excellent (Score:5, Funny)
(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))
Re:Excellent (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."
Re: Excellent (Score:2)
Yeah, but being able to do a switch on strings is pretty cool, is it not?
Re: (Score:2)
Re: (Score:1)
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.
Re: (Score:2)
Sounds like the kind of non-portable hacks C is infamous for.
Re: (Score:2)
I wouldn't say that C in infamous for it.
I would. The mess around pointers, types, and casting, and what is portable and what isn't, along with clever C hacks abounds.
You can certainly do things like that but even the most experienced programmer knows that it is an ugly unportable hack.
Which is why somebody recommended it and it got upmodded.
As for portability, properly written C is actually portable unlike most high level languages out there.
Well, sure, if you believe in the myth that we just need to have more education, hire better C coders, or strive to do better, C is a good choice for a "portable" language. But in reality C has inherent design flaws that make it easy to write non-portable code.
If you want to write programs that have actual portability there aren't that many alternatives to C.
That's because C has inertia and everybody ports it to their platfor
Re: (Score:2)
Unlike in some other languages, there's no guarantee in C that two identical strings must be the same object,
I don't think that's true in any language.
Re: (Score:2)
A lot of C code will fail in low-memory situations. If the program doesn't test every return from malloc() or calloc(), it will potentially do operations on the null pointer. As long as you stay out of embedded software, a low memory situation is probably from the process using all available memory, and the question is not whether it will fail but when.
Limited work (Score:2)
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
Re: (Score:1)
I don't know about you (Score:2)
But, upon reading the summary, I shuddered - I felt a chill as if Satan himself had walked across my grave...
Re: (Score:3, Funny)
Who's providing internet access to your grave?
Re: (Score:2)
Yeah another IoT security breach.
Code as structured data, and pre-parsed source (Score:2)
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
Re: (Score:2)
Have you looked at rust and d?
Re: (Score:2)
Interesting idea and I like where it could go. Thank you.
Re: (Score:2)
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.
Re: (Score:2)
You're pretty much describing ici [wikipedia.org]
Re: (Score:2)
Unfortunately, the editors messed up a bit with the explanation of what sxc is and what it is good for. It's not about compiling your Lisp code to C, it's about writing C code using Lisp. With the S-Expression syntax of sxc, a whole world of macro programming and code generation become possible in a natural matter leading from the using of S-Expression and macros. This, so far, has been uncharted territory, although his type of project is quite popular to implement. It seems that nobody has actually use
Re: (Score:2)
The performance of C or any low level language is up to the ability of the programmer and the deadlines placed on them, or the fact they may get board or wiring that one section and say good enough. The reason why most software sucks and every developer thinks they can do x, y or z better is probably because they can with some time. However most people don't have the time to just sit there and fix every part until it reaches optimal because they could waste their life away on a piece of code that may not
Use unparser (Score:2)
There is only one answer (Score:2)
With great trepidation and a bottle of aquavit, and the obvious subconscious yearning for discipline ultimately resulting in years of medication augmented therapy.
Re: (Score:2)
The beverage or the lab chemical (though one can be both but not the other)?
How? (Score:4, Funny)
Weeping and at gunpoint. ;)
I wouldn't (Score:2)
I wouldn't, because I'm not a masochist. Next question.
Lisp to C (Score:4, 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... [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.
Re: (Score:2)
Re: (Score:2)
Re: (Score:2)
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?
Re: (Score:2)
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.
Re: (Score:3)
Re: (Score:2)
Are you implying that functional approach doesn't play well with vectorized code? If so, the APL family of languages would like a word with you.
In fact, Lisp's compiler macros are probably the place where you'd want to plug in a custom vectorized code generator "for any vector units available".
Re: (Score:2)
Re: (Score:2)
Re: (Score:2)
C ... (Score:2)
Kyoto Common Lisp (Score:3)
Kyoto Common Lisp [wikipedia.org] and its descendants compile into C.
MRD Required. (Score:2)
Can you describe the business requirement behind the ask please?
Back to the future (Score:2)
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
Re: (Score:2)
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
Facebook (Score:3)
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
Re: (Score:3)
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
Re: (Score:3)
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... [github.com] ;-)
Editing changed the question, unfortunately (Score:3)
I'm glad this story got posted and sxc is getting some press, but the real question I asked was:
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.
Re: (Score:3)
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
Re: (Score:2)
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.
Re: (Score:2)
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
Re: (Score:2)
The slashdot of Christmas' Past... (Score:3)
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?
C + LISP -> Pliant (Score:1)
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
Re:C + LISP - Pliant (Score:2)
Easy (Score:2)