Sindbad~EG File Manager

Current Path : /home/beeson/Otter-Lambda/otter2/
Upload File :
Current File : //home/beeson/Otter-Lambda/otter2/evalbignum.c

// Beeson 10.23.03
// Enable Otter2 to evaluate terms using bignum arithmetic.
// This process goes from strings to strings as Otter does not 
// store values of terms.

#include "export.h"  // Beeson
#include "bignum.h"  // Beeson
#include "header.h"  // Otter 
#include "heap.h"
#include "heaps.h"

static int heap_ready;
static unit *mem;

int *eval_bignum(char *ret, unsigned N, int opcode, char *s, char *t)
// ans should have space for N decimal digits
// s and t are strings defining integers in decimal representation.
// opcode is one of SUM_SYM, PROD_SYM,DIFF_SYM, DIV_SYM, MOD_SYM.
// perform the operation and put the answer in *ans as a 
// string in decimal representation,  if it will have at 
// most N digits.  Return 0 for success, 1 for too long an answer.
{ bignum ans, junk,x,y;
  char *r;
  if(!heap_ready)
     { mem = (unit *) malloc(256 * sizeof(unit));
       if(!mem)
          abend("Malloc failed when allocating heap for bignum arithmetic."); 
       create_heap(0, mem,256);
       init_heap(256);
     }
  if(string_bignum(s,strlen(s),&x))
     return NULL;
  if(string_bignum(t,strlen(t),&y))
     return NULL;
  // OK,  both successfully converted to bignums
  switch (opcode) 
     {  case SUM_SYM:   bigplus(x,y,&ans); break;
        case PROD_SYM:  bigmult(x,y,&ans); break;
        case DIFF_SYM:  bigminus(x,y,&ans);  break;
        case DIV_SYM:   bigdivide(x,y,&ans,&junk);  
                        freespace(junk.val); 
                        break;
        case MOD_SYM:   bigdivide(x,y,&junk,&ans); 
                        freespace(junk.val);
                        break;
     }
  r = bignum_string(ans,1);
  freespace(ans.val);
  freespace(x.val);
  freespace(y.val);
  if(strlen(r) > N)
     return 0; // this will cause an abend after the return 
  strcpy(ret,r);
  return 0;
}  

Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists