VirtualBox

source: kBuild/trunk/src/kash/arith.y@ 2438

Last change on this file since 2438 was 2415, checked in by bird, 14 years ago

kash: trimmed down the arith stuff, making it not drag in libc bits.

  • Property svn:eol-style set to LF
  • Property svn:keywords set to Id
File size: 5.2 KB
Line 
1%{
2/* $NetBSD: arith.y,v 1.17 2003/09/17 17:33:36 jmmv Exp $ */
3
4/*-
5 * Copyright (c) 1993
6 * The Regents of the University of California. All rights reserved.
7 *
8 * This code is derived from software contributed to Berkeley by
9 * Kenneth Almquist.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36#if 0
37#ifndef lint
38static char sccsid[] = "@(#)arith.y 8.3 (Berkeley) 5/4/95";
39#else
40__RCSID("$NetBSD: arith.y,v 1.17 2003/09/17 17:33:36 jmmv Exp $");
41#endif /* not lint */
42#endif
43
44#include <stdlib.h>
45#include "expand.h"
46#include "shell.h"
47#include "error.h"
48#include "output.h"
49#include "memalloc.h"
50#include "shinstance.h"
51
52shinstance *arith_psh;
53const char *arith_buf, *arith_startbuf;
54
55void yyerror(const char *);
56#ifdef TESTARITH
57int main(int , char *[]);
58int error(char *);
59#else
60# undef malloc
61# define malloc(cb) sh_malloc(NULL, (cb))
62# undef realloc
63# define realloc(pv,cb) sh_realloc(NULL, (pv), (cb))
64# undef free
65# define free(pv) sh_free(NULL, (pv))
66#endif
67
68%}
69%token ARITH_NUM ARITH_LPAREN ARITH_RPAREN
70
71%left ARITH_OR
72%left ARITH_AND
73%left ARITH_BOR
74%left ARITH_BXOR
75%left ARITH_BAND
76%left ARITH_EQ ARITH_NE
77%left ARITH_LT ARITH_GT ARITH_GE ARITH_LE
78%left ARITH_LSHIFT ARITH_RSHIFT
79%left ARITH_ADD ARITH_SUB
80%left ARITH_MUL ARITH_DIV ARITH_REM
81%left ARITH_UNARYMINUS ARITH_UNARYPLUS ARITH_NOT ARITH_BNOT
82%%
83
84exp: expr {
85 return ($1);
86 }
87 ;
88
89
90expr: ARITH_LPAREN expr ARITH_RPAREN { $$ = $2; }
91 | expr ARITH_OR expr { $$ = $1 ? $1 : $3 ? $3 : 0; }
92 | expr ARITH_AND expr { $$ = $1 ? ( $3 ? $3 : 0 ) : 0; }
93 | expr ARITH_BOR expr { $$ = $1 | $3; }
94 | expr ARITH_BXOR expr { $$ = $1 ^ $3; }
95 | expr ARITH_BAND expr { $$ = $1 & $3; }
96 | expr ARITH_EQ expr { $$ = $1 == $3; }
97 | expr ARITH_GT expr { $$ = $1 > $3; }
98 | expr ARITH_GE expr { $$ = $1 >= $3; }
99 | expr ARITH_LT expr { $$ = $1 < $3; }
100 | expr ARITH_LE expr { $$ = $1 <= $3; }
101 | expr ARITH_NE expr { $$ = $1 != $3; }
102 | expr ARITH_LSHIFT expr { $$ = $1 << $3; }
103 | expr ARITH_RSHIFT expr { $$ = $1 >> $3; }
104 | expr ARITH_ADD expr { $$ = $1 + $3; }
105 | expr ARITH_SUB expr { $$ = $1 - $3; }
106 | expr ARITH_MUL expr { $$ = $1 * $3; }
107 | expr ARITH_DIV expr {
108 if ($3 == 0)
109 yyerror("division by zero");
110 $$ = $1 / $3;
111 }
112 | expr ARITH_REM expr {
113 if ($3 == 0)
114 yyerror("division by zero");
115 $$ = $1 % $3;
116 }
117 | ARITH_NOT expr { $$ = !($2); }
118 | ARITH_BNOT expr { $$ = ~($2); }
119 | ARITH_SUB expr %prec ARITH_UNARYMINUS { $$ = -($2); }
120 | ARITH_ADD expr %prec ARITH_UNARYPLUS { $$ = $2; }
121 | ARITH_NUM
122 ;
123%%
124int
125arith(shinstance *psh, const char *s)
126{
127 long result;
128
129 INTOFF;
130/* todo lock */
131 arith_psh = psh;
132 arith_buf = arith_startbuf = s;
133 result = yyparse();
134 arith_lex_reset(); /* reprime lex */
135 arith_psh = NULL;
136/* todo unlock */
137 INTON;
138
139 return (result);
140}
141
142
143/*
144 * The exp(1) builtin.
145 */
146int
147expcmd(shinstance *psh, int argc, char **argv)
148{
149 const char *p;
150 char *concat;
151 char **ap;
152 long i;
153
154 if (argc > 1) {
155 p = argv[1];
156 if (argc > 2) {
157 /*
158 * concatenate arguments
159 */
160 STARTSTACKSTR(psh, concat);
161 ap = argv + 2;
162 for (;;) {
163 while (*p)
164 STPUTC(psh, *p++, concat);
165 if ((p = *ap++) == NULL)
166 break;
167 STPUTC(psh, ' ', concat);
168 }
169 STPUTC(psh, '\0', concat);
170 p = grabstackstr(psh, concat);
171 }
172 } else
173 p = "";
174
175 i = arith(psh, p);
176
177 out1fmt(psh, "%ld\n", i);
178 return (! i);
179}
180
181/*************************/
182#ifdef TEST_ARITH
183#include <stdio.h>
184main(argc, argv)
185 char *argv[];
186{
187 printf("%d\n", exp(argv[1]));
188}
189error(s)
190 char *s;
191{
192 fprintf(stderr, "exp: %s\n", s);
193 exit(1);
194}
195#endif
196
197void
198yyerror(const char *s)
199{
200 shinstance *psh = arith_psh;
201#ifndef YYBISON /* yyerrok references yyerrstatus which is a local variable in yyparse().*/
202 yyerrok;
203#endif
204 yyclearin;
205 arith_lex_reset(); /* reprime lex */
206/** @todo unlock */
207 error(psh, "arithmetic expression: %s: \"%s\"", s, arith_startbuf);
208 /* NOTREACHED */
209}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette