C interpreter written in javascript [closed]
Asked Answered
F

7

34

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.

Falito answered 26/5, 2011 at 16:51 Comment(17)
You could have a look at this JavaScript PC Emuluator which also runs some form of Linux and has a C compiler (but maybe it is a bit too much out of scope).Sid
@Felix: That's interesting but rather separate from what OP wanted. @Loïc: C does not really execute in "steps" or at least it's not required to. The closest thing would be sequence points. I only mention this because learning C as if it executed in "high level asm" steps according to the source is a major source of misunderstanding for beginnings in C.Earing
@Felix: That's about 50 levels of abstractions off. The JS code emulates some x86 hardware, a linux kernel runs atop of it and runs various executables including tcc which was written in C but is run in the form of x86 opcodes. TCC compiles C programs to x86 executables pretty much directly and that x86 code is again run by the stack mentioned above. Good luck using that from Javascript!Jujube
@all: yeah... for just inspecting the values and the stack it is really not suited... my apologies.Sid
@Felix: not what I was looking for but interresting @R..: by step I mean a basic operation such as affectation, evaluation of expression, function call. I want to follow the execution line by line.Diantha
C REPLs in general are pretty rare and rarely reliable for the reasons @R. detailed. You'll notice that no C programmers ever use one to bang out spike solutions — there is a reason this doesn't happen despite C being the most popular language in the world. C is just very poorly suited to this sort of treatment.Symposiarch
@Chuck: I can perfectly imagine some horrible C code where REPL are not reliable, but with simples affectations, int/float/double, for, while, functions, arrays I think we can do somethingDiantha
@chuck : I've updated my post and I'm showing the current prototypeDiantha
C is a very small language. Your requirement asking for "not the whole of C" doesn't really cut you any slack. I don't think this is trivially doable.Marney
@Noufal Ibrahim: I mean by that that I don't need to emulates all the system calls, no forking... Math functions will be sufficient.Diantha
Forking is not C but POSIX. C itself is very simple. The only things that could be left out to make it simpler seem to be in the area you need: math.Earing
@Chuck: You statement may be overly broad. We particle physics types use a c++ interpreter to bang out trial solutions on a regular basis. For details look at ROOT which uses cint as a REPL. Cint includes full support for ANSI c.Straddle
@R: Alright. Adding functions (ctypes.h, string.h...) takes only times but it not that difficult. However they can be left out as a first step.Diantha
I don't think you understand my point. ctypes.h, string.h etc. are not part of C. They're header files and external libraries. If you have a C interpreter implemented in JAvascript, there's no reason why it cannot compile and run those programs that use those libraries. C proper is about 2 dozen keywords, a few operators and a few types. You'll need these even for the simplest of programs and even the most complex ones will need exactly these. Saying that you don't need "string.h" doesn't make the problem any simpler.Marney
The lex/parse part of a c compiler is available already done: ANSI C Yacc grammar and ANSI C grammar, Lex specification. Problem is, that's the easy half.Straddle
@Noufal Ibrahim: I understand that, as I said adding functions only takes time but is not difficult. The basic syntax/keywords is what I'm interested in.Diantha
@dmckee yes, building the AST is not the difficult part (can be done remotely as I said), but the AST walking / stack simulation is what's more difficult. I'm just trying not to start from scratch.Diantha
W
8

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

Weal answered 26/5, 2011 at 17:18 Comment(2)
link to C grammar is brokenNumerary
@Weal You could replace the link with a wayback machine link: web.archive.org/web/20120614203958/http://www.antlr.org/grammar/…Hotel
C
5

felixh's JSCPP project provides a C++ interpreter in Javascript, though with some limitations.

https://github.com/felixhao28/JSCPP

So an example program might look like this:

var JSCPP = require('JSCPP');
var launcher = JSCPP.launcher;
var code = 'int main(){int a;cin>>a;cout<<a;return 0;}';
var input = '4321';
var exitcode = launcher.run(code, input);
console.info('program exited with code ' + exitcode);

As of March 2015 this is under active development, so while it's usable there are still areas where it may continue to expand. Check the documentation for limitations. It looks like you can use it as a straight C interpreter with limited library support for now with no further issues.

Carabao answered 3/4, 2015 at 14:13 Comment(2)
Unfortunately that interpreter fails already on printf("foo")Faggoting
printf needs #include <cstdio> to work .. here is an online version felixhao28.github.io/JSCPPLuciano
S
4

There is em-scripten which converts LLVM languages to JS a little hacking on it and you may be able to produce a C interperter.

Shem answered 3/7, 2012 at 3:7 Comment(1)
picoc is a C interpreter that is written in C. It might be possible to compile it to JavaScript using Emscripten.Aquila
B
2

I don't know of any C interpreters written in JavaScript, but here is a discussion of available C interpreters:

Is there an interpreter for C?

You might do better to look for any sort of virtual machine that runs on top of JavaScript, and then see if you can find a C compiler that emits the proper machine code for the VM. A likely one would seem to be LLVM; if you can find a JavaScript VM that can run LLVM, then you will be in great shape.

I did a few Google searches and found Emscripten, which translates C code into JavaScript directly! Perhaps you can do something with this:

https://github.com/kripken/emscripten/wiki

Perhaps you can modify Emscripten to emit a "sequence point" after each compiled line of C, and then you can make your simulated environment single-step from sequence point to sequence point.

I believe Emscripten is implementing LLVM, so it may actually have virtual registers; if so it might be ideal for your purposes.

Brede answered 3/7, 2012 at 3:27 Comment(0)
B
2

I know you specified C code, but you might want to consider a JavaScript emulation of a simpler language. In particular, please consider FORTH.

FORTH runs on an extremely simple virtual machine. In FORTH there are two stacks, a data stack and a flow-of-control stack (called the "return" stack); plus some global memory. Originally FORTH was a 16-bit language but there are plenty of 32-bit FORTH implementations out there now.

Because FORTH code is sort of "close to the machine" it is easy to understand how it all works when you see it working. I learned FORTH before I learned C, and I found it to be a valuable learning experience.

There are several FORTH interpreters available in JavaScript already. The FORTH virtual machine is so simple, it doesn't take very long to implement it!

You could even then get a C-to-FORTH translator and let the students watch the FORTH virtual machine interpret compiled C code.

I consider this answer a long shot for you, so I'll stop writing here. If you are in fact interested in the idea, comment below and ask for more details and I will be happy to share them. It's been a long time since I wrote any FORTH code but I still remember it fondly, and I'd be happy to talk more about FORTH.

EDIT: Despite this answer being downvoted to a negative score, I am going to leave it up here. A simulation for educational purposes is IMHO more valuable if the simulation is simple and easy to understand. The simple stack-based virtual machine for FORTH is very simple, yet you could compile C code to run on it. (In the 80's there was even a CPU chip made that had FORTH instructions as its native machine code.) And, as I said, I personally studied FORTH when I was a complete beginner and it helped me to understand assembly language and C.

The question has no accepted answer, now over two years after it was asked. It could be that Loïc Février didn't find any suitable JavaScript interpreter. As I said, there already exist several JavaScript interpreters for the FORTH virtual machine. Therefore, this answer is a practical one.

Brede answered 4/7, 2012 at 1:26 Comment(0)
R
0

C is a compiled language, not an interpreted language, and has features like pointers which JS completely doesn't support, so interpreting C in Javascript doesn't really make sense

Rockwood answered 11/2, 2021 at 3:1 Comment(1)
Sorry, but such binary answers don't really make sense: there exists C interpreters! See above.Therein
B
0

Yes, there is one (written on TypeScript actually but types can be skipped): https://github.com/Mati365/ts-c-compiler

it compiles C code to x86 16bit bootable binary

Bagger answered 18/3, 2023 at 17:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.