Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

suported a few more items of the AST

  • Loading branch information...
commit dc2035e36473e7b6ad363f21bac4876a290de8c4 1 parent bf31a74
@dresende authored
Showing with 85 additions and 46 deletions.
  1. +85 −46 lib/ast-transformer.js
View
131 lib/ast-transformer.js
@@ -30,13 +30,21 @@ Transformer.prototype.__crawlAST = function (ast, prefix) {
if (ast == undefined) return;
switch (ast[0]) {
- case "toplevel":
case "block":
- case "var":
+ if (ast.length == 1 || !ast[1].length) break;
+ case "toplevel":
+ case "try":
+ // console.log("------------------");
+ // console.log(JSON.stringify(ast));
for (var i = 0; i < ast[1].length; i++) {
ast[1][i] = this.__crawlAST(ast[1][i]);
}
break;
+ case "var":
+ for (var i = 0; i < ast[1].length; i++) {
+ ast[1][i][1] = this.__crawlAST(ast[1][i][1]);
+ }
+ break;
case "function":
case "defun":
// @TODO: check function arguments
@@ -45,6 +53,9 @@ Transformer.prototype.__crawlAST = function (ast, prefix) {
}
break;
case "if":
+ case "conditional":
+ case "for":
+ case "for-in":
for (var i = 1; i < ast.length; i++) {
ast[i] = this.__crawlAST(ast[i]);
}
@@ -60,6 +71,7 @@ Transformer.prototype.__crawlAST = function (ast, prefix) {
case "stat":
case "return":
case "form":
+ case "throw":
ast[1] = this.__crawlAST(ast[1]);
break;
case "new":
@@ -74,30 +86,21 @@ Transformer.prototype.__crawlAST = function (ast, prefix) {
ast[1] = this.__crawlAST(ast[1]);
break;
case "call":
- var functionCall = getCallPath(ast[1]);
+ var functionCall = this.__callPath(ast[1]);
if (functionCall !== null && this.functionCalls.hasOwnProperty(functionCall)) {
- if (typeof this.functionCalls[functionCall] == "function") {
- var args = [];
-
- for (var i = 0; i < ast[2].length; i++) {
- // @TODO: check parameter type
- args.push(convertASTToVar(ast[2][i]));
- }
-
- ast = convertVarToAST(this.functionCalls[functionCall].apply(null, args));
- } else {
- ast = convertVarToAST(this.functionCalls[functionCall]);
- }
+ ast = this.__handleFunctionCall(functionCall, ast, true);
break;
}
switch (ast[1][0]) {
case "call":
+ case "function":
ast[1] = this.__crawlAST(ast[1]);
break;
case "name":
- break;
+ case "sub":
+ //console.log(ast);
case "dot":
for (var i = 0; i < ast[2].length; i++) {
ast[2][i] = this.__crawlAST(ast[2][i], ast);
@@ -106,6 +109,7 @@ Transformer.prototype.__crawlAST = function (ast, prefix) {
default:
console.log("---------- unknown call --------------");
console.log(ast);
+ console.log(JSON.stringify(ast));
}
break;
case "object":
@@ -120,33 +124,74 @@ Transformer.prototype.__crawlAST = function (ast, prefix) {
break;
case "string":
case "num":
+ case "break":
+ case "continue":
+ case "sub": // I actually don't know this much
// ignore
break;
case "name":
if (this.identifiers.hasOwnProperty(ast[1])) {
- ast = convertVarToAST(this.identifiers[ast[1]]);
+ ast = this.__toAST(this.identifiers[ast[1]]);
}
break;
- default:
- for (var i = 1; i < ast.length; i++) {
- ast[i] = this.__crawlAST(ast[i]);
+ case "switch":
+ ast[1] = this.__crawlAST(ast[1]);
+ for (var i = 0; i < ast[2].length; i++) {
+ for (var j = 0; j < ast[2][i][1].length; j++) {
+ this.__crawlAST(ast[2][i][1][j]);
+ }
}
- // console.log("---------- unknown --------------");
- // console.log(ast);
+ break;
+ default:
+ console.log("---------- unknown --------------");
+ console.log(ast);
+ //console.log(JSON.stringify(ast));
}
return ast;
};
+Transformer.prototype.__handleFunctionCall = function (functionCall, ast, return_ast) {
+ if (typeof this.functionCalls[functionCall] == "function") {
+ var args = [];
+
+ for (var i = 0; i < ast[2].length; i++) {
+ args.push(this.__toVar(ast[2][i]));
+ }
-function getCallPath(ast) {
+ if (return_ast) {
+ return this.__toAST(this.functionCalls[functionCall].apply(null, args));
+ }
+ return this.functionCalls[functionCall].apply(null, args);
+ } else {
+ if (return_ast) {
+ return this.__toAST(this.functionCalls[functionCall]);
+ }
+ return this.functionCalls[functionCall];
+ }
+};
+Transformer.prototype.__toVar = function (ast) {
switch (ast[0]) {
- case "name":
+ case "num":
+ case "string":
return ast[1];
- case "dot":
- return getCallPath(ast[1]) + "." + ast[2];
+ case "array":
+ for (var i = 0; i < ast[1].length; i++) {
+ ast[1][i] = this.__toVar(ast[1][i]);
+ }
+ return ast[1];
+ case "call":
+ var functionCall = this.__callPath(ast[1]);
+ if (functionCall !== null && this.functionCalls.hasOwnProperty(functionCall)) {
+ return this.__handleFunctionCall(functionCall, ast, false);
+ }
+ return null;
+ case "object":
+ // @TODO
+ default:
+ console.log(ast);
+ throw { code: 999, message: "__toVar(): type '" + ast[0] + "' not done yet" };
}
- return null;
-}
-function convertVarToAST(v) {
+};
+Transformer.prototype.__toAST = function (v) {
switch (typeof v) {
case "string":
return [ "string", v ];
@@ -159,36 +204,30 @@ function convertVarToAST(v) {
case "object":
if (v instanceof Array) {
for (k in v) {
- v[k] = convertVarToAST(v[k]);
+ v[k] = this.__toAST(v[k]);
}
return [ "array", v ];
}
for (k in v) {
if (v.hasOwnProperty(k)) {
- v[k] = convertVarToAST(v[k]);
+ v[k] = this.__toAST(v[k]);
}
}
return [ "object", v ];
default:
- throw { code: 999, message: "convertVarToAST(): typeof v '" + (typeof v) + "' not done yet" };
+ throw { code: 999, message: "__toAST(): typeof v '" + (typeof v) + "' not done yet" };
}
};
-
-function convertASTToVar(ast) {
+Transformer.prototype.__callPath = function (ast) {
switch (ast[0]) {
- case "num":
- case "string":
- return ast[1];
- case "array":
- for (var i = 0; i < ast[1].length; i++) {
- ast[1][i] = convertASTToVar(ast[1][i]);
- }
+ case "name":
return ast[1];
- case "object":
- // @TODO
- default:
- throw { code: 999, message: "convertASTToVar(): type '" + ast[0] + "' not done yet" };
+ case "sub":
+ return this.__callPath(ast[1]) + "[" + this.__callPath(ast[2]) + "]";
+ case "dot":
+ return this.__callPath(ast[1]) + "." + ast[2];
}
-};
+ return null;
+};
Please sign in to comment.
Something went wrong with that request. Please try again.