Skip to content

Instantly share code, notes, and snippets.

@cevek
Created February 8, 2015 16:03
Show Gist options
  • Save cevek/af0f3064325b114d29c8 to your computer and use it in GitHub Desktop.
Save cevek/af0f3064325b114d29c8 to your computer and use it in GitHub Desktop.
atomExpression
function atomExpression(node){
var name = '';
var atomNames = 'Atom|AtomFormula|List|ListFormula';
if (atomNames.indexOf(node.expression.text) > -1){
var exps = [];
var n = node;
var ns = [];
var isReturn = false;
var isThis = false;
var ex = false;
while (n = n.parent){
if (n.name && n.kind !== 128){
ns.push(n.name.text);
}
if (isReturn && (n.kind == 128)){
exps.push(n.name.text);
}
if (n.kind == 180){
isReturn = true;
ex = true;
}
if (n.left && !ex){
ex = true;
var exp = n.left;
do {
if (exp.argumentExpression){
exps.push('[]');
}
if (exp.text) {
exps.push(exp.text);
}
if (exp.name) {
exps.push(exp.name.text);
}
if (exp.kind == 92){
isThis = true;
}
}
while (exp = exp.expression);
}
}
ns = ns.reverse().join(".");
exps = exps.reverse().join(".").replace(/\.\[\]/g, '[]');
//console.log("ns", ns);
//console.log("exps", exps);
//console.log("isThis", isThis);
var prevParams = node.arguments.length ? ',' : '';
for (var i = node.arguments.length; i < 3; i++) {
prevParams += 'void 0,';
}
name = prevParams + "'" + ns + (ns && exps ? (isThis ? '.' : ':') : '') + exps + "'";
/*
var cache = [];
var json = JSON.stringify(node, function(key, value) {
if (typeof value === 'object' && value !== null) {
if (cache.indexOf(value) !== -1) {
// Circular reference found, discard key
return '[circular '+ (value.id ? value.id : '') +']';
}
// Store value in our collection
cache.push(value);
}
if (typeof value == 'object' && value && value.kind){
value._kind = value.kind;
}
return value;
});
*/
return name;
}
return '';
}
function emitNewExpression(node) {
write("new ");
emit(node.expression);
cache = null; // Enable garbage collection
//console.log(json);
if (node.arguments) {
write("(");
emitCommaList(node.arguments);
write(atomExpression(node));
write(")");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment