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();
    }
  }
Example #2
0
 void Scope::visit(RecordPattern& pattern, int dummy)
 {
   // Recurse into the fields.
   for (int i = 0; i < pattern.fields().count(); i++)
   {
     pattern.fields()[i].value->accept(*this, dummy);
   }
 }
Example #3
0
  void SignatureBuilder::writeParam(gc<Pattern> pattern)
  {
    // If it's a record, destructure it into the signature.
    RecordPattern* record = pattern->asRecordPattern();
    if (record != NULL)
    {
      for (int i = 0; i < record->fields().count(); i++)
      {
        add(record->fields()[i].name);
        add(":");
      }

      return;
    }

    // Any other pattern is implicitly a single-field record.
    add("0:");
  }
Example #4
0
 void Resolver::allocateSlotsForParam(gc<Pattern> pattern)
 {
   // No parameter so do nothing.
   if (pattern.isNull()) return;
   
   RecordPattern* record = pattern->asRecordPattern();
   if (record != NULL)
   {
     // Allocate each field.
     for (int i = 0; i < record->fields().count(); i++)
     {
       makeParamSlot(record->fields()[i].value);
     }
   }
   else
   {
     // If we got here, the pattern isn't a record, so it's a single slot.
     makeParamSlot(pattern);
   }
 }
Example #5
0
 void Resolver::destructureParam(gc<Pattern> pattern)
 {
   // No parameter so do nothing.
   if (pattern.isNull()) return;
   
   RecordPattern* record = pattern->asRecordPattern();
   if (record != NULL)
   {
     // Resolve each field.
     for (int i = 0; i < record->fields().count(); i++)
     {
       resolveParam(record->fields()[i].value);
     }
   }
   else
   {
     // If we got here, the pattern isn't a record, so its a single slot.
     resolveParam(pattern);
   }
 }
Example #6
0
  void ExprCompiler::compileParam(PatternCompiler& compiler,
                                    gc<Pattern> param, int& slot)
  {
    // No parameter so do nothing.
    if (param.isNull()) return;

    RecordPattern* record = param->asRecordPattern();
    if (record != NULL)
    {
      // Compile each field.
      for (int i = 0; i < record->fields().count(); i++)
      {
        compileParamField(compiler, record->fields()[i].value, slot++);
      }
    }
    else
    {
      // If we got here, the pattern isn't a record, so it's a single slot.
      compileParamField(compiler, param, slot++);
    }
  }
Example #7
0
  void Compiler::declareVariables(gc<Pattern> pattern, Module* module)
  {
    RecordPattern* record = pattern->asRecordPattern();
    if (record != NULL)
    {
      for (int i = 0; i < record->fields().count(); i++)
      {
        declareVariables(record->fields()[i].value, module);
      }

      return;
    }

    VariablePattern* variable = pattern->asVariablePattern();
    if (variable != NULL)
    {
      declareVariable(variable->pos(), variable->name(), module);

      if (!variable->pattern().isNull())
      {
        declareVariables(variable->pattern(), module);
      }
    }
  }