Permalink
Browse files

compiling monocles, good sir .{

  • Loading branch information...
1 parent 53c82de commit 3f42f3577e5ce299da68c86d319bd1774e7bb1df @zaach zaach committed Aug 6, 2011
Showing with 150 additions and 2 deletions.
  1. +6 −0 examples/compile.js
  2. +11 −0 lib/grammar.y
  3. +10 −0 lib/reflect.js
  4. +59 −0 lib/transform.js
  5. +5 −2 package.json
  6. +24 −0 test/monocle-test.js
  7. +35 −0 test/monocle.js
View
@@ -0,0 +1,6 @@
+var source = require('fs').readFileSync(require('path').resolve(process.argv[2]), "utf8");
+
+var Reflect = require("../dist/reflect");
+
+console.log(Reflect.compile(source));
+
View
@@ -245,6 +245,15 @@ MemberExpr
{ $$ = yy.Node('MemberExpression',$1,yy.Node('Identifier', String($3)),false,yy.loc([@$,@3])); }
| NEW MemberExpr Arguments
{ $$ = yy.Node('NewExpression',$MemberExpr,$Arguments,yy.loc([@$,@3])); }
+ | MemberExpr '.' ObjectLiteral
+ { $$ = yy.Node('MemberExpression',$1,$3,false,yy.loc([@$,@3])); }
+ ;
+
+ObjectLiteral
+ : OPENBRACE PropertyList CLOSEBRACE
+ { $$ = yy.Node('ObjectExpression',$2,yy.loc([@$,@3])); }
+ | OPENBRACE PropertyList ',' CLOSEBRACE
+ { $$ = yy.Node('ObjectExpression',$2,yy.loc([@$,@4])); }
;
MemberExprNoBF
@@ -255,6 +264,8 @@ MemberExprNoBF
{ $$ = yy.Node('MemberExpression',$1,yy.Node('Identifier', String($3)),false,yy.loc([@$,@3])); }
| NEW MemberExpr Arguments
{ $$ = yy.Node('NewExpression',$MemberExpr,$Arguments,yy.loc([@$,@3])); }
+ | MemberExprNoBF '.' ObjectLiteral
+ { $$ = yy.Node('MemberExpression',$1,$3,false,yy.loc([@$,@3])); }
;
NewExpr
View
@@ -108,3 +108,13 @@ exports.parse = exports.Reflect.parse;
exports.stringify = stringify;
exports.builder = builder;
+exports.compile = function (source) {
+ var b = require("reflect-tree-builder");
+ var transform = require("./transform").transform;
+ var ast = exports.Reflect.parse(source, {builder: b});
+
+ transform(ast, b);
+
+ return stringify(ast);
+};
+
View
@@ -0,0 +1,59 @@
+var JSONSelect = require("JSONSelect");
+
+exports.transform = function (ast, b /*builder*/) {
+
+ JSONSelect.forEach(':has(:root > .property .type:val("ObjectExpression"))', ast,
+ function (match) {
+ var parent = match.parent;
+ var name = match.object.type == 'Identifier' ?
+ match.object.name :
+ '$IIFEparam';
+ //var exp = b.callExpression(
+ //buildFunction(name, match.property),
+ //[match.object]
+ //);
+ var exp = buildExtend(match.object, match.property);
+
+ parent.replace(match, exp);
+ }
+ );
+
+ function buildFunction (name, property) {
+ return b.functionExpression(
+ null,
+ [b.identifier(name)],
+ b.blockStatement(
+ buildBody(name, property.properties)
+ .concat(b.returnStatement(b.identifier(name)))
+ ),
+ false, false
+ );
+ }
+
+ function buildBody (name, properties) {
+ return properties.map(function (prop) {
+ return expStmt(b.assignmentExpression(
+ "=",
+ b.memberExpression(b.identifier(name), prop.key, prop.key.type === 'Literal'),
+ prop.value
+ ));
+ });
+ }
+
+ function buildGetSet (name, prop) {
+ }
+
+ function expStmt (expr) {
+ return b.expressionStatement(expr);
+ }
+
+ function buildExtend (obj, extension) {
+ return b.callExpression(
+ b.memberExpression(
+ b.identifier("Object"),
+ b.identifier("extend")),
+ [obj, extension]
+ );
+ }
+};
+
View
@@ -27,5 +27,8 @@
"scripts": {
"test": "node test/all-tests.js"
},
- "dependencies": {}
-}
+ "dependencies": {
+ "JSONSelect": "*"
+ "reflect-tree-builder": "*"
+ }
+}
View
@@ -0,0 +1,24 @@
+var fs = require("fs");
+var path = require("path");
+var source = fs.readFileSync(path.resolve(path.join(__dirname, "monocle.js")), "utf8");
+
+var Reflect = require("../dist/reflect");
+var b = require("reflect-tree-builder");
+var transform = require("../dist/transform").transform;
+
+var ast = Reflect.parse(source, {builder: b});
+
+transform(ast, b);
+
+console.log(Reflect.stringify(ast));
+
+Object.extend = function (a, b) {
+ Object.getOwnPropertyNames(b).forEach(function (name) {
+ var desc = Object.getOwnPropertyDescriptor(b, name);
+ Object.defineProperty(a, name, desc);
+ });
+ return a;
+};
+
+eval(Reflect.stringify(ast));
+
View
@@ -0,0 +1,35 @@
+var assert = require("assert");
+var a = {};
+
+a.{
+ foo: "bar",
+ hello: "world"
+};
+
+assert.equal(a.foo, "bar");
+
+var b = {foo: "bar"}.{extras: "yes please"};
+
+assert.equal(b.extras, "yes please");
+
+a.{
+ 2: "computed",
+ "$$": "cache money"
+};
+
+assert.equal(a["$$"], "cache money");
+
+var c = {foo: "bar"}.{extras: "yes please"}.{more: "wow"};
+
+assert.equal(c.more, "wow");
+
+c.{
+ get dynamic () {return this.foo},
+ set name (val) { this.more = val; }
+};
+
+assert.equal(c.dynamic, "bar");
+
+c.name = "setter";
+assert.equal(c.more, "setter");
+

0 comments on commit 3f42f35

Please sign in to comment.