5 ms·
> A way to create a new language in just a few weeks As a Lisp programmer, I regularly create new languages in a matter of hours.
by lisper 10y ago
> A way to create a new language in just a few weeks
As a Lisp programmer, I regularly create new languages in a matter of hours.
- bostonpete 10y agoI'm vaguely familiar with Lisp, but I'm not a Lisp programmer. Can you explain this comment?
- nilved 10y agoLisp languages are easy to build because of their simple syntax. It's also easy to use a Lisp to build a Lisp, since it is homoiconic (this is what you're doing with the macro system.)
- stcredzero 10y agoIt's much the same in Smalltalk. Often, one could create a domain-specific superset of Smalltalk, complete with its own control structures.
- chrisseaton 10y agoDo you mean macros? Could you re-implement a language such as C as a macro? And have it be about as fast as GCC? I didn't think they were that sophisticated.
- ktRolster 10y agoCould you re-implement a language such as C as a macro? You can do that pretty quickly in IO language (admittedly, that's not Java)
- wcrichton 10y agoYou cannot. Because Lisp is homoiconic, you don't code in (what I personally believe to be) reasonable syntax, you code in abstract syntax trees. Lisp is so easy to metaprogram because everything is parentheses and the burden is on the user, not the parser, to determine the program's abstract syntactic structure. So, Lisp can only metaprogram its own syntax, you can't introduce a C-like syntax.
- lisper 10y ago> You cannot. You can. Reader macros let you put whatever syntax you want on top of Sexprs. For example: Welcome to Clozure Common Lisp Version 1.10-r16479M (DarwinX8664)! ? (spark-init) #P"/Users/ron/devel/spark/ergolib/init.lisp" ? (require :parcil) ... ? (in-readtable infix) |NIL| ? infix(x=1.23+4.56) 5.79 ? sin(x*x+1) 0.03341171 ? The code for this is here: https://github.com/rongarret/ergolib https://github.com/rongarret/ergolib
- wcrichton 10y agoFor sure, I was just considering normal macros.
- lisper 10y agoNo, you were just flat-out wrong. > Lisp can only metaprogram its own syntax, you can't introduce a C-like syntax. This is a flat-out false statement, and it reflects a deep but sadly common misunderstanding of how Lisp works and why it's cool. Even if Lisp did not have reader macros as a standard feature, you could still write a C compiler in Lisp more easily than you could write one in any other language. The whole concept of "metaprogramming a syntax" (so that you can talk about whether or not a language can "only metaprogram its own syntax") is non-sensical, a category error. Metaprogramming is simply writing programs that write programs. What makes Lisp cool is that it separates the syntax from the program. A Lisp program, unlike all other languages, is not text. A Lisp program is a data structure. Lisp happens to define a textual surface syntax (S-expressions) that allows you to easily convert text into the particular data structures that are Lisp programs (and also incidentally data structures that are not Lisp programs) but you can also produce these data structures in other ways, like, for example, writing programs that produce them. The textual surface syntax is a detail. Writing an infix parser in Lisp is an elementary exercise, and you could use that parser to parse C-like code whether or not you had reader macros. All reader macros let you do is seamlessly integrate that C-like syntax into the Lisp REPL rather than having to embed your new language in strings or read it from files.
- pnathan 10y agoYes, you could. You'd implement a program to take C code and emit an equivalent Lisp program (i.e., parse C and emit the tree into Lisp), then compile the Lisp program. The annotations and care the parser/semantic analysis code uses will provide the semantics of C. This will, however, not "play" like C without a great deal of work. You will be dragging an entire Lisp VM around and have to shake all of that out to actually produce something as lean as a C program at the end. You're probably best suited to take the Lisp compiler and build a "independant code" generator to generate binaries that don't rely on Lisp. But I'm waving my hands, you'd need to talk to the SBCL or CCL development teams there.
- chrisseaton 10y agoThat doesn't sound significantly easier than what we've done to implement C using Truffle. I'm not sure I buy this argument that what we've done could have been done trivially using Lisp macros.
- pnathan 10y agoHonestly, the front end would be about as hard, unquestionably! But the Lisp ecosystem is already in play, whereas you seem to have had to build that ecosystem. That is to say, it's not a major effort to scrape through the Lisp systems to understand what is going on, whereas your ecosystem with Graal etc required a great deal of work, it seems to be. Would that be correct? Or was it pretty plug and play after you had JVM bytecode?
- qwertyuiop924 10y agoWhy compile to lisp in the first place? Build a nanopass framework (https://www.google.com/url?sa=t&source=web&rct=j&url=http://andykeep.com/pubs/dissertation.pdf&ved=0ahUKEwj9m-WusIDOAhWLcD4KHb8BAnMQFggkMAI&usg=AFQjCNHE-7CTB5VqJ0uN4T9yFtpFD6KAhA&sig2=ZbV961-1dtoCBBDWJRx_bA https://www.google.com/url?sa=t&source=web&rct=j&url=http://...) that acts on sexprs, and then emit the thing to a target of your choice. The advantage is that Lisp is really good at transforming sexprs, and macros make writing the infrastructure for that sort of compiler just that much easier.
- ScottBurson 10y agoA Lisp macro is simply a function from an AST (in s-expression form) to an AST. Other than that, it's an ordinary function and has full access to the power of the language. Implementing C as a parser that builds s-expressions followed by a collection of macros to translate it into Lisp is certainly possible; I did it in 1984. I wouldn't say it was trivial, though; nor was the performance anything special (even then). I will definitely take a closer look at Graal and Truffle.
- pnathan 10y agoI was thinking that myself. If they had started with a modern Lisp implementation, they would be worlds ahead. :-/