How can I write a JavaScript compiler for Arduino or similar microcontroller? [closed]

烂漫一生 提交于 2021-02-07 03:57:41

问题


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.


回答1:


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.




回答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.

C and asm are the most common languages for these platforms that is why those languages dominate.

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.

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.

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

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.

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.

By far your shortest path is to just learn C, it is a very easy programming language.

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.

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.



来源:https://stackoverflow.com/questions/23764967/how-can-i-write-a-javascript-compiler-for-arduino-or-similar-microcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!