Transpiles lisp code represented as array to JS code
$ npm install --save lisp-array-to-js
or, to install it globally:
$ npm install -g lisp-array-to-js
var lispArrayToJs = require("lisp-array-to-js");
console.log(lispArrayToJs(["do", ["def", "console", ["js", "console"]], [".", "console", ["`", "log"], 42]]));
console.log();
console.log(lispArrayToJs.transpile(["do", ["def", "console", ["js", "console"]], [".", "console", ["`", "log"], 42]]));
console.log();
console.log(lispArrayToJs.exec(["do", ["def", "console", ["js", "console"]], [".", "console", ["`", "log"], 42]]));
should output:
(function(env) {
return (function() {
env["console"] = console;
return env["console"]["log"](42);
})();
})(Object.create(null));
(function() {
env["console"] = console;
return env["console"]["log"](42);
})()
42
undefined
Assuming the file test.json with the following content:
["do", ["def", "console", ["js", "console"]], [".", "console", ["`", "log"], 42]]
lisp-array-to-js test.json
lisp-array-to-js -o test.js test.json
lisp-array-to-js -e test.json
should output:
(function(env) {
return (function() {
env["console"] = console;
return env["console"]["log"](42);
})();
})(Object.create(null));
42
and should write into test.js:
(function(env) {
return (function() {
env["console"] = console;
return env["console"]["log"](42);
})();
})(Object.create(null));
Gets or sets a property.
[".-", "document", ["`", "body"]]
[".-", "obj", ["`", "prop"], "newValue"]
compiles to:
env["document"]["body"]
env["obj"]["prop"] = env["newValue"]
Calls a method.
[".", "document", ["`", "querySelector"], ["`", "body"]]
compiles to:
env["document"]["querySelector"]("body")
Returns the first argument.
["`", "querySelector"]
["`", ["example1", "example2"]]
compile to:
"querySelector"
["example1", "example2"]
Checks for strict equality.
["=", "a", "b"]
compiles to:
env["a"] === env["b"]
Defines a variable.
["def", "var", "val"]
compiles to:
env["var"] = env["val"]
Executes multiple functions and returns the last.
["do", ["def", "var", ["`", "val"]], ["+", "var", ["`", "val2"]]]
compiles to:
(function() {
env["var"] = "val";
return env["var"] + "val2";
})()
A function expression.
["fn", ["arg1", "&", "arg 2"], [".", "env", ["`", "arg1"], "arg 2"]]
compiles to:
(function() {
var __args = arguments;
return (function(env) {
env["arg1"] = __args[0];
env["arg 2"] = __args[2];
return env["env"]["arg1"](env["arg 2"]);
})(Object.create(env));
})
Note: the __args and the IIFE are required because variable names can contain symbols which would be invalid in JavaScript, such as whitespaces.
Returns the second argument if the first argument is true, the third argument otherwise.
["if", ["=", "var1", "var2"], ["`", "isTrue"], ["`", "isFalse"]]
compiles to:
((env["var1"] === env["var2"])
? "isTrue"
: "isFalse")
Imports a module.
["import", ["`", "lisp-array-to-js"]]
compiles to:
require("lisp-array-to-js")
Note: this currently only supports CommonJS
Exports a module.
["export", ["`", "test"]]
["export", ["`", "exec"], ["`", "whatever"]]
compiles to:
module.exports = "test"
module.exports["exec"] = "whatever"
Note: this currently only supports CommonJS
Executes javascript code.
["js", "document.body.appendChild(new Text('test'))"]
compiles to:
document.body.appendChild(new Text('test'))
Creates a new lexical scope, defines variables in it and then executes code in it.
["let", ["a", 5, "b", 10], ["+", "a", "b"]]
compiles to:
(function(env) {
env["a"] = 5;
env["b"] = 10;
return env["a"] + env["b"];
})(Object.create(env))
All of these are simply converted to infix notation.
For example, <
:
["<", 1, 2]
compiles to:
1 < 2
A try/catch block. Note that the second argument must be a call to catch.
["try", "test", ["catch", "e", ["list", ["`", "Error: "], "e"]]]
compiles to:
try {
env["test"]
} catch(__e) {
(function(env) {
env["e"] = env["__e"];
return env["list"]("Error: ", env["e"]);
})(Object.create(env))
}
With replaces the current scope.
["with", ["js", "window"], [".", "document", ["`", "querySelector"], ["`", "head"]]]
compiles to:
(function(env) {
return env["document"]["querySelector"]("head");
})(window)