Ejemplo n.º 1
0
  void ExprCompiler::visit(ForExpr& expr, int dest)
  {
    // TODO(bob): Hackish. An actual intermediate representation would help
    // here.
    int iterateMethod = compiler_.findMethod(String::create("0:iterate"));
    ASSERT(iterateMethod != -1, "Should have 'iterate' method in core.");

    int advanceMethod = compiler_.findMethod(String::create("0:advance"));
    ASSERT(advanceMethod != -1, "Should have 'advance' method in core.");

    // Evaluate the iteratable expression.
    int iterator = makeTemp();
    compile(expr.iterator(), iterator);

    // Then call "iterate" on it to get an iterator.
    // TODO(bob): Hackish. An actual intermediate representation would help
    // here.
    write(expr, OP_CALL, iterateMethod, iterator, iterator);

    int loopStart = startJumpBack();

    // Call "advance" on the iterator.
    write(expr, OP_CALL, advanceMethod, iterator, dest);

    // If done, jump to exit.
    int doneSlot = makeTemp();
    write(expr, OP_BUILT_IN, BUILT_IN_DONE, doneSlot);
    write(expr, OP_EQUAL, dest, doneSlot, doneSlot);

    int loopExit = startJump(expr);
    releaseTemp(); // doneSlot.

    // Match on the loop pattern.
    compile(expr.pattern(), dest);

    // Compile the body.
    Loop loop(this);
    compile(expr.body(), dest);

    endJumpBack(expr, loopStart);
    endJump(loopExit, OP_JUMP_IF_TRUE, doneSlot);
    loop.end();
    
    releaseTemp(); // iterator.

    // TODO(bob): Need to figure out what the result value should be.
  }
Ejemplo n.º 2
0
  void Resolver::visit(ForExpr& expr, int dummy)
  {
    // Resolve the iterator in its own scope.
    Scope iteratorScope(this);
    resolve(expr.iterator());
    iteratorScope.end();

    // Resolve the body (including the loop pattern) in its own scope.
    Scope loopScope(this);
    
    scope_->resolve(*expr.pattern());
    
    numLoops_++;
    resolve(expr.body());
    numLoops_--;

    loopScope.end();
  }