Exemple #1
0
/**
 * mpsl_set_symbol - Sets value to a symbol.
 * @s: symbol name
 * @v: value
 * @l: local symbol table
 *
 * Assigns the value @v to the @s symbol. If the value exists as
 * a local symbol, it's assigned to it; otherwise, it's set as a global
 * symbol (and created if it does not exist).
 */
mpdm_t mpsl_set_symbol(mpdm_t s, mpdm_t v, mpdm_t l)
{
    int n;
    mpdm_t r, p, w;

    mpdm_ref(l);
    mpdm_ref(s);
    mpdm_ref(v);

    /* get the local or global symbol table */
    if ((r = find_local_symtbl(s, l)) == NULL)
        r = mpdm_root();

    /* splits the path, if needed */
    if (MPDM_IS_ARRAY(s))
        p = mpdm_ref(s);
    else
        p = mpdm_ref(mpdm_split_s(s, L"."));

    w = r;

    for (n = 0; w != NULL && n < mpdm_size(p); n++) {
        /* is executable? run it and take its output */
        while (MPDM_IS_EXEC(w))
            w = mpdm_exec(w, NULL, NULL);

        /* last component? */
        if (n == mpdm_size(p) - 1) {
            /* yes; do the setting */
            if (MPDM_IS_HASH(w))
                w = mpdm_hset(w, mpdm_aget(p, n), v);
            else
            if (MPDM_IS_ARRAY(w))
                w = mpdm_aset(w, v, mpdm_ival(mpdm_aget(p, n)));
        }
        else {
            if (MPDM_IS_HASH(w))
                w = mpdm_hget(w, mpdm_aget(p, n));
            else
            if (MPDM_IS_ARRAY(w))
                w = mpdm_aget(w, mpdm_ival(mpdm_aget(p, n)));
            else {
                mpdm_void(w);
                w = NULL;
            }
        }
    }

    mpdm_unref(p);
    mpdm_unref(v);
    mpdm_unref(s);
    mpdm_unref(l);

    return w;
}
Exemple #2
0
/**
 * mpdm_count_o - Returns the number of elements in an object.
 * @o: the object
 *
 * Returns the number of elements inside an object.
 * [Objects]
 */
int mpdm_count_o(const mpdm_t o)
/* do not use it; use mpdm_count() */
{
    int n;
    int ret = 0;

    for (n = 0; n < mpdm_size(o); n++) {
        mpdm_t b = mpdm_get_i(o, n);
        ret += mpdm_size(b);
    }

    return ret / 2;
}
Exemple #3
0
static mpdm_t constant_fold(mpdm_t i)
/* tries to fold complex but constant expressions into a literal */
{
    int n;
    mpdm_t v;

    /* get the number opcode */
    n = mpdm_ival(mpdm_aget(i, 0));

    if (op_table[n].foldable) {
        /* test if all arguments are literal (opcode 0) */
        for (n = 1; n < mpdm_size(i); n++) {
            mpdm_t t = mpdm_aget(i, n);

            /* if it's not LITERAL, abort immediately */
            if (mpdm_ival(mpdm_aget(t, 0)) != 0)
                return i;
        }

        /* execute the instruction and convert to LITERAL */
        v = mpsl_exec_p(i, NULL, NULL);
        i = mpsl_mkins(L"LITERAL", 1, v, NULL, NULL, NULL);
    }

    return i;
}
Exemple #4
0
O_TYPE O_list(O_ARGS)
/* build list from instructions */
{
    mpdm_t ret = RF(mpdm_size(c) == 2 ? MPDM_A(0) : M(2));

    mpdm_push(ret, M(1));

    return UFND(ret);
}
Exemple #5
0
O_TYPE O_ilist(O_ARGS)
/* build and inverse list from instructions */
{
    mpdm_t ret = RF(mpdm_size(c) == 2 ? MPDM_A(0) : M(2));

    mpdm_ins(ret, M(1), 0);

    return UFND(ret);
}
Exemple #6
0
O_TYPE O_hash(O_ARGS)
/* build hash from instructions */
{
    mpdm_t ret = RF(mpdm_size(c) == 3 ? MPDM_H(0) : M(3));

    mpdm_hset(ret, M1, M2);

    return UFND(ret);
}
Exemple #7
0
/** integer = write(fd, arg1 [,arg2 ... argn]); */
static mpdm_t F_write(F_ARGS)
{
    mpdm_t f = A0;
    int n;

    for (n = 1; n < mpdm_size(a); n++)
        mpdm_write(f, A(n));

    return f;
}
Exemple #8
0
/** argn = push(a, arg1 [, arg2, ... argn]); */
static mpdm_t F_push(F_ARGS)
{
    int n;
    mpdm_t r = NULL;

    for (n = 1; n < mpdm_size(a); n++) {
        mpdm_unref(r);
        r = mpdm_push(A0, A(n));
        mpdm_ref(r);
    }

    return mpdm_unrefnd(r);
}
Exemple #9
0
/**
 * mpdm_set_o - Sets a value in an object.
 * @o: the object
 * @v: the value
 * @i: the index
 *
 * Sets the value @v inside the object @o, accesible by index @i.
 * Returns @v.
 * [Objects]
 */
mpdm_t mpdm_set_o(mpdm_t o, mpdm_t v, mpdm_t i)
/* do not use it; use mpdm_set() */
{
    mpdm_t b, r;
    int n;

    mpdm_ref(i);
    mpdm_ref(v);

    /* if hash is empty, create an optimal number of buckets */
    if (mpdm_size(o) == 0)
        mpdm_expand(o, 0, mpdm_hash_buckets);

    n = HASH_BUCKET(o, i);

    if ((b = mpdm_get_i(o, n)) != NULL) {
        int pos;

        /* bucket exists; try to find the key there */
        n = mpdm_bseek(b, i, 2, &pos);

        if (n < 0) {
            /* the pair does not exist: create it */
            n = pos;
            mpdm_expand(b, n, 2);

            mpdm_set_i(b, i, n);
        }
    }
    else {
        /* the bucket does not exist; create it */
        b = MPDM_A(2);

        /* put the bucket into the hash */
        mpdm_set_i(o, b, n);

        /* offset 0 */
        n = 0;

        /* insert the key */
        mpdm_set_i(b, i, n);
    }

    r = mpdm_set_i(b, v, n + 1);

    mpdm_unref(v);
    mpdm_unref(i);

    return r;
}
Exemple #10
0
/** print(arg1 [,arg2 ... argn]); */
static mpdm_t F_print(F_ARGS)
{
    int n;

    for (n = 0; n < mpdm_size(a); n++) {
        if (n)
            mpdm_write_wcs(stdout, L" ");

        mpdm_write_wcs(stdout, mpdm_string(A(n)));
    }

    mpdm_write_wcs(stdout, L"\n");

    return NULL;
}
Exemple #11
0
static void set_local_symbols(mpdm_t s, mpdm_t v, mpdm_t l)
/* sets (or creates) a list of local symbols with a list of values */
{
    if (l != NULL) {
        mpdm_t h;

        mpdm_ref(s);
        mpdm_ref(v);
        mpdm_ref(l);

        /* gets the top local variable frame */
        h = mpdm_aget(l, -1);

        if (MPDM_IS_ARRAY(s) || MPDM_IS_ARRAY(v)) {
            int n;
            mpdm_t a;

            for (n = 0; n < mpdm_size(s); n++)
                mpdm_hset(h, mpdm_aget(s, n), mpdm_aget(v, n));

            if (n < mpdm_size(v)) {
                /* store the rest of arguments into _ */
                a = mpdm_hset_s(h, L"_", MPDM_A(0));

                for (; n < mpdm_size(v); n++)
                    mpdm_push(a, mpdm_aget(v, n));
            }
        }
        else
            mpdm_hset(h, s, v);

        mpdm_unref(l);
        mpdm_unref(v);
        mpdm_unref(s);
    }
}
Exemple #12
0
/**
 * mpdm_get_wcs - Gets the value from an object by string index (string version).
 * @o: the object
 * @i: the index
 *
 * Returns the value from @o by index @i, or NULL if there is no
 * value addressable by that index.
 * [Objects]
 */
mpdm_t mpdm_get_wcs(const mpdm_t o, const wchar_t *i)
{
    mpdm_t b;
    mpdm_t v = NULL;
    int n = 0;

    if (mpdm_size(o)) {
        /* if hash is not empty... */
        if ((b = mpdm_get_i(o, HASH_BUCKET_S(o, i))) != NULL)
            n = mpdm_bseek_wcs(b, i, 2, NULL);

        if (n >= 0)
            v = mpdm_get_i(b, n + 1);
    }

    return v;
}
Exemple #13
0
O_TYPE O_global(O_ARGS)
{
    mpdm_t v = RF(M1);

    if (MPDM_IS_ARRAY(v)) {
        int n;

        for (n = 0; n < mpdm_size(v); n++)
            mpdm_hset(mpdm_root(), mpdm_aget(v, n), NULL);
    }
    else
        mpdm_hset(mpdm_root(), v, NULL);

    UF(v);

    return NULL;
}
Exemple #14
0
/**
 * mpdm_exists - Tests if there is a value available by index.
 * @o: the object
 * @i: the index
 *
 * Returns 1 if exists a value indexable by @i in @h, or 0 othersize.
 * [Hashes]
 */
int mpdm_exists(const mpdm_t o, const mpdm_t i)
{
    mpdm_t b;
    int ret = 0;

    mpdm_ref(i);

    if (mpdm_size(o)) {
        if ((b = mpdm_get_i(o, HASH_BUCKET(o, i))) != NULL) {
            /* if bucket exists, binary-seek it */
            if (mpdm_bseek(b, i, 2, NULL) >= 0)
                ret = 1;
        }
    }

    mpdm_unref(i);

    return ret;
}
Exemple #15
0
/** o = new(c1 [, c2, ...cn]); */
static mpdm_t F_new(F_ARGS)
{
    int n;
    mpdm_t r = MPDM_O();

    for (n = 0; n < mpdm_size(a); n++) {
        mpdm_t w, v, i;
        int m = 0;

        w = mpdm_ref(A(n));

        if (mpdm_type(w) == MPDM_TYPE_OBJECT) {
            while (mpdm_iterator(w, &m, &v, &i))
                mpdm_set(r, mpdm_clone(v), i);
        }

        mpdm_unref(w);
    }

    return r;
}
Exemple #16
0
O_TYPE O_foreach(O_ARGS)
/* foreach loop */
{
    mpdm_t s = RF(M1);
    mpdm_t v = RF(M2);
    mpdm_t r = NULL;
    int n;

    for (n = 0; n < mpdm_size(v) && !*f; n++) {
        SET(s, mpdm_aget(v, n));
        UF(r);
        r = RF(M3);
    }

    if (*f == 1)
        *f = 0;

    UF(s);
    UF(v);

    return UFND(r);
}
Exemple #17
0
/**
 * mpdm_del_o - Deletes a value from an object.
 * @o: the value
 * @i: the index
 *
 * Deletes the element accesible by index @i from @o. Returns NULL
 * (versions prior to 1.0.10 returned the deleted value).
 * [Objects]
 */
mpdm_t mpdm_del_o(mpdm_t o, const mpdm_t i)
/* do not use it; use mpdm_del() */
{
    mpdm_t b;
    int n;

    mpdm_ref(i);

    if (mpdm_size(o)) {
        if ((b = mpdm_get_i(o, HASH_BUCKET(o, i))) != NULL) {
            /* bucket exists */
            if ((n = mpdm_bseek(b, i, 2, NULL)) >= 0) {
                /* collapse the bucket */
                mpdm_collapse(b, n, 2);
            }
        }
    }

    mpdm_unref(i);

    return NULL;
}
Exemple #18
0
static mpdm_t find_local_symtbl(mpdm_t s, mpdm_t l)
/* finds the local symbol table hash that holds l */
{
    int n;
    mpdm_t v = NULL;

    if (l != NULL) {
        /* if s is multiple, take just the first element */
        if (MPDM_IS_ARRAY(s))
            s = mpdm_aget(s, 0);

        /* travel the local symbol table trying to find it */
        for (n = mpdm_size(l) - 1; n >= 0; n--) {
            mpdm_t h = mpdm_aget(l, n);

            if (mpdm_exists(h, s)) {
                v = h;
                break;
            }
        }
    }

    return v;
}
Exemple #19
0
int mpdm_iterator_o(mpdm_t set, int *context, mpdm_t *v, mpdm_t *i)
/* do not use it; use mpdm_iterator() */
{
    int ret = 0;

    mpdm_ref(set);

    if (mpdm_size(set)) {
        int bi, ei;

        /* get bucket and element index */
        bi = (*context) % mpdm_size(set);
        ei = (*context) / mpdm_size(set);

        while (ret == 0 && bi < mpdm_size(set)) {
            mpdm_t b;

            /* if bucket is empty or there are no more
               elements in it, pick the next one */
            if (!(b = mpdm_get_i(set, bi)) || ei >= mpdm_size(b)) {
                ei = 0;
                bi++;
            }
            else {
                /* get pair */
                if (v) *v = mpdm_get_i(b, ei + 1);
                if (i) *i = mpdm_get_i(b, ei);

                ei += 2;

                /* update context */
                *context = (ei * mpdm_size(set)) + bi;
                ret = 1;
            }
        }
    }

    mpdm_unrefnd(set);

    return ret;
}
Exemple #20
0
/* ; */
static mpdm_t F_size(F_ARGS)
{
    return MPDM_I(mpdm_size(A0));
}
Exemple #21
0
static mpdm_t kde4_drv_form(mpdm_t a, mpdm_t ctxt)
{
    int n;
    mpdm_t widget_list;
    QWidget *qlist[100];
    mpdm_t r;

    KDialog *dialog = new KDialog(window);

    dialog->setModal(true);
    dialog->setButtons(KDialog::Ok | KDialog::Cancel);

    widget_list = mpdm_aget(a, 0);

    KVBox *vb = new KVBox(dialog);
    dialog->setMainWidget(vb);

    for (n = 0; n < mpdm_size(widget_list); n++) {
        mpdm_t w = mpdm_aget(widget_list, n);
        wchar_t *type;
        mpdm_t t;
        KHBox *hb = new KHBox(vb);

        type = mpdm_string(mpdm_hget_s(w, L"type"));

        if ((t = mpdm_hget_s(w, L"label")) != NULL) {
            QLabel *ql = new QLabel(hb);
            ql->setText(str_to_qstring(mpdm_gettext(t)));
        }

        t = mpdm_hget_s(w, L"value");

        if (wcscmp(type, L"text") == 0) {
            mpdm_t h;
            QComboBox *ql = new QComboBox(hb);

            ql->setEditable(true);
            ql->setMinimumContentsLength(30);
            ql->setMaxVisibleItems(8);

            if (t != NULL)
                ql->setEditText(str_to_qstring(t));

            qlist[n] = ql;

            if ((h = mpdm_hget_s(w, L"history")) != NULL) {
                int i;

                /* has history; fill it */
                h = mp_get_history(h);

                for (i = mpdm_size(h) - 1; i >= 0; i--)
                    ql->addItem(str_to_qstring(mpdm_aget(h, i)));
            }
        }
        else
        if (wcscmp(type, L"password") == 0) {
            QLineEdit *ql = new QLineEdit(hb);

            ql->setEchoMode(QLineEdit::Password);

            qlist[n] = ql;
        }
        else
        if (wcscmp(type, L"checkbox") == 0) {
            QCheckBox *qc = new QCheckBox(hb);

            if (mpdm_ival(t))
                qc->setCheckState(Qt::Checked);

            qlist[n] = qc;
        }
        else
        if (wcscmp(type, L"list") == 0) {
            int i;
            QListWidget *ql = new QListWidget(hb);
            ql->setMinimumWidth(480);

            /* use a monospaced font */
            ql->setFont(QFont(QString("Mono")));

            mpdm_t l = mpdm_hget_s(w, L"list");

            for (i = 0; i < mpdm_size(l); i++)
                ql->addItem(str_to_qstring(mpdm_aget(l, i)));

            ql->setCurrentRow(mpdm_ival(t));

            qlist[n] = ql;
        }

        if (n == 0)
            qlist[n]->setFocus(Qt::OtherFocusReason);
    }

    n = dialog->exec();

    if (!n)
        return NULL;

    r = MPDM_A(mpdm_size(widget_list));

    /* fill the return values */
    for (n = 0; n < mpdm_size(widget_list); n++) {
        mpdm_t w = mpdm_aget(widget_list, n);
        mpdm_t v = NULL;
        wchar_t *type;

        type = mpdm_string(mpdm_hget_s(w, L"type"));

        if (wcscmp(type, L"text") == 0) {
            mpdm_t h;
            QComboBox *ql = (QComboBox *) qlist[n];

            v = qstring_to_str(ql->currentText());

            /* if it has history, add to it */
            if ((h = mpdm_hget_s(w, L"history")) != NULL &&
                v != NULL && mpdm_cmp_s(v, L"") != 0) {
                h = mp_get_history(h);

                if (mpdm_cmp(v, mpdm_aget(h, -1)) != 0)
                    mpdm_push(h, v);
            }
        }
        else
        if (wcscmp(type, L"password") == 0) {
            QLineEdit *ql = (QLineEdit *) qlist[n];

            v = qstring_to_str(ql->text());
        }
        else
        if (wcscmp(type, L"checkbox") == 0) {
            QCheckBox *qb = (QCheckBox *) qlist[n];

            v = MPDM_I(qb->checkState() == Qt::Checked);
        }
        else
        if (wcscmp(type, L"list") == 0) {
            QListWidget *ql = (QListWidget *) qlist[n];

            v = MPDM_I(ql->currentRow());
        }

        mpdm_aset(r, v, n);
    }

    return r;
}