I realize that this question will raise some eyebrows, and I realize that JavaScript is traditionally an interpreted language, please let me explain:
我意识到这个问题会引起一些人的质疑,我意识到JavaScript是一种传统的解释性语言,请允许我解释一下:
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.
我是专门研究web应用程序的软件工程师。网络堆栈)。作为一个爱好,我喜欢创建基于arduino的组件的RC无人机。
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.
我还想用Arduino做一些其他的事情,但是坦率地说,C / c++并不是我最擅长的语言,我不想把有限的业余时间花在阅读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.
我和许多其他人都想到,如果与Arduino /嵌入式生态系统交互的语言是一种更常见的语言,那么Arduino /嵌入式生态系统将会更加丰富。在我看来,JavaScript是一个很好的候选者,因为大多数软件开发人员都知道它,在JavaScript世界中,构建开源框架和插件的文化非常强大。
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.
所以,回到我的第一个问题:如果我想要用JavaScript编写和编译我的Arduino代码,我该如何开始呢?当然,我正在构想一个开源项目,但我需要一些帮助来获得吸引力。我从来没有写过编译器,所以任何帮助都是值得赞赏的。
2 个解决方案
#1
9
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.
这是一个很好的问题,Arduino UNO的微控制器,LEO, etc是ATmega328p,它有32K的flash用于程序存储,2K内存,2K的EEPROM(用于持久存储)。对于像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.
现在有人为ATmega128编写了一个Javascript编译器,您将在Arduino Mega中找到它,它有4K的RAM和更多的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.
如果你移动到Arduino到期,Arduino 0,或者Teensy 3。x——所有这些都是基于ARM的——然后您可以查看Espruino,它是ARM的JavaScript版本,但是您仍然需要将它移植到Arduino硬件上。
So if what you really want is an embedded board that can run JavaScript then I would just look at the Esprino board itself.
如果你真正想要的是一个可以运行JavaScript的嵌入式板,那么我只需要看看Esprino板本身。
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.
最后,如果您还在为ATmega328p设置JavaScript,那么您应该考虑为JavaScript语言的一个子集编写一个JavaScript到c++转换器。这样做的范围远远超出了SO回复的范围,因此我建议从著名的《龙之书》开始,因为它可能仍然是学习如何编写编译器的最佳资源。
#2
2
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.
我和许多其他人都想到,如果与Arduino /嵌入式生态系统交互的语言是一种更常见的语言,那么Arduino /嵌入式生态系统将会更加丰富。在我看来,JavaScript是一个很好的候选者,因为大多数软件开发人员都知道它,在JavaScript世界中,构建开源框架和插件的文化非常强大。
C and asm are the most common languages for these platforms that is why those languages dominate.
C和asm是这些平台最常见的语言,这也是这些语言占主导地位的原因。
Because those are the languages of choice, you are basically going to have to build your compiler to generate one or the other, which means you need to be strong in one or the other. Basically to complete this task you need to do what you dont want to do.
因为这些是选择的语言,你基本上需要构建你的编译器来生成一个或另一个,这意味着你需要在其中一个或另一个强。要完成这个任务,你需要做你不想做的事情。
I don't want to spend limited spare time reading books on C.
我不想把有限的业余时间花在阅读有关C的书籍上。
This is a major undertaking, worthy of a team of folks so if you dont have spare time then just write your programs in C. The Arduino environment does so much handholding for you that it is quite trivial.
这是一项重要的任务,如果你没有空闲时间,就把你的程序写在c语言里。Arduino的环境对你来说是非常重要的。
Instead, I am looking only to be able to write the code in JavaScript, and then compile it to the same machine language it would receive if it were written in C originally
相反,我只希望能够编写JavaScript中的代码,然后将其编译为它将收到的相同的机器语言,如果它最初是用C编写的。
That is unlikely. getting machine code that performs the same task is how it works but the same machine code as some other language via some other compiler, only works for very simple programs, add two numbers return a result, that kind of thing. The same C code on various compilers does not return the same machine code, so there is no expectation for different languages on different compilers to return the same code.
这是不可能的。获得执行相同任务的机器代码是如何工作的,但是通过其他编译器获得与其他语言相同的机器代码,只对非常简单的程序有效,添加两个数字返回结果,诸如此类。不同编译器上相同的C代码不会返回相同的机器码,因此对于不同的编译器,不同的语言没有期望返回相同的代码。
There are a myriad of web pages and books out there on compilers. The typical approach is to use flex/bison or antlr to generate the parser. that means learning yet another programming language, what you need to feed the parser. At least for lex/yacc or flex/bison the output is a very brute force program (which you could have just written yourself, but is very tedious) that does the parsing and then generates whatever you want it to so you can turn that add two numbers into two allocates an add and return result in some form of pseudocode. Then you have to somehow turn that into machine code (easiest is to output assembly then assemble it). Optimizing, etc is a huge part of it being successful on such a resource limited platform, that is not just a research project but takes years of experience to get halfway decent. Ideally you want to output some language that has a compiler for that platform that optimizes, so that means C, which means you have to get strong in C if you want to pull this off in a shorter amount of time or with less effort.
在编译器上有无数的网页和书籍。典型的方法是使用flex/bison或antlr生成解析器。这意味着要学习另一种编程语言,即您需要提供给解析器的内容。至少对于lex和yacc或flex /野牛的输出是一个非常强力的程序(你可以自己写的,但是非常乏味)的解析,然后生成任何你想要的,这样你就可以把两个数字添加到两个分配添加和返回导致某种形式的伪代码。然后,您必须以某种方式将其转换为机器码(最简单的方法是输出汇编然后进行组装)。优化等等是在这样一个资源有限的平台上取得成功的一个重要部分,这不仅仅是一个研究项目,还需要多年的经验才能做到一半好。理想情况下,你想要输出一种有编译器的语言,这意味着C,这意味着如果你想要在更短的时间内或更少的努力下完成它,你必须在C语言中增强。
By far your shortest path is to just learn C, it is a very easy programming language.
到目前为止,你的最短路径就是学习C语言,这是一种非常简单的编程语言。
As someone mentioned, LLVM is not a bad way to go in some respects, it is a non-trivial research project to add a language, but what you get is the LLVM backend for the targets that LLVM supports (which the avr is not one if I remember correctly), ARM and MIPS are so you could use your llvm base tool to generate code for microcontrollers other than those used on the arduino. Granted there are some arm based boards with arduino shield connectors that can be arduino like. Now this requires some strength in C++ to pull off unfortunately. but is probably one of the better paths to making a compiler for a language not already supported by another compiler for that target. gcc has hooks too but gcc is far more messy inside than llvm, both are getting more messy over time, but one uses more duct tape and bailing wire than the other at the moment. C is going to be easier to learn than C++ and you will find far more embedded support for C, actually you will find far more support for C everywhere not just embedded.
有人说过,LLVM是个不错的路要走在某些方面,这是一个重要的研究项目添加一个语言,但是你得到的是LLVM的目标后端LLVM支持(avr的不是一个如果我没记错的话),手臂和MIPS所以你可以使用LLVM基础工具来生成代码微控制器用于arduino以外。当然,也有一些基于arm的板,带有arduino屏蔽连接器,可以像arduino一样。现在,这需要c++的一些力量来实现。但这可能是为一种尚未被另一种编译器支持的语言创建编译器的较好的途径之一。gcc也有钩子,但是gcc比llvm要复杂得多,随着时间的推移,两者都变得越来越乱,但是现在有一个使用了更多的管道胶带和捞砂线。C语言比c++更容易学习,而且你会发现C语言的支持度要高得多,实际上,你会发现,C语言在任何地方都能得到更多的支持,而不仅仅是嵌入。
Short answer, google, find some free classes on line, and/or a myriad of webpages showing compiler basics. Look for lex/yacc or flex/bison or antlr to assist with the messy text parsing (or just do it yourself directly), but you still have to do a fair amount of work to make a usable compiler. Writing a compiler for a language is a significantly larger task than just learning a language where there is an existing compiler. So if the goal is to avoid learning a language, this solution wont work, if the goal is to try to attract a new audience who like you doesnt want to learn a new language, well you have to learn one a few languages, so that the others dont have to (if you succeed), the end result hopefully being a tool you actually want to use yourself.
简短的回答,谷歌,在网上找到一些免费的类,和/或无数显示编译器基础的网页。寻找lex/yacc或flex/bison或antlr来帮助处理杂乱的文本解析(或者直接自己做),但是您仍然需要做大量的工作来创建一个可用的编译器。为一种语言编写编译器比只学习一种现有编译器的语言要重要得多。如果我们的目标是避免学习一门语言,这个解决方案不会工作,如果目标是试图吸引新观众喜欢你不想学习一门新的语言,你必须学习一些语言,以便其他人没有(如果你成功),希望最后的结果是你实际上想要使用自己的工具。
#1
9
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.
这是一个很好的问题,Arduino UNO的微控制器,LEO, etc是ATmega328p,它有32K的flash用于程序存储,2K内存,2K的EEPROM(用于持久存储)。对于像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.
现在有人为ATmega128编写了一个Javascript编译器,您将在Arduino Mega中找到它,它有4K的RAM和更多的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.
如果你移动到Arduino到期,Arduino 0,或者Teensy 3。x——所有这些都是基于ARM的——然后您可以查看Espruino,它是ARM的JavaScript版本,但是您仍然需要将它移植到Arduino硬件上。
So if what you really want is an embedded board that can run JavaScript then I would just look at the Esprino board itself.
如果你真正想要的是一个可以运行JavaScript的嵌入式板,那么我只需要看看Esprino板本身。
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.
最后,如果您还在为ATmega328p设置JavaScript,那么您应该考虑为JavaScript语言的一个子集编写一个JavaScript到c++转换器。这样做的范围远远超出了SO回复的范围,因此我建议从著名的《龙之书》开始,因为它可能仍然是学习如何编写编译器的最佳资源。
#2
2
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.
我和许多其他人都想到,如果与Arduino /嵌入式生态系统交互的语言是一种更常见的语言,那么Arduino /嵌入式生态系统将会更加丰富。在我看来,JavaScript是一个很好的候选者,因为大多数软件开发人员都知道它,在JavaScript世界中,构建开源框架和插件的文化非常强大。
C and asm are the most common languages for these platforms that is why those languages dominate.
C和asm是这些平台最常见的语言,这也是这些语言占主导地位的原因。
Because those are the languages of choice, you are basically going to have to build your compiler to generate one or the other, which means you need to be strong in one or the other. Basically to complete this task you need to do what you dont want to do.
因为这些是选择的语言,你基本上需要构建你的编译器来生成一个或另一个,这意味着你需要在其中一个或另一个强。要完成这个任务,你需要做你不想做的事情。
I don't want to spend limited spare time reading books on C.
我不想把有限的业余时间花在阅读有关C的书籍上。
This is a major undertaking, worthy of a team of folks so if you dont have spare time then just write your programs in C. The Arduino environment does so much handholding for you that it is quite trivial.
这是一项重要的任务,如果你没有空闲时间,就把你的程序写在c语言里。Arduino的环境对你来说是非常重要的。
Instead, I am looking only to be able to write the code in JavaScript, and then compile it to the same machine language it would receive if it were written in C originally
相反,我只希望能够编写JavaScript中的代码,然后将其编译为它将收到的相同的机器语言,如果它最初是用C编写的。
That is unlikely. getting machine code that performs the same task is how it works but the same machine code as some other language via some other compiler, only works for very simple programs, add two numbers return a result, that kind of thing. The same C code on various compilers does not return the same machine code, so there is no expectation for different languages on different compilers to return the same code.
这是不可能的。获得执行相同任务的机器代码是如何工作的,但是通过其他编译器获得与其他语言相同的机器代码,只对非常简单的程序有效,添加两个数字返回结果,诸如此类。不同编译器上相同的C代码不会返回相同的机器码,因此对于不同的编译器,不同的语言没有期望返回相同的代码。
There are a myriad of web pages and books out there on compilers. The typical approach is to use flex/bison or antlr to generate the parser. that means learning yet another programming language, what you need to feed the parser. At least for lex/yacc or flex/bison the output is a very brute force program (which you could have just written yourself, but is very tedious) that does the parsing and then generates whatever you want it to so you can turn that add two numbers into two allocates an add and return result in some form of pseudocode. Then you have to somehow turn that into machine code (easiest is to output assembly then assemble it). Optimizing, etc is a huge part of it being successful on such a resource limited platform, that is not just a research project but takes years of experience to get halfway decent. Ideally you want to output some language that has a compiler for that platform that optimizes, so that means C, which means you have to get strong in C if you want to pull this off in a shorter amount of time or with less effort.
在编译器上有无数的网页和书籍。典型的方法是使用flex/bison或antlr生成解析器。这意味着要学习另一种编程语言,即您需要提供给解析器的内容。至少对于lex和yacc或flex /野牛的输出是一个非常强力的程序(你可以自己写的,但是非常乏味)的解析,然后生成任何你想要的,这样你就可以把两个数字添加到两个分配添加和返回导致某种形式的伪代码。然后,您必须以某种方式将其转换为机器码(最简单的方法是输出汇编然后进行组装)。优化等等是在这样一个资源有限的平台上取得成功的一个重要部分,这不仅仅是一个研究项目,还需要多年的经验才能做到一半好。理想情况下,你想要输出一种有编译器的语言,这意味着C,这意味着如果你想要在更短的时间内或更少的努力下完成它,你必须在C语言中增强。
By far your shortest path is to just learn C, it is a very easy programming language.
到目前为止,你的最短路径就是学习C语言,这是一种非常简单的编程语言。
As someone mentioned, LLVM is not a bad way to go in some respects, it is a non-trivial research project to add a language, but what you get is the LLVM backend for the targets that LLVM supports (which the avr is not one if I remember correctly), ARM and MIPS are so you could use your llvm base tool to generate code for microcontrollers other than those used on the arduino. Granted there are some arm based boards with arduino shield connectors that can be arduino like. Now this requires some strength in C++ to pull off unfortunately. but is probably one of the better paths to making a compiler for a language not already supported by another compiler for that target. gcc has hooks too but gcc is far more messy inside than llvm, both are getting more messy over time, but one uses more duct tape and bailing wire than the other at the moment. C is going to be easier to learn than C++ and you will find far more embedded support for C, actually you will find far more support for C everywhere not just embedded.
有人说过,LLVM是个不错的路要走在某些方面,这是一个重要的研究项目添加一个语言,但是你得到的是LLVM的目标后端LLVM支持(avr的不是一个如果我没记错的话),手臂和MIPS所以你可以使用LLVM基础工具来生成代码微控制器用于arduino以外。当然,也有一些基于arm的板,带有arduino屏蔽连接器,可以像arduino一样。现在,这需要c++的一些力量来实现。但这可能是为一种尚未被另一种编译器支持的语言创建编译器的较好的途径之一。gcc也有钩子,但是gcc比llvm要复杂得多,随着时间的推移,两者都变得越来越乱,但是现在有一个使用了更多的管道胶带和捞砂线。C语言比c++更容易学习,而且你会发现C语言的支持度要高得多,实际上,你会发现,C语言在任何地方都能得到更多的支持,而不仅仅是嵌入。
Short answer, google, find some free classes on line, and/or a myriad of webpages showing compiler basics. Look for lex/yacc or flex/bison or antlr to assist with the messy text parsing (or just do it yourself directly), but you still have to do a fair amount of work to make a usable compiler. Writing a compiler for a language is a significantly larger task than just learning a language where there is an existing compiler. So if the goal is to avoid learning a language, this solution wont work, if the goal is to try to attract a new audience who like you doesnt want to learn a new language, well you have to learn one a few languages, so that the others dont have to (if you succeed), the end result hopefully being a tool you actually want to use yourself.
简短的回答,谷歌,在网上找到一些免费的类,和/或无数显示编译器基础的网页。寻找lex/yacc或flex/bison或antlr来帮助处理杂乱的文本解析(或者直接自己做),但是您仍然需要做大量的工作来创建一个可用的编译器。为一种语言编写编译器比只学习一种现有编译器的语言要重要得多。如果我们的目标是避免学习一门语言,这个解决方案不会工作,如果目标是试图吸引新观众喜欢你不想学习一门新的语言,你必须学习一些语言,以便其他人没有(如果你成功),希望最后的结果是你实际上想要使用自己的工具。