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