Annotation of OpenXM_contrib2/asir2000/parse/arith.c, Revision 1.4
1.2 noro 1: /*
2: * Copyright (c) 1994-2000 FUJITSU LABORATORIES LIMITED
3: * All rights reserved.
4: *
5: * FUJITSU LABORATORIES LIMITED ("FLL") hereby grants you a limited,
6: * non-exclusive and royalty-free license to use, copy, modify and
7: * redistribute, solely for non-commercial and non-profit purposes, the
8: * computer program, "Risa/Asir" ("SOFTWARE"), subject to the terms and
9: * conditions of this Agreement. For the avoidance of doubt, you acquire
10: * only a limited right to use the SOFTWARE hereunder, and FLL or any
11: * third party developer retains all rights, including but not limited to
12: * copyrights, in and to the SOFTWARE.
13: *
14: * (1) FLL does not grant you a license in any way for commercial
15: * purposes. You may use the SOFTWARE only for non-commercial and
16: * non-profit purposes only, such as academic, research and internal
17: * business use.
18: * (2) The SOFTWARE is protected by the Copyright Law of Japan and
19: * international copyright treaties. If you make copies of the SOFTWARE,
20: * with or without modification, as permitted hereunder, you shall affix
21: * to all such copies of the SOFTWARE the above copyright notice.
22: * (3) An explicit reference to this SOFTWARE and its copyright owner
23: * shall be made on your publication or presentation in any form of the
24: * results obtained by use of the SOFTWARE.
25: * (4) In the event that you modify the SOFTWARE, you shall notify FLL by
1.3 noro 26: * e-mail at risa-admin@sec.flab.fujitsu.co.jp of the detailed specification
1.2 noro 27: * for such modification or the source code of the modified part of the
28: * SOFTWARE.
29: *
30: * THE SOFTWARE IS PROVIDED AS IS WITHOUT ANY WARRANTY OF ANY KIND. FLL
31: * MAKES ABSOLUTELY NO WARRANTIES, EXPRESSED, IMPLIED OR STATUTORY, AND
32: * EXPRESSLY DISCLAIMS ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS
33: * FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT OF THIRD PARTIES'
34: * RIGHTS. NO FLL DEALER, AGENT, EMPLOYEES IS AUTHORIZED TO MAKE ANY
35: * MODIFICATIONS, EXTENSIONS, OR ADDITIONS TO THIS WARRANTY.
36: * UNDER NO CIRCUMSTANCES AND UNDER NO LEGAL THEORY, TORT, CONTRACT,
37: * OR OTHERWISE, SHALL FLL BE LIABLE TO YOU OR ANY OTHER PERSON FOR ANY
38: * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, PUNITIVE OR CONSEQUENTIAL
39: * DAMAGES OF ANY CHARACTER, INCLUDING, WITHOUT LIMITATION, DAMAGES
40: * ARISING OUT OF OR RELATING TO THE SOFTWARE OR THIS AGREEMENT, DAMAGES
41: * FOR LOSS OF GOODWILL, WORK STOPPAGE, OR LOSS OF DATA, OR FOR ANY
42: * DAMAGES, EVEN IF FLL SHALL HAVE BEEN INFORMED OF THE POSSIBILITY OF
43: * SUCH DAMAGES, OR FOR ANY CLAIM BY ANY OTHER PARTY. EVEN IF A PART
44: * OF THE SOFTWARE HAS BEEN DEVELOPED BY A THIRD PARTY, THE THIRD PARTY
45: * DEVELOPER SHALL HAVE NO LIABILITY IN CONNECTION WITH THE USE,
46: * PERFORMANCE OR NON-PERFORMANCE OF THE SOFTWARE.
47: *
1.4 ! noro 48: * $OpenXM: OpenXM_contrib2/asir2000/parse/arith.c,v 1.3 2000/08/22 05:04:25 noro Exp $
1.2 noro 49: */
1.1 noro 50: #include "ca.h"
51: #include "parse.h"
52:
53: struct oAFUNC {
54: void (*add)();
55: void (*sub)();
56: void (*mul)();
57: void (*div)();
58: void (*pwr)();
59: void (*chsgn)();
60: int (*comp)();
61: };
62:
63: struct oARF arf[6];
64: ARF addfs, subfs, mulfs, divfs, remfs, pwrfs;
65:
66: void divsdc();
67:
68: struct oAFUNC afunc[] = {
1.4 ! noro 69: /* ???=0 */ {0,0,0,0,0,0,0},
! 70: /* O_N=1 */ {addnum,subnum,mulnum,divnum,pwrnum,chsgnnum,compnum},
! 71: /* O_P=2 */ {addp,subp,mulp,divr,pwrp,chsgnp,compp},
! 72: /* O_R=3 */ {addr,subr,mulr,divr,pwrr,chsgnr,compr},
! 73: /* O_LIST=4 */ {notdef,notdef,notdef,notdef,notdef,notdef,complist},
! 74: /* O_VECT=5 */ {addvect,subvect,mulvect,divvect,notdef,chsgnvect,compvect},
! 75: /* O_MAT=6 */ {addmat,submat,mulmat,divmat,pwrmat,chsgnmat,compmat},
! 76: /* O_STR=7 */ {addstr,notdef,notdef,notdef,notdef,notdef,compstr},
! 77: /* O_COMP=8 */ {addcomp,subcomp,mulcomp,divcomp,pwrcomp,chsgncomp,compcomp},
! 78: /* O_DP=9 */ {addd,subd,muld,divsdc,notdef,chsgnd,compd},
! 79: /* O_USINT=10 */ {notdef,notdef,notdef,notdef,notdef,notdef,compui},
! 80: /* O_ERR=11 */ {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
! 81: /* O_GF2MAT=12 */ {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
! 82: /* O_MATHCAP=13 */ {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
! 83: /* O_F=14 */ {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
! 84: /* O_GFMMAT=15 */ {notdef,notdef,notdef,notdef,notdef,notdef,(int(*)())notdef},
! 85: /* O_BYTEARRAY=16 */ {notdef,notdef,notdef,notdef,notdef,notdef,compbytearray},
1.1 noro 86: };
87:
88: void arf_init() {
89: addfs = &arf[0]; addfs->name = "+"; addfs->fp = arf_add;
90: subfs = &arf[1]; subfs->name = "-"; subfs->fp = arf_sub;
91: mulfs = &arf[2]; mulfs->name = "*"; mulfs->fp = arf_mul;
92: divfs = &arf[3]; divfs->name = "/"; divfs->fp = arf_div;
93: remfs = &arf[4]; remfs->name = "%"; remfs->fp = arf_remain;
94: pwrfs = &arf[5]; pwrfs->name = "^"; pwrfs->fp = arf_pwr;
95: }
96:
97: void arf_add(vl,a,b,r)
98: VL vl;
99: Obj a,b,*r;
100: {
101: int mid;
102:
103: if ( !a )
104: *r = b;
105: else if ( !b )
106: *r = a;
107: else if ( OID(a) == OID(b) )
108: (*afunc[OID(a)].add)(vl,a,b,r);
109: else if ( (mid = MAX(OID(a),OID(b))) <= O_R )
110: (*afunc[mid].add)(vl,a,b,r);
111: else
112: notdef(vl,a,b,r);
113: }
114:
115: void arf_sub(vl,a,b,r)
116: VL vl;
117: Obj a,b,*r;
118: {
119: int mid;
120:
121: if ( !a )
122: if ( !b )
123: *r = 0;
124: else
125: (*afunc[OID(b)].chsgn)(b,r);
126: else if ( !b )
127: *r = a;
128: else if ( OID(a) == OID(b) )
129: (*afunc[OID(a)].sub)(vl,a,b,r);
130: else if ( (mid = MAX(OID(a),OID(b))) <= O_R )
131: (*afunc[mid].sub)(vl,a,b,r);
132: else
133: notdef(vl,a,b,r);
134: }
135:
136: void arf_mul(vl,a,b,r)
137: VL vl;
138: Obj a,b,*r;
139: {
140: int mid;
141:
142: if ( !a || !b )
143: *r = 0;
144: else if ( OID(a) == OID(b) )
145: (*(afunc[OID(a)].mul))(vl,a,b,r);
146: else if ( (mid = MAX(OID(a),OID(b))) <= O_R ||
147: (mid == O_MAT) || (mid == O_VECT) || (mid == O_DP) )
148: (*afunc[mid].mul)(vl,a,b,r);
149: else
150: notdef(vl,a,b,r);
151: }
152:
153: void arf_div(vl,a,b,r)
154: VL vl;
155: Obj a,b,*r;
156: {
157: int mid;
158:
159: if ( !b )
160: error("div : division by 0");
161: if ( !a )
162: *r = 0;
163: else if ( (OID(a) == OID(b)) )
164: (*(afunc[OID(a)].div))(vl,a,b,r);
165: else if ( (mid = MAX(OID(a),OID(b))) <= O_R ||
166: (mid == O_MAT) || (mid == O_VECT) || (mid == O_DP) )
167: (*afunc[mid].div)(vl,a,b,r);
168: else
169: notdef(vl,a,b,r);
170: }
171:
172: void arf_remain(vl,a,b,r)
173: VL vl;
174: Obj a,b,*r;
175: {
176: if ( !b )
177: error("rem : division by 0");
178: else if ( !a )
179: *r = 0;
180: else if ( MAX(OID(a),OID(b)) <= O_P )
181: cmp((Q)b,(P)a,(P *)r);
182: else
183: notdef(vl,a,b,r);
184: }
185:
186: void arf_pwr(vl,a,e,r)
187: VL vl;
188: Obj a,e,*r;
189: {
190: R t;
191:
192: if ( !a )
193: *r = 0;
194: else if ( !e )
195: *r = (pointer)ONE;
196: else if ( (OID(e) <= O_N) && INT(e) ) {
197: if ( (OID(a) == O_P) && (SGN((Q)e) < 0) ) {
198: MKRAT((P)a,(P)ONE,1,t);
199: (*(afunc[O_R].pwr))(vl,t,e,r);
200: } else
201: (*(afunc[OID(a)].pwr))(vl,a,e,r);
202: } else if ( OID(a) <= O_R )
203: mkpow(vl,a,e,r);
204: else
205: notdef(vl,a,e,r);
206: }
207:
208: void arf_chsgn(a,r)
209: Obj a,*r;
210: {
211: if ( !a )
212: *r = 0;
213: else
214: (*(afunc[OID(a)].chsgn))(a,r);
215: }
216:
217: int arf_comp(vl,a,b)
218: VL vl;
219: Obj a,b;
220: {
221: if ( !a )
222: if ( !b )
223: return 0;
224: else
225: return (*afunc[OID(b)].comp)(vl,a,b);
226: else if ( !b )
227: return (*afunc[OID(a)].comp)(vl,a,b);
228: else if ( OID(a) != OID(b) )
229: return OID(a)>OID(b) ? 1 : -1;
230: else
231: return (*afunc[OID(a)].comp)(vl,a,b);
232: }
233:
234: int complist(vl,a,b)
235: VL vl;
236: LIST a,b;
237: {
238: int i,t;
239: NODE an,bn;
240:
241: if ( !a )
242: if ( !b )
243: return 0;
244: else
245: return -1;
246: else if ( !b )
247: return 1;
248: for ( i = 0, an = BDY(a); an; i++, an = NEXT(an) );
249: for ( an = BDY(b); an; i--, an = NEXT(an) );
250: if ( i )
251: return i > 0 ? 1 : -1;
252: for ( an = BDY(a), bn = BDY(b); an; an = NEXT(an), bn = NEXT(bn) )
253: if ( t = arf_comp(vl,BDY(an),BDY(bn)) )
254: return t;
255: return 0;
256: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>