0.4.1 • Published 9 years ago

lisp-json-to-js v0.4.1

Weekly downloads
-
License
GPL-3.0
Repository
github
Last release
9 years ago

lisp-json-to-js

Transpiles lisp code represented as object to JS-code

Install

$ npm install --save lisp-json-to-js

or, to install it globally:

$ npm install -g lisp-json-to-js

Usage

io.js / node.js

var lispJsonToJs = require("lisp-json-to-js");

console.log(lispJsonToJs(["do", ["def", "console", ["js", "console"]], [".", "console", ["`", "log"], 42]]));
console.log();
console.log(lispJsonToJs.transpile(["do", ["def", "console", ["js", "console"]], [".", "console", ["`", "log"], 42]]));
console.log();
console.log(lispJsonToJs.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

global

Assuming the file test.json with the following content:

["do", ["def", "console", ["js", "console"]], [".", "console", ["`", "log"], 42]]
lisp-json-to-js test.json
lisp-json-to-js -o test.js test.json
lisp-json-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));

Functions

.-

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"]

def

Defines a variable.

["def", "var", "val"]

compiles to:

env["var"] = env["val"]

do

Executes multiple functions and returns the last.

["do", ["def", "var", ["`", "val"]], ["+", "var", ["`", "val2"]]]

compiles to:

(function() {
    env["var"] = "val";
    return env["var"] + "val2";
})()

fn

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.

if

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")

js

Executes javascript code.

["js", "document.body.appendChild(new Text('test'))"]

compiles to:

document.body.appendChild(new Text('test'))

let

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))

try and catch

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))
}
0.4.1

9 years ago

0.4.0

9 years ago

0.3.0

9 years ago

0.2.0

9 years ago

0.1.0

9 years ago