mirror of
https://github.com/Jermolene/TiddlyWiki5
synced 2024-07-04 11:13:15 +00:00
Refactored JavaScriptParseTree handling of output array
This commit is contained in:
parent
f10d58ed2b
commit
8cfff2c2fa
|
@ -27,112 +27,111 @@ var JavaScriptParseTree = function(tree) {
|
||||||
|
|
||||||
// Render the entire JavaScript tree object to JavaScript source code
|
// Render the entire JavaScript tree object to JavaScript source code
|
||||||
JavaScriptParseTree.prototype.render = function() {
|
JavaScriptParseTree.prototype.render = function() {
|
||||||
this.output = [];
|
var output = [];
|
||||||
this.renderSubTree(this.tree);
|
this.renderSubTree(output,this.tree);
|
||||||
var r = this.output.join("");
|
var r = output.join("");
|
||||||
this.output = null;
|
|
||||||
return r;
|
return r;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Render a subtree of the parse tree to an array of fragments of JavaScript source code
|
// Render a subtree of the parse tree to an array of fragments of JavaScript source code
|
||||||
JavaScriptParseTree.prototype.renderSubTree = function(tree) {
|
JavaScriptParseTree.prototype.renderSubTree = function(output,tree) {
|
||||||
for(var t=0; t<tree.length; t++) {
|
for(var t=0; t<tree.length; t++) {
|
||||||
if(t) {
|
if(t) {
|
||||||
this.output.push(";\n");
|
this.output.push(";\n");
|
||||||
}
|
}
|
||||||
this.renderNode(tree[t]);
|
this.renderNode(output,tree[t]);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Compile a JavaScript node to an array of fragments of JavaScript source code
|
// Compile a JavaScript node to an array of fragments of JavaScript source code
|
||||||
JavaScriptParseTree.prototype.renderNode = function(node) {
|
JavaScriptParseTree.prototype.renderNode = function(output,node) {
|
||||||
var p;
|
var p;
|
||||||
switch(node.type) {
|
switch(node.type) {
|
||||||
case "StringLiteral":
|
case "StringLiteral":
|
||||||
this.output.push(utils.stringify(node.value));
|
output.push(utils.stringify(node.value));
|
||||||
break;
|
break;
|
||||||
case "StringLiterals":
|
case "StringLiterals":
|
||||||
this.output.push(utils.stringify(node.value.join("")));
|
output.push(utils.stringify(node.value.join("")));
|
||||||
break;
|
break;
|
||||||
case "FunctionCall":
|
case "FunctionCall":
|
||||||
this.output.push("(");
|
output.push("(");
|
||||||
this.renderNode(node.name);
|
this.renderNode(output,node.name);
|
||||||
this.output.push(")(");
|
output.push(")(");
|
||||||
for(p=0; p<node["arguments"].length; p++) {
|
for(p=0; p<node["arguments"].length; p++) {
|
||||||
if(p) {
|
if(p) {
|
||||||
this.output.push(",");
|
output.push(",");
|
||||||
}
|
}
|
||||||
this.renderNode(node["arguments"][p]);
|
this.renderNode(output,node["arguments"][p]);
|
||||||
}
|
}
|
||||||
this.output.push(")");
|
output.push(")");
|
||||||
break;
|
break;
|
||||||
case "PropertyAccess":
|
case "PropertyAccess":
|
||||||
this.renderNode(node.base);
|
this.renderNode(output,node.base);
|
||||||
if(typeof node.name === "string") {
|
if(typeof node.name === "string") {
|
||||||
this.output.push("." + node.name);
|
output.push("." + node.name);
|
||||||
} else {
|
} else {
|
||||||
this.output.push("[");
|
output.push("[");
|
||||||
this.renderNode(node.name);
|
this.renderNode(output,node.name);
|
||||||
this.output.push("]");
|
output.push("]");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case "ArrayLiteral":
|
case "ArrayLiteral":
|
||||||
this.output.push("[");
|
output.push("[");
|
||||||
for(p=0; p<node.elements.length; p++) {
|
for(p=0; p<node.elements.length; p++) {
|
||||||
if(p) {
|
if(p) {
|
||||||
this.output.push(",");
|
output.push(",");
|
||||||
}
|
}
|
||||||
this.renderNode(node.elements[p]);
|
this.renderNode(output,node.elements[p]);
|
||||||
}
|
}
|
||||||
this.output.push("]");
|
output.push("]");
|
||||||
break;
|
break;
|
||||||
case "Variable":
|
case "Variable":
|
||||||
this.output.push(node.name);
|
output.push(node.name);
|
||||||
break;
|
break;
|
||||||
case "ObjectLiteral":
|
case "ObjectLiteral":
|
||||||
this.output.push("{");
|
output.push("{");
|
||||||
for(p=0; p<node.properties.length; p++) {
|
for(p=0; p<node.properties.length; p++) {
|
||||||
if(p) {
|
if(p) {
|
||||||
this.output.push(",");
|
output.push(",");
|
||||||
}
|
}
|
||||||
this.renderNode(node.properties[p]);
|
this.renderNode(output,node.properties[p]);
|
||||||
}
|
}
|
||||||
this.output.push("}");
|
output.push("}");
|
||||||
break;
|
break;
|
||||||
case "PropertyAssignment":
|
case "PropertyAssignment":
|
||||||
this.output.push(node.name);
|
output.push(node.name);
|
||||||
this.output.push(":");
|
output.push(":");
|
||||||
this.renderNode(node.value);
|
this.renderNode(output,node.value);
|
||||||
break;
|
break;
|
||||||
case "BinaryExpression":
|
case "BinaryExpression":
|
||||||
this.output.push("(");
|
output.push("(");
|
||||||
this.renderNode(node.left);
|
this.renderNode(output,node.left);
|
||||||
this.output.push(")");
|
output.push(")");
|
||||||
this.output.push(node.operator);
|
output.push(node.operator);
|
||||||
this.output.push("(");
|
output.push("(");
|
||||||
this.renderNode(node.right);
|
this.renderNode(output,node.right);
|
||||||
this.output.push(")");
|
output.push(")");
|
||||||
break;
|
break;
|
||||||
case "NumericLiteral":
|
case "NumericLiteral":
|
||||||
this.output.push(node.value);
|
output.push(node.value);
|
||||||
break;
|
break;
|
||||||
case "Function":
|
case "Function":
|
||||||
this.output.push("(function ");
|
output.push("(function ");
|
||||||
if(node.name !== null) {
|
if(node.name !== null) {
|
||||||
this.output.push(node.name);
|
output.push(node.name);
|
||||||
}
|
}
|
||||||
this.output.push("(");
|
output.push("(");
|
||||||
this.output.push(node.params.join(","));
|
output.push(node.params.join(","));
|
||||||
this.output.push("){");
|
output.push("){");
|
||||||
this.renderSubTree(node.elements);
|
this.renderSubTree(output,node.elements);
|
||||||
this.output.push("})");
|
output.push("})");
|
||||||
break;
|
break;
|
||||||
case "ReturnStatement":
|
case "ReturnStatement":
|
||||||
this.output.push("return ");
|
output.push("return ");
|
||||||
this.renderNode(node.value);
|
this.renderNode(output,node.value);
|
||||||
break;
|
break;
|
||||||
case "This":
|
case "This":
|
||||||
this.output.push("this");
|
output.push("this");
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
console.log(node);
|
console.log(node);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user