A simple and Super Bare JavaScript runtime using the small elk runtime for microcontrollers, using Nim.
I wanted to dip into some C programming and decided to connect a small C library to Nim. So I started by creating a c binding for the elk using nimterop fiddling around with the settings. Until something worked. Finally it worked and I slapped on a simple cli interface with docopt. This might not be the best js runtime as it misses a lot of features and thus writing js is a pain. So this project can be helpful to some people as it lays down the framework for embedding the elk runtime in Nim.
Make sure to have Nim and Git installed.
# Get repo
git clone https://github.com/imagineeeinc/nimelk.git
# Git submoulde elk
git submodule init
git submoudle update
# Install nimterop
nimble install nimterop -y
# Generate Nim bindings of elk
nim c -r elk_wrraper.nim
# Install deps
nimble install
# Test out
nimble run -- run tests/index.jsYou have no input method. You can output using the print function. e.g.:
let a = 1;
let b = 2;
let c = a + b;
print(c);
a+b >= c ? true : false;this should output true if a+b is greater than c. (which always is going to be).
Here are some of the features (and unsupported features) from the elk readme:
- Supported
- Operations: all standard JS operations except:
!=,==. Use strict comparison!==,===- No computed member access
a[b]- No exponentiation operation
a ** b- Typeof:
typeof('a') === 'string'- For loop:
for (...;...;...) ...- Conditional:
if (...) ... else ...- Ternary operator
a ? b : c- Simple types:
let a, b, c = 12.3, d = 'a', e = null, f = true, g = false;- Functions:
let f = function(x, y) { return x + y; };- Objects:
let obj = {f: function(x) { return x * 2}}; obj.f(3);- Every statement must end with a semicolon
;- Strings are binary data chunks, not Unicode strings:
'Київ'.length === 8- Not supported
- No
var, noconst. Uselet(strict mode only)- No
do,switch,while. Usefor- No
=>functions. Uselet f = function(...) {...};- No arrays, closures, prototypes,
this,new,delete- No standard library: no
Date,Regexp,Function,String,Number