Annotation of OpenXM/src/ox_math/sm.c, Revision 1.3
1.1 ohara 1: /* -*- mode: C; coding: euc-japan -*- */
1.3 ! ohara 2: /* $OpenXM: OpenXM/src/ox_math/sm.c,v 1.2 2003/01/11 12:38:57 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: {
36: #if DEBUG
37: symbol_t symp;
38:
39: if (ob->tag == CMO_STRING) {
1.3 ! ohara 40: ox_printf("ox_math:: a CMO_STRING(%s) was pushed.\n", ((cmo_string *)ob)->s);
1.1 ohara 41: }else {
42: symp = lookup_by_tag(ob->tag);
1.3 ! ohara 43: ox_printf("ox_math:: a %s was pushed.\n", symbol_get_key(symp));
1.1 ohara 44: }
45: #endif
46: if (stack_ptr >= stack_size) {
47: stack_extend();
48: }
49: stack[stack_ptr] = ob;
50: stack_ptr++;
51: }
52:
53: /* if the stack is empty, then pop() returns (CMO_NULL). */
54: cmo *pop()
55: {
56: if (stack_ptr > 0) {
57: return stack[--stack_ptr];
58: }
59: return new_cmo_null();
60: }
61:
62: void pops(int n)
63: {
64: stack_ptr -= n;
65: if (stack_ptr < 0) {
66: stack_ptr = 0;
67: }
68: }
69:
70: void push_error(int errcode, cmo* pushback)
71: {
72: return push((cmo *)make_error_object(errcode, pushback));
73: }
74:
75: /*
76: If error occurs, then
77: an sm_* function, called by sm_run, pushes an error obect.
78: */
79: void sm_popCMO()
80: {
81: cmo* m = pop();
82: #ifdef DEBUG
83: symbol_t symp = lookup_by_tag(m->tag);
1.3 ! ohara 84: ox_printf("ox_math:: opecode = SM_popCMO. (%s)\n", symbol_get_key(symp));
1.1 ohara 85: #endif
86: send_ox_cmo(stack_oxfp, m);
87: }
88:
89: void sm_pops()
90: {
91: cmo* m = pop();
92: if (m->tag == CMO_INT32) {
93: pops(((cmo_int32 *)m)->i);
94: }else {
95: push_error(ERROR_ID_UNKNOWN_SM, m);
96: }
97: }
98:
99: void sm_run(int code)
100: {
101: int (*func)(OXFILE *) = sm_search_f(code);
102: #ifdef DEBUG
103: symbol_t sp = lookup_by_tag(code);
1.3 ! ohara 104: ox_printf("ox_math:: %s received.\n", symbol_get_key(sp));
1.1 ohara 105: #endif
106: if (func != NULL) {
107: func(stack_oxfp);
108: }else {
1.3 ! ohara 109: ox_printf("unknown command: %d.\n", code);
1.1 ohara 110: push_error(ERROR_ID_UNKNOWN_SM, new_cmo_null());
111: }
112: }
113:
114: int oxf_error(OXFILE *oxfp)
115: {
116: int e = oxfp->error;
117: if (e != 0) {
118: oxfp->error = 0;
119: }
120: return e;
121: }
122:
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>