static PT_Tree pList(PT_Tree tree, int *i, SDF_Symbol ssym) { PT_Tree layout; PT_Args elems; PT_Args newElems; ATbool isLayout; int indent = *i; assert(PT_isTreeApplList(tree) && "This tool only supports AsFix2ME"); assert(SDF_isSymbolIterStar(ssym) || SDF_isSymbolIter(ssym)); layout = (PT_Tree) SDF_getSymbolWsAfterSymbol(ssym); elems = PT_getTreeArgs(tree); newElems = PT_makeArgsEmpty(); isLayout = ATfalse; for (; !PT_isArgsEmpty(elems); elems = PT_getArgsTail(elems)) { PT_Tree head = PT_getArgsHead(elems); if (isLayout) { newElems = PT_makeArgsList(createLayoutTree(*i, layout), newElems); *i = calcIndentationLevel(*i, layout); } else { newElems = PT_makeArgsList(pTree(head, i), newElems); } isLayout = !isLayout; *i = indent; } return PT_setTreeArgs(tree, PT_reverseArgs(newElems)); }
PT_Tree PT_findTreeParentRecursive(PT_Tree needle, PT_Tree haystack) { assert(needle != NULL); assert(haystack != NULL); assert(needle != haystack); if (ATtableGet(findParentCache, PT_TreeToTerm(haystack)) != NULL) { return NULL; } if (PT_hasTreeArgs(haystack)) { PT_Args children = PT_getTreeArgs(haystack); while (!PT_isArgsEmpty(children)) { PT_Tree child = PT_getArgsHead(children); if (PT_isEqualTree(child, needle)) { return haystack; } else { PT_Tree suspect = PT_findTreeParentRecursive(needle, child); if (suspect != NULL) { return suspect; } } children = PT_getArgsTail(children); } } ATtablePut(findParentCache, PT_TreeToTerm(haystack), PT_TreeToTerm(NeedleNotHere)); return NULL; }
static ATbool PT_containsArgsCycle(PT_Args args) { for ( ; !PT_isArgsEmpty(args); args = PT_getArgsTail(args)) { if (PT_containsTreeCycle(PT_getArgsHead(args))) { return ATtrue; } } return ATfalse; }
static PT_Args flattenArgsAgain(PT_Args args) { PT_Args newArgs = PT_makeArgsEmpty(); for ( ; !PT_isArgsEmpty(args); args = PT_getArgsTail(args)) { PT_Tree arg = PT_getArgsHead(args); newArgs = PT_makeArgsMany(flattenTreeAgain(arg), newArgs); } return PT_reverseArgs(newArgs); }
static PT_Args pArgs(PT_Args args, int *i) { int indent = *i; PT_Args newArgs = PT_makeArgsEmpty(); for(; !PT_isArgsEmpty(args); args = PT_getArgsTail(args)) { PT_Tree head = PT_getArgsHead(args); newArgs = PT_makeArgsList(pTree(head, i), newArgs); *i = indent; } return PT_reverseArgs(newArgs); }
static PT_Args flattenListAgain(PT_Production prod, PT_Args elems) { PT_Args newElems = PT_makeArgsEmpty(); for ( ; !PT_isArgsEmpty(elems); elems = PT_getArgsTail(elems)) { PT_Tree arg = PT_getArgsHead(elems); if (PT_hasTreeProd(arg)) { ATbool nested = ATfalse; PT_Production argProd = PT_getTreeProd(arg); if (PT_isEqualProduction(prod, argProd)) { nested = ATtrue; } else if (PT_isProductionList(argProd)) { PT_Symbol rhs = PT_getProductionRhs(prod); PT_Symbol rhsArg = PT_getProductionRhs(argProd); if (PT_isSymbolCf(rhs) || PT_isSymbolLex(rhs)) { rhs = PT_getSymbolSymbol(rhs); } if (PT_isSymbolCf(rhsArg) || PT_isSymbolLex(rhsArg)) { rhsArg = PT_getSymbolSymbol(rhsArg); } if (PT_isSymbolIterStar(rhs) && PT_isSymbolIterPlus(rhsArg) && PT_isEqualSymbol(PT_getSymbolSymbol(rhs), PT_getSymbolSymbol(rhsArg))) { nested = ATtrue; } } if (nested) { PT_Args nestedElems = PT_getTreeArgs(arg); newElems = PT_concatArgs(PT_reverseArgs(nestedElems), newElems); continue; } } newElems = PT_makeArgsMany(arg, newElems); } return PT_reverseArgs(newElems); }
static PT_Tree pAppl(PT_Tree tree, int *i) { int indent = *i; PT_Args args = PT_getTreeArgs(tree); PT_Production prod = PT_getTreeProd(tree); SDF_Production sprod = (SDF_Production) ATtableGet(mapping, (ATerm) prod); if (sprod != NULL) { PT_Args newArgs = PT_makeArgsEmpty(); SDF_SymbolList slhs = SDF_getSymbolsList(SDF_getProductionSymbols(sprod)); PT_Symbols lhs = PT_getProductionLhs(prod); PT_Tree nextLayout = NULL; for (; !PT_isArgsEmpty(args); args = PT_getArgsTail(args), lhs = PT_getSymbolsTail(lhs)) { PT_Tree head = PT_getArgsHead(args); PT_Symbol sym = PT_getSymbolsHead(lhs); // here we put in the layout that's in between the symbols on the lhs if (PT_isOptLayoutSymbol(sym)) { assert(nextLayout != NULL); newArgs = PT_makeArgsList(createLayoutTree(indent, nextLayout), newArgs); *i = calcIndentationLevel(indent, nextLayout); nextLayout = NULL; } else { SDF_Symbol ssym = SDF_getSymbolListHead(slhs); PT_Symbol real = PT_getSymbolSymbol(sym); newArgs = PT_makeArgsList(pTreeWithSymbol(head, i, real, ssym), newArgs); if (SDF_hasSymbolListTail(slhs)) { nextLayout = (PT_Tree) SDF_getSymbolListWsAfterFirst(slhs); slhs = SDF_getSymbolListTail(slhs); } } } tree = PT_setTreeArgs(tree, PT_reverseArgs(newArgs)); } else { tree = PT_setTreeArgs(tree, pArgs(args, i)); } return tree; }
static PT_Args renameInArgs(PT_Args trees, PT_Symbol formalParam, PT_Symbol actualParam) { if (PT_isArgsEmpty(trees)) { return trees; } else { PT_Tree head = PT_getArgsHead(trees); PT_Args tail = PT_getArgsTail(trees); PT_Tree newHead; PT_Args newTail; newHead = PT_renameInTree(head, formalParam, actualParam); newTail = renameInArgs(tail, formalParam, actualParam); return PT_setArgsHead(PT_setArgsTail(trees, newTail), newHead); } }