4 ms·
Is that even possible? Practically, not theoretically.
by sillysaurusx 6y ago
Is that even possible? Practically, not theoretically.
- shakna 6y agoWell, there's this [0] Scheme->C compiler, so if you were to port Cling [1] (C++ interpreter) to it, then you'd have achieved that goal pretty well. It'd be a large project, but practical to achieve. [0] https://github.com/sph-mn/sph-sc https://github.com/sph-mn/sph-sc [1] https://github.com/root-project/cling https://github.com/root-project/cling
- bjoli 6y agoOf course it is. A friend of mine wrote a C99 interpreter in python in high school. He had the wonderful idea of not implementing any undefined behaviour that wasn't strictly necessary :) we used it to test our C code later on in the course. It probably won't be very fast though. There are parts of C++ that doesn't map very well to common lisp, and which means some parts will have more overhead than necessary.
- sillysaurusx 6y agoC99, sure. C++?
- bjoli 6y agoOf course it is doable. Nontrivial of course. C++ is probably not the language I would start with.
- MaxBarraclough 6y agoVery impressive for a high-schooler. > He had the wonderful idea of not implementing any undefined behaviour that wasn't strictly necessary What does this mean? It sounds like the way an ordinary C compiler works: it ignores the possibility of undefined behaviour occurring, as the C standard allows it to do this.
- bjoli 6y agoI just remember that it crashed if you tried to modify a string literal, which IIRC is undefined. And that it was very diligent about scope. If you had signed integer over/underflow it wrote insulting error messages.
- postalrat 6y agoProbably not. Not in any language. If it was it would have been done a dozen times.
- swiftcoder 6y agoIt has been done several times, at least. http://www.hanno.jp/gotom/Cint.html http://www.hanno.jp/gotom/Cint.html https://github.com/root-project/cling https://github.com/root-project/cling https://www.softintegration.com https://www.softintegration.com You can argue whether some of those are strictly interpreters, versus just a REPL hooked up to a compiler (as in the case of Cling). But they do exist.
- jjtheblunt 6y agoit seems you could dlopen (etc) a compiled-in-a-repl code snippet, but it's seemingly not often thought of. not sure why. (I've done it, but just prefer straight compiling, as in "i am the repl".) p.s. here's the most concise two refs i found by googling. https://www.quora.com/What-is-the-purpose-of-dlopen-dlload-in-C-programming-in-Linux-environment https://www.quora.com/What-is-the-purpose-of-dlopen-dlload-i... https://man7.org/linux/man-pages/man3/dlopen.3.html https://man7.org/linux/man-pages/man3/dlopen.3.html
- rottc0dd 6y agoNot a complete transpiler, butI read about an interview about JVM implementation done on common lisp of Pascal Costanza and ability to port all things then supported in Java to lisp[0]. "...I decided to implement a Java Virtual Machine in Common Lisp - under normal circumstances, I wouldn't have dared to do this, because this is quite a complex undertaking, but I had read in several places that Lisp would be suitable for projects that you would normally not dare to do otherwise, so I thought I would give it a try. Over the course of 8 weeks, with something like 2 hours per day, or so (because I was still doing other stuff during the day), I was able to get a first prototype that would execute a simple "Hello, World!" program. On top of that, it was a portable (!) just-in-time compiler: It loaded the bytecode from a classfile, translated it into s-expressions that resemble the bytecodes, and then just called Common Lisp's compile function to compile those s-expressions, relying on macro and function definitions for realizing these "bytecodes as s-expressions." I was really impressed that this was all so easy to do. The real moment of revelation was this: to make sure to reuse as many of the built-in Common Lisp features as possible, I actually translated Java classes into CLOS classes, and Java methods into CLOS methods. Java's super calls posed a problem, because it was not straightforward to handle super calls with plain call-next-method calls. Then I discovered user-defined method combinations, which seemed like the right way to solve this issue, but I was still stuck for a while. Until I discovered that moving a backquote and a corresponding unquote around actually finally fixed everything. That was a true Eureka moment: In every other programming language that I am aware of, all the problems I encountered until that stage would have required a dozen redesigns, and several attempts to start the implementation completely from scratch, until I would have found the right way to get everything in the right places." [0] - lisp-univ-etc.blogspot.com/2012/04/lisp-hackers-pascal-costanza.html