当前位置: 首页>>代码示例>>C++>>正文


C++ Argument::path方法代码示例

本文整理汇总了C++中Argument::path方法的典型用法代码示例。如果您正苦于以下问题:C++ Argument::path方法的具体用法?C++ Argument::path怎么用?C++ Argument::path使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Argument的用法示例。


在下文中一共展示了Argument::path方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: bind

  void bind(string callee, Parameters* ps, Arguments* as, Context& ctx, Env* env, Eval* eval)
  {
    map<string, Parameter*> param_map;

    // Set up a map to ensure named arguments refer to actual parameters. Also
    // eval each default value left-to-right, wrt env, populating env as we go.
    for (size_t i = 0, L = ps->length(); i < L; ++i) {
      Parameter*  p = (*ps)[i];
      param_map[p->name()] = p;
      // if (p->default_value()) {
      //   env->current_frame()[p->name()] = p->default_value()->perform(eval->with(env));
      // }
    }

    // plug in all args; if we have leftover params, deal with it later
    size_t ip = 0, LP = ps->length();
    size_t ia = 0, LA = as->length();
    while (ia < LA) {
      if (ip >= LP) {
        stringstream msg;
        msg << callee << " only takes " << LP << " arguments; "
            << "given " << LA;
        error(msg.str(), as->path(), as->position());
      }
      Parameter* p = (*ps)[ip];
      Argument*  a = (*as)[ia];

      // If the current parameter is the rest parameter, process and break the loop
      if (p->is_rest_parameter()) {
        if (a->is_rest_argument()) {
          // rest param and rest arg -- just add one to the other
          if (env->current_frame_has(p->name())) {
            *static_cast<List*>(env->current_frame()[p->name()])
            += static_cast<List*>(a->value());
          }
          else {
            env->current_frame()[p->name()] = a->value();
          }
        } else {

          // copy all remaining arguments into the rest parameter, preserving names
          List* arglist = new (ctx.mem) List(p->path(),
                                             p->position(),
                                             0,
                                             List::COMMA,
                                             true);
          env->current_frame()[p->name()] = arglist;
          while (ia < LA) {
            a = (*as)[ia];
            (*arglist) << new (ctx.mem) Argument(a->path(),
                                                 a->position(),
                                                 a->value(),
                                                 a->name(),
                                                 false);
            ++ia;
          }
        }
        ++ip;
        break;
      }

      // If the current argument is the rest argument, extract a value for processing
      else if (a->is_rest_argument()) {
        // normal param and rest arg
        List* arglist = static_cast<List*>(a->value());
        // empty rest arg - treat all args as default values
        if (!arglist->length()) {
          break;
        }
        // otherwise move one of the rest args into the param, converting to argument if necessary
        if (arglist->is_arglist()) {
          a = static_cast<Argument*>((*arglist)[0]);
        } else {
          Expression* a_to_convert = (*arglist)[0];
          a = new (ctx.mem) Argument(a_to_convert->path(), a_to_convert->position(), a_to_convert, "", false);
        }
        arglist->elements().erase(arglist->elements().begin());
        if (!arglist->length() || (!arglist->is_arglist() && ip + 1 == LP)) {
          ++ia;
        }
      } else if (a->is_keyword_argument()) {
        Map* argmap = static_cast<Map*>(a->value());

        for (auto key : argmap->keys()) {
          string name = "$" + unquote(static_cast<String_Constant*>(key)->value());

          if (!param_map.count(name)) {
            stringstream msg;
            msg << callee << " has no parameter named " << name;
            error(msg.str(), a->path(), a->position());
          }
          env->current_frame()[name] = argmap->at(key);
        }
        ++ia;
        continue;
      } else {
        ++ia;
      }

      if (a->name().empty()) {
//.........这里部分代码省略.........
开发者ID:02002love,项目名称:ioniclub,代码行数:101,代码来源:bind.cpp

示例2: bind

  void bind(string callee, Parameters* ps, Arguments* as, Context& ctx, Env* env, Eval* eval)
  {
    map<string, Parameter*> param_map;

    // Set up a map to ensure named arguments refer to actual parameters. Also
    // eval each default value left-to-right, wrt env, populating env as we go.
    for (size_t i = 0, L = ps->length(); i < L; ++i) {
      Parameter*  p = (*ps)[i];
      param_map[p->name()] = p;
      // if (p->default_value()) {
      //   env->current_frame()[p->name()] = p->default_value()->perform(eval->with(env));
      // }
    }

    // plug in all args; if we have leftover params, deal with it later
    size_t ip = 0, LP = ps->length();
    size_t ia = 0, LA = as->length();
    while (ia < LA) {
      if (ip >= LP) {
        stringstream msg;
        msg << callee << " only takes " << LP << " arguments; "
            << "given " << LA;
        error(msg.str(), as->path(), as->line());
      }
      Parameter* p = (*ps)[ip];
      Argument*  a = (*as)[ia];

      if (a->is_rest_argument() && p->is_rest_parameter()) {
        // rest param and rest arg -- just add one to the other
        if (env->current_frame_has(p->name())) {
          *static_cast<List*>(env->current_frame()[p->name()])
                              += static_cast<List*>(a->value());
        }
        else {
          env->current_frame()[p->name()] = a->value();
        }
        ++ia;
        ++ip;
      }
      else if (p->is_rest_parameter()) {
        List* arglist = 0;
        if (!env->current_frame_has(p->name())) {
          arglist = new (ctx.mem) List(p->path(),
                                       p->line(),
                                       0,
                                       List::COMMA,
                                       true);
          env->current_frame()[p->name()] = arglist;
        }
        else {
          arglist = static_cast<List*>(env->current_frame()[p->name()]);
        }
        *arglist << a->value(); // TODO: named args going into rest-param?
        ++ia;
      }
      else if (a->is_rest_argument()) {
        // normal param and rest arg
        if (env->current_frame_has(p->name())) {
          stringstream msg;
          msg << "parameter " << p->name()
              << " provided more than once in call to " << callee;
          error(msg.str(), a->path(), a->line());
        }
        List* arglist = static_cast<List*>(a->value());
        // if it's the last param, move the whole arglist into it
        if (ip == LP-1) {
          env->current_frame()[p->name()] = arglist;
          ++ia;
        }
        // otherwise move one of the rest args into the param and loop
        else {
          env->current_frame()[p->name()] = (*arglist)[0];
          arglist->elements().erase(arglist->elements().begin());
        }
        ++ip;
      }
      else if (a->name().empty()) {
        // ordinal arg -- bind it to the next param
        env->current_frame()[p->name()] = a->value();
        ++ip;
        ++ia;
      }
      else {
        // named arg -- bind it to the appropriately named param
        if (!param_map.count(a->name())) {
          stringstream msg;
          msg << callee << " has no parameter named " << a->name();
          error(msg.str(), a->path(), a->line());
        }
        if (param_map[a->name()]->is_rest_parameter()) {
          stringstream msg;
          msg << "argument " << a->name() << " of " << callee
              << "cannot be used as named argument";
          error(msg.str(), a->path(), a->line());
        }
        if (env->current_frame_has(a->name())) {
          stringstream msg;
          msg << "parameter " << p->name()
              << "provided more than once in call to " << callee;
          error(msg.str(), a->path(), a->line());
//.........这里部分代码省略.........
开发者ID:7strel,项目名称:revamp-framework,代码行数:101,代码来源:bind.cpp


注:本文中的Argument::path方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。