Пример #1
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;
}
Пример #2
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;
}
Пример #3
0
static mpdm_t kde4_drv_confirm(mpdm_t a, mpdm_t ctxt)
/* confirm driver function */
{
    int r;

    /* 1# arg: prompt */
    r = KMessageBox::questionYesNoCancel(0,
                                         str_to_qstring(mpdm_aget(a, 0)),
                                         i18n("mp" VERSION));

    switch (r) {
    case KMessageBox::Yes:
        r = 1;
        break;

    case KMessageBox::No:
        r = 2;
        break;

    case KMessageBox::Cancel:
        r = 0;
        break;
    }

    return MPDM_I(r);
}
Пример #4
0
static mpdm_t kde4_drv_alert(mpdm_t a, mpdm_t ctxt)
/* alert driver function */
{
    /* 1# arg: prompt */
    KMessageBox::information(0, str_to_qstring(mpdm_aget(a, 0)),
                             i18n("mp " VERSION));

    return NULL;
}
Пример #5
0
O_TYPE execsym(O_ARGS, int th, int m)
{
    mpdm_t s, v, p, o = NULL, r = NULL;

    /* gets the symbol name */
    s = RF(M1);

    /* gets the arguments */
    p = RF(M2);

    /* if it's to be called as a method, the object
       should be inserted into the local symtable
       before searching the symbol */
    if (m && (o = mpdm_aget(p, 0)) && MPDM_IS_HASH(o)) {
        mpdm_push(l, o);
    }
    else
        m = 0;

    /* gets the symbol value */
    v = GET(s);

    if (!MPDM_IS_EXEC(v)) {
        /* not found or NULL value? error */
        mpdm_t t, w;
        char tmp[128];

        w = RF(mpdm_join_s(s, L"."));
        t = RF(MPDM_2MBS((wchar_t *) w->data));

        snprintf(tmp, sizeof(tmp), "Undefined function %s()",
                 (char *) t->data);

        mpsl_error(MPDM_MBS(tmp));

        UF(w);
        UF(t);
    }
    else {
        /* execute */
        r = RF(th ? mpdm_exec_thread(v, p, l) : mpdm_exec(v, p, l));
    }

    UF(s);
    UF(p);

    /* drop the object from the local symtable */
    if (m)
        mpdm_adel(l, -1);

    return UFND(r);
}
Пример #6
0
static mpdm_t kde4_drv_savefile(mpdm_t a, mpdm_t ctxt)
{
    QString r;
    char tmp[128];

    getcwd(tmp, sizeof(tmp));

    /* 1# arg: prompt */
    r = KFileDialog::getSaveFileName(KUrl::fromPath(tmp), "*", 0,
                                     str_to_qstring(mpdm_aget(a, 0)));

    return qstring_to_str(r);
}
Пример #7
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;
}
Пример #8
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);
    }
}
Пример #9
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;
}
Пример #10
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);
}
Пример #11
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;
}