如何为 Arduino 或类似的微控制器编写 JavaScript 编译器? [英] How can I write a JavaScript compiler for Arduino or similar microcontroller?

查看:28
本文介绍了如何为 Arduino 或类似的微控制器编写 JavaScript 编译器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我意识到这个问题会引起一些人的注意,而且我意识到 JavaScript 传统上是一种解释性语言,请让我解释一下:

我是专门研究 Web 应用程序(特别是 .NET 堆栈)的软件工程师.作为一种爱好,我喜欢创建在基于 Arduino 的组件上运行的 RC UAV.>

我还想用 Arduino 做其他几件事,但坦率地说,C/C++ 不是我最强的语言,我不想花有限的业余时间阅读有关 C 的书籍.

我已经想到——我相信还有很多其他人——如果用来与之交互的语言是一种更常见的语言,Arduino/嵌入式生态系统将会更加丰富.JavaScript 对我来说似乎是一个不错的候选人,因为大多数软件开发人员都知道它,并且在 JavaScript 世界中构建开源框架和插件的文化非常强大.

那么,回到我的第一个问题:如果我想用 JavaScript 为我的 Arduino 编写和编译代码,我应该如何开始?我当然正在设想一个开源项目,但我需要一些帮助来吸引注意力.我从未编写过编译器,因此非常感谢您的帮助.

解决方案

这真是一个问题,Arduino UNO、LEO 等中的微控制器是 ATmega328p,它有 32K 的闪存用于程序存储,2K 的 RAM,和 2K 的 EEPROM(用于持久存储).这对于像 Javascript 这样的语言来说是相当紧张的.

现在有人为 ATmega128 编写了一个 Javascript 编译器,您可以在Arduino Mega,它有 4K 的 RAM 和更多的闪存.

如果您升级到 Arduino DUE、Arduino Zero 或 Teensy 3.x——所有这些都是基于 ARM 的——那么您可以查看 Espruino 这是 ARM 的 JavaScript 版本,但您仍然需要将其移植到 Arduino 硬件.

因此,如果您真正想要的是一个可以运行 JavaScript 的嵌入式开发板,那么我只会看看 Esprino 开发板本身.

最后,如果您仍然为 ATmega328p 设置 JavaScript,那么您应该考虑为 JavaScript 语言的一个子集编写一个 JavaScript 到 C++ 的翻译器.这样做的范围远远超出了 SO 回复,所以我建议从著名的 龙之书 因为它仍然可能是学习如何编写编译器的最佳资源.

I realize that this question will raise some eyebrows, and I realize that JavaScript is traditionally an interpreted language, please let me explain:

I am software engineer specializing in web applications (.NET stack specifically). As a hobby, I enjoy creating RC UAVs that run on Arduino-based components.

There are several other things I would like to do with Arduino as well but, frankly, C / C++ is not my strongest language and I don't want to spend limited spare time reading books on C.

It has occurred to me--and many others I am sure--that the Arduino / embedded ecosystem would be far richer if the language used to interface with it were a more common one. JavaScript seems like a good candidate to me because most software developers know it and the culture of building open source frameworks and plugins is very strong in the JavaScript world.

So, back to my first question: If I wanted to be able to write and compile code for my Arduino in JavaScript, how would I get started? I am envisioning an open-source project of course, but I need some help getting traction. I have never written a compiler, so any help is appreciated.

解决方案

This is quite an ask, the microcontroller in the Arduino UNO, LEO, etc is the ATmega328p, which has 32K of flash for program storage, 2K of RAM, and 2K of EEPROM (for persistent storage). That is quite tight for a language like Javascript.

Now someone has written a Javascript compiler for the ATmega128, which you will find in the Arduino Mega, which has 4K of RAM and much more flash.

If you move up to the Arduino DUE, the Arduino Zero, or the Teensy 3.x — all of which are ARM based — then you can look into Espruino which is a version of JavaScript for ARM, but you will still have to port it to the Arduino hardware.

So if what you really want is an embedded board that can run JavaScript then I would just look at the Esprino board itself.

Finally if you are still set on JavaScript for the ATmega328p, then you should look into writing a JavaScript to C++ translator for a subset of the JavaScript language. The scope of doing this is well outside of a SO reply, so I would suggest starting with the famous Dragon Book as it is still probably the best resource for learning how to write compilers.

这篇关于如何为 Arduino 或类似的微控制器编写 JavaScript 编译器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆