Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Show HN: Cpi – Immediately compile and execute c++ program like a scripting lang (treefrogframework.github.io)
65 points by kazzn on Feb 26, 2017 | hide | past | favorite | 14 comments


This is great! Nice to see another project in the history of C/C++ interpreters including:

- cling: https://root.cern.ch/cling

- cint: http://www.hanno.jp/gotom/Cint.html

- Ch: https://www.softintegration.com/

If people know of other ones, I'd love to hear about them in the comments!


From what I remember from watching Terry's videos he also did something like this in TempleOS with his compiler HolyC. His shell is apperently just his compiler running in an interactive mode.

HolyC is "kind of" C from a compiler perspective so I think it counts.


TCC: http://www.bellard.org/tcc/

It's not an interpreter but can be used as one, because it compiles so quickly.



Self-plug: https://github.com/RhysU/c99sh

A shebang-friendly script for "interpreting" single C99, C11, and C++ files, including rcfile support.

No interpreted mode.


- SquiLu: https://github.com/mingodad/squilu

A scripting language that accepts a subset of javascript and C/C++


This is a task on Rosetta:

https://rosettacode.org/wiki/Native_shebang

Also:

https://rosettacode.org/wiki/Multiline_shebang

I contributed the C one here.


Rather than using shebang notation for running C source files, which as noted leads to code which isn't legal input to a normal C compiler, there is a standard trick to please both the shell and the C compiler:

    #if 0
    /bin/env cpi "$0"
    exit
    #endif
And you don't need this cpi program depending on your tolerance for hacks:

    #if 0
    out=/tmp/$RANDOM
    gcc "$0" -o $out
    $out
    rm $out
    exit
    #endif


This only works if invoked by the shell, I believe. If you exec that file directly it will fail unless I'm mistaken (can't test at the moment).


Yeah, I should have mentioned that. Whereas shebang notation is understood by the executable loader, not the shell.


Use exec. Save an exit.


I do something very similar to this, but just using cmake and basic bash scripts to launch the compilation and then run the executable all at once. And for the interpreter mode, a bash script can just automatically dump that into a main function, and then compile and run it all at once.

It's cool, but you can set something up like this just a few lines of bash code.


The page claims that CPI is an interpreter, but as this post says it is not. It just uses a regular compiler to build the input, then fiddled with the file descriptors to hook up the new executable's I/O to cpi's existing fds.


If nothing else, the interactive mode is going to be a life saver for me.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: