0.4.0 • Published 9 years ago

lisp-array-to-js v0.4.0

Weekly downloads
-
License
MIT
Repository
github
Last release
9 years ago

lisp-array-to-js Code Climate Build Status js-standard-style

Transpiles lisp code represented as array to JS code

Install

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

or, to install it globally:

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

Usage

io.js / node.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

global

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

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

import

Imports a module.

["import", ["`", "lisp-array-to-js"]]

compiles to:

require("lisp-array-to-js")

Note: this currently only supports CommonJS

export

Exports a module.

["export", ["`", "test"]]
["export", ["`", "exec"], ["`", "whatever"]]

compiles to:

module.exports = "test"
module.exports["exec"] = "whatever"

Note: this currently only supports CommonJS

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

<, +, -, *, /, %

All of these are simply converted to infix notation. For example, <:

["<", 1, 2]

compiles to:

1 < 2

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

with

With replaces the current scope.

["with", ["js", "window"], [".", "document", ["`", "querySelector"], ["`", "head"]]]

compiles to:

(function(env) {
    return env["document"]["querySelector"]("head");
})(window)
0.4.0

9 years ago

0.3.0

9 years ago

0.2.1

9 years ago

0.2.0

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago