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

Annotation of OpenXM/src/ox_math/sm.c, Revision 1.4

1.1       ohara       1: /* -*- mode: C; coding: euc-japan -*- */
1.4     ! ohara       2: /* $OpenXM: OpenXM/src/ox_math/sm.c,v 1.3 2003/01/13 12:04:53 ohara Exp $ */
1.1       ohara       3:
                      4: #include <stdio.h>
                      5: #include <stdlib.h>
                      6: #include <unistd.h>
                      7: #include <signal.h>
                      8: #include <mathlink.h>
                      9: #include <ox_toolkit.h>
                     10: #include "sm.h"
                     11:
                     12: /* MathLink independent */
                     13:
                     14: /* WARNING: you must NOT use stack[stack_ptr]. */
                     15: static cmo **stack = NULL;
                     16: static int stack_ptr = 0;
                     17: static int stack_size = 0;
                     18: OXFILE *stack_oxfp = NULL;
                     19:
                     20: #define DIFFERENCE_OF_STACK  1024
                     21:
                     22: void stack_extend()
                     23: {
                     24:     int newsize = stack_size + DIFFERENCE_OF_STACK;
                     25:     cmo **newstack = (cmo **)malloc(sizeof(cmo *)*newsize);
                     26:     if (stack != NULL) {
                     27:         memcpy(newstack, stack, sizeof(cmo *)*stack_size);
                     28:         free(stack);
                     29:     }
                     30:     stack_size = newsize;
                     31:     stack = newstack;
                     32: }
                     33:
                     34: void push(cmo *ob)
                     35: {
1.4     ! ohara      36:     ox_printf("push<%s>.\n", get_symbol_by_tag(ob->tag));
1.1       ohara      37:     if (stack_ptr >= stack_size) {
                     38:         stack_extend();
                     39:     }
                     40:     stack[stack_ptr] = ob;
                     41:     stack_ptr++;
                     42: }
                     43:
                     44: /* if the stack is empty, then pop() returns (CMO_NULL). */
                     45: cmo *pop()
                     46: {
                     47:     if (stack_ptr > 0) {
                     48:         return stack[--stack_ptr];
                     49:     }
                     50:     return new_cmo_null();
                     51: }
                     52:
                     53: void pops(int n)
                     54: {
                     55:     stack_ptr -= n;
                     56:     if (stack_ptr < 0) {
                     57:         stack_ptr = 0;
                     58:     }
                     59: }
                     60:
                     61: void push_error(int errcode, cmo* pushback)
                     62: {
                     63:     return push((cmo *)make_error_object(errcode, pushback));
                     64: }
                     65:
                     66: /*
                     67: If error occurs, then
                     68: an sm_* function, called by sm_run, pushes an error obect.
                     69: */
                     70: void sm_popCMO()
                     71: {
                     72:     cmo* m = pop();
1.4     ! ohara      73:     ox_printf(" <%s>", get_symbol_by_tag(m->tag));
1.1       ohara      74:     send_ox_cmo(stack_oxfp, m);
                     75: }
                     76:
                     77: void sm_pops()
                     78: {
                     79:     cmo* m = pop();
                     80:     if (m->tag == CMO_INT32) {
                     81:         pops(((cmo_int32 *)m)->i);
                     82:     }else {
                     83:         push_error(ERROR_ID_UNKNOWN_SM, m);
                     84:     }
                     85: }
                     86:
                     87: void sm_run(int code)
                     88: {
                     89:     int (*func)(OXFILE *) = sm_search_f(code);
1.4     ! ohara      90:     ox_printf("ox_math:: opecode=<%s>[%d]", get_symbol_by_tag(code), code);
1.1       ohara      91:     if (func != NULL) {
                     92:         func(stack_oxfp);
                     93:     }else {
                     94:         push_error(ERROR_ID_UNKNOWN_SM, new_cmo_null());
                     95:     }
1.4     ! ohara      96:     ox_printf("\n");
1.1       ohara      97: }
                     98:
                     99: int oxf_error(OXFILE *oxfp)
                    100: {
                    101:     int e = oxfp->error;
                    102:     if (e != 0) {
                    103:         oxfp->error = 0;
                    104:     }
                    105:     return e;
                    106: }

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