6 ms·
PicoC: a very small C interpreter for scripting
- antirez 16y agoVery cool, there is even an interactive prompt. I wonder how hard is to make this interpreter "safe".
- rw- 16y agodefine "safe". :)
- antirez 16y agoRunning every kind of script will never corrupt the application memory :)
- deleted 16y ago[deleted]
- _delirium 16y agoWell, if it's interpreted, the interpreter could sandbox the C code it's running, rather than passing it through directly to its own memory space.
- arnorhs 16y agoThat would kind of defy the whole premise of this thing wouldn't it? The biggest and most sought after "feature" of C is it's speed and direct memory access (and simplicity, I guess). If you take that away, what's left?
- _delirium 16y agoDepends on the application, but I could see some mixture of: 1) familiarity; 2) simplicity; and 3) minimal memory usage. Browsing the source code briefly, it really does look like it's an interpreter, with C structs representing Variables, Expressions, and so on.
- unwind 16y agoOf course it is possible in an interpreter. You simply need to verify each access before allowing it, much like Valgrind does. Of course it's quite costly do to so, and since C gives the programmer a lot of freedom I guess it's hard to optimize the tests, i.e. to know which accesses are safe without explicitly keeping track.
- davidw 16y agoI wonder how it compares to other small systems like Lua, or, say Antirez' Jim interpreter, in terms of speed and features.
- antirez 16y agoHey David, what I think it's cool about picoC is that you can use it in places where you need to script at "hardware" level, like in robotics, embedded systems, and alike. Otherwise it is probably better to use Lua for instance, since I bet it is much faster (I just read part of the PicoC implementation and it is a pretty straightforward interpreter, very cool to read but hardly the faster) and safer.
- davidw 16y agoPresumably, it wouldn't be that hard to create some Lua functions to interface with memory addresses, I would think.
- antirez 16y agoYes but it's a pain to write complex code accessing memory when you need high level functions, can't simply cast pointers to C structures and alike.
- pietrofmaggi 16y agoLua can and is used in robotics, embedded system and alike. Take a look at: * pbLua, for Lego Mindstorm (http://www.hempeldesigngroup.com/lego/pblua/ http://www.hempeldesigngroup.com/lego/pblua/) * eLua for small MCU (http://www.eluaproject.net/ http://www.eluaproject.net/)
- nitrogen 16y agoWould this be allowed in an iPhone application, since it's all C?
- rw- 16y agoAFAIK no programs are allowed which interpret code.
- brownleej 16y agoActually, Apple allows interpreted code under certain circumstances. Link provides more details. I'm not sure how helpful it would be in this case, though. My understanding is that the main motivation for having scripted languages as a small part of an app, as this rule allows, is for game engines which have high-performance graphics stuff in C, and some of the application logic in a language like Lua. The use of a scripting language can reduce development time, but if you were using C, you might as well compile it. http://arstechnica.com/apple/news/2010/06/devs-cautiously-optimistic-about-ios-4-nonnative-code-change.ars http://arstechnica.com/apple/news/2010/06/devs-cautiously-op...
- eru 16y agoSo no postscript viewer ever? And no browsers? That's a sad state.
- teamonkey 16y agoBut in theory you could use it to script C in debug mode, then hard compile the script before you send it to Apple.
- igravious 16y agoThe links for these should read (code.google.com)
- shrikant 16y agoYep, time to add google.com to the "long domain" list . (See http://github.com/nex3/arc/blob/arc3.1/news.arc#L1556 http://github.com/nex3/arc/blob/arc3.1/news.arc#L1556)
- mahmud 16y agoI used Ch[1] in a commercial product and I would highly dis-recommend C as a scripting language. Something has to be said for high-level semantics, dynamism and syntax designed for interactive work. I find Lisp, Lua and Groovy to be joy to work with, but C is not an ideal extension/embedded/scripting language. Use something easier. http://en.wikipedia.org/wiki/Ch_interpreter http://en.wikipedia.org/wiki/Ch_interpreter
- imurray 16y agoSee also: http://bellard.org/tcc/ http://bellard.org/tcc/ a compiler, but small and #!tcc can be put at the top of .c files to make them scripts. Also see: http://root.cern.ch/drupal/content/cint http://root.cern.ch/drupal/content/cint — an interpretor, but large.
- Erwin 16y agoTCC has two even more cool things: 1) a library that lets you compile C code within a process, relocate it and execute it. This is rather faster than shelling out to run gcc to create a shared object and open it using dlopen. 2) an awesome 2004 demonstration where TCC was used as a boot loader. Take the Linux kernel source (slightly modified to remove some of the gccisms), add TCCBOOT and reboot the machine: as part of the boot process TCC recompiles the kernel and the continue to execute the freshly compiled kernel -- all in 15 seconds: http://bellard.org/tcc/tccboot.html http://bellard.org/tcc/tccboot.html
- coryrc 16y agoHow would you like to have a REPL for kernel development, like on the Lisp Machines? (Yes, that's a rhetorical question!) Take TCC with your point 1 above. Mix it with the ideas from Schemix ( http://abstractnonsense.com/schemix/ http://abstractnonsense.com/schemix/ ). Now you can iteratively development a kernel driver! I hacked on Schemix a while back, updating it to 2.6 module system (unfortunately lost the source code before it got uploaded). It's a seemingly great idea but what good is scheme code if you have to rewrite in a different language for the final version?
- mahmud 16y agoI really love Fabrice, but "TCC" will, forever, remain Turbo C, not Tiny C. DOS weenie pride!
- dugmartin 16y agoCtrl+F9 ftw.
- 16y ago
- jacquesm 16y agoHow refreshing to see something that only has 'readline' as a dependency and otherwise just requires a working C compiler and 'make'.
- jrockway 16y agoYeah. Why reuse something when you can write it yourself!
- jacquesm 16y agoSure, but still. It's rare to see a program that does not require at least another 10 fold its own size in super structure. And the fact that it is targeted to embedded use means that in its destined environment there is none of that stuff available anyway.
- RiderOfGiraffes 16y agoI was wondering if you really meant that, given that this is intended to be useful in embedded applications. To try to get a feel for the sort of programming you do and the sort of opinions you hold, I checked your profile - Bang - there's the answer right there: > I'm not trolling: > I actually think that. Is it really the case that you don't see the point in writing a minimal, small, clean, self-contained system that has no external dependences and is directly applicable and useful for embedded systems.
- jrockway 16y agoI don't find it refreshing to see people writing the same code over and over and over and over again. Does it have a point? Yes. Does it refresh me? No. (Libraries and dependencies don't necessarily mean "dynamic relinking at runtime". It can mean "link that standard code from ../lets-not-write-a-hashtable-again/hashtable.o".)
- euroclydon 16y agoJust curious: Is it trolling, if I write out a strong opinion, that I don't necessarily believe, but that sounds reasonable to me, on a topic I'm trying to figure out what to believe, and then, when my sentiments get refuted, I say to myself, "Okay, now there's a reasonable way to think."
- jakevoytko 16y agoLanguage interpreters are great for experimenting with new or buggy pieces of code - just experiment at the prompt and copy what works into a file for posterity. I've started looking through the parsing code for PicoC, and its all nice and readable. But I'm surprised this was used in an actual system, embedded or not. C is an absolutely miserable language whose saving graces are executable size and speed. It lacks reasonable features to use for scripting, like code-as-data in Bash or the dynamism of Python and Ruby. There are even embedded scripting languages like LUA. What does C offer as a scripting language, except perhaps familiarity?
- listic 16y agoAs you suggested, it offers the possibility to switch from interpreted to compiled code at no extra effort.
- deleted 16y ago[deleted]
- sgt 16y agoPersonally I would use Forth for this. PicoC looks interesting but if I were to weigh my options (i.e. PicoC vs Forth in his example app UAV-onboard system, I would definitely go Forth).
- listic 16y agoCare to explain your reasoning behind this? As I understand, the main appeal of using C as a scripting language is the fact that you use the same language in compiled and interpreted part of your application. Drawbacks of using C interpreter for scripting could include large footprint and external dependencies, but as far as I can understand, they are not the case here. So what is your reasoning for Forth?
- sgt 16y agoIn the case of embedded programming, Forth is often made for the job. C is great for embedded programming too, and you can always mix the two languages. C isn't made for scripting, and hacks like PicoC don't appeal to me since it will always remain a hack. While not exactly a scripting language per se, Forth does have amazing interactive capabilities. It's extremely light-weight and it's easy to implement a new Forth on a new hardware architecture. Footprint issues aside, development time in Forth is usually shorter, and the ability to connect directly to a device's Forth "shell" and try out Forth words (functions), create new words, play around with hardware in real-time is invaluable. Forth code is less readable if you don't know Forth, and occasionally Forth programmers write ugly code, but that is definitely the case for embedded C programmers too.