Annotation of OpenXM/src/ox_gsl/ox_gsl.c, Revision 1.1
1.1 ! takayama 1: /* $OpenXM$
! 2: */
! 3:
! 4: #include <stdio.h>
! 5: #include <stdlib.h>
! 6: #include "ox_toolkit.h"
! 7:
! 8: OXFILE *fd_rw;
! 9:
! 10: #define INIT_S_SIZE 2048
! 11: #define EXT_S_SIZE 2048
! 12:
! 13: static int stack_size = 0;
! 14: static int stack_pointer = 0;
! 15: static cmo **stack = NULL;
! 16:
! 17: int Debug=1;
! 18:
! 19: int show_stack_top() {
! 20: cmo *data;
! 21: if (stack_pointer > 0) {
! 22: data=stack[stack_pointer-1];
! 23: printf("tag=%d\n",data->tag);
! 24: }else {
! 25: printf("The stack is empty.\n");
! 26: }
! 27: }
! 28:
! 29: void init_gc()
! 30: {
! 31: GC_INIT();
! 32: }
! 33:
! 34: int initialize_stack()
! 35: {
! 36: stack_pointer = 0;
! 37: stack_size = INIT_S_SIZE;
! 38: stack = malloc(stack_size*sizeof(cmo*));
! 39: }
! 40:
! 41: static int extend_stack()
! 42: {
! 43: int size2 = stack_size + EXT_S_SIZE;
! 44: cmo **stack2 = malloc(size2*sizeof(cmo*));
! 45: memcpy(stack2, stack, stack_size*sizeof(cmo *));
! 46: free(stack);
! 47: stack = stack2;
! 48: stack_size = size2;
! 49: }
! 50:
! 51: int push(cmo* m)
! 52: {
! 53: stack[stack_pointer] = m;
! 54: stack_pointer++;
! 55: if (stack_pointer >= stack_size) {
! 56: extend_stack();
! 57: }
! 58: }
! 59:
! 60: cmo* pop()
! 61: {
! 62: if (stack_pointer > 0) {
! 63: stack_pointer--;
! 64: return stack[stack_pointer];
! 65: }
! 66: return new_cmo_null();
! 67: }
! 68:
! 69: void pops(int n)
! 70: {
! 71: stack_pointer -= n;
! 72: if (stack_pointer < 0) {
! 73: stack_pointer = 0;
! 74: }
! 75: }
! 76:
! 77: #define OX_GSL_VERSION 2018032901
! 78: #define ID_STRING "2018/03/29 13:56:00"
! 79:
! 80: int sm_mathcap()
! 81: {
! 82: mathcap_init(OX_GSL_VERSION, ID_STRING, "ox_gsl", NULL, NULL);
! 83: push(oxf_cmo_mathcap(fd_rw));
! 84: return 0;
! 85: }
! 86:
! 87: int sm_popCMO()
! 88: {
! 89: cmo* m = pop();
! 90:
! 91: if (m != NULL) {
! 92: send_ox_cmo(fd_rw, m);
! 93: return 0;
! 94: }
! 95: return SM_popCMO;
! 96: }
! 97:
! 98: cmo_error2 *make_error2(int code)
! 99: {
! 100: return new_cmo_int32(-1);
! 101: }
! 102:
! 103: int get_i()
! 104: {
! 105: cmo *c = pop();
! 106: if (c->tag == CMO_INT32) {
! 107: return ((cmo_int32 *)c)->i;
! 108: }else if (c->tag == CMO_ZZ) {
! 109: return mpz_get_si(((cmo_zz *)c)->mpz);
! 110: }
! 111: make_error2(-1);
! 112: return 0;
! 113: }
! 114:
! 115: int get_xy(int *x, int *y)
! 116: {
! 117: pop();
! 118: *x = get_i();
! 119: *y = get_i();
! 120: }
! 121:
! 122: int my_add_int32()
! 123: {
! 124: int x, y;
! 125: get_xy(&x, &y);
! 126: push(new_cmo_int32(x+y));
! 127: }
! 128:
! 129:
! 130: int sm_executeFunction()
! 131: {
! 132: cmo_string *func = (cmo_string *)pop();
! 133: if (func->tag != CMO_STRING) {
! 134: push(make_error2(0));
! 135: return -1;
! 136: }
! 137: if (strcmp(func->s, "add_int32") == 0) {
! 138: my_add_int32();
! 139: }else {
! 140: push(make_error2(0));
! 141: return -1;
! 142: }
! 143: }
! 144:
! 145:
! 146: int receive_and_execute_sm_command()
! 147: {
! 148: int code = receive_int32(fd_rw);
! 149: switch(code) {
! 150: case SM_popCMO:
! 151: sm_popCMO();
! 152: break;
! 153: case SM_executeFunction:
! 154: sm_executeFunction();
! 155: break;
! 156: case SM_mathcap:
! 157: sm_mathcap();
! 158: break;
! 159: case SM_setMathCap:
! 160: pop();
! 161: break;
! 162: default:
! 163: ;
! 164: }
! 165: }
! 166:
! 167: int receive()
! 168: {
! 169: int tag;
! 170:
! 171: tag = receive_ox_tag(fd_rw);
! 172: switch(tag) {
! 173: case OX_DATA:
! 174: push(receive_cmo(fd_rw));
! 175: if (Debug) show_stack_top();
! 176: break;
! 177: case OX_COMMAND:
! 178: if (Debug) show_stack_top();
! 179: receive_and_execute_sm_command();
! 180: break;
! 181: default:
! 182: ;
! 183: }
! 184: return 0;
! 185: }
! 186:
! 187: int main()
! 188: {
! 189: ox_stderr_init(stderr);
! 190: initialize_stack();
! 191: init_gc();
! 192:
! 193: fd_rw = oxf_open(3);
! 194: oxf_determine_byteorder_server(fd_rw);
! 195:
! 196: while(1) {
! 197: receive();
! 198: }
! 199: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>