Skip to content

Instantly share code, notes, and snippets.

@srikumarks
Created March 29, 2014 02:28
Show Gist options
  • Select an option

  • Save srikumarks/9847260 to your computer and use it in GitHub Desktop.

Select an option

Save srikumarks/9847260 to your computer and use it in GitHub Desktop.
sweetjs's resolveCtx rewritten recursively
// (Syntax) -> String
function resolveCtx(originalName, ctx, stop_spine, stop_branch) {
if (ctx instanceof Mark) {
return resolveCtx(originalName, ctx.context, stop_spine, stop_branch);
}
if (ctx instanceof Def) {
if (stop_spine.indexOf(ctx.defctx) !== -1) {
return resolveCtx(originalName, ctx.context, stop_spine, stop_branch);
}
return resolveCtx(originalName, renames(ctx.defctx, ctx.context, originalName), stop_spine, unionEl(stop_branch, ctx.defctx));
}
if (ctx instanceof Rename) {
if (originalName === ctx.id.token.value) {
var idName = resolveCtx(ctx.id.token.value, ctx.id.context, stop_branch, stop_branch);
var subName = resolveCtx(originalName, ctx.context, unionEl(stop_spine, ctx.def), stop_branch);
if (idName === subName) {
var idMarks = marksof(ctx.id.context, originalName + "$" + ctx.name, originalName);
var subMarks = marksof(ctx.context, originalName + "$" + ctx.name, originalName);
if (arraysEqual(idMarks, subMarks)) {
return originalName + "$" + ctx.name;
}
}
}
return resolveCtx(originalName, ctx.context, stop_spine, stop_branch);
}
return originalName;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment