use transform insted treewalker
This commit is contained in:
parent
a2f7a07c11
commit
363019dc5a
119
lib/translate.js
119
lib/translate.js
|
|
@ -37,9 +37,8 @@
|
|||
"use strict";
|
||||
|
||||
function translate(tree){
|
||||
var queue = [];
|
||||
|
||||
tree.walk(new TreeWalker(function(node){
|
||||
var tt = new TreeTransformer(null, function(node){
|
||||
var newNode, props;
|
||||
|
||||
if(node instanceof AST_Binary && node.operator == '**'){
|
||||
|
|
@ -55,10 +54,7 @@ function translate(tree){
|
|||
expression : new AST_SymbolRef({ name : 'Math', start : props.start, end : props.start })
|
||||
});
|
||||
|
||||
newNode = new AST_Call(props);
|
||||
queue.push(function(){
|
||||
ReplaceObject(node, newNode);
|
||||
});
|
||||
node = new AST_Call(props);
|
||||
} else
|
||||
|
||||
if(node instanceof AST_Binary && node.operator == '%%'){
|
||||
|
|
@ -73,64 +69,52 @@ function translate(tree){
|
|||
end : props.start
|
||||
});
|
||||
|
||||
newNode = new AST_Call(props);
|
||||
queue.push(function(){
|
||||
ReplaceObject(node, newNode);
|
||||
node = new AST_Call(props);
|
||||
} else
|
||||
|
||||
if(node instanceof AST_SimpleStatement && node.body instanceof AST_Assign && node.body.operator == '?='){
|
||||
node.body.operator = '=';
|
||||
|
||||
props = {
|
||||
args : [node.body.left],
|
||||
start : new AST_Token({ nlb : false, type : 'name', value : '$_cola_isntset' }),
|
||||
end : new AST_Token({ nlb : false, type : 'punc', value : ')' })
|
||||
};
|
||||
props.expression = new AST_SymbolRef({
|
||||
name : '$_cola_isntset',
|
||||
start : props.start,
|
||||
end : props.start
|
||||
});
|
||||
|
||||
node = new AST_If({
|
||||
body : node.clone(),
|
||||
start : new AST_Token({ nlb : false, type : 'keyword', value : 'if' }),
|
||||
end : new AST_Token({ nlb : false, type : 'punc', value : ';' }),
|
||||
condition : new AST_Call(props)
|
||||
});
|
||||
} else
|
||||
|
||||
if(node instanceof AST_Assign && node.operator == '?='){
|
||||
var parent = this.parent();
|
||||
node.operator = '=';
|
||||
|
||||
if(parent instanceof AST_SimpleStatement){
|
||||
node.operator = '=';
|
||||
|
||||
props = {
|
||||
args : [node.left],
|
||||
start : new AST_Token({ nlb : false, type : 'name', value : '$_cola_isntset' }),
|
||||
end : new AST_Token({ nlb : false, type : 'punc', value : ')' })
|
||||
};
|
||||
props.expression = new AST_SymbolRef({
|
||||
name : '$_cola_isntset',
|
||||
start : props.start,
|
||||
end : props.start
|
||||
props = {
|
||||
args : [node.left],
|
||||
start : new AST_Token({ nlb : false, type : 'name', value : '$_cola_isntset' }),
|
||||
end : new AST_Token({ nlb : false, type : 'punc', value : ')' })
|
||||
};
|
||||
props.expression = new AST_SymbolRef({
|
||||
name : '$_cola_isntset',
|
||||
start : props.start,
|
||||
end : props.start
|
||||
});
|
||||
|
||||
newNode = new AST_If({
|
||||
body : CopyObject(parent),
|
||||
start : new AST_Token({ nlb : false, type : 'keyword', value : 'if' }),
|
||||
end : new AST_Token({ nlb : false, type : 'punc', value : ';' }),
|
||||
condition : new AST_Call(props)
|
||||
});
|
||||
queue.push(function(){
|
||||
ReplaceObject(parent, newNode);
|
||||
});
|
||||
} else {
|
||||
node.operator = '=';
|
||||
|
||||
props = {
|
||||
args : [node.left],
|
||||
start : new AST_Token({ nlb : false, type : 'name', value : '$_cola_isntset' }),
|
||||
end : new AST_Token({ nlb : false, type : 'punc', value : ')' })
|
||||
};
|
||||
props.expression = new AST_SymbolRef({
|
||||
name : '$_cola_isntset',
|
||||
start : props.start,
|
||||
end : props.start
|
||||
});
|
||||
|
||||
newNode = new AST_Conditional({
|
||||
start : new AST_Token({ nlb : false, type : 'punc', value : '(' }),
|
||||
end : new AST_Token({ nlb : false, type : 'punc', value : ')' }),
|
||||
condition : new AST_Call(props),
|
||||
consequent : CopyObject(node),
|
||||
alternative : node.left
|
||||
});
|
||||
queue.push(function(){
|
||||
ReplaceObject(node, newNode);
|
||||
});
|
||||
}
|
||||
|
||||
node = new AST_Conditional({
|
||||
start : new AST_Token({ nlb : false, type : 'punc', value : '(' }),
|
||||
end : new AST_Token({ nlb : false, type : 'punc', value : ')' }),
|
||||
condition : new AST_Call(props),
|
||||
consequent : node.clone(),
|
||||
alternative : node.left
|
||||
});
|
||||
} else
|
||||
|
||||
if(node instanceof AST_Binary && node.operator == 'is'){
|
||||
|
|
@ -145,10 +129,7 @@ function translate(tree){
|
|||
end : props.start
|
||||
});
|
||||
|
||||
newNode = new AST_Call(props);
|
||||
queue.push(function(){
|
||||
ReplaceObject(node, newNode);
|
||||
});
|
||||
node = new AST_Call(props);
|
||||
} else
|
||||
|
||||
if(node instanceof AST_Binary && node.operator == 'isnt'){
|
||||
|
|
@ -163,10 +144,7 @@ function translate(tree){
|
|||
end : props.start
|
||||
});
|
||||
|
||||
newNode = new AST_Call(props);
|
||||
queue.push(function(){
|
||||
ReplaceObject(node, newNode);
|
||||
});
|
||||
node = new AST_Call(props);
|
||||
} else
|
||||
|
||||
if(node instanceof AST_StringTemplate){
|
||||
|
|
@ -186,20 +164,15 @@ function translate(tree){
|
|||
end : node.body[i].end
|
||||
});
|
||||
|
||||
queue.push(function(){
|
||||
ReplaceObject(node, newNode);
|
||||
});
|
||||
node = newNode;
|
||||
} else
|
||||
|
||||
if(node instanceof AST_RegExp && (node.value.indexOf('\n') != -1 || /\/[\w]*x[\w]*$/.test(node.value))){
|
||||
node.value = node.value.replace(/[\r\n\s]/g,'').replace(/(\/[\w]*)x([\w]*$)/, '$1$2');
|
||||
}
|
||||
|
||||
}));
|
||||
|
||||
queue.forEach(function(f){
|
||||
f();
|
||||
return node;
|
||||
});
|
||||
|
||||
return tree;
|
||||
return tree.transform(tt);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user