Skip to content

Commit

Permalink
pinecone2
Browse files Browse the repository at this point in the history
  • Loading branch information
zekesonxx committed Aug 14, 2016
1 parent b34d22e commit fb4dc03
Show file tree
Hide file tree
Showing 23 changed files with 399 additions and 739 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
npm-debug.log
try.*
!/lib/converters/try.js
3 changes: 2 additions & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"undef": true,
"indent": 2,
"quotmark": "single",
"predef": ["describe", "it", "before", "beforeEach", "after", "afterEach"]
"predef": ["describe", "it", "before", "beforeEach", "after", "afterEach"],
"esversion": 6
}
41 changes: 0 additions & 41 deletions JSVSLUA.md

This file was deleted.

89 changes: 4 additions & 85 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,92 +1,10 @@
# pinecone: A JavaScript->Lua converter

So, I wrote a JS->Lua converter. Instead of being based on regex or some other dumb idea, it's based on [acorn][acorn], an awesome and superfast JavaScript Annotated Source Tree (AST) generator. Pinecone takes that and turns it into mostly-valid Lua.
pinecone converts JavaScript to Lua using the [ESTree standard][estree], converted by default using [acorn][acorn]. Pinecone takes that and turns it into mostly-valid Lua.

It was written by running `$ acorn try.js > try.json` and looking at the JSON over and over, so don't expect very good standard conformity.
This is the second version of pinecone, the first version was written back in mid-2014, rewritten in late-2014, and added on to in early 2016. It wasn't very well designed, but you can still see it preserved in the git branch `orig-pinecone`.

## Some notes
Pinecone is designed for writing JavaScript that's written with the intention of being compiled in pinecone. It's not designed to handle any arbitrary JavaScript code, and as such will not be self-compiling anytime soon. :(

A relatively small runtime is required, you can choose where it goes using `pinecone__runtime()` to embed it in that location. (note this isn't actually implemented yet)

Please read `JSVSLUA.md` before trying to use pinecone so you at least have some idea of how it works. Eventually I'll write a user guide but right now I'm just concerned about getting pinecone functional.

## What works
* Variables `var a = 1`
* Multiple Variables `var a = 1, b = 2`
* Changing variables `a = 2`
* Functions `function a() { return 1 }`
* Functions declared as variables `var a = function () { return 1 }`
* If/ifelse/else
* `typeof thing` (turned into `type(thing)`)
* Graceful failing
* While loops
* Do While loops (converted to `repeat` loops)
* for..in.. loops

## What doesn't
* Comments
* Other types of for loops
* Everything else


## Usage
**CLI**
````text
$ npm install -g pinecone
$ pinecone file.js -o file.lua
````

**Programmically**
````js
var pinecone = require('pinecone'); // npm install pinecone
var input = "var k = 9;";

pinecone.convert(input);
//Alternatively, if you already have the AST:
pinecone.convertFromAST(require('acorn').parse(input, {}));

````


## Example
````js
var k = 'blue', b, f = 12;

var p = 4;

if (typeof k == 'string') {
print('String!');
}

function bacon() {
return 'bacon';
}

var f = function () {

};
````
turns into
````lua
--# Converted using pinecone v0.1.0

local k, b, f = "blue", nil, 12
local p = 4
if type(k) == "string" then
print("String!")

end
function bacon()
return "bacon"

end
local f = function() end
````

## Contributing
Contributions are welcome. Use two spaces for tabs, and try to keep your code looking like the rest of it.
Make sure your code passes a jshint lint. Outputted Lua code made from JS code that passes jshint (that pinecone fully supports) should pass luac.
##### *missing the rest of the readme; I'm still converting pinecone to the new system*

## License
LGPL3 licensed. This means that any modifications to Pinecone must be released under the (L)GPL3, but that anything you compile with pinecone isn't bound by the (L)GPL3.
Expand All @@ -95,3 +13,4 @@ Refer to `LICENSE`.


[acorn]: https://github.com/marijnh/acorn
[estree]: https://github.com/estree/estree/
38 changes: 0 additions & 38 deletions WEIRDSTUFF.md

This file was deleted.

74 changes: 0 additions & 74 deletions bin/pinecone

This file was deleted.

81 changes: 0 additions & 81 deletions lib/convert.js

This file was deleted.

40 changes: 40 additions & 0 deletions lib/converters/basic.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';
const luaize = require('../luaize');
module.exports = function(h) {
h('Literal', function(node, state, c) {
var inp = node.value;
if (inp === null) {
node.code = 'nil';
return;
}
switch (typeof inp) {
case 'string':
node.code = '"'+inp.replace(/"/g, '\\"').replace(/\n/g, '\\n')+'"';
break;
case 'number':
node.code = inp.toString();
break;
case 'boolean':
node.code = (inp) ? 'true' : 'false';
break;
case 'object': //regex
//TODO figure out regex
node.code = luaize.failed(node, 'Can\'t convert regex into Lua yet.');
break;
default:
throw new Error(`Malformed AST, got Literal type: ${typeof node}`);
}
});
h('Identifier', function(node, state, c) {
node.code = luaize.idents(node.name);
});
h(['Program', 'BlockStatement', 'SequenceExpression'], function(node, state, c) {
node.code = '';
(node.body || node.expressions).forEach(function(internal) {
c(internal, state);
node.code += '\n'+internal.code;
});
//remove the leading \n
node.code = node.code.substr(1);
});
};
Loading

0 comments on commit fb4dc03

Please sign in to comment.