Ejemplo n.º 1
0
// Converts string to simple numeric format
std::string strToNumeric(std::string inp) {
    std::string o = "0";
    if (inp == "") {
        o = "";
    }
    else if ((inp[0] == '"' && inp[inp.length()-1] == '"')
            || (inp[0] == '\'' && inp[inp.length()-1] == '\'')) {
		for (unsigned i = 1; i < inp.length() - 1; i++) {
            o = decimalAdd(decimalMul(o,"256"), unsignedToDecimal((unsigned char)inp[i]));
        }
    }
    else if (inp.substr(0,2) == "0x") {
		for (unsigned i = 2; i < inp.length(); i++) {
            int dig = std::string("0123456789abcdef").find(inp[i]);
            if (dig < 0) return "";
            o = decimalAdd(decimalMul(o,"16"), unsignedToDecimal(dig));
        }
    }
    else {
        bool isPureNum = true;
		for (unsigned i = 0; i < inp.length(); i++) {
            isPureNum = isPureNum && inp[i] >= '0' && inp[i] <= '9';
        }
        o = isPureNum ? inp : "";
    }
    return o;
}
Ejemplo n.º 2
0
// Converts binary transaction data into a list of integer values
std::vector<std::string> decodeDatalist(std::string ser) {
    std::vector<std::string> out;
    for (unsigned i = 0; i < ser.length(); i+= 32) {
        std::string o = "0";
		for (unsigned j = i; j < i + 32; j++) {
            int vj = (int)(unsigned char)ser[j];
            o = decimalAdd(decimalMul(o, "256"), unsignedToDecimal(vj));
        }
        out.push_back(o);
    }
    return out;
}
Ejemplo n.º 3
0
//Modulo the two strings representing decimal values
std::string decimalMod(std::string a, std::string b) {
    return decimalSub(a, decimalMul(decimalDiv(a, b), b));
}
Ejemplo n.º 4
0
//Modulo the two strings representing decimal values
std::string decimalMod(const std::string &a, const std::string &b) {
    return decimalSub(a, decimalMul(decimalDiv(a, b), b));
}
Ejemplo n.º 5
0
// Populate an svObj with the arguments needed to determine
// the storage position of a node
svObj getStorageVars(svObj pre, Node node, std::string prefix,
                     int index) {
    Metadata m = node.metadata;
    if (!pre.globalOffset.size()) pre.globalOffset = "0";
    std::vector<Node> h;
    std::vector<std::string> coefficients;
    // Array accesses or atoms
    if (node.val == "access" || node.type == TOKEN) {
        std::string tot = "1";
        h = listfyStorageAccess(node);
        coefficients.push_back("1");
        for (unsigned i = h.size() - 1; i >= 1; i--) {
            // Array sizes must be constant or at least arithmetically
            // evaluable at compile time
            if (!isPureArithmetic(h[i]))
                err("Array size must be fixed value", m);
            // Create a list of the coefficient associated with each
            // array index
            coefficients.push_back(decimalMul(coefficients.back(), h[i].val));
        }
    }
    // Tuples
    else {
        int startc;
        // Handle the (fun <fun_astnode> args...) case
        if (node.val == "fun") {
            startc = 1;
            h = listfyStorageAccess(node.args[0]);
        }
        // Handle the (<fun_name> args...) case, which
        // the serpent parser produces when the function
        // is a simple name and not a complex astnode
        else {
            startc = 0;
            h = listfyStorageAccess(token(node.val, m));
        }
        svObj sub = pre;
        sub.globalOffset = "0";
        // Evaluate tuple elements recursively
        for (unsigned i = startc; i < node.args.size(); i++) {
            sub = getStorageVars(sub,
                                 node.args[i],
                                 prefix+h[0].val.substr(2)+".",
                                 i-startc);
        }
        coefficients.push_back(sub.globalOffset);
        for (unsigned i = h.size() - 1; i >= 1; i--) {
            // Array sizes must be constant or at least arithmetically
            // evaluable at compile time
            if (!isPureArithmetic(h[i]))
               err("Array size must be fixed value", m);
            // Create a list of the coefficient associated with each
            // array index
            coefficients.push_back(decimalMul(coefficients.back(), h[i].val));
        }
        pre.offsets = sub.offsets;
        pre.coefficients = sub.coefficients;
        pre.nonfinal = sub.nonfinal;
        pre.nonfinal[prefix+h[0].val.substr(2)] = true;
    }
    pre.coefficients[prefix+h[0].val.substr(2)] = coefficients;
    pre.offsets[prefix+h[0].val.substr(2)] = pre.globalOffset;
    pre.indices[prefix+h[0].val.substr(2)] = index;
    if (decimalGt(tt176, coefficients.back()))
        pre.globalOffset = decimalAdd(pre.globalOffset, coefficients.back());
    return pre;
}