[BACK]Return to sm_ext.c CVS log [TXT][DIR] Up to [local] / OpenXM / src / ox_math

Annotation of OpenXM/src/ox_math/sm_ext.c, Revision 1.2

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.2     ! ohara       2: /* $OpenXM: OpenXM/src/ox_math/sm_ext.c,v 1.1 2000/12/03 21:45:48 ohara Exp $ */
1.1       ohara       3:
                      4: /*
                      5:    Copyright (C) Katsuyoshi OHARA, 2000.
                      6:    Portions copyright 1999 Wolfram Research, Inc.
                      7:
                      8:    You must see OpenXM/Copyright/Copyright.generic.
                      9:    The MathLink Library is licensed from Wolfram Research Inc..
                     10:    See OpenXM/Copyright/Copyright.mathlink for detail.
                     11: */
                     12:
                     13: #include <stdio.h>
                     14: #include <stdlib.h>
                     15: #include <unistd.h>
                     16: #include <gmp.h>
                     17: #include <mathlink.h>
                     18: #include <ox_toolkit.h>
                     19: #include "sm.h"
                     20:
1.2     ! ohara      21: static struct { int (*func_ptr)(); int key; } tbl_smcmd[] = {
        !            22:     {sm_executeFunction, SM_executeFunction},
        !            23:     {sm_executeStringByLocalParser, SM_executeStringByLocalParser},
        !            24:     {sm_executeStringByLocalParser, SM_executeStringByLocalParserInBatchMode},
        !            25:     {sm_mathcap,         SM_mathcap},
        !            26:     {sm_set_mathcap,     SM_setMathCap},
        !            27:     {sm_popString,       SM_popString},
        !            28:     {sm_popCMO,          SM_popCMO},
        !            29:     {sm_pops,            SM_pops},
        !            30:        {shutdown,           SM_shutdown},
        !            31:     {NULL, NULL}
        !            32: };
        !            33:
        !            34: extern OXFILE *stack_oxfp;
        !            35:
        !            36: int (*sm_search_f(int code))()
        !            37: {
        !            38:     int i;
        !            39:     for (i=0; tbl_smcmd[i].key != NULL; i++) {
        !            40:         if (code == tbl_smcmd[i].key) {
        !            41:             return tbl_smcmd[i].func_ptr;
        !            42:         }
1.1       ohara      43:     }
1.2     ! ohara      44:     return NULL;
1.1       ohara      45: }
                     46:
                     47: /* MathLink dependent */
1.2     ! ohara      48: void sm_popString()
1.1       ohara      49: {
                     50:     char *s;
                     51:     cmo *err;
                     52:     cmo *m;
                     53:
                     54: #ifdef DEBUG
                     55:     fprintf(stderr, "ox_math:: opecode = SM_popString.\n");
                     56: #endif
                     57:
                     58:     m = pop();
                     59:     if (m->tag == CMO_STRING) {
1.2     ! ohara      60:         send_ox_cmo(stack_oxfp, m);
1.1       ohara      61:     }else if ((s = new_string_set_cmo(m)) != NULL) {
1.2     ! ohara      62:         send_ox_cmo(stack_oxfp, (cmo *)new_cmo_string(s));
1.1       ohara      63:     }else {
                     64:         err = make_error_object(SM_popString, m);
1.2     ! ohara      65:         send_ox_cmo(stack_oxfp, err);
1.1       ohara      66:     }
                     67: }
                     68:
                     69: int local_execute(char *s)
                     70: {
1.2     ! ohara      71:     extern int flag_mlo_symbol;
        !            72:
1.1       ohara      73:     if(*s == 'i') {
                     74:         switch(s[1]) {
                     75:         case '+':
                     76:             flag_mlo_symbol = FLAG_MLTKSYM_IS_STRING;
                     77:             break;
                     78:         case '-':
                     79:         case '=':
                     80:         default:
                     81:             flag_mlo_symbol = FLAG_MLTKSYM_IS_INDETERMINATE;
                     82:         }
                     83:     }
                     84:     return 0;
                     85: }
                     86:
                     87: /* The following function is depend on an implementation of a server. */
1.2     ! ohara      88: void sm_executeStringByLocalParser()
1.1       ohara      89: {
                     90:     symbol_t symp;
                     91:     cmo* m = pop();
                     92:     char *s = NULL;
                     93: #ifdef DEBUG
                     94:     fprintf(stderr, "ox_math:: opecode = SM_executeStringByLocalParser.\n");
                     95: #endif
                     96:
                     97:     if (m->tag == CMO_STRING
                     98:         && strlen(s = ((cmo_string *)m)->s) != 0) {
                     99:         if (s[0] == ':') {
                    100:             local_execute(++s);
                    101:         }else {
                    102:             /* for mathematica */
                    103:             /* Sending the string `s' to mathematica for its evaluation. */
                    104:             ml_evaluateStringByLocalParser(s);
                    105:             ml_select();
                    106:             push(receive_mlo());
                    107:         }
1.2     ! ohara     108:     }else {
1.1       ohara     109: #ifdef DEBUG
1.2     ! ohara     110:                symp = lookup_by_tag(m->tag);
        !           111:                fprintf(stderr, "ox_math:: error. the top of stack is %s.\n", symbol_get_key(symp));
1.1       ohara     112: #endif
1.2     ! ohara     113:                push_error(SM_executeStringByLocalParser, m);
        !           114:        }
1.1       ohara     115: }
                    116:
1.2     ! ohara     117: void sm_executeFunction()
1.1       ohara     118: {
                    119:     int i, argc;
                    120:     cmo **argv;
                    121:     char* func;
                    122:     cmo* m;
                    123:
                    124:     if ((m = pop()) == NULL || m->tag != CMO_STRING) {
1.2     ! ohara     125:         push_error(SM_executeFunction, m);
1.1       ohara     126:     }
                    127:     func = ((cmo_string *)m)->s;
                    128:
                    129:     if ((m = pop()) == NULL || m->tag != CMO_INT32) {
1.2     ! ohara     130:         push_error(SM_executeFunction, m);
1.1       ohara     131:     }
                    132:
                    133:     argc = ((cmo_int32 *)m)->i;
                    134:     argv = malloc(argc*sizeof(cmo *));
                    135:     for (i=0; i<argc; i++) {
                    136:         argv[i] = pop();
                    137:     }
                    138:     ml_executeFunction(func, argc, argv);
                    139:     ml_select();
                    140:     push(receive_mlo());
                    141: }
                    142:
1.2     ! ohara     143: void sm_mathcap()
1.1       ohara     144: {
1.2     ! ohara     145:     push((cmo *)oxf_cmo_mathcap(stack_oxfp));
1.1       ohara     146: }
                    147:
1.2     ! ohara     148: void sm_set_mathcap()
1.1       ohara     149: {
                    150:     cmo_mathcap *m = (cmo_mathcap *)pop();
                    151:     if (m->tag == CMO_MATHCAP) {
1.2     ! ohara     152:         oxf_mathcap_update(stack_oxfp, m);
1.1       ohara     153:     }else {
                    154:         push_error(-1, m);
                    155:         /* an error object must be pushed */
                    156:     }
                    157: }
                    158:

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>