[bca6c6] | 1 | // as this file provides only example code, |
---|
| 2 | // no trace of it must appear in Singular and Singularg |
---|
| 3 | // |
---|
| 4 | #if 0 |
---|
| 5 | // |
---|
[f5b40a] | 6 | #include <kernel/mod2.h> |
---|
[295381] | 7 | #include <Singular/ipid.h> |
---|
| 8 | #include <Singular/blackbox.h> |
---|
[a49a36] | 9 | #include <omalloc/omalloc.h> |
---|
| 10 | #include <kernel/febase.h> |
---|
| 11 | #include <kernel/longrat.h> |
---|
| 12 | #include <Singular/subexpr.h> |
---|
[295381] | 13 | #include <Singular/bigintm.h> |
---|
| 14 | |
---|
| 15 | |
---|
[115639] | 16 | static char * bigintm_String(blackbox *b, void *d) |
---|
[295381] | 17 | { if (d==NULL) return omStrDup("oo"); |
---|
| 18 | else |
---|
| 19 | { |
---|
| 20 | StringSetS(""); |
---|
| 21 | number n=(number)d; nlWrite(n,NULL); d=(void*)n; |
---|
| 22 | return omStrDup(StringAppendS("")); |
---|
| 23 | } |
---|
| 24 | } |
---|
[115639] | 25 | static void * bigintm_Copy(blackbox*b, void *d) |
---|
[295381] | 26 | { number n=(number)d; return nlCopy(n); } |
---|
| 27 | |
---|
[115639] | 28 | static BOOLEAN bigintm_Assign(leftv l, leftv r) |
---|
[295381] | 29 | { |
---|
| 30 | blackbox *ll=getBlackboxStuff(l->Typ()); |
---|
[115639] | 31 | |
---|
[295381] | 32 | if (r->Typ()>MAX_TOK) |
---|
| 33 | { |
---|
[bca6c6] | 34 | if (l->Typ() == r->Typ()) |
---|
[295381] | 35 | { |
---|
[115639] | 36 | blackbox *rr=getBlackboxStuff(r->Typ()); |
---|
| 37 | |
---|
[295381] | 38 | if (l->Data()!=NULL) { number n1=(number)l->Data(); nlDelete(&n1,NULL); } |
---|
| 39 | number n2=(number)r->CopyD(); |
---|
| 40 | if (l->rtyp==IDHDL) |
---|
| 41 | { |
---|
| 42 | IDDATA((idhdl)l->data)=(char *)n2; |
---|
| 43 | } |
---|
| 44 | else |
---|
| 45 | { |
---|
| 46 | l->data=(void *)n2; |
---|
| 47 | } |
---|
| 48 | return FALSE; |
---|
| 49 | } |
---|
| 50 | else |
---|
| 51 | { |
---|
[115639] | 52 | Werror("bigintm_Assign: assign %s (%d) = %s (%d)", |
---|
| 53 | getBlackboxName(l->Typ()), l->Typ(), |
---|
| 54 | getBlackboxName(r->Typ()), r->Typ()); |
---|
[295381] | 55 | return TRUE; |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | else if (r->Typ()==INT_CMD) |
---|
| 59 | { |
---|
| 60 | if (l->Data()!=NULL) { number n1=(number)l->Data(); nlDelete(&n1,NULL); } |
---|
| 61 | number n2=nlInit((int)(long)r->Data(),NULL); |
---|
| 62 | if (l->rtyp==IDHDL) |
---|
| 63 | { |
---|
| 64 | IDDATA((idhdl)l->data)=(char *)n2; |
---|
| 65 | } |
---|
| 66 | else |
---|
| 67 | { |
---|
| 68 | l->data=(void *)n2; |
---|
| 69 | } |
---|
| 70 | return FALSE; |
---|
| 71 | } |
---|
| 72 | else |
---|
| 73 | Werror("assign %d = %d",l->Typ(),r->Typ()); |
---|
| 74 | return TRUE; |
---|
| 75 | } |
---|
[115639] | 76 | static BOOLEAN bigintm_OpM(int op, leftv res, leftv args); |
---|
[295381] | 77 | |
---|
| 78 | |
---|
[115639] | 79 | static BOOLEAN bigintm_Op2(int op, leftv res, leftv a1, leftv a2) |
---|
[295381] | 80 | { |
---|
| 81 | // interpreter: a1 is ist bigintm |
---|
[115639] | 82 | |
---|
[295381] | 83 | blackbox *a=getBlackboxStuff(a1->Typ()); |
---|
| 84 | number n1=(number)a1->Data(); |
---|
| 85 | switch(op) |
---|
| 86 | { |
---|
| 87 | case '+': |
---|
| 88 | { |
---|
| 89 | if (a2->Typ()==INT_CMD) |
---|
| 90 | { |
---|
| 91 | number n2=nlInit((int)(long)a2->Data(),NULL); |
---|
| 92 | number n=nlAdd(n1,n2); |
---|
| 93 | res->data=(void *)n; |
---|
| 94 | res->rtyp=a1->Typ(); |
---|
| 95 | return FALSE; |
---|
| 96 | } |
---|
| 97 | else if (a2->Typ()==a1->Typ()) |
---|
| 98 | { |
---|
| 99 | number n2=(number)a2->Data(); |
---|
| 100 | number n=nlAdd(n1,n2); |
---|
| 101 | res->data=(void *)n; |
---|
| 102 | res->rtyp=a1->Typ(); |
---|
| 103 | return FALSE; |
---|
| 104 | } |
---|
| 105 | return TRUE; |
---|
| 106 | } |
---|
[115639] | 107 | default: |
---|
[bca6c6] | 108 | return blackboxDefaultOp2(op,res,a1,a2); |
---|
[295381] | 109 | } |
---|
[c728c0] | 110 | return blackboxDefaultOp2(op,res,a1,a2); |
---|
[295381] | 111 | } |
---|
| 112 | // BOOLEAN opM(int op, leftv res, leftv args) |
---|
[115639] | 113 | static BOOLEAN bigintm_OpM(int op, leftv res, leftv args) |
---|
[295381] | 114 | { |
---|
| 115 | // interpreter: args->1. arg is ist bigintm |
---|
| 116 | blackbox *a=getBlackboxStuff(args->Typ()); |
---|
| 117 | switch(op) |
---|
| 118 | { |
---|
| 119 | case STRING_CMD: |
---|
| 120 | { |
---|
| 121 | res->data=(void *)a->blackbox_String(a,args->Data()); |
---|
| 122 | res->rtyp=STRING_CMD; |
---|
| 123 | return FALSE; |
---|
| 124 | } |
---|
| 125 | default: |
---|
[bca6c6] | 126 | return blackboxDefaultOpM(op,res,args); |
---|
[295381] | 127 | break; |
---|
| 128 | } |
---|
[bca6c6] | 129 | return blackboxDefaultOpM(op,res,args); |
---|
[295381] | 130 | } |
---|
[115639] | 131 | static void bigintm_destroy(blackbox *b, void *d) |
---|
[295381] | 132 | { |
---|
| 133 | if (d!=NULL) |
---|
| 134 | { |
---|
| 135 | number n=(number)d; |
---|
| 136 | nlDelete(&n,NULL); |
---|
| 137 | } |
---|
| 138 | } |
---|
[115639] | 139 | |
---|
| 140 | |
---|
| 141 | BOOLEAN bigintm_setup() |
---|
| 142 | { |
---|
[bca6c6] | 143 | // it is not very usefull to call this setup routine twice, |
---|
| 144 | // but (as this is an example) it is possible. |
---|
| 145 | // Other blackbox type really require multiple setup calls |
---|
| 146 | // (with different names). |
---|
| 147 | // Also the format of this setup routine is not fixed, |
---|
| 148 | // it may take additional parameters, it may return a value or not |
---|
| 149 | // - it really depends. |
---|
| 150 | |
---|
| 151 | blackbox *b=(blackbox*)omAlloc0(sizeof(blackbox)); |
---|
| 152 | // all undefined entries will be set to default in setBlackboxStuff |
---|
| 153 | // the default Print is quite usefule, |
---|
| 154 | // all other are simply error messages |
---|
| 155 | b->blackbox_destroy=bigintm_destroy; |
---|
| 156 | b->blackbox_String=bigintm_String; |
---|
| 157 | //b->blackbox_Print=blackbox_default_Print; |
---|
| 158 | //b->blackbox_Init=blackbox_default_Init; |
---|
| 159 | b->blackbox_Copy=bigintm_Copy; |
---|
| 160 | b->blackbox_Assign=bigintm_Assign; |
---|
| 161 | //b->blackbox_Op1=blackboxDefaultOp1; |
---|
| 162 | b->blackbox_Op2=bigintm_Op2; |
---|
| 163 | //b->blackbox_Op3=blackbox_default_Op3; |
---|
| 164 | b->blackbox_OpM=bigintm_OpM; |
---|
| 165 | int rt=setBlackboxStuff(b,"bigintm"); |
---|
| 166 | |
---|
| 167 | Print("bigintm_setup: create type %d (%s)\n",rt, getBlackboxName(rt)); |
---|
| 168 | |
---|
| 169 | return FALSE; // ok, TRUE = error!: this example never fails |
---|
[115639] | 170 | } |
---|
| 171 | |
---|
[bca6c6] | 172 | #endif |
---|