UglifyJS/lib/translate.js

278 lines
11 KiB
JavaScript
Raw Normal View History

/***********************************************************************
AST-Tree translator, ColaScript -> JavaScript.
Distributed under the BSD license:
2014-04-15 12:47:04 +00:00
Copyright 2014 (c) TrigenSoftware <danon0404@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above
copyright notice, this list of conditions and the following
disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following
disclaimer in the documentation and/or other materials
provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AS IS AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
SUCH DAMAGE.
***********************************************************************/
"use strict";
2014-04-17 16:21:45 +00:00
!window.Cola && (window.Cola = {});
2014-04-17 16:21:45 +00:00
Cola.AST_Toplevel.prototype.toJavaScript = function(){
if(this.language == 'js') return this;
this.language = 'js';
2014-04-18 06:10:20 +00:00
var $_cola_ast = Cola.parse($_cola, { is_js : true}), $_cola_hash = {}, _this,
tt = new Cola.TreeTransformer(null, function(node){
2014-04-20 18:56:47 +00:00
var newNode, props, parent = this.parent();
2014-04-16 16:43:40 +00:00
if(node instanceof Cola.AST_Binary && node.operator == '**'){
props = {
args : [node.left, node.right],
2014-04-16 16:43:40 +00:00
start : new Cola.AST_Token({ nlb : false, type : 'name', value : 'Math' }),
end : new Cola.AST_Token({ nlb : false, type : 'punc', value : ')' })
};
2014-04-16 16:43:40 +00:00
props.expression = new Cola.AST_Dot({
property : 'pow',
start : props.start,
2014-04-16 16:43:40 +00:00
end : new Cola.AST_Token({ nlb : false, type : 'name', value : 'pow' }),
expression : new Cola.AST_SymbolRef({ name : 'Math', start : props.start, end : props.start })
});
2014-04-16 16:43:40 +00:00
node = new Cola.AST_Call(props);
} else
2014-04-16 16:43:40 +00:00
if(node instanceof Cola.AST_Binary && node.operator == '%%'){
2014-04-18 06:10:20 +00:00
$_cola_hash[$_cola_modulo.i] = true;
props = {
args : [node.left, node.right],
2014-04-16 16:43:40 +00:00
start : new Cola.AST_Token({ nlb : false, type : 'name', value : '$_cola_modulo' }),
end : new Cola.AST_Token({ nlb : false, type : 'punc', value : ')' })
};
2014-04-16 16:43:40 +00:00
props.expression = new Cola.AST_SymbolRef({
name : '$_cola_modulo',
start : props.start,
end : props.start
});
2014-04-16 16:43:40 +00:00
node = new Cola.AST_Call(props);
2014-04-15 14:48:09 +00:00
} else
2014-04-16 16:43:40 +00:00
if(node instanceof Cola.AST_SimpleStatement && node.body instanceof Cola.AST_Assign && node.body.operator == '?='){
2014-04-18 06:10:20 +00:00
$_cola_hash[$_cola_isntset.i] = true;
2014-04-15 14:48:09 +00:00
node.body.operator = '=';
props = {
args : [node.body.left],
2014-04-16 16:43:40 +00:00
start : new Cola.AST_Token({ nlb : false, type : 'name', value : '$_cola_isntset' }),
end : new Cola.AST_Token({ nlb : false, type : 'punc', value : ')' })
2014-04-15 14:48:09 +00:00
};
2014-04-16 16:43:40 +00:00
props.expression = new Cola.AST_SymbolRef({
2014-04-15 14:48:09 +00:00
name : '$_cola_isntset',
start : props.start,
end : props.start
});
2014-04-16 16:43:40 +00:00
node = new Cola.AST_If({
2014-04-15 14:48:09 +00:00
body : node.clone(),
2014-04-16 16:43:40 +00:00
start : new Cola.AST_Token({ nlb : false, type : 'keyword', value : 'if' }),
end : new Cola.AST_Token({ nlb : false, type : 'punc', value : ';' }),
condition : new Cola.AST_Call(props)
});
} else
2014-04-16 16:43:40 +00:00
if(node instanceof Cola.AST_Assign && node.operator == '?='){
2014-04-18 06:10:20 +00:00
$_cola_hash[$_cola_isntset.i] = true;
2014-04-15 14:48:09 +00:00
node.operator = '=';
2014-04-15 14:48:09 +00:00
props = {
args : [node.left],
2014-04-16 16:43:40 +00:00
start : new Cola.AST_Token({ nlb : false, type : 'name', value : '$_cola_isntset' }),
end : new Cola.AST_Token({ nlb : false, type : 'punc', value : ')' })
2014-04-15 14:48:09 +00:00
};
2014-04-16 16:43:40 +00:00
props.expression = new Cola.AST_SymbolRef({
2014-04-15 14:48:09 +00:00
name : '$_cola_isntset',
start : props.start,
end : props.start
});
2014-04-16 16:43:40 +00:00
node = new Cola.AST_Conditional({
start : new Cola.AST_Token({ nlb : false, type : 'punc', value : '(' }),
end : new Cola.AST_Token({ nlb : false, type : 'punc', value : ')' }),
condition : new Cola.AST_Call(props),
2014-04-15 14:48:09 +00:00
consequent : node.clone(),
alternative : node.left
});
} else
if(node instanceof Cola.AST_Conditional && node.alternative instanceof Cola.AST_Noop){
$_cola_hash[$_cola_isset.i] = true;
props = {
args : [node.condition],
start : new Cola.AST_Token({ nlb : false, type : 'name', value : '$_cola_isset' }),
end : new Cola.AST_Token({ nlb : false, type : 'punc', value : ')' })
};
props.expression = new Cola.AST_SymbolRef({
name : '$_cola_isset',
start : props.start,
end : props.start
});
node.alternative = node.consequent;
node.consequent = node.condition;
node.condition = new Cola.AST_Call(props);
} else
2014-04-16 16:43:40 +00:00
if(node instanceof Cola.AST_Binary && node.operator == 'is'){
2014-04-18 06:10:20 +00:00
$_cola_hash[$_cola_is.i] = true;
props = {
args : [node.left, node.right],
2014-04-16 16:43:40 +00:00
start : new Cola.AST_Token({ nlb : false, type : 'name', value : '$_cola_is' }),
end : new Cola.AST_Token({ nlb : false, type : 'punc', value : ')' })
};
2014-04-16 16:43:40 +00:00
props.expression = new Cola.AST_SymbolRef({
name : '$_cola_is',
start : props.start,
end : props.start
});
2014-04-16 16:43:40 +00:00
node = new Cola.AST_Call(props);
} else
2014-04-16 16:43:40 +00:00
if(node instanceof Cola.AST_Binary && node.operator == 'isnt'){
2014-04-18 06:10:20 +00:00
$_cola_hash[$_cola_isnt.i] = true;
props = {
args : [node.left, node.right],
2014-04-16 16:43:40 +00:00
start : new Cola.AST_Token({ nlb : false, type : 'name', value : '$_cola_isnt' }),
end : new Cola.AST_Token({ nlb : false, type : 'punc', value : ')' })
};
2014-04-16 16:43:40 +00:00
props.expression = new Cola.AST_SymbolRef({
name : '$_cola_isnt',
start : props.start,
end : props.start
});
2014-04-16 16:43:40 +00:00
node = new Cola.AST_Call(props);
} else
if(node instanceof Cola.AST_UnaryPostfix && node.operator == '??' || node instanceof Cola.AST_UnaryPrefix && node.operator == 'isset'){
$_cola_hash[$_cola_isset.i] = true;
props = {
args : [node.expression],
start : new Cola.AST_Token({ nlb : false, type : 'name', value : '$_cola_isset' }),
end : new Cola.AST_Token({ nlb : false, type : 'punc', value : ')' })
};
props.expression = new Cola.AST_SymbolRef({
name : '$_cola_isset',
start : props.start,
end : props.start
});
node = new Cola.AST_Call(props);
} else
if(node instanceof Cola.AST_UnaryPrefix && node.operator == 'clone'){
$_cola_hash[$_cola_clone.i] = true;
props = {
args : [node.expression],
start : new Cola.AST_Token({ nlb : false, type : 'name', value : '$_cola_clone' }),
end : new Cola.AST_Token({ nlb : false, type : 'punc', value : ')' })
};
props.expression = new Cola.AST_SymbolRef({
name : '$_cola_clone',
start : props.start,
end : props.start
});
node = new Cola.AST_Call(props);
} else
2014-04-20 18:56:47 +00:00
if(node instanceof Cola.AST_Assign && node.operator == "=" &&
node.left instanceof Cola.AST_Sub && node.left.property instanceof Cola.AST_Noop){
props = {
property : "push",
start : node.left.start,
end : new Cola.AST_Token({ nlb : false, type : 'name', value : 'push' }),
expression : node.left.expression
};
props = {
args : [node.right],
start : props.start,
end : new Cola.AST_Token({ nlb : false, type : 'punc', value : ')' }),
expression : new Cola.AST_Dot(props)
};
node = new Cola.AST_Call(props);
} else
if(node instanceof Cola.AST_Sub && node.property instanceof Cola.AST_Noop && !(parent instanceof Cola.AST_Assign) && parent.operator != "=" && node != parent.left){
$_cola_hash[$_cola_array_last.i] = true;
props = {
args : [node.expression],
start : new Cola.AST_Token({ nlb : false, type : 'name', value : '$_cola_array_last' }),
end : new Cola.AST_Token({ nlb : false, type : 'punc', value : ')' })
};
props.expression = new Cola.AST_SymbolRef({
name : '$_cola_array_last',
start : props.start,
end : props.start
});
node = new Cola.AST_Call(props);
} else
2014-04-16 16:43:40 +00:00
if(node instanceof Cola.AST_StringTemplate){
newNode = new Cola.AST_Binary({
operator : '+',
left : node.body[0],
right : node.body[1],
start : node.body[0].start,
end : node.body[1].end
});
for(var i = 2; i < node.body.length; i++)
2014-04-16 16:43:40 +00:00
newNode = new Cola.AST_Binary({
operator : '+',
left : newNode,
right : node.body[i],
start : newNode.start,
end : node.body[i].end
});
2014-04-15 14:48:09 +00:00
node = newNode;
} else
2014-04-16 16:43:40 +00:00
if(node instanceof Cola.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');
}
2014-04-15 14:48:09 +00:00
return node;
});
2014-04-18 06:10:20 +00:00
_this = this.transform(tt);
for(var i in $_cola_hash) if($_cola_hash.hasOwnProperty(i))
_this.body.unshift($_cola_ast.body[i]);
return _this;
2014-04-17 16:21:45 +00:00
};