bool CJson:: matchObject(const ValueP &value, const std::string &match, ValueP &value1) { if (isDebug()) std::cerr << "matchObject \'" << match << "\'" << std::endl; if (! value->isObject()) { if (! isQuiet()) std::cerr << value->typeName() << " is not an object" << std::endl; return false; } Object *obj = value->cast<Object>(); if (match == "?" || match == "?keys") { std::vector<std::string> names; obj->getNames(names); Array *array = createArray(); for (const auto &n : names) { String *str = createString(n); array->addValue(ValueP(str)); } value1 = ValueP(array); } else if (match == "?type") { String *str = createString(obj->typeName()); value1 = ValueP(str); } else if (match == "?values") { Values values; obj->getValues(values); Array *array = createArray(); for (const auto &v : values) array->addValue(v); value1 = ValueP(array); } else { if (! obj->getNamedValue(match, value1)) { if (! isQuiet()) std::cerr << "no value \'" << match << "\'" << std::endl; return false; } } return true; }
ValueP MPIStackFrame::evalFunctionRefExp(SgFunctionSymbol *sym) { if (sym->get_name() == "MPI_Init") return ValueP(new MPI_InitValue(PTemp, shared_from_this())); return StackFrame::evalFunctionRefExp(sym); }
bool CJson:: matchHier1(const ValueP &value, int /*ind*/, const std::string &lhs, const std::string &rhs, const std::vector<std::string> &keys, Values &ivalues, Values &values) { if (! value->isObject()) { if (! isQuiet()) std::cerr << value->typeName() << " is not an object" << std::endl; return false; } Object *obj = value->cast<Object>(); // name ValueP lvalue; if (obj->getNamedValue(lhs, lvalue)) ivalues.push_back(lvalue); // hier object ValueP rvalue; if (obj->getNamedValue(rhs, rvalue)) { if (! rvalue->isArray()) { if (! isQuiet()) std::cerr << rvalue->typeName() << " is not an object" << std::endl; return false; } Array *array = rvalue->cast<Array>(); int i = 0; for (auto &v : array->values()) { Values ivalues1 = ivalues; matchHier1(v, i, lhs, rhs, keys, ivalues1, values); ++i; } } else { Values kvalues; for (const auto &k : keys) { ValueP kvalue; if (obj->getNamedValue(k, kvalue)) kvalues.push_back(kvalue); } String *str = hierValuesToKey(ivalues, kvalues); values.push_back(ValueP(str)); } return true; }
static kbool_t AppendJsonArray(KonohaContext *kctx, struct JsonBuf *jsonbuf, struct JsonBuf *otherbuf) { JSON json = AsJSON(jsonbuf); if(JSON_TYPE_CHECK(Array, json)) { JSONArray_append((JSONMemoryPool *)(JSONAPI JsonHandler), json, toJSON(ValueP(otherbuf->json_i))); return true; } return false; }
static kbool_t SetJsonArrayAt(KonohaContext *kctx, struct JsonBuf *jsonbuf, size_t index, struct JsonBuf *otherbuf) { JSON json = AsJSON(jsonbuf); if(JSON_TYPE_CHECK(Array, json)) { JSONArray_set(json, index, toJSON(ValueP(otherbuf->json_i))); return true; } return false; }
bool CJson:: loadString(const std::string &lines, ValueP &value) { std::vector<std::string> strs; CStrParse parse(lines); parse.skipSpace(); if (parse.isChar('{')) { // object Object *obj; if (! readObject(parse, obj)) return false; value = ValueP(obj); } else if (parse.isChar('[')) { // array Array *array; if (! readArray(parse, array)) return false; value = ValueP(array); } else { ValueP value1; if (! readValue(parse, value1)) return false; value = value1; } parse.skipSpace(); if (! parse.eof()) return false; return true; }
ValueP ExternalCallingStackFrame::externEvalFunctionRefExp(SgFunctionSymbol *sym) { string symName = sym->get_name().getString(); for (vector<void *>::const_iterator sharedLib = sharedLibraries.begin(); sharedLib != sharedLibraries.end(); ++sharedLib) { void (*fnPtr)() = reinterpret_cast<void(*)()>(dlsym(*sharedLib, symName.c_str())); if (fnPtr != NULL) { return ValueP(new ExternalCallingFunctionValue(symName, fnPtr, PTemp, shared_from_this())); } } throw InterpError("Could not find the function symbol " + symName + " in the current list of shared libraries"); }
ValueP ExternalCallingFunctionValue::call(SgFunctionType *fnType, const vector<ValueP> &args) const { if (fnType->get_argument_list()->get_arguments().size() != args.size()) { throw InterpError("ExternalCalling: function's formal parameter list length differs from actual parameter list length"); } RoseFFI ffi; if (!ffi.prepare(fnType)) { throw InterpError("Could not prepare the function type " + fnType->unparseToString()); } ffi.call(fnPtr, args); return ValueP(); // TODO: get return value }
bool CJson:: matchList(const ValueP &value, int ind, const std::string &lhs, const std::string &rhs, Values &values) { if (isDebug()) std::cerr << "matchList \'" << lhs << "\' \'" << rhs << "\'" << std::endl; if (lhs[0] != '{' || lhs[lhs.size() - 1] != '}') return false; std::string names = lhs.substr(1, lhs.size() - 2); std::vector<std::string> fields; auto p = names.find(","); while (p != std::string::npos) { std::string name = names.substr(0, p); names = names.substr(p + 1); fields.push_back(name); p = names.find(","); } fields.push_back(names); Array *array = createArray(); for (const auto &f : fields) { Values values1; std::string match = (rhs != "" ? f + "/" + rhs : f); matchValues(value, ind, match, values1); for (const auto &v1 : values1) array->addValue(v1); } values.push_back(ValueP(array)); return true; }
static kbool_t SetJsonValue(KonohaContext *kctx, struct JsonBuf *jsonbuf, const char *key, size_t keylen_or_zero, KJSONTYPE type, ...) { JSON val; kbool_t ret = true; va_list ap; va_start(ap, type); val = toJSON(ValueP(NewJsonI((JSONMemoryPool *)(JSONAPI JsonHandler), type, ap))); if(key != NULL) { size_t keylen = KeyLen(key, keylen_or_zero); JSONObject_set((JSONMemoryPool *)(JSONAPI JsonHandler), AsJSON(jsonbuf), key, keylen, val); } else { jsonbuf->json_i = val.bits; } va_end(ap); return ret; }
// load file and return root value bool CJson:: loadFile(const std::string &filename, ValueP &value) { value = ValueP(); FILE *fp = fopen(filename.c_str(), "r"); if (! fp) return false; std::string lines; while (! feof(fp)) { std::string line; if (readLine(fp, line)) lines += line; } fclose(fp); //--- return loadString(lines, value); }
bool CJson:: matchValues(const ValueP &value, int ind, const std::string &match, Values &values) { ValueP value1 = value; std::string match1 = match; auto p = match.find("..."); if (p != std::string::npos) { std::string lhs = match1.substr(0, p); std::string rhs = match1.substr(p + 3); return matchHier(value1, ind, lhs, rhs, values); } if (match1 != "" && match1[0] != '{') { auto p = match1.find("/"); while (p != std::string::npos) { std::string lhs = match1.substr(0, p); std::string rhs = match1.substr(p + 1); if (lhs == "") return false; if (lhs[0] == '[') { return matchArray(value1, lhs, rhs, values); } else if (lhs[0] == '{') { return matchList(value1, ind, lhs, rhs, values); } else { ValueP value2; if (! matchObject(value1, lhs, value2)) return false; value1 = value2; } match1 = rhs; p = match1.find("/"); } } if (match1 == "") return true; if (match1[0] == '[') { return matchArray(value1, match1, "", values); } else if (match1[0] == '{') { return matchList(value1, ind, match1, "", values); } else if (match1[0] == '#') { int base = 0; if (match1.size() > 1) { bool ok; base = CJson::stol(match1.substr(1), ok); } Number *n = createNumber(base + ind); values.push_back(ValueP(n)); } else { ValueP value2; if (! matchObject(value1, match1, value2)) return false; value1 = value2; if (value1) values.push_back(value1); } return true; }
bool CJson:: matchArray(const ValueP &value, const std::string &lhs, const std::string &rhs, Values &values) { if (isDebug()) std::cerr << "matchArray \'" << lhs << "\' \'" << rhs << "\'" << std::endl; if (! value->isArray()) { if (! isQuiet()) std::cerr << value->typeName() << " is not an array" << std::endl; return false; } Array *array = value->cast<Array>(); if (lhs[0] != '[' || lhs[lhs.size() - 1] != ']') return false; std::string range = lhs.substr(1, lhs.size() - 2); if (range == "?size") { Number *n = createNumber(array->size()); values.push_back(ValueP(n)); return true; } auto p = range.find(','); if (p != std::string::npos) { std::string match1 = range; std::string lhs1 = match1.substr(0, p); std::string rhs1 = match1.substr(p + 1); bool ok1, ok2; int i1 = CJson::stol(lhs1, ok1); int i2 = CJson::stol(rhs1, ok2); if (! ok1 || ! ok2) { if (! isQuiet()) std::cerr << "Invalid array indices '" << lhs1 << "', '" << rhs1 << "'" << std::endl; return false; } for (int i = i1; i <= i2 && i < int(array->size()); ++i) { ValueP value1 = array->at(i); if (rhs1 != "") matchValues(value1, i, rhs1, values); else values.push_back(value1); } } else if (range != "") { bool ok; int i1 = CJson::stol(range, ok); if (! ok) { if (! isQuiet()) std::cerr << "Invalid array index '" << lhs << "'" << std::endl; return false; } int i = 0; for (const auto &v : array->values()) { if (i == i1) { if (rhs != "") matchValues(v, i, rhs, values); else values.push_back(v); } ++i; } } else { int i = 0; for (const auto &v : array->values()) { if (rhs != "") matchValues(v, i, rhs, values); else values.push_back(v); ++i; } } return true; }
// read value at file pos bool CJson:: readValue(CStrParse &parse, ValueP &value) { if (parse.eof()) return false; char c = parse.getCharAt(); if (c == '\"') { std::string str1; if (! readString(parse, str1)) return false; value = ValueP(createString(str1)); } else if (c == '-' || isdigit(c)) { std::string str1; if (! readNumber(parse, str1)) return false; bool ok; double n = CJson::stod(str1, ok); value = ValueP(createNumber(n)); } else if (c == '{') { Object *obj; if (! readObject(parse, obj)) return false; value = ValueP(obj); } else if (c == '[') { Array *array; if (! readArray(parse, array)) return false; value = ValueP(array); } else if (parse.isString("true")) { parse.skipChars("true"); value = ValueP(createTrue()); } else if (parse.isString("false")) { parse.skipChars("false"); value = ValueP(createFalse()); } else if (parse.isString("null")) { parse.skipChars("null"); value = ValueP(createNull()); } else return false; return true; }
ValueP MPI_InitValue::call(SgFunctionType *fnType, const vector<ValueP> &args) const { cerr << "MPI_Init called" << endl; return ValueP(); }