[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.4

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.4     ! ohara       2: /* $OpenXM: OpenXM/src/ox_math/sm_ext.c,v 1.3 2003/01/11 12:38:57 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:
1.4     ! ohara      54:     ox_printf("ox_math:: opecode = SM_popString.\n");
1.1       ohara      55:     m = pop();
                     56:     if (m->tag == CMO_STRING) {
1.2       ohara      57:         send_ox_cmo(stack_oxfp, m);
1.1       ohara      58:     }else if ((s = new_string_set_cmo(m)) != NULL) {
1.2       ohara      59:         send_ox_cmo(stack_oxfp, (cmo *)new_cmo_string(s));
1.1       ohara      60:     }else {
                     61:         err = make_error_object(SM_popString, m);
1.2       ohara      62:         send_ox_cmo(stack_oxfp, err);
1.1       ohara      63:     }
                     64: }
                     65:
                     66: int local_execute(char *s)
                     67: {
1.2       ohara      68:     extern int flag_mlo_symbol;
                     69:
1.1       ohara      70:     if(*s == 'i') {
                     71:         switch(s[1]) {
                     72:         case '+':
                     73:             flag_mlo_symbol = FLAG_MLTKSYM_IS_STRING;
                     74:             break;
                     75:         case '-':
                     76:         case '=':
                     77:         default:
                     78:             flag_mlo_symbol = FLAG_MLTKSYM_IS_INDETERMINATE;
                     79:         }
                     80:     }
                     81:     return 0;
                     82: }
                     83:
                     84: /* The following function is depend on an implementation of a server. */
1.2       ohara      85: void sm_executeStringByLocalParser()
1.1       ohara      86: {
                     87:     symbol_t symp;
                     88:     cmo* m = pop();
                     89:     char *s = NULL;
                     90:
1.4     ! ohara      91:     ox_printf("ox_math:: opecode = SM_executeStringByLocalParser.\n");
1.1       ohara      92:     if (m->tag == CMO_STRING
                     93:         && strlen(s = ((cmo_string *)m)->s) != 0) {
                     94:         if (s[0] == ':') {
                     95:             local_execute(++s);
                     96:         }else {
                     97:             /* for mathematica */
                     98:             /* Sending the string `s' to mathematica for its evaluation. */
                     99:             ml_evaluateStringByLocalParser(s);
                    100:             ml_select();
                    101:             push(receive_mlo());
                    102:         }
1.2       ohara     103:     }else {
1.1       ohara     104: #ifdef DEBUG
1.2       ohara     105:                symp = lookup_by_tag(m->tag);
1.4     ! ohara     106:                ox_printf("ox_math:: error. the top of stack is %s.\n", symbol_get_key(symp));
1.1       ohara     107: #endif
1.2       ohara     108:                push_error(SM_executeStringByLocalParser, m);
                    109:        }
1.1       ohara     110: }
                    111:
1.2       ohara     112: void sm_executeFunction()
1.1       ohara     113: {
                    114:     int i, argc;
                    115:     cmo **argv;
                    116:     char* func;
                    117:     cmo* m;
                    118:
                    119:     if ((m = pop()) == NULL || m->tag != CMO_STRING) {
1.2       ohara     120:         push_error(SM_executeFunction, m);
1.1       ohara     121:     }
                    122:     func = ((cmo_string *)m)->s;
                    123:
                    124:     if ((m = pop()) == NULL || m->tag != CMO_INT32) {
1.2       ohara     125:         push_error(SM_executeFunction, m);
1.1       ohara     126:     }
                    127:
                    128:     argc = ((cmo_int32 *)m)->i;
                    129:     argv = malloc(argc*sizeof(cmo *));
                    130:     for (i=0; i<argc; i++) {
                    131:         argv[i] = pop();
                    132:     }
                    133:     ml_executeFunction(func, argc, argv);
                    134:     ml_select();
                    135:     push(receive_mlo());
                    136: }
                    137:
1.2       ohara     138: void sm_mathcap()
1.1       ohara     139: {
1.2       ohara     140:     push((cmo *)oxf_cmo_mathcap(stack_oxfp));
1.1       ohara     141: }
                    142:
1.2       ohara     143: void sm_set_mathcap()
1.1       ohara     144: {
                    145:     cmo_mathcap *m = (cmo_mathcap *)pop();
                    146:     if (m->tag == CMO_MATHCAP) {
1.2       ohara     147:         oxf_mathcap_update(stack_oxfp, m);
1.1       ohara     148:     }else {
                    149:         push_error(-1, m);
                    150:         /* an error object must be pushed */
                    151:     }
                    152: }
                    153:

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