4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 static char sccsid
[] = "@(#)arith.y 8.3 (Berkeley) 5/4/95";
40 #include <sys/cdefs.h>
41 __FBSDID("$FreeBSD: src/bin/sh/arith.y,v 1.19 2004/05/24 10:11:31 stefanf Exp $");
56 %token
<l_value
> ARITH_NUM ARITH_LPAREN ARITH_RPAREN
57 %token
<s_value
> ARITH_VAR
61 %right ARITH_ADDASSIGN ARITH_SUBASSIGN
62 %right ARITH_MULASSIGN ARITH_DIVASSIGN ARITH_REMASSIGN
63 %right ARITH_RSHASSIGN ARITH_LSHASSIGN
64 %right ARITH_BANDASSIGN ARITH_BXORASSIGN ARITH_BORASSIGN
70 %left ARITH_EQ ARITH_NE
71 %left ARITH_LT ARITH_GT ARITH_GE ARITH_LE
72 %left ARITH_LSHIFT ARITH_RSHIFT
73 %left ARITH_ADD ARITH_SUB
74 %left ARITH_MUL ARITH_DIV ARITH_REM
75 %left ARITH_UNARYMINUS ARITH_UNARYPLUS ARITH_NOT ARITH_BNOT
84 ARITH_LPAREN expr ARITH_RPAREN
87 { $$
= $1 ?
$1 : $3 ?
$3 : 0; } |
89 { $$
= $1 ?
( $3 ?
$3 : 0 ) : 0; } |
108 expr ARITH_LSHIFT expr
110 expr ARITH_RSHIFT expr
121 yyerror("division by zero");
127 yyerror("division by zero");
134 ARITH_SUB expr %prec ARITH_UNARYMINUS
136 ARITH_ADD expr %prec ARITH_UNARYPLUS
145 if
(lookupvar
($1) == NULL
)
146 setvarsafe
($1, "0", 0);
147 str_val
= lookupvar
($1);
148 arith_val
= strtoarith_t
(str_val
, &p
, 0);
150 * Conversion is successful only in case
151 * we've converted _all_ characters.
154 yyerror("variable conversion error");
157 ARITH_VAR ARITH_ASSIGN expr
159 if
(arith_assign
($1, $3) != 0)
160 yyerror("variable assignment error");
163 ARITH_VAR ARITH_ADDASSIGN expr
167 value
= atoarith_t
(lookupvar
($1)) + $3;
168 if
(arith_assign
($1, value
) != 0)
169 yyerror("variable assignment error");
172 ARITH_VAR ARITH_SUBASSIGN expr
176 value
= atoarith_t
(lookupvar
($1)) - $3;
177 if
(arith_assign
($1, value
) != 0)
178 yyerror("variable assignment error");
181 ARITH_VAR ARITH_MULASSIGN expr
185 value
= atoarith_t
(lookupvar
($1)) * $3;
186 if
(arith_assign
($1, value
) != 0)
187 yyerror("variable assignment error");
190 ARITH_VAR ARITH_DIVASSIGN expr
195 yyerror("division by zero");
197 value
= atoarith_t
(lookupvar
($1)) / $3;
198 if
(arith_assign
($1, value
) != 0)
199 yyerror("variable assignment error");
202 ARITH_VAR ARITH_REMASSIGN expr
207 yyerror("division by zero");
209 value
= atoarith_t
(lookupvar
($1)) %
$3;
210 if
(arith_assign
($1, value
) != 0)
211 yyerror("variable assignment error");
214 ARITH_VAR ARITH_RSHASSIGN expr
218 value
= atoarith_t
(lookupvar
($1)) >> $3;
219 if
(arith_assign
($1, value
) != 0)
220 yyerror("variable assignment error");
223 ARITH_VAR ARITH_LSHASSIGN expr
227 value
= atoarith_t
(lookupvar
($1)) << $3;
228 if
(arith_assign
($1, value
) != 0)
229 yyerror("variable assignment error");
232 ARITH_VAR ARITH_BANDASSIGN expr
236 value
= atoarith_t
(lookupvar
($1)) & $3;
237 if
(arith_assign
($1, value
) != 0)
238 yyerror("variable assignment error");
241 ARITH_VAR ARITH_BXORASSIGN expr
245 value
= atoarith_t
(lookupvar
($1)) ^
$3;
246 if
(arith_assign
($1, value
) != 0)
247 yyerror("variable assignment error");
250 ARITH_VAR ARITH_BORASSIGN expr
254 value
= atoarith_t
(lookupvar
($1)) |
$3;
255 if
(arith_assign
($1, value
) != 0)
256 yyerror("variable assignment error");
262 #include "memalloc.h"
264 #define lstrlen(var) (3 + (2 + CHAR_BIT * sizeof((var))) / 3)
266 char *arith_buf
, *arith_startbuf
;
272 arith_assign
(char *name
, arith_t value
)
277 str
= (char *)ckmalloc
(lstrlen
(value
));
278 sprintf
(str
, ARITH_FORMAT_STR
, value
);
279 ret
= setvarsafe
(name
, str
, 0);
289 arith_buf
= arith_startbuf
= s
;
293 arith_lex_reset
(); /* Reprime lex. */
305 arith_lex_reset
(); /* Reprime lex. */
306 error("arithmetic expression: %s: \"%s\"", s
, arith_startbuf
);
310 * The exp(1) builtin.
313 expcmd
(int argc
, char **argv
)
324 * Concatenate arguments.
326 STARTSTACKSTR
(concat
);
330 STPUTC
(*p
++, concat
);
331 if
((p
= *ap
++) == NULL
)
335 STPUTC
('\0', concat
);
336 p
= grabstackstr
(concat
);
347 /*************************/
350 main
(int argc
, char *argv
[])
352 printf
("%d\n", exp
(argv
[1]));
357 fprintf
(stderr
, "exp: %s\n", s
);