1 /* $Id: cpy.y,v 1.18 2010/02/25 15:49:00 ragge Exp $ */
4 * Copyright (c) 2004 Anders Magnusson (ragge@ludd.luth.se).
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 * Copyright(C) Caldera International Inc. 2001-2002. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
37 * Redistributions of source code and documentation must retain the above
38 * copyright notice, this list of conditions and the following disclaimer.
39 * Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * All advertising materials mentioning features or use of this software
43 * must display the following acknowledgement:
44 * This product includes software developed or owned by Caldera
46 * Neither the name of Caldera International, Inc. nor the names of other
47 * contributors may be used to endorse or promote products derived from
48 * this software without specific prior written permission.
50 * USE OF THE SOFTWARE PROVIDED FOR UNDER THIS LICENSE BY CALDERA
51 * INTERNATIONAL, INC. AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR
52 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
53 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
54 * DISCLAIMED. IN NO EVENT SHALL CALDERA INTERNATIONAL, INC. BE LIABLE
55 * FOR ANY DIRECT, INDIRECT INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OFLIABILITY, WHETHER IN CONTRACT,
59 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
60 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
61 * POSSIBILITY OF SUCH DAMAGE.
68 void yyerror(const char *);
70 int setd
(int l
, int r
);
72 #define EVALUNARY(tok, l, r) l.nd_val = tok r.nd_val; l.op = r.op
73 #define EVALBIN(tok, d, l, r) \
74 d.op
= setd
(l.op
, r.op
); d.nd_val
= l.nd_val tok r.nd_val
75 #define EVALUBIN(tok, d, l, r, t) \
76 d.op
= setd
(l.op
, r.op
); \
77 if
(d.op
== NUMBER
) d.nd_val
= l.nd_val tok r.nd_val
; \
78 else d.nd_uval
= l.nd_uval tok r.nd_uval
; \
79 if
(t
&& d.op
) d.op
= NUMBER
80 #define XEVALUBIN(tok, d, l, r) \
81 if
(r.nd_val
) { EVALUBIN
(tok
, d
, l
, r
, 0); } else d.op
= 0
85 %term EQ NE LE GE LS RS
86 %term ANDAND OROR IDENT NUMBER UNUMBER DEFINED
88 * The following terminals are not used in the yacc code.
90 %term STRING WSPACE CMNT
103 %right
'!' '~' UMINUS
110 %type
<node
> term e NUMBER UNUMBER
115 error("division by zero");
120 { EVALUBIN
(*, $$
, $1, $3, 0); }
122 { XEVALUBIN
(/, $$
, $1, $3); }
124 { XEVALUBIN
(%
, $$
, $1, $3); }
126 { EVALBIN
(+, $$
, $1, $3); }
128 { EVALBIN
(-, $$
, $1, $3); }
130 { EVALBIN
(<<, $$
, $1, $3); }
132 { EVALUBIN
(>>, $$
, $1, $3, 0); }
134 { EVALUBIN
(<, $$
, $1, $3, 1); }
136 { EVALUBIN
(>, $$
, $1, $3, 1); }
138 { EVALUBIN
(<=, $$
, $1, $3, 1); }
140 { EVALUBIN
(>=, $$
, $1, $3, 1); }
142 { EVALUBIN
(==, $$
, $1, $3, 1); }
144 { EVALUBIN
(!=, $$
, $1, $3, 1); }
146 { EVALBIN
(&, $$
, $1, $3); }
148 { EVALBIN
(^
, $$
, $1, $3); }
150 { EVALBIN
(|
, $$
, $1, $3); }
154 $$.op
= setd
($1.op
, $3.op
);
155 $$.nd_val
= ($3.nd_val
!= 0);
157 if
($$.op
== UNUMBER
) $$.op
= NUMBER
;
160 if
($1.nd_val
!= 0) {
161 $$.nd_val
= ($1.nd_val
!= 0);
164 $$.nd_val
= ($3.nd_val
!= 0);
165 $$.op
= setd
($1.op
, $3.op
);
167 if
($$.op
== UNUMBER
) $$.op
= NUMBER
;
178 $$.op
= setd
($1.op
, $3.op
);
179 $$.nd_val
= $3.nd_val
;
180 if
($$.op
) $$.op
= $3.op
;
185 '-' term %prec UMINUS
186 { EVALUNARY
(-, $$
, $2); }
187 |
'+' term %prec UMINUS
190 { $$.nd_val
= ! $2.nd_val
; $$.op
= $2.op ? NUMBER
: 0; }
192 { EVALUNARY
(~
, $$
, $2); }
195 | DEFINED
'(' NUMBER
')'
204 yyerror(const char *err
)
210 * Set return type of an expression.
216 return
0; /* div by zero involved */
217 if
(l
== UNUMBER || r
== UNUMBER
)