OpenCores
URL https://opencores.org/ocsvn/c16/c16/trunk

Subversion Repositories c16

[/] [c16/] [trunk/] [compiler/] [ansic.flex] - Blame information for rev 29

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jsauermann
 
2
D                       [0-9]
3
L                       [a-zA-Z_]
4
NQ                      [\x20-\x21\x23-\x5B\x5D-\x7E]
5
H                       [a-fA-F0-9]
6
E                       [Ee][+-]?{D}+
7
FS                      (f|F|l|L)
8
IS                      (u|U|l|L)*
9
 
10
%x com
11
%x strg
12
 
13
%{
14
#include 
15
#include "Node.hh"
16
#include "Name.hh"
17 29 jsauermann
#include "ansic_bison.hh"
18 2 jsauermann
 
19
extern FILE * out;
20
 
21
void count();
22
int check_type();
23
int StringConstant::str_count = 0;
24
StringConstant * StringConstant::
25
                 all_strings[StringConstant::MAX_STRINGS] = { 0 };
26
 
27
%}
28
 
29
%%
30
"//".*                  { count(); }
31
"/*"                    { count(); BEGIN(com); }
32
"*/"            { count(); BEGIN(0);   }
33
\r                      { count();    }
34
\n                      { count();    }
35
.                       { count();    }
36
 
37
"ASM"                   { count(); return(ASM); }
38
"auto"                  { count(); return(AUTO); }
39
"break"                 { count(); return(BREAK); }
40
"case"                  { count(); return(CASE); }
41
"char"                  { count(); return(CHAR); }
42
"const"                 { count(); return(CONST); }
43
"continue"              { count(); return(CONTINUE); }
44
"default"               { count(); return(DEFAULT); }
45
"do"                    { count(); return(DO); }
46
"double"                { count(); return(DOUBLE); }
47
"else"                  { count(); return(ELSE); }
48
"enum"                  { count(); return(ENUM); }
49
"extern"                { count(); return(EXTERN); }
50
"float"                 { count(); return(FLOAT); }
51
"for"                   { count(); return(FOR); }
52
"goto"                  { count(); return(GOTO); }
53
"if"                    { count(); return(IF); }
54
"int"                   { count(); return(INT); }
55
"long"                  { count(); return(LONG); }
56
"register"              { count(); return(REGISTER); }
57
"return"                { count(); return(RETURN); }
58
"short"                 { count(); return(SHORT); }
59
"signed"                { count(); return(SIGNED); }
60
"sizeof"                { count(); return(SIZEOF); }
61
"static"                { count(); return(STATIC); }
62
"struct"                { count(); return(STRUCT); }
63
"switch"                { count(); return(SWITCH); }
64
"typedef"               { count(); return(TYPEDEF); }
65
"union"                 { count(); return(UNION); }
66
"unsigned"              { count(); return(UNSIGNED); }
67
"void"                  { count(); return(VOID); }
68
"volatile"              { count(); return(VOLATILE); }
69
"while"                 { count(); return(WHILE); }
70
 
71
{L}({L}|{D})*           { count(); yylval._name = new char[strlen(yytext)+1];
72
                          strcpy((char *)(yylval._name), yytext);
73
                                   return(check_type()); }
74
 
75
0[xX]{H}+{IS}?          { count(); yylval._num = new NumericConstant(yytext);
76
                                   return(CONSTANT); }
77
0{D}+{IS}?              { count(); yylval._num = new NumericConstant(yytext);
78
                                   return(CONSTANT); }
79
{D}+{IS}?               { count(); yylval._num = new NumericConstant(yytext);
80
                                   return(CONSTANT); }
81
L?'(\\.|[^\\'])+'       { count(); yylval._num = new NumericConstant(yytext);
82
                                   return(CONSTANT); }
83
{D}+{E}{FS}?            { count(); yylval._num = 0;   /* TODO */
84
                                   return(CONSTANT); }
85
{D}*"."{D}+({E})?{FS}?  { count(); yylval._num = 0;   /* TODO */
86
                                   return(CONSTANT); }
87
{D}+"."{D}*({E})?{FS}?  { count(); yylval._num = 0;   /* TODO */
88
                                   return(CONSTANT); }
89
 
90
L?\"                    { count();   BEGIN(strg);
91
                          yylval._string_constant = new StringConstant(); }
92
\"                { count();   BEGIN(0);   return(STRING_LITERAL);  }
93
\\\"              { count();   *yylval._string_constant += '"';     }
94
\\a               { count();   *yylval._string_constant += '\a';    }
95
\\b               { count();   *yylval._string_constant += '\b';    }
96
\\f               { count();   *yylval._string_constant += '\f';    }
97
\\n               { count();   *yylval._string_constant += '\n';    }
98
\\r               { count();   *yylval._string_constant += '\r';    }
99
\\t               { count();   *yylval._string_constant += '\t';    }
100
{NQ}              { count();   *yylval._string_constant += *yytext; }
101
\t                { count();   *yylval._string_constant += '\t';    }
102
\r                { count();                                        }
103
\n                { count();   *yylval._string_constant += '\n';    }
104
.                 { count();   *yylval._string_constant += *yytext; }
105
 
106
"..."                   { count(); return(ELLIPSIS); }
107
">>="                   { count(); return(RIGHT_ASSIGN); }
108
"<<="                   { count(); return(LEFT_ASSIGN); }
109
"+="                    { count(); return(ADD_ASSIGN); }
110
"-="                    { count(); return(SUB_ASSIGN); }
111
"*="                    { count(); return(MUL_ASSIGN); }
112
"/="                    { count(); return(DIV_ASSIGN); }
113
"%="                    { count(); return(MOD_ASSIGN); }
114
"&="                    { count(); return(AND_ASSIGN); }
115
"^="                    { count(); return(XOR_ASSIGN); }
116
"|="                    { count(); return(OR_ASSIGN); }
117
">>"                    { count(); return(RIGHT_OP); }
118
"<<"                    { count(); return(LEFT_OP); }
119
"++"                    { count(); return(INC_OP); }
120
"--"                    { count(); return(DEC_OP); }
121
"->"                    { count(); return(PTR_OP); }
122
"&&"                    { count(); return(AND_OP); }
123
"||"                    { count(); return(OR_OP); }
124
"<="                    { count(); return(LE_OP); }
125
">="                    { count(); return(GE_OP); }
126
"=="                    { count(); return(EQ_OP); }
127
"!="                    { count(); return(NE_OP); }
128
";"                     { count(); return(';'); }
129
("{"|"<%")              { count(); return('{'); }
130
("}"|"%>")              { count(); return('}'); }
131
","                     { count(); return(','); }
132
":"                     { count(); return(':'); }
133
"="                     { count(); return('='); }
134
"("                     { count(); return('('); }
135
")"                     { count(); return(')'); }
136
("["|"<:")              { count(); return('['); }
137
("]"|":>")              { count(); return(']'); }
138
"."                     { count(); return('.'); }
139
"&"                     { count(); return('&'); }
140
"!"                     { count(); return('!'); }
141
"~"                     { count(); return('~'); }
142
"-"                     { count(); return('-'); }
143
"+"                     { count(); return('+'); }
144
"*"                     { count(); return('*'); }
145
"/"                     { count(); return('/'); }
146
"%"                     { count(); return('%'); }
147
"<"                     { count(); return('<'); }
148
">"                     { count(); return('>'); }
149
"^"                     { count(); return('^'); }
150
"|"                     { count(); return('|'); }
151
"?"                     { count(); return('?'); }
152
 
153
[ \t\v\n\f]             { count(); }
154
 
155
<>                       { return EOFILE; };
156
.                       { return ERROR ; };
157
%%
158
 
159
int yywrap()
160
{
161
        return(1);
162
}
163
 
164
int column = 0;
165
int row    = 1;
166
 
167
//-----------------------------------------------------------------------------
168
enum { QUOTE = '\'', BACKSLASH = '\\' };
169
 
170
NumericConstant::NumericConstant(const char * txt)
171
   : Constant("NumericConstant"),
172
     size(0)
173
{
174
   if (*txt == 'L')    txt++;
175
   if (*txt == QUOTE)   // TODO: make it proper
176
      {
177
        value = 0;
178
        for (txt++; *txt != QUOTE; txt++)
179
            {
180
              value <<= 8;
181
              int next_val = *txt;
182
              if (*txt == BACKSLASH)
183
                 {
184
                   txt++;   // skip backslash
185
                   next_val = *txt;
186
                   switch(*txt)
187
                      {
188
                        case 'a': next_val = '\a';   break;
189
                        case 'b': next_val = '\b';   break;
190
                        case 'f': next_val = '\f';   break;
191
                        case 'n': next_val = '\n';   break;
192
                        case 'r': next_val = '\r';   break;
193
                        case 't': next_val = '\t';   break;
194
 
195
                        case '0': // octal
196
                             {
197
                               int len;
198
                               int s = sscanf(txt, "%i%n", &next_val, &len);
199
                               assert(s == 2);
200
                               txt += len;
201
                             }
202
                             break;
203
 
204
                        case 'x': // hex
205
                             {
206
                               int len;
207
                               txt++;   // skip 'x'
208
                               int s = sscanf(txt, "%x%n", &next_val, &len);
209
                               assert(s == 2);
210
                               txt += len;
211
                             }
212
                             break;
213
                      }
214
                 }
215
              value |= 0x00FF & next_val;
216
            }
217
        return;
218
      }
219
 
220
   if (txt[1] == 'x' || txt[1] == 'X')
221
      {
222
        assert(*txt == '0');
223
        int cnt = sscanf(txt + 2, "%X", &value);
224
        assert(cnt == 1);
225
        return;
226
      }
227
 
228
   if (*txt == '0')
229
      {
230
        int cnt = sscanf(txt, "%o", &value);
231
        assert(cnt == 1);
232
        return;
233
      }
234
 
235
int cnt = sscanf(txt, "%d", &value);
236
   assert(cnt == 1);
237
}
238
//-----------------------------------------------------------------------------
239
StringConstant::StringConstant()
240
   : Constant("StringConstant"),
241
     buffer(new char[80]),
242
     buffer_len(80),
243
     string_number(str_count++),
244
     value_len(0)
245
{
246
   assert(buffer);
247
   *buffer = 0;
248
   if (string_number == 0)   // first string
249
      {
250
         for (int i = 0; i < MAX_STRINGS; i++)   all_strings[i] = 0;
251
      }
252
 
253
   all_strings[string_number] = this;
254
}
255
//-----------------------------------------------------------------------------
256
void StringConstant::EmitAll(FILE * out)
257
{
258
   for (int i = 0; i < MAX_STRINGS; i++)
259
       {
260
         StringConstant * sc = all_strings[i];
261
         if (sc == NULL)   continue;
262
 
263
         fprintf(out, "Cstr_%d:\t\t\t\t;\n", sc->string_number);
264
         for (int i = 0; i < sc->value_len; i++)
265
             fprintf(out, "\t.BYTE\t0x%2.2X\t\t\t;\n", sc->buffer[i]);
266
 
267
         fprintf(out, "\t.BYTE\t0\t\t\t;\n");
268
       }
269
}
270
//-----------------------------------------------------------------------------
271
void StringConstant::EmitAndRemove(FILE * out, int length)
272
{
273
   if (length < (value_len + 1))
274
      {
275
        fprintf(stderr,
276
                "Initialization string too long (length %d, size %d)\n",
277
                value_len + 1, length);
278
        semantic_errors++;
279
      }
280
 
281
   for (int b = 0; b < length; b++)
282
       {
283
         if (b > value_len)
284
             fprintf(out, "\t.BYTE\t0\t\t\t; [%d]\n", b);
285
         else
286
             fprintf(out, "\t.BYTE\t0x%2.2X\t\t\t; [%d]\n", buffer[b], b);
287
       }
288
 
289
   all_strings[string_number] = 0;
290
}
291
//-----------------------------------------------------------------------------
292
StringConstant::~StringConstant()
293
{
294
   delete buffer;
295
 
296
   assert(all_strings[string_number] == this);
297
   all_strings[string_number] = 0;
298
}
299
//-----------------------------------------------------------------------------
300
StringConstant * StringConstant::operator & (StringConstant * other)
301
{
302
   buffer_len = value_len + other->value_len;
303
char * cp = new char[buffer_len + 1];
304
   assert(cp);
305
   memcpy(cp, buffer, value_len);
306
   memcpy(cp + value_len, other->buffer, other->value_len);
307
   cp[buffer_len] = 0;
308
   value_len += other->value_len;
309
 
310
   delete buffer;
311
   buffer = cp;
312
   delete other;
313
   return this;
314
}
315
//-----------------------------------------------------------------------------
316
void StringConstant::operator += (char txt)
317
{
318
   if (value_len + 1 >= buffer_len)
319
      {
320
        assert(buffer);
321
 
322
        char * cp = new char[2*buffer_len];
323
        assert(cp);
324
        memcpy(cp, buffer, buffer_len);
325
        delete buffer;
326
        buffer = cp;
327
        buffer_len *= 2;
328
      }
329
 
330
   buffer[value_len++] = txt;
331
   buffer[value_len]   = 0;
332
}
333
//-----------------------------------------------------------------------------
334
void count()
335
{
336
   for (int i = 0; yytext[i]; i++)
337
       {
338
         if (yytext[i] == '\n')        { column = 0;   row++; }
339
         else if (yytext[i] == '\t')   column += 8 - (column % 8);
340
         else                          column++;
341
       }
342
}
343
//-----------------------------------------------------------------------------
344
int check_type()
345
{
346
   if (TypedefName::IsDefined(yytext))   return TYPE_NAME;
347
   return(IDENTIFIER);
348
}
349
//-----------------------------------------------------------------------------
350
int yyerror(const char *s)
351
{
352
   printf("\n%s Line %d Col %d\n", s, row, column);
353
   fflush(stdout);
354
}
355
//-----------------------------------------------------------------------------

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.