C interpreter written in javascript

Loïc Février picture Loïc Février · May 26, 2011 · Viewed 9.7k times · Source

Is there any C interpreter written in javascript or java ?

I don't need a full interpreter but I need to be able to do a step by step execution of the program and being able to see the values of variables, the stack...all that in a web interface.

The idea is to help C beginners by showing them the step by step execution of the program. We are using GWT to build the interface so if something exists in Java we should be able to use it.

I can modify it to suit my needs but if I can avoid to write the parser / abstract-syntax tree walker / stack manipulation... that would be great.


Edit :

To be clear I don't want to simulate the complete C because some programs can be really tricky.

By step I mean a basic operation such as : expression evaluation, affectation, function call.

The C I want to simulate will contains : variables, for, while, functions, arrays, pointers, maths functions. No goto, string functions, ctypes.h, setjmp.h... (at least for now).

Here is a prototype : http://www.di.ens.fr/~fevrier/war/simu.html

In this example we have manually converted the C code to a javascript representation but it's limited (expressions such as a == 2 || a = 1 are not handled) and is limited to programs manually converted.

We have a our disposal a C compiler on a remote server so we can check if the code is correct (and doesn't have any undefined behavior). The parsing / AST construction can also be done remotely (so any language) but the AST walking needs to be in javascript in order to run on the client side.

Answer

ataylor picture ataylor · May 26, 2011

There's a C grammar available for antlr that you can use to generate a C parser in Java, and possibly JavaScript too.