예제 #1
0
  void rb_undef(VALUE handle, ID name) {
    NativeMethodEnvironment* env = NativeMethodEnvironment::get();

    Symbol* sym = reinterpret_cast<Symbol*>(name);
    rb_undef_method(handle, sym->c_str(env->state()));
    // In MRI, rb_undef also calls the undef_method hooks, maybe we should?
  }
예제 #2
0
  void CallFrame::print_backtrace(STATE) {
    CallFrame* cf = this;

    while(cf) {
      if(!cf->cm) {
        cf = static_cast<CallFrame*>(cf->previous);
        continue;
      }

      std::cout << static_cast<void*>(cf) << ": ";

      if(cf->is_block_p(state)) {
        std::cout << "__block__";
      } else {
        if(MetaClass* meta = try_as<MetaClass>(cf->module())) {
          if(Module* mod = try_as<Module>(meta->attached_instance())) {
            std::cout << mod->name()->c_str(state) << ".";
          } else {
            std::cout << "#<" <<
              meta->attached_instance()->class_object(state)->name()->c_str(state) <<
              ":" << (void*)meta->attached_instance() << ">.";
          }
        } else {
          const char* mod_name;
          if(cf->module()->nil_p()) {
            mod_name = cf->cm->scope()->module()->name()->c_str(state);
          } else if(cf->module()->name()->nil_p()) {
            mod_name = cf->cm->scope()->module()->name()->c_str(state);
          } else {
            mod_name = cf->module()->name()->c_str(state);
          }
          std::cout << mod_name << "#";
        }

        Symbol* name = try_as<Symbol>(cf->name());
        if(name) {
          std::cout << name->c_str(state);
        } else {
          std::cout << cf->cm->name()->c_str(state);
        }
      }

      std::cout << " in ";
      if(Symbol* file_sym = try_as<Symbol>(cf->cm->file())) {
        std::cout << file_sym->c_str(state) << ":" << cf->line(state);
      } else {
        std::cout << "<unknown>";
      }

      std::cout << " (+" << cf->ip();
      if(cf->is_inline_frame()) {
        std::cout << " inline";
      }
      std::cout << ")";

      std::cout << std::endl;
      cf = static_cast<CallFrame*>(cf->previous);
    }

  }
예제 #3
0
SEXP mutate_not_grouped(DataFrame df, const LazyDots& dots) {
  int nexpr = dots.size();
  int nrows = df.nrows();

  NamedListAccumulator<DataFrame> accumulator;
  int nvars = df.size();
  if (nvars) {
    CharacterVector df_names = df.names();
    for (int i=0; i<nvars; i++) {
      accumulator.set(Symbol(df_names[i]), df[i]);
    }
  }

  CallProxy call_proxy(df);
  List results(nexpr);

  for (int i=0; i<nexpr; i++) {
    Rcpp::checkUserInterrupt();
    const Lazy& lazy = dots[i];

    Shield<SEXP> call_(lazy.expr());
    SEXP call = call_;
    Symbol name = lazy.name();
    Environment env = lazy.env();
    call_proxy.set_env(env);

    if (TYPEOF(call) == SYMSXP) {
      if (call_proxy.has_variable(call)) {
        results[i] = call_proxy.get_variable(PRINTNAME(call));
      } else {
        results[i] = shared_SEXP(env.find(CHAR(PRINTNAME(call))));
      }
    } else if (TYPEOF(call) == LANGSXP) {
      call_proxy.set_call(call);
      results[i] = call_proxy.eval();
    } else if (Rf_length(call) == 1) {
      boost::scoped_ptr<Gatherer> gather(constant_gatherer(call, nrows));
      results[i] = gather->collect();
    } else if (Rf_isNull(call)) {
      accumulator.rm(name);
      continue;
    } else {
      stop("cannot handle");
    }

    check_supported_type(results[i], name.c_str());

    if (Rf_inherits(results[i], "POSIXlt")) {
      stop("`mutate` does not support `POSIXlt` results");
    }
    int n_res = Rf_length(results[i]);
    if (n_res == nrows) {
      // ok
    } else if (n_res == 1) {
      // recycle
      boost::scoped_ptr<Gatherer> gather(constant_gatherer(results[i] , df.nrows()));
      results[i] = gather->collect();
    } else {
      stop("wrong result size (%d), expected %d or 1", n_res, nrows);
    }

    call_proxy.input(name, results[i]);
    accumulator.set(name, results[i]);
  }
  List res = structure_mutate(accumulator, df, classes_not_grouped());

  return res;
}
예제 #4
0
파일: symbol.cpp 프로젝트: gustin/rubinius
 void Symbol::Info::show(STATE, Object* self, int level) {
   Symbol* sym = try_as<Symbol>(self);
   std::cout << ":" << sym->c_str(state) << std::endl;
 }
예제 #5
0
template<bool isHumanReadable> void TextualOutputPort::print(const VM* theVM, Object o)
{
    if (o.isTrue()) {
        putString(UC("#t"));
    } else if (o.isFalse()) {
        putString(UC("#f"));
    } else if (o.isNil()) {
        putString(UC("()"));
    } else if (o.isUndef()) {
        putString(UC("#<unspecified>"));
    } else if (o.isUnbound()) {
        putString(UC("#<unbound variable>"));
    } else if (o.isEof()) {
        putString(UC("#<eof-object>"));
    } else if (o.isCallable()) {
        putString(UC("callable"));
    } else if (o.isFixnum()) {
        char buf[32];
        snprintf(buf, 32, "%ld", (long)o.toFixnum());
        putString(buf);
    } else if (o.isFlonum()) {
        Flonum* const flonum = o.toFlonum();
        putString(FlonumUtil::flonumToUcs4String(flonum->value(), false));
//         char buf[512];
//         if (flonum->isNan()) {
//             putString(UC("+nan.0"));
//         } else if (flonum->isInfinite()) {
//             if (flonum->value() > 0.0) {
//                 putString(UC("+inf.0"));
//             } else {
//                 putString(UC("-inf.0"));
//             }
//         } else {
//             snprintf(buf, sizeof(buf), "%f", flonum->value());
//             putString(buf);
//             #if 0
//             snprintf(buf, sizeof(buf), "%.20g", flonum->value());
//             size_t n = strcspn(buf, ".eE");
//             if (buf[n]) {
//                 putString(buf);
//             } else {
//                 strcat(buf,".0");
//                 putString(buf);
//             }
//             #endif
//         }
    } else if (o.isInstruction()) {
        char buf[32];
        snprintf(buf, 32, "[insn %d]", o.toInstruction());
        putString(buf);
    } else if (o.isCompilerInstruction()) {
        char buf[32];
        snprintf(buf, 32, "[comp:%d]", o.toCompilerInstruction());
        putString(buf);
    } else if (o.isChar()) {
        if (isHumanReadable) {
            putCharHandleSpecial(o.toChar());
        } else { // isHumanReadable = false
            putString(UC("#\\"));
            ucs4char c = o.toChar();
            switch (c) {
            case 0:
                putString(UC("nul"));
                break;
            case ' ':
                putString(UC("space"));
                break;
            case '\n':
                // R6RS (4.2.6 Characters) says: "The #\newline notation is retained for backward compatibility. Its
                // use is deprecated; #\linefeed should be used instead."
                putString(UC("linefeed"));
                break;
            case '\a':
                putString(UC("alarm"));
                break;
            case '\b':
                putString(UC("backspace"));
                break;
            case '\t':
                putString(UC("tab"));
                break;
            case '\v':
                putString(UC("vtab"));
                break;
            case 0x0C:
                putString(UC("page"));
                break;
            case 0x0D:
                putString(UC("return"));
                break;
            case 0x1B:
                putString(UC("esc"));
                break;
            case 0x7F:
                putString(UC("delete"));
                break;

            default:
                putCharHandleSpecial(c);
            }
        }
    } else if (o.isString()) {
        if (isHumanReadable) {
            putString(o.toString());
        } else {
            const ucs4char DOUBLE_QUOTE = '\"';
            const ucs4char ESCAPSE      = '\\';

            // Escape patterns.
            //   [a][\n][b]                           => [\"][a][\\][n][b][\"]
            //   [a][\\][\n][b]                       => [\"][a][\\][\\][\\][n][b][\"]
            //   [a][\"][b][\"][c]                    => [\"][a][\\][\"][b][\\][\"][c]
            //   [a][\"][b][\\][\"][c][\\][\"][\"][d] => [\"][a][\\][\"][[b][\\][\\][\\][\"][c][\\][\\][\\][\"][\\][\"][d]
            ucs4string& s = o.toString()->data();
            putChar(DOUBLE_QUOTE);
            for (size_t i = 0; i < s.size(); i++) {
                const ucs4char ch = s[i];
                switch(ch) {
                case(ESCAPSE):
                    putChar(ESCAPSE);
                    putChar(ESCAPSE);
                    break;
                case '\n':
                    putChar(ESCAPSE);
                    putChar('n');
                    break;
                case '\a':
                    putChar(ESCAPSE);
                    putChar('a');
                    break;
                case '\b':
                    putChar(ESCAPSE);
                    putChar('b');
                    break;
                case '\t':
                    putChar(ESCAPSE);
                    putChar('t');
                    break;
                case '\v':
                    putChar(ESCAPSE);
                    putChar('v');
                    break;
                case '\r':
                    putChar(ESCAPSE);
                    putChar('r');
                    break;

                case DOUBLE_QUOTE:
                    putChar(ESCAPSE);
                    putChar(DOUBLE_QUOTE);
                    break;
                default:
                    putCharHandleSpecial(ch);
                }
            }
            putChar(DOUBLE_QUOTE);
        }
    } else if (o.isPair()) {
        bool abbreviated = o.cdr().isPair() && o.cdr().cdr().isNil() && writeAbbreviated(o.car());
        if (abbreviated) {
            o = o.cdr();
        } else {
            putChar('(');
        }
        bool head = true;
        for (Object e = o; e != Object::Nil; e = e.cdr()) {
            if (head) head = false;
            else putChar(' ');
            if (e.isPair()) {
                if (e.car() == Symbol::UNQUOTE) {
                    if (e.cdr().isPair() && e.cdr().cdr().isNil()) {
                        putString(". ,");
                        print<isHumanReadable>(theVM, e.cdr().car());
                        break;
                    }
                }
                print<isHumanReadable>(theVM, e.car());
            } else {
                putString(". ");
                print<isHumanReadable>(theVM, e);
                break;
            }
        }
        if (!abbreviated) {
            putChar(')');
        }
        return;
    } else if (o.isVector()) {
        Vector* v = o.toVector();
        putString(UC("#("));
        for (int i = 0; i < v->length(); i++) {
            print<isHumanReadable>(theVM, v->ref(i));
            if (i != v->length() - 1) putChar(' ');
        }
        putString(UC(")"));
    } else if (o.isSymbol()) {
        Symbol* symbol = o.toSymbol();
//        Object s = symbol->toString();
        const ucs4string& content = symbol->c_str();
        const ucs4char start = content[0];
        if ((start >= '0' && start <= '9') || (start == ' ')) {
            char buf[16];
            snprintf(buf, 16, "\\x%x;", start);
            putString(buf);
        } else {
            putChar(start);
        }

        for (uintptr_t i = 1; i < content.size(); i++) {
            const ucs4char ch = content[i];
            // not enough
            if (ch == ' ') {
                char buf[16];
                snprintf(buf, 16, "\\x%x;", ch);
                putString(buf);
            } else {
                putChar(ch);
            }
        }
    } else if (o.isRegexp()) {
        putChar('#');
        putChar('/');
        putString(o.toRegexp()->pattern());
        putChar('/');
    } else if (o.isSocket()) {
        putString(o.toSocket()->toString());
    } else if (o.isRegMatch()) {
        putString(UC("#<reg-match>"));
    } else if (o.isEqHashTable()) {
        putString(UC("#<eq-hashtable>"));
    } else if (o.isEqvHashTable()) {
        putString(UC("#<eqv-hashtable>"));
    } else if (o.isGenericHashTable()) {
        putString(UC("#<hashtable>"));
    } else if (o.isClosure()) {
        putString(UC("#<closure "));
        print<isHumanReadable>(theVM, Object::makeFixnum(o.val));
        putString(UC(">"));
    } else if (o.isCProcedure()) {

        // Reader.y doesn't have VM instance.
        if (theVM != NULL) {
            putString(UC("#<subr "));
            print<isHumanReadable>(theVM, theVM->getCProcedureName(o));
            putString(UC(">"));
        } else {
            putString(UC("#<subr>"));
        }
    } else if (o.isByteVector()) {
        ByteVector* const byteVector = o.toByteVector();
        const int length = byteVector->length();
        putString(UC("#vu8("));
        for (int i = 0; i < length; i++) {
            if (i != 0) {
                putString(" ");
            }
            print<isHumanReadable>(theVM, Object::makeFixnum(byteVector->u8Ref(i)));
        }
        putString(UC(")"));
    } else if (o.isBox()) {
        putString(UC("#<box>"));
    } else if (o.isTextualOutputPort()) {
        putString(o.toTextualOutputPort()->toString());
    } else if (o.isStack()) {
        putString(UC("#<stack>"));
    } else if (o.isCodec()) {
        Codec* codec = o.toCodec();
        putString(UC("#<codec "));
        putString(codec->getCodecName());
        putString(UC(">"));
    } else if (o.isBinaryInputPort()) {
        putString(o.toBinaryInputPort()->toString().data());
    } else if (o.isBinaryOutputPort()) {
        putString(o.toBinaryOutputPort()->toString().data());
    } else if (o.isBinaryInputOutputPort()) {
        putString(o.toBinaryInputOutputPort()->toString().data());
    } else if (o.isRecordConstructorDescriptor()) {
        putString(UC("#<record-constructor-descriptor>"));
    } else if (o.isRecordTypeDescriptor()) {
        putString(UC("#<record-type-descriptor>"));
    } else if (o.isCompoundCondition()) {
        putString(UC("#<compound-condition "));
        CompoundCondition* const c = o.toCompoundCondition();
        const ObjectVector& conditions = c->conditions();
        for (ObjectVector::const_iterator it = conditions.begin(); it != conditions.end(); ++it) {
            if (it != conditions.begin()) {
                putString(UC(" "));
            }
            print<isHumanReadable>(theVM, *it);
        }
        putString(UC(">"));
    } else if (o.isRecord()) {
        Record* const record = o.toRecord();
        putString(UC("#<record "));
        print<isHumanReadable>(theVM, record->recordTypeDescriptor()->name());
//         for (int i = 0; i < record->fieldsLength(); i++) {
//             print<isHumanReadable>(record->fieldAt(i));
//             putString(UC(" "));
//         }
        putString(UC(">"));
    } else if (o.isSimpleStruct()) {
        putString(UC("#<"));
        print<isHumanReadable>(theVM, o.toSimpleStruct()->name());
        putString(UC(">"));
    } else if (o.isObjectPointer()) {
        putString(UC("#<object pointer>"));
    } else if (o.isTextualInputPort()) {
        putString(o.toTextualInputPort()->toString());
    } else if (o.isTextualOutputPort()) {
        putString(UC("#<textual-output-port>"));
    } else if (o.isRatnum()) {
        putString(o.toRatnum()->toString());
    } else if (o.isBignum()) {
        putString(o.toBignum()->toString());
    } else if (o.isVM()) {
        putString(o.toVM()->toString());
    } else if (o.isConditionVariable()) {
        putString(o.toConditionVariable()->toString());
    } else if (o.isMutex()) {
        putString(UC("#<mutex>"));
    } else if (o.isCompnum()) {
        Compnum* const c = o.toCompnum();
        const Object real = c->real();
        const Object imag = c->imag();
        if (!Arithmetic::isExactZero(real)) {
            print<isHumanReadable>(theVM, real);
        }
        if (Arithmetic::ge(imag, Object::makeFixnum(0)) &&
            !(imag.isFlonum() && (imag.toFlonum()->isNegativeZero() || (imag.toFlonum()->isInfinite())))) {
            putString(UC("+"));
        } else {
        }
        print<isHumanReadable>(theVM, imag);
        putString(UC("i"));
    } else if (o.isCodeBuilder()) {
        putString(UC("<code-builder "));
        print<isHumanReadable>(theVM, Object::makeFixnum(o.val));
        putString(UC(">"));
    } else if (o.isTranscoder()) {
        Transcoder* transcoder = o.toTranscoder();
        putString(UC("<transcoder codec="));
        print<isHumanReadable>(theVM, transcoder->codec());
        putString(UC(", eol-style="));
        print<isHumanReadable>(theVM, transcoder->eolStyleSymbol());
        putString(UC(", error-handling-mode="));
        print<isHumanReadable>(theVM, transcoder->errorHandlingModeSymbol());
        putString(UC(">"));
    } else if (o.isPointer()) {
        putString(UC("#<pointer "));
        char buf[16];
        snprintf(buf, 16, "%x", o.toPointer()->pointer());
        putString(buf);
        putString(UC(">"));
    } else if (o.isContinuation()) {
        putString(UC("#<continuation>"));
    } else {
        putString(UC("#<unknown datum>"));
    }
}