3

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.

Matt Cashatt
  • 23,490
  • 28
  • 78
  • 111
  • http://blog.safaribooksonline.com/2013/07/16/javascript-powered-arduino-with-johnny-five/ – aglasser May 20 '14 at 16:12
  • @aglasser--Thanks. I am already aware of that link, but it is discussing an interpreting situation where the Arduino must be tethered to a PC. The JavaScript is not actually compiled and run on the device. – Matt Cashatt May 20 '14 at 16:15
  • @T.J.Crowder--Fair enough and good point. I suppose that is why I am asking the question to begin with. – Matt Cashatt May 20 '14 at 16:17
  • @MatthewPatrickCashatt, have you considered solutions like `V8`? http://code.google.com/p/v8/ – Mahonri Moriancumer May 20 '14 at 16:31
  • @MahonriMoriancumer--Thanks, but I am not looking to run a JavaScript interpreter on a microcontroller. 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. – Matt Cashatt May 20 '14 at 16:54
  • Why the downvote? The question was put on hold--is that not enough? – Matt Cashatt May 21 '14 at 15:09
  • It occurs to me there is one other alternative I didn't put into my answer: Have you looked into the the Netduino? The Netduino mini (by Secret Labs) is roughly the size of the Arduino micro, and runs the .NET micro framework. I don't recall if it supports any of the DLR scripting languages, but it might be an option. – JockM May 21 '14 at 19:12
  • To me, your statement comes across as in "JavaScript seems like a good candidate to me [only] because [I am very familiar with it]". JavaScript is a common Web language but apart from that no applications are written in it. C/C++ and Java are candidates for "real" application's languages, Python maybe. Besides, JavaScript follows some paradigms that don't translate well to resource-constrained environments at all, dynamic typing for instance. – JimmyB May 23 '14 at 11:37
  • I have to disagree with the powers that be that this question is too broad. The author is asking a very specific question, it is the commenters who seem to miss the core of what Cashatt is asking and giving too broad of an answer. I am even guilty of that to some degree though I was trying to use the other options to put my answer in context. – JockM May 24 '14 at 23:28

2 Answers2

11

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.

JockM
  • 209
  • 1
  • 8
  • Instead of JavaScript to C++ he could also look into JavaScript to LLVM compiler (I think JXCore is working on that). However I'm not sure if Arduino has an LLVM compiler. – Adam Gent May 20 '14 at 16:39
  • Thanks, I will get that book. One thing that confuses me about your post; however, is the assertion that the hardware would have a hard time running code that was originally written in JavaScript. Once compiled, wouldn't it be the exact same machine language that would result from C? That is what I am after: the same machine code, just written originally with JavaScript rather than C. – Matt Cashatt May 20 '14 at 16:39
  • @AdamGent There is a version of LLVM for AVR (http://sourceforge.net/projects/avr-llvm/) though I have no idea how good it's code generation is. JXCore is a heavyweight implementation (IE normal desktop), thus you would have to do a significant amount of work to get it to work in a low memory 16Mhz environment – JockM May 20 '14 at 16:48
  • @MatthewPatrickCashatt I don't think you are taking into account just how heavyweight Javascript is. All the standard JavaScript classes, the fact that it is a dynamic language means that every operator, and use of a variable means that it needs to be checked at run time, and converted (if possible). Unless you implement static typing or limit the language to integer math (which will take extra work), every line of code will generate a lot more than the equivalent C++ Honestly were it me, I would just get the Espruino board if you want to work in JavaScript. They already did the work... – JockM May 20 '14 at 16:53
  • Ok, I see what you mean now. I will be getting the Espruino, but I am concerned about the limited availability of it. It appears to only come in a single format whereas there are dozens of Arduino configurations. For example, some of my smaller aircraft only have room for Arduino Micros which have a width of about 1.77cm. – Matt Cashatt May 20 '14 at 17:03
  • @MatthewPatrickCashatt Well you could try porting to the Teensy 3.1, it is faster, has more ram, and is smaller (IIRC) than the Arduino Micro. But that will take a bit of work on your side. – JockM May 20 '14 at 17:08
  • I tried to edit this answer, but StackOverflow insisted that my change should be at least 6 characters. Typo: DUO => DUE – Rick May 21 '14 at 12:56
  • @Rick my bad, fixing that now... – JockM May 21 '14 at 19:08
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.

old_timer
  • 69,149
  • 8
  • 89
  • 168
  • Thank you for this answer, your points are well made. – Matt Cashatt May 21 '14 at 15:08
  • 1
    Cant tell you how many times I have wanted to invent something so that I dont have to learn a new language. Not that it will never work out but so far it has not... – old_timer May 21 '14 at 15:13