Example #1
0
 void recurseSetContext(PythonTree tree, expr_contextType context) {
   if (tree instanceof Context) {
     ((Context) tree).setContext(context);
   }
   if (tree instanceof GeneratorExp) {
     GeneratorExp g = (GeneratorExp) tree;
     recurseSetContext(g.getInternalElt(), context);
   } else if (tree instanceof ListComp) {
     ListComp lc = (ListComp) tree;
     recurseSetContext(lc.getInternalElt(), context);
   } else if (tree instanceof SetComp) {
     SetComp sc = (SetComp) tree;
     recurseSetContext(sc.getInternalElt(), context);
   } else if (tree instanceof DictComp) {
     DictComp dc = (DictComp) tree;
     recurseSetContext(dc.getInternalKey(), context);
     recurseSetContext(dc.getInternalValue(), context);
   } else if (!(tree instanceof ListComp)
       && (!(tree instanceof DictComp))
       && (!(tree instanceof SetComp))) {
     for (int i = 0; i < tree.getChildCount(); i++) {
       recurseSetContext(tree.getChild(i), context);
     }
   }
 }
Example #2
0
  public void visitDictComp(DictComp node, int level) throws Exception {
    String iter = getUnique("for$iter");
    String result = getUnique("compResult$");
    String start = getUnique("compStart");
    String end = getUnique("compEnd");

    addLine("PUSH $newDict");
    addLine("CALL 0");
    addLine("POP " + result);

    comprehension comp = node.getInternalGenerators().get(level);

    visit(comp.getInternalIter());
    addLine("SELECTMEMBER __iter__");
    addLine("PUSHMEMBER");
    addLine("CALL 0");
    addLine("POP " + iter);

    addLine("LABEL " + start);

    addLine("EXCEPT StopIteration " + end);
    addLine("PUSH " + iter);
    addLine("SELECTMEMBER next");
    addLine("PUSHMEMBER");
    addLine("CALL 0");
    visit(comp.getInternalTarget());
    addLine("ENDEXCEPT");

    for (expr ifexpr : comp.getInternalIfs()) {
      visit(ifexpr);
      addLine("JUMPIFFALSE " + start);
    }

    if (node.getInternalGenerators().size() == level + 1) {
      visit(node.getInternalValue());
      addLine("PUSH " + result);
      visit(node.getInternalKey());
      addLine("POPITEM");
    } else {
      visitDictComp(node, level + 1);
      addLine("PUSH " + result);
      addLine("SELECTMEMBER update");
      addLine("PUSHMEMBER");
      addLine("CALL 1");
    }

    addLine("JUMP " + start);
    addLine("LABEL " + end);
    addLine("POP $");
    addLine("PUSH " + result);
  }