1: # include <ingres.h>
2: # include <aux.h>
3: # include <tree.h>
4: # include <symbol.h>
5: # include "../decomp/globs.h"
6: # include <sccs.h>
7: # include <errors.h>
8:
9: SCCSID(@(#)typecheck.c 8.2 2/8/85)
10:
11: /*
12: ** TYPECHECK
13: **
14: ** Performs typechecking and conversions where appropriate .
15: ** Prohibits mixed type expressions.
16: */
17:
18: typecheck(pp1, pp2, opval)
19: SYMBOL *pp1, *pp2;
20: int opval;
21: {
22: register int i;
23: register SYMBOL *p1, *p2;
24:
25: p1 = pp1;
26: p2 = pp2;
27:
28: i = (p1->type == CHAR & p2->type == CHAR); /* c is true only if both are chars */
29: switch (opval)
30: {
31:
32: case opCONCAT:
33: if (!i)
34: ov_err(NUMERIC); /* numeric in a char operator */
35: return; /* else no further checking is needed */
36:
37:
38: case opADD:
39: case opSUB:
40: if (i)
41: return; /* make opADD and opSUB legal operators with char */
42: case opMUL:
43: case opDIV:
44: case opPOW:
45: case opMOD:
46: if (i)
47: ov_err(BADCHAR); /* arithmetic operation on two character fields */
48: }
49:
50: /* first check for identical types of symbols */
51: if (p1->type == p2->type)
52: {
53: if (p1->len == p2->len)
54: return;
55: /* lengths are different. make p2 point to the smaller one */
56: if (p1->len < p2->len)
57: {
58: p2 = p1;
59: p1 = pp2;
60: }
61:
62: switch (p2->type)
63: {
64: case INT:
65: if (p1->len == 2)
66: p2->len = 2;
67: else if (p1->len == 4)
68: i2toi4(p2);
69:
70: case CHAR:
71: return; /* done if char or int */
72:
73: case FLOAT:
74: f8tof4(p1);
75: return;
76: }
77: }
78:
79: /* at least one symbol is an INT or FLOAT. The other can't be a CHAR */
80: if (p1->type == CHAR || p2->type == CHAR)
81: ov_err(BADMIX); /* attempting binary operation on one CHAR field with a numeric */
82:
83: /* one symbol is an INT and the other a FLOAT */
84: if (p2->type == INT)
85: {
86: /* exchange so that p1 is an INT and p2 is a FLOAT */
87: p1 = p2;
88: p2 = pp1;
89: }
90:
91: /* p1 is an INT and p2 a FLOAT */
92: itof(p1);
93: if (p2->len == 4)
94: f8tof4(p1);
95: }
96: /*
97: ** Coerce the top of stack symbol to the
98: ** specified type and length. If the current
99: ** value is a character then it must be converted
100: ** to numeric. A user error will occure is the
101: ** char is not syntaxtically correct.
102: */
103:
104: typecoerce(tosx, ntype, nlen)
105: SYMBOL *tosx;
106: int ntype;
107: int nlen;
108: {
109: register SYMBOL *tos;
110: register char *cp;
111: register int *val;
112: int ret;
113: char temp[256];
114:
115: tos = tosx;
116:
117: if (tos->type == CHAR)
118: {
119: val = (int *) &tos->value.sym_data;
120: cp = temp;
121: bmove(tos->value.sym_data.cptype, cp, tos->len & I1MASK);
122: cp[tos->len & I1MASK] = '\0';
123: if (ntype == FLOAT)
124: ret = atof(cp, val);
125: else
126: {
127: if (nlen == 4)
128: ret = atol(cp, val);
129: else
130: *val = atoi(cp);
131: }
132: if (ret < 0)
133: ov_err(CHARCONVERT);
134: tos->type = ntype;
135: tos->len = nlen;
136: }
137: else
138: rcvt(tos, ntype, nlen);
139: }
140:
141:
142: i2toi4(pp)
143: SYMBOL *pp;
144: {
145: register SYMBOL *p;
146:
147: # ifdef xOTR3
148: if (tTf(87, 0))
149: {
150: printf("i2toi4: ");
151: prsym(pp);
152: }
153: # endif
154: p = pp;
155:
156: *(i4type *)&p->value = *(i2type *)&p->value;
157: p->len = 4;
158: # ifdef xOTR3
159: if (tTf(87, 0))
160: {
161: printf("i2toi4 rets: ");
162: prsym(p);
163: }
164: # endif
165: }
166:
167:
168: i4toi2(pp)
169: SYMBOL *pp;
170: {
171: register SYMBOL *p;
172:
173: # ifdef xOTR3
174: if (tTf(87, 1))
175: {
176: printf("i4toi2: ");
177: prsym(pp);
178: }
179: # endif
180: p = pp;
181:
182: *(i2type *)&p->value = *(i4type *)&p->value;
183: p->len = 2;
184: # ifdef xOTR3
185: if (tTf(87, 1))
186: {
187: printf("i4toi2 rets: ");
188: prsym(p);
189: }
190: # endif
191: }
192:
193:
194: itof(p)
195: register SYMBOL *p;
196: {
197:
198: # ifdef xOTR3
199: if (tTf(87, 2))
200: {
201: printf("itof: ");
202: prsym(p);
203: }
204: # endif
205:
206: if (p->len == 4)
207: p->value.sym_data.f8type = p->value.sym_data.i4type;
208: else
209: p->value.sym_data.f8type = p->value.sym_data.i2type;
210: p->type = FLOAT;
211: p->len= 8;
212: # ifdef xOTR3
213: if (tTf(87, 2))
214: {
215: printf("itof rets: ");
216: prsym(p);
217: }
218: # endif
219: }
220:
221:
222: ftoi2(p)
223: register SYMBOL *p;
224: {
225: # ifdef xOTR3
226: if (tTf(87, 3))
227: {
228: printf("ftoi: ");
229: prsym(p);
230: }
231: # endif
232:
233: p->value.sym_data.i2type = p->value.sym_data.f8type;
234: p->type = INT;
235: p->len = 2;
236: # ifdef xOTR3
237: if (tTf(87, 3))
238: {
239: printf("ftoi rets: ");
240: prsym(p);
241: }
242: # endif
243: }
244:
245:
246: ftoi4(p)
247: register SYMBOL *p;
248: {
249: # ifdef xOTR3
250: if (tTf(87, 4))
251: {
252: printf("ftoi4: ");
253: prsym(p);
254: }
255: # endif
256:
257: p->value.sym_data.i4type = p->value.sym_data.f8type;
258: p->type = INT;
259: p->len = 4;
260: # ifdef xOTR3
261: if (tTf(87, 4))
262: {
263: printf("ftoi4 rets: ");
264: prsym(p);
265: }
266: # endif
267: }
268:
269:
270: f8tof4(pp)
271: SYMBOL *pp;
272: {
273: pp->len = 4;
274: }
Defined functions
itof
defined in line
194; used 8 times