evan_tech

Previous Entry Share Next Entry
05:10 pm, 10 Mar 06

C REPL

I've often wondered if you could construct a read-eval-print loop for C (or a language atop C that wanted to use C libraries). I think the hard part would be handling error conditions. One way of looking at it (as Dan put it) is that the C language spec allows you to crash whenever something unexpected occurs, while most languages have defined semantics for all inputs. (This isn't a slam on C -- leaving lots of things unspecified allows for a faster implementation. For example, array indexing can be done without bounds checking because indexing off the bounds of an array is one of those undefined conditions.)

It just occurred to me, though, that you could fork() before running each line of code. Then if that code caused a crash, you'd still have the old process state around, while if it didn't crash you could kill the old process. The only problem is for code that involved I/O, you wouldn't get a second chance... hmm. I suppose the same is true for a "normal" REPL anyway.

[Edit: I later got around to implementing this. See the C REPL tag.]