annotate calc.c @ 26:8550260c18fa

fix aarch on mac and llvm
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Fri, 18 Oct 2024 16:06:51 +0900
parents e5c8532ab38d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
6bf66c125dbc Initial revision
kono
parents:
diff changeset
1 /*
6bf66c125dbc Initial revision
kono
parents:
diff changeset
2 Simple Calculator
6bf66c125dbc Initial revision
kono
parents:
diff changeset
3 $Id$
6bf66c125dbc Initial revision
kono
parents:
diff changeset
4 */
6bf66c125dbc Initial revision
kono
parents:
diff changeset
5
6bf66c125dbc Initial revision
kono
parents:
diff changeset
6 #include <stdio.h>
6bf66c125dbc Initial revision
kono
parents:
diff changeset
7
6bf66c125dbc Initial revision
kono
parents:
diff changeset
8 char *ptr,*last_ptr;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
9 int value,lvalue;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
10 int last_token;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
11 int variable[48];
6bf66c125dbc Initial revision
kono
parents:
diff changeset
12
6bf66c125dbc Initial revision
kono
parents:
diff changeset
13 static int expr();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
14 static int aexpr();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
15 static int mexpr();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
16 static int term();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
17
6bf66c125dbc Initial revision
kono
parents:
diff changeset
18
6bf66c125dbc Initial revision
kono
parents:
diff changeset
19 #define T_EQUAL (0x100|'=')
6bf66c125dbc Initial revision
kono
parents:
diff changeset
20 #define T_NEQUAL (0x100|'!')
6bf66c125dbc Initial revision
kono
parents:
diff changeset
21 #define T_LSHIFT (0x100|'<')
6bf66c125dbc Initial revision
kono
parents:
diff changeset
22 #define T_RSHIFT (0x100|'>')
6bf66c125dbc Initial revision
kono
parents:
diff changeset
23
6bf66c125dbc Initial revision
kono
parents:
diff changeset
24 static void
6bf66c125dbc Initial revision
kono
parents:
diff changeset
25 line_skip()
6bf66c125dbc Initial revision
kono
parents:
diff changeset
26 {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
27 while(*ptr++);
6bf66c125dbc Initial revision
kono
parents:
diff changeset
28 last_ptr = ptr;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
29 last_token = EOF;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
30 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
31
6bf66c125dbc Initial revision
kono
parents:
diff changeset
32 static void
26
8550260c18fa fix aarch on mac and llvm
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 17
diff changeset
33 error(char *description)
0
6bf66c125dbc Initial revision
kono
parents:
diff changeset
34 {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
35 fprintf(stderr,"%s on %s\n",description,last_ptr);
6bf66c125dbc Initial revision
kono
parents:
diff changeset
36 line_skip();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
37 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
38
6bf66c125dbc Initial revision
kono
parents:
diff changeset
39 static int
6bf66c125dbc Initial revision
kono
parents:
diff changeset
40 token()
6bf66c125dbc Initial revision
kono
parents:
diff changeset
41 {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
42 int c,d;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
43
6bf66c125dbc Initial revision
kono
parents:
diff changeset
44 last_ptr = ptr; /* for error position */
6bf66c125dbc Initial revision
kono
parents:
diff changeset
45 c= *ptr;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
46 while(c<=' ' && c) {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
47 c = *++ptr;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
48 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
49 if(!c) {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
50 last_token = EOF;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
51 return last_token;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
52 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
53 ptr++;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
54 if ('/'==c && ('/'== *ptr || '*'== *ptr)) { /* comment */
6bf66c125dbc Initial revision
kono
parents:
diff changeset
55 line_skip();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
56 return EOF;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
57 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
58 if('1'<=c && c<='9') { /* Decimal */
6bf66c125dbc Initial revision
kono
parents:
diff changeset
59 d = c-'0';
17
e5c8532ab38d fix for Mountain lion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
60 while((c= *ptr++)) {
0
6bf66c125dbc Initial revision
kono
parents:
diff changeset
61 if('0'<=c && c<='9') {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
62 d = d*10 + (c - '0');
6bf66c125dbc Initial revision
kono
parents:
diff changeset
63 } else {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
64 break;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
65 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
66 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
67 c && ptr--;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
68 value = d;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
69 last_token = '0';
6bf66c125dbc Initial revision
kono
parents:
diff changeset
70 return last_token;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
71 } else if ('0'==c && 'x'== *ptr) { /* Hex */
6bf66c125dbc Initial revision
kono
parents:
diff changeset
72 ptr++;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
73 d = 0;
17
e5c8532ab38d fix for Mountain lion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
74 while((c= *ptr++)) {
0
6bf66c125dbc Initial revision
kono
parents:
diff changeset
75 if('0'<=c && c<='9') {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
76 d = d*16 + (c - '0');
6bf66c125dbc Initial revision
kono
parents:
diff changeset
77 } else if('a'<=c && c<='f') {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
78 d = d*16 + (c - 'a' + 10);
6bf66c125dbc Initial revision
kono
parents:
diff changeset
79 } else if('A'<=c && c<='F') {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
80 d = d*16 + (c - 'a' + 10);
6bf66c125dbc Initial revision
kono
parents:
diff changeset
81 } else {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
82 break;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
83 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
84 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
85 c && ptr--;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
86 value = d;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
87 last_token = '0';
6bf66c125dbc Initial revision
kono
parents:
diff changeset
88 return last_token;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
89 } else if ('0'==c) { /* Octal */
6bf66c125dbc Initial revision
kono
parents:
diff changeset
90 d = c-'0';
17
e5c8532ab38d fix for Mountain lion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
91 while((c= *ptr++)) {
0
6bf66c125dbc Initial revision
kono
parents:
diff changeset
92 if('0'<=c && c<='7') {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
93 d = d*8 + (c - '0');
6bf66c125dbc Initial revision
kono
parents:
diff changeset
94 } else {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
95 break;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
96 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
97 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
98 c && ptr--;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
99 value = d;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
100 last_token = '0';
6bf66c125dbc Initial revision
kono
parents:
diff changeset
101 return last_token;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
102 } else if ('\''==c) { /* Ascii */
6bf66c125dbc Initial revision
kono
parents:
diff changeset
103 d = 0;
17
e5c8532ab38d fix for Mountain lion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
104 while((c= *ptr++)) {
0
6bf66c125dbc Initial revision
kono
parents:
diff changeset
105 if('\''!=c && c<=0x7f) {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
106 d = d*256 + c;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
107 } else if(c>=0x80 && *ptr) {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
108 d = d*256*256 + c*256 + *ptr++;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
109 } else {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
110 break;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
111 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
112 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
113 c && ptr--;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
114 value = d;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
115 last_token = '0';
6bf66c125dbc Initial revision
kono
parents:
diff changeset
116 return last_token;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
117 } else if (('a'<=c && c<='z') || /* variable */
6bf66c125dbc Initial revision
kono
parents:
diff changeset
118 ('A'<=c && c<='Z')) {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
119 value = ((c>'Z')?c-'a'+'Z'-'A'+1:c-'A');
6bf66c125dbc Initial revision
kono
parents:
diff changeset
120 last_token = 'v';
6bf66c125dbc Initial revision
kono
parents:
diff changeset
121 return last_token;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
122 } else if ('='==c && '='== *ptr) { /* equal */
6bf66c125dbc Initial revision
kono
parents:
diff changeset
123 ptr++;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
124 last_token = T_EQUAL;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
125 return last_token;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
126 } else if ('!'==c && '='== *ptr) { /* equal */
6bf66c125dbc Initial revision
kono
parents:
diff changeset
127 ptr++;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
128 last_token = T_NEQUAL;
17
e5c8532ab38d fix for Mountain lion
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 0
diff changeset
129 return last_token;
0
6bf66c125dbc Initial revision
kono
parents:
diff changeset
130 } else if ('<'==c && '<'== *ptr) { /* shift */
6bf66c125dbc Initial revision
kono
parents:
diff changeset
131 ptr++;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
132 last_token = T_LSHIFT;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
133 return last_token;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
134 } else if ('>'==c && '>'== *ptr) { /* shift */
6bf66c125dbc Initial revision
kono
parents:
diff changeset
135 ptr++;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
136 last_token = T_RSHIFT;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
137 return last_token;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
138 } else {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
139 last_token = c;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
140 return last_token;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
141 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
142 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
143
6bf66c125dbc Initial revision
kono
parents:
diff changeset
144 static int
6bf66c125dbc Initial revision
kono
parents:
diff changeset
145 expr()
6bf66c125dbc Initial revision
kono
parents:
diff changeset
146 {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
147 int d,assign;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
148
6bf66c125dbc Initial revision
kono
parents:
diff changeset
149 d = aexpr();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
150 assign = lvalue;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
151 while(last_token!=EOF) {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
152 switch(last_token) {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
153 case '<':
6bf66c125dbc Initial revision
kono
parents:
diff changeset
154 d = (d < aexpr());
6bf66c125dbc Initial revision
kono
parents:
diff changeset
155 break;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
156 case '>':
6bf66c125dbc Initial revision
kono
parents:
diff changeset
157 d = (d > aexpr());
6bf66c125dbc Initial revision
kono
parents:
diff changeset
158 break;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
159 case T_EQUAL:
6bf66c125dbc Initial revision
kono
parents:
diff changeset
160 d = (d == aexpr());
6bf66c125dbc Initial revision
kono
parents:
diff changeset
161 break;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
162 case T_NEQUAL:
6bf66c125dbc Initial revision
kono
parents:
diff changeset
163 d = (d != aexpr());
6bf66c125dbc Initial revision
kono
parents:
diff changeset
164 break;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
165 case T_LSHIFT:
6bf66c125dbc Initial revision
kono
parents:
diff changeset
166 d <<= aexpr();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
167 break;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
168 case T_RSHIFT:
6bf66c125dbc Initial revision
kono
parents:
diff changeset
169 d >>= aexpr();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
170 break;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
171 case '?':
6bf66c125dbc Initial revision
kono
parents:
diff changeset
172 {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
173 int true;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
174 true = expr();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
175 if(last_token != ':') {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
176 error("? expression not terminated with :");
6bf66c125dbc Initial revision
kono
parents:
diff changeset
177 return true;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
178 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
179 if(d) {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
180 expr();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
181 return true;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
182 } else {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
183 return expr();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
184 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
185 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
186 break;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
187 case '=':
6bf66c125dbc Initial revision
kono
parents:
diff changeset
188 if(assign>=0) {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
189 d = expr();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
190 variable[assign] = d;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
191 return d;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
192 } else {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
193 error("Bad assignment");
6bf66c125dbc Initial revision
kono
parents:
diff changeset
194 return 0;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
195 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
196 break;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
197 case ':':
6bf66c125dbc Initial revision
kono
parents:
diff changeset
198 case ')':
6bf66c125dbc Initial revision
kono
parents:
diff changeset
199 return d;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
200 default:
6bf66c125dbc Initial revision
kono
parents:
diff changeset
201 error("Bad expression");
6bf66c125dbc Initial revision
kono
parents:
diff changeset
202 token();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
203 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
204 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
205 last_token = EOF;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
206 return d;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
207 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
208
6bf66c125dbc Initial revision
kono
parents:
diff changeset
209 static int
6bf66c125dbc Initial revision
kono
parents:
diff changeset
210 aexpr()
6bf66c125dbc Initial revision
kono
parents:
diff changeset
211 {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
212 int d;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
213
6bf66c125dbc Initial revision
kono
parents:
diff changeset
214 d = mexpr();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
215 while(last_token!=EOF) {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
216 switch(last_token) {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
217 case '^':
6bf66c125dbc Initial revision
kono
parents:
diff changeset
218 d ^= mexpr();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
219 break;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
220 case '|':
6bf66c125dbc Initial revision
kono
parents:
diff changeset
221 d |= mexpr();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
222 break;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
223 case '&':
6bf66c125dbc Initial revision
kono
parents:
diff changeset
224 d &= mexpr();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
225 break;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
226 case '-':
6bf66c125dbc Initial revision
kono
parents:
diff changeset
227 d -= mexpr();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
228 break;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
229 case '+':
6bf66c125dbc Initial revision
kono
parents:
diff changeset
230 d += mexpr();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
231 break;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
232 default:
6bf66c125dbc Initial revision
kono
parents:
diff changeset
233 return d;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
234 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
235 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
236 return d;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
237 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
238
6bf66c125dbc Initial revision
kono
parents:
diff changeset
239 static int
6bf66c125dbc Initial revision
kono
parents:
diff changeset
240 mexpr()
6bf66c125dbc Initial revision
kono
parents:
diff changeset
241 {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
242 int d;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
243
6bf66c125dbc Initial revision
kono
parents:
diff changeset
244 d = term();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
245 while(last_token!=EOF) {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
246 switch(last_token) {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
247 case '*':
6bf66c125dbc Initial revision
kono
parents:
diff changeset
248 d *= term();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
249 break;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
250 case '/':
6bf66c125dbc Initial revision
kono
parents:
diff changeset
251 d /= term();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
252 break;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
253 case '%':
6bf66c125dbc Initial revision
kono
parents:
diff changeset
254 d %= term();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
255 break;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
256 case '^':
6bf66c125dbc Initial revision
kono
parents:
diff changeset
257 d ^= term();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
258 break;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
259 default:
6bf66c125dbc Initial revision
kono
parents:
diff changeset
260 return d;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
261 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
262 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
263 return d;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
264 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
265
6bf66c125dbc Initial revision
kono
parents:
diff changeset
266 static int term()
6bf66c125dbc Initial revision
kono
parents:
diff changeset
267 {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
268 int d;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
269
6bf66c125dbc Initial revision
kono
parents:
diff changeset
270 lvalue= -1;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
271 token();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
272 if(last_token==EOF) {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
273 error("Term expected");
6bf66c125dbc Initial revision
kono
parents:
diff changeset
274 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
275 switch(last_token) {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
276 case '0':
6bf66c125dbc Initial revision
kono
parents:
diff changeset
277 d = value;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
278 token();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
279 return d;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
280 case 'v':
6bf66c125dbc Initial revision
kono
parents:
diff changeset
281 d = lvalue = value;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
282 token();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
283 return variable[d];
6bf66c125dbc Initial revision
kono
parents:
diff changeset
284 case '-':
6bf66c125dbc Initial revision
kono
parents:
diff changeset
285 return - term();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
286 case '!':
6bf66c125dbc Initial revision
kono
parents:
diff changeset
287 return ! term();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
288 case '(':
6bf66c125dbc Initial revision
kono
parents:
diff changeset
289 d = expr();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
290 if(last_token != ')') {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
291 error("Unbalanced parenthsis");
6bf66c125dbc Initial revision
kono
parents:
diff changeset
292 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
293 token();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
294 return d;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
295 default:
6bf66c125dbc Initial revision
kono
parents:
diff changeset
296 token();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
297 error("Unknown term");
6bf66c125dbc Initial revision
kono
parents:
diff changeset
298 return 0;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
299 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
300 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
301
6bf66c125dbc Initial revision
kono
parents:
diff changeset
302 int
6bf66c125dbc Initial revision
kono
parents:
diff changeset
303 main(int ac,char *av[])
6bf66c125dbc Initial revision
kono
parents:
diff changeset
304 {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
305 int d;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
306 char buf[BUFSIZ];
6bf66c125dbc Initial revision
kono
parents:
diff changeset
307
6bf66c125dbc Initial revision
kono
parents:
diff changeset
308 while (fgets(buf,BUFSIZ,stdin)) {
6bf66c125dbc Initial revision
kono
parents:
diff changeset
309 ptr = buf;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
310 d = expr();
6bf66c125dbc Initial revision
kono
parents:
diff changeset
311 printf("%s = 0x%08x = %d\n",buf,d,d);
6bf66c125dbc Initial revision
kono
parents:
diff changeset
312 fflush(stdout);
6bf66c125dbc Initial revision
kono
parents:
diff changeset
313 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
314 return 0;
6bf66c125dbc Initial revision
kono
parents:
diff changeset
315 }
6bf66c125dbc Initial revision
kono
parents:
diff changeset
316
6bf66c125dbc Initial revision
kono
parents:
diff changeset
317 /* end */