added concrete implementations of putc(), getc(), getchar() and gets()
[tangerine.git] / workbench / c / evalParser.y
blobe5d762608d73bbceefb6e0d0abfbc915c38e6f8a
1 /*
2 Copyright © 1995-2001, The AROS Development Team. All rights reserved.
3 $Id$
5 Desc: Bison file for the Eval grammar.
6 Lang: English
7 */
9 %{
10 #define YYSTYPE int
12 #include <ctype.h>
13 #include <math.h>
14 #include <string.h>
15 #include <stdio.h>
17 char *text;
19 extern int yylval;
20 extern void yyerror(char *s);
22 int g_result;
24 int yylex()
26 int c;
30 c = *text++;
31 } while (c == ' ' || c == '\t');
33 if (c == '0')
35 switch (*text)
37 case 'x':
38 sscanf(text + 1, "%x", &yylval);
40 text++;
42 while ((*text >= '0' && *text <= '9') ||
43 (*text >= 'a' && *text <= 'f') ||
44 (*text >= 'A' && *text <= 'F'))
46 text++;
49 return NUM;
51 case '0':
52 case '1':
53 case '2':
54 case '3':
55 case '4':
56 case '5':
57 case '6':
58 case '7':
59 sscanf(text - 1, "%o", &yylval);
61 while ((*text >= '0' && *text <= '7'))
63 text++;
66 return NUM;
68 case '8':
69 case '9':
70 /* Skip 08... and 09... constructions and let them be ordinary
71 decimal stuff */
72 break;
74 default:
75 /* It was just the constant 0 */
76 yylval = 0;
78 return NUM;
83 if (c == '#')
85 int d = *text++;
87 if (d == 'x')
89 sscanf(text, "%x", &yylval);
91 while ((*text >= '0' && *text <= '9') ||
92 (*text >= 'a' && *text <= 'f') ||
93 (*text >= 'A' && *text <= 'F'))
95 text++;
98 return NUM;
100 else if (isdigit(d))
102 sscanf(text - 1, "%o", &yylval);
104 while ((*text >= '0' && *text <= '7'))
106 text++;
109 return NUM;
111 else
113 yyerror("Lexer error");
118 if (c == '\'')
120 yylval = *text++;
122 return NUM;
125 if (isdigit(c))
127 sscanf(text - 1, "%i", &yylval);
129 while (isdigit(*text))
131 text++;
134 return NUM;
137 if (isalpha(c))
139 int i = 1;
140 char *textCopy = text - 1;
142 while (isalpha(text[i]))
144 i++;
147 text += i;
149 if (strncasecmp(textCopy, "m", i) == 0 ||
150 strncmp(textCopy, "mod", i) == 0)
152 return '%';
155 if (strncmp(textCopy, "xor", i) == 0 ||
156 strncasecmp(textCopy, "x", i) == 0)
158 return 'x';
161 if (strncmp(textCopy, "eqv", i) == 0 ||
162 strncasecmp(textCopy, "e", i) == 0)
164 return 'e';
167 if (strncmp(textCopy, "lsh", i) == 0 ||
168 strncasecmp(textCopy, "l", i) == 0)
170 return 'l';
173 if (strncmp(textCopy, "rsh", i) == 0 ||
174 strncasecmp(textCopy, "r", i) == 0)
176 return 'r';
179 yyerror("Lexing error");
182 return c;
186 int intPow(int x, int y)
188 int result = 1;
190 while (y > 0)
192 result *= x;
193 y--;
196 return result;
200 void yyerror(char *s)
202 printf("%s\n", s);
208 %token NUM
209 %left 'l' 'r'
210 %left 'e'
211 %left '|'
212 %left 'x'
213 %left '&'
214 %left '-' '+'
215 %left '*' '/' '%'
216 %left NEG '~'
217 %right '^'
221 val: expr
223 g_result = $1;
227 expr: NUM { $$ = $1; }
228 | expr 'l' expr { $$ = $1 << $3; }
229 | expr 'r' expr { $$ = $1 >> $3; }
230 | expr 'e' expr { $$ = ($1 & $3) | (~$1 & ~$3); }
231 | expr '|' expr { $$ = $1 | $3; }
232 | expr 'x' expr { $$ = $1 ^ $3; }
233 | expr '&' expr { $$ = $1 & $3; }
234 | expr '+' expr { $$ = $1 + $3; }
235 | expr '-' expr { $$ = $1 - $3; }
236 | expr '*' expr { $$ = $1 * $3; }
237 | expr '/' expr { $$ = $1 / $3; }
238 | expr '%' expr { $$ = $1 % $3; }
239 | '-' expr %prec NEG { $$ = -$2; }
240 | '~' expr { $$ = ~$2; }
241 | expr '^' expr { $$ = intPow($1, $3); }
242 | '(' expr ')' { $$ = $2; }
247 #if 0
249 int main()
252 text = "(1 lsh 4) mod 2";
254 if (yyparse() == 1)
256 printf("Parse error\n");
258 else
260 printf("The answer is %i\n", g_result);
263 return 0;
266 #endif