An interpreter for C
I recently thought about writing an interpreter for C, probably in C. Do you know of any similar projects?
My idea was that the interpreter would be able to perfectly simulate the C standard, notice any undefined/implementation-defined behavior and simulate different compilers. This would allow simple debugging/visualization. You can also easily track memory, recognize memory leaks, recognize out of bounds error, etc...
Posted Jun 24, 2020, 4:27 PM UTC
I had a go at it once, and (IIRC) got to where I could run very simple programs, but I found it hard to see the point of it so dropped it.
Tricky issues I found included calling external binaries. In particular the C runtime in MSVCRT.DLL. If C source for the runtime functions can be found, that could be interpreted too. But the wider problem of calling existing binary code remains.
An associated one, but more tricky, was callback functions, where you pass a function pointer to an external library, then it calls that function. Now, binary code can't directly call interpreted code; you need to arrange something in between.
But then, out of the 1000s function pointers used in a program, how many will be called solely from interpreted code, and how many from outside?
You get similar problems with any interpreted language, but there at least there is a clear line between the interpreted language, and that of the external library.
C interpreters exist, so either they are limited in some way, or a lot of work has gone into solving such problems.