用javascript编写的C解释器

发布于 2024-11-09 18:50:16 字数 1802 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

被你宠の有点坏 2024-11-16 18:50:17

有一个C 语法可用于antlr,您可以使用它在 Java 中生成 C 解析器,也可能是 JavaScript。

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

空袭的梦i 2024-11-16 18:50:17

felixhJSCPP 项目 提供了 Javascript 中的 C++ 解释器,但有一些限制。

https://github.com/felixhao28/JSCPP

因此示例程序可能如下所示

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);

: 2015 年 3 月,该项目正在积极开发中,因此虽然可用,但仍有一些领域可以继续扩展。检查文档以了解限制。看起来您现在可以将它用作直接的 C 解释器,但库支持有限,不会再出现任何问题。

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.

一世旳自豪 2024-11-16 18:50:17

em-scripten 可以将 LLVM 语言转换为 JS,稍微修改一下,你就可以生成一个 C 解释器。

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.

英雄似剑 2024-11-16 18:50:17

我不知道有任何用 JavaScript 编写的 C 解释器,但这里讨论了可用的 C 解释器:

有 C 语言的解释器吗?

你可能会更好地寻找在 JavaScript 之上运行的任何类型的虚拟机,然后看看是否可以找到一个 C 编译器来为 C 语言发出正确的机器代码虚拟机。 LLVM 是一个可能的选择;如果你能找到一个可以运行 LLVM 的 JavaScript VM,那么你就处于最佳状态。

我在 Google 上搜索了一下,发现了 Emscripten,它可以将 C 代码直接翻译成 JavaScript!也许你可以用这个做点什么:

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

也许您可以修改 Emscripten 以在 C 的每个编译行之后发出一个“序列点”,然后您可以使您的模拟环境从一个序列点到另一个序列点单步执行。

我相信 Emscripten 正在实现 LLVM,所以它实际上可能有虚拟寄存器;如果是这样,它可能非常适合您的目的。

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.

滥情空心 2024-11-16 18:50:17

我知道您指定了 C 代码,但您可能需要考虑更简单语言的 JavaScript 模拟。请特别考虑 FORTH。

FORTH 运行在一个极其简单的虚拟机上。 FORTH 有两个堆栈,一个数据堆栈和一个控制流堆栈(称为“返回”堆栈);加上一些全局内存。 FORTH 最初是一种 16 位语言,但现在有很多 32 位 FORTH 实现。

因为 FORTH 代码有点“接近机器”,所以当您看到它工作时,很容易理解它是如何工作的。在学习 C 之前我学习了 FORTH,我发现这是一次宝贵的学习经历。

JavaScript 中已经有几种可用的 FORTH 解释器。 FORTH虚拟机就这么简单,实现起来并不需要很长时间!

您甚至可以得到一个 C 到 FORTH 的翻译器,让学生观看 FORTH 虚拟机解释编译的 C 代码。

我认为这个答案对你来说不太可能,所以我将停止在这里写。如果您确实对这个想法感兴趣,请在下面发表评论并询问更多详细信息,我将很乐意与您分享。我已经很久没有编写 FORTH 代码了,但我仍然深深地记得它,并且我很乐意更多地谈论 FORTH。

编辑:尽管这个答案被否决为负分,但我将把它留在这里。如果模拟简单且易于理解,那么用于教育目的的模拟恕我直言更有价值。 FORTH 的简单的基于堆栈的虚拟机非常简单,但您可以编译 C 代码在其上运行。 (在 80 年代,甚至有一种 CPU 芯片将 FORTH 指令作为其本机机器代码。)而且,正如我所说,当我还是一个完全的初学者时,我亲自学习了 FORTH,它帮助我理解了汇编语言和

C。这个问题现在还没有被接受的答案,距离提出已经两年多了。可能是 Loïc Février 没有找到任何合适的 JavaScript 解释器。正如我所说,FORTH 虚拟机已经存在多个 JavaScript 解释器。因此,这个答案是一个实用的答案。

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.

没︽人懂的悲伤 2024-11-16 18:50:17

C是编译型语言,而不是解释型语言,并且具有JS完全不支持的指针之类的功能,因此在Javascript中解释C并没有真正的意义

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

忆依然 2024-11-16 18:50:17

是的,有一个(实际上是用 TypeScript 编写的,但可以跳过类型): https://github .com/Mati365/ts-c-compiler

将 C 代码编译为 x86 16 位可启动二进制文件

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文