Example #1
0
  void PatternCompiler::visit(RecordPattern& pattern, int value)
  {
    // Recurse into the fields.
    for (int i = 0; i < pattern.fields().count(); i++)
    {
      const PatternField& field = pattern.fields()[i];
      
      // Test and destructure the field. This takes two instructions to encode
      // all of the operands.
      int fieldSlot = compiler_.makeTemp();
      int symbol = compiler_.compiler_.addSymbol(field.name);

      if (jumpOnFailure_)
      {
        compiler_.write(pattern.pos(), OP_TEST_FIELD, value, symbol, fieldSlot);
        tests_.add(MatchTest(compiler_.chunk_->count(), -1));
        compiler_.startJump(pattern.pos());
      }
      else
      {
        compiler_.write(pattern.pos(), OP_GET_FIELD, value, symbol, fieldSlot);
      }

      // Recurse into the pattern, using that field.
      field.value->accept(*this, fieldSlot);

      compiler_.releaseTemp();
    }
  }