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

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [sw/] [zasm/] [zpp.l] - Blame information for rev 46

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 13 dgisselq
/*******************************************************************************
2
**
3
** Filename:    zpp.l
4
**
5
** Project:     Zip CPU -- a small, lightweight, RISC CPU core
6
**
7
** Purpose:     The preprocessor for the Zip Assembler.
8
**
9
**      This routine strips comments, handles #define's, #ifdef's, and
10
**      #include statements in a C fashion.
11
**
12
**      #define macro's are also defined in the language and therefore
13
**      supposed to be supported, but the support isn't there yet.
14
**
15
** Creator:     Dan Gisselquist, Ph.D.
16
**              Gisselquist Tecnology, LLC
17
**
18
********************************************************************************
19
**
20
** Copyright (C) 2015, Gisselquist Technology, LLC
21
**
22
** This program is free software (firmware): you can redistribute it and/or
23
** modify it under the terms of  the GNU General Public License as published
24
** by the Free Software Foundation, either version 3 of the License, or (at
25
** your option) any later version.
26
**
27
** This program is distributed in the hope that it will be useful, but WITHOUT
28
** ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
29
** FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
30
** for more details.
31
**
32
** You should have received a copy of the GNU General Public License along
33
** with this program.  (It's in the $(ROOT)/doc directory, run make with no
34
** target there if the PDF file isn't present.)  If not, see
35
**  for a copy.
36
**
37
** License:     GPL, v3, as defined and found on www.gnu.org,
38
**              http://www.gnu.org/licenses/gpl.html
39
**
40
**
41
*******************************************************************************/
42
 
43
%{
44
// #include 
45
#include 
46
#include 
47
#include 
48
#include 
49 34 dgisselq
#include 
50
#include 
51 13 dgisselq
 
52
using namespace std;
53
 
54 16 dgisselq
int yylex();
55
void    mark_line(void);
56
int     end_of_file(void);
57
void    pushf(const char *fname);
58
 
59 13 dgisselq
// #include "zprepr.tab.h"
60 16 dgisselq
int     ndef = 0, structno = 0;
61
char    *structid = NULL;
62 13 dgisselq
void    stb_define(const char *str);
63 34 dgisselq
bool    stb_current(const char *str);
64
bool    stb_hasargs(const char *str);
65
void    stb_expand(FILE *fout, const char *str);
66
std::string     stb_expand(const char *str);
67 13 dgisselq
void    stb_args(const char *str);
68
void    stb_macro(const char *value);
69
void    stb_addmacro(const char *value);
70
bool    stb_isdefined(const char *str);
71
const char      *stb_getdefn(const char *str);
72
%}
73 16 dgisselq
%x DEF DFA DFV DFV_EOL INDEF IFDEFV INNOTDEF NODEF NVRDEF COMMENT INSTRUCT
74
%x INDEF_EOL INNOTDEF_EOL GETSTRUCTID
75 13 dgisselq
%option noyywrap
76
%option stack
77
ID [_:A-Za-z][_:A-Za-z0-9]*
78 16 dgisselq
IDDOT {ID}("."{ID})*
79 13 dgisselq
 
80
%%
81
"*/"            { yy_pop_state(); }
82
[^*]+           { /* Ignore comments */ }
83
"*"+[^/]        { /* Ignore comments */ }
84
^"#include"[ \t]+\"[^\"]+\"     {
85
                char *ptr = &yytext[9], *start, *end, *str;
86
                while(isspace(*ptr))
87
                        ptr++;
88 16 dgisselq
                start = ++ptr;
89 13 dgisselq
                ptr++;
90
                while((*ptr)&&(*ptr != '\"'))
91
                        ptr++;
92
                *ptr = '\0';
93 16 dgisselq
                pushf(start);
94 13 dgisselq
                // push_file_state(yylineno); // and filename ...
95 16 dgisselq
                mark_line();
96 13 dgisselq
        }
97
^"#define"[ \t]+        { yy_push_state(DEF); }
98
 /* <*>^"#line"[ \t]+(0-9)+[ \t]+["][^"]*["][ \t]*\n       { } */
99 16 dgisselq
{IDDOT}/[^(]    {
100 34 dgisselq
                // fprintf(stderr, "Defining \'%s\'\n", yytext);
101 13 dgisselq
                stb_define(yytext);
102
                BEGIN DFV;
103
        }
104 16 dgisselq
{IDDOT}/[(]     { stb_define(yytext); BEGIN DFA; }
105
"("[^)]+")"                             {
106
                /* Process macro arguments */
107 13 dgisselq
                stb_args(yytext);
108
                BEGIN DFV;
109
        }
110 34 dgisselq
^[ \t]+ { stb_macro(yytext); /* Replicate initial spaces */ }
111
[ \t]+  { stb_macro(" "); /* Ignore all  but one internal space */ }
112
{ID}    {/* Parse to end of line, get value for our define */
113
                // fprintf(stderr, "%s may be a macro, line %d\n", yytext, yylineno);
114
                if ((!stb_current(yytext))&&(stb_isdefined(yytext))) {
115
                        // fprintf(stderr, "Recursive MACRO!\n");
116
                        stb_macro(stb_getdefn(yytext));
117
                } else {
118
                        // fprintf(stderr, "But it is not defined\n");
119
                        stb_macro(yytext);
120
                }
121
        }
122
{ID}[ \t]*[(][ \t]*{ID}[ \t]*(,[ \t]*{ID}[ \t]*)*[)]    {
123
                // fprintf(stderr, "%s may be a macro within a macro!\n", yytext);
124
                if ((!stb_current(yytext))&&(stb_isdefined(yytext))) {
125
                        if (stb_hasargs(yytext)) {
126
                                std::string str = stb_expand(yytext);
127
                                stb_macro(str.c_str());
128
                        } else {
129
                                char *dup = strdup(yytext), *ptr;
130
                                ptr = strchr(dup, '(');
131
                                *ptr = '\0';
132
                                stb_macro(stb_getdefn(dup));
133
                                free(dup);
134
                                yyless(strchr(yytext,'(')-yytext);
135
                        }
136
                } else {
137
                        char *dup = strdup(yytext), *ptr;
138
                        ptr = strchr(dup, '(');
139
                        *ptr = '\0';
140
                        stb_macro(stb_getdefn(dup));
141
                        free(dup);
142
                        yyless(strchr(yytext,'(')-yytext);
143
                }
144
        }
145
[^a-zA-Z_0-9 \t\n]+     {/* Parse to end of line, get value for our define */
146
                // fprintf(stderr, "A: Adding to macro %s\n", yytext);
147 13 dgisselq
                stb_macro(yytext);
148 16 dgisselq
        }
149 34 dgisselq
0[xX][0-9A-Fa-f]+       {/* Get any hexadecimal constants */
150
                // fprintf(stderr, "B: Adding to macro %s\n", yytext);
151
                stb_macro(yytext);
152
        }
153
[0-9A-Fa-f]+[hH]        {/* Get any hexadecimal constants */
154
                // fprintf(stderr, "C: Adding to macro %s\n", yytext);
155
                stb_macro(yytext);
156
        }
157
[0-7]+[oO]      {/* Get any hexadecimal constants */
158
                // fprintf(stderr, "D: Adding to macro %s\n", yytext);
159
                stb_macro(yytext);
160
        }
161
[0-9]+  {/* Get any hexadecimal constants */
162
                // fprintf(stderr, "E: Adding to macro %s\n", yytext);
163
                stb_macro(yytext);
164
        }
165 16 dgisselq
[ \t]*((;|"//").*)?$    {/* Parse to end of line, get value for our define */
166 13 dgisselq
                yy_pop_state();
167
        }
168 16 dgisselq
[ \t]*"\\"[ \t]*((;|"//").*)?\n {/* Continue onto next line */
169
                fprintf(yyout, "\n"); mark_line(); yylineno++;
170
                stb_macro("\n");
171
        }
172 13 dgisselq
^[ \t]+.[dD][aA][tT][aA]?       { fprintf(yyout, "\tWORD"); }
173
^"#defcont"[ \t]+       { yy_push_state(DFV); }
174
^"#ifdef"[ \t]* { ndef = 0; yy_push_state(IFDEFV); }
175
^"#ifndef"[ \t]*        { ndef = 1; yy_push_state(IFDEFV); }
176 16 dgisselq
{IDDOT} {
177 13 dgisselq
                bool    known = stb_isdefined(yytext);
178
                if ( ((known)&&(ndef==0)) || ((!known)&&(ndef!=0)) ) {
179
                        BEGIN INDEF_EOL;
180
                } else {
181
                        BEGIN INNOTDEF_EOL;
182
                }
183
        }
184
        /* Not yet: ^"#if"[ \t]*                { yy_push_state(IFDEFE); }
185
        /* Not yet: ^"#if"[ \t]*                { yy_push_state(IFDEFE); }
186
        /* Not yet:  { yy_push_state(IFDEFE); } */
187
        /* ^"#elsif"[ \t]*      { yy_pop_state(); yy_push_state(IFDEFE); } */
188
[ \t]*$         { BEGIN INDEF; }
189
(;|"//").*$             { BEGIN INDEF; }
190
[ \t]*$         { BEGIN INNOTDEF; }
191
(;|"//").*$     { BEGIN INNOTDEF; }
192 16 dgisselq
[^ \t\n].*$             { BEGIN INDEF; fprintf(stderr, "WARNING! Unexpected characters on IFDEF line, \'%s\'\n", yytext); mark_line(); }
193
[^ \t\n].*$     { BEGIN INNOTDEF; fprintf(stderr, "WARNING! Unexpected characters on IFNDEF line, %s\n", yytext); mark_line(); }
194 13 dgisselq
^"#else"[ \t]*((;|"//").*)?$    { BEGIN NODEF; }
195
^"#else"[ \t]*((;|"//").*)?$            { BEGIN INDEF; }
196
(.*)                    { }
197
^"#elsif"[ \t]*         { BEGIN NVRDEF; }
198 26 dgisselq
^"#endif"[ \t]*((;|"//").*)?$   { yy_pop_state(); }
199
^"#endif"[ \t]*"/*"     { BEGIN COMMENT; }
200 13 dgisselq
<*>^"#endif"[ \t]*         { fprintf(stderr, "ERR: Unknown endif!!\n");}
201 16 dgisselq
^"#struct"[ \t]*        {
202
                yy_push_state(GETSTRUCTID); structno  = 0; }
203 26 dgisselq
<*>^"#"{ID}[ \t]*          { fprintf(stderr, "ERR: Unrecognized preprocessor instruction, %s\n", yytext);}
204 16 dgisselq
{ID}/[ \t\n;/]  { BEGIN INSTRUCT;
205
                structid = strdup(yytext);
206
                }
207
{ID}("."{ID})*  {
208
                fprintf(yyout, "\t%s.%s\tequ\t%d", structid, yytext, structno++); }
209
^"#endstruct".*$        { yy_pop_state(); }
210 13 dgisselq
        /* Not yet: ^"#struct"[ \t]*    {}      */
211
        /* Not yet: ^"#endstruct"[ \t]* {}      */
212
        /* Not yet: ^"#seg"[ \t]*       {}      */
213
.*              { /* Ignore everything in these states*/ }
214
{ID}/[^(]       {
215
                if (stb_isdefined(yytext))
216
                        fprintf(yyout, "%s", stb_getdefn(yytext));
217
                else
218
                        fprintf(yyout, "%s", yytext);
219
        }
220 34 dgisselq
{ID}([ \t]*)    {
221
                if (stb_isdefined(yytext))
222
                        fprintf(yyout, "%s", stb_getdefn(yytext));
223
                else
224
                        fprintf(yyout, "%s", yytext);
225
        }
226
{ID}[ \t]*[(][ \t]*{ID}[ \t]*(,[ \t]*{ID}[ \t]*)*[)]    {
227
                // fprintf(stderr, "%s may be a macro!\n", yytext);
228
                if (stb_isdefined(yytext)) {
229
                        if (stb_hasargs(yytext)) {
230
                                stb_expand(yyout, yytext);
231
                        } else {
232
                                fprintf(yyout, "%s", stb_getdefn(yytext));
233
                                yyless(strchr(yytext,'(')-yytext);
234
                        }
235
                } else {
236
                        // fprintf(stderr, "But it is not defined\n");
237
                        fprintf(yyout, "%s", yytext);
238
                }
239
        }
240 16 dgisselq
<*>[ \t]*"//".*$   { /* Ignore (trailing) comment only lines */ }
241
<*>[ \t]*";".*$            { /* Ignore (trailing) comment only lines */ }
242 26 dgisselq
<*>"#warning".*$   { fprintf(stderr, "WARNING: %s\n", &yytext[8]); }
243
<*>"#error".*$             { fprintf(stderr, "ERROR: %s\n", &yytext[8]); exit(-1); }
244 13 dgisselq
<*>"/*"                    { yy_push_state(COMMENT); }
245
<*>[ \t]+          { ECHO; }
246 16 dgisselq
<*>\n                      { ECHO; yylineno++; mark_line(); }
247 13 dgisselq
        /* <*>.                    { printf("Unmatched \'%c\'\n", yytext[0]); } */
248 26 dgisselq
<>       { fprintf(stderr, "Unexpected EOF!  Expecting #endif\n"); }
249
<>               { fprintf(stderr, "Unexpected EOF!  Expecting #endif\n"); }
250
<>       { fprintf(stderr, "Unexpected EOF!  Expecting */\n"); }
251
<>       { fprintf(stderr, "Unexpected EOF!  Expecting #endstruct\n"); }
252
<>       { fprintf(stderr, "Unexpected EOF!  Expecting #struct ID, then #endstruct\n"); }
253
<> { fprintf(stderr, "Unexpected EOF!  Expecting end of line\n"); }
254
<        { fprintf(stderr, "Unexpected EOF  Expecting end of line, then #endif\n"); }
255
<>       { if (end_of_file()) yyterminate(); }
256 13 dgisselq
 
257
%%
258
 
259 34 dgisselq
class   SYMTABLE_ACTION {
260
public:
261
        // Types: 0 (end of actions), 1-X, argument number, <0 raw string
262
        int             m_type;
263
        std::string     m_str;          // if m_type < 0, have m_str
264
};
265
 
266 13 dgisselq
class   SYMTABLE_ENTRY {
267
private:
268 34 dgisselq
        bool    m_reduced;
269 13 dgisselq
        std::string     &trim(std::string &s) {
270
                std::string::iterator   ptr = s.end();
271
 
272
                while((ptr >= s.begin())&&(isspace(*ptr)))
273
                        *ptr-- = '\0';
274
 
275
                return s;
276
        }
277
 
278
public:
279
        std::string     m_name, m_value, m_args;
280 34 dgisselq
        std::vector     m_actions;
281 13 dgisselq
        SYMTABLE_ENTRY(const char *str) : m_name(str) {
282
                trim(m_name);
283 34 dgisselq
                m_reduced = false;
284 13 dgisselq
        }
285
        SYMTABLE_ENTRY &operator+=(const char *str) {
286
                const char      *start = str;
287
 
288
                while(isspace(*start))
289
                        start++;
290
                if (m_value.length()!=0)
291
                        m_value += " ";
292
 
293 34 dgisselq
                m_value += str;
294 13 dgisselq
 
295
                /*
296
                printf("ENTRY::SYMBOL \'%s\' NOW = \'%s\'\n",
297
                        m_name.c_str(), m_value.c_str());
298
                */
299
                return *this;
300
        }
301
        SYMTABLE_ENTRY &setargs(const char *str) {
302
                m_args += str;
303
                return *this;
304
        }
305
 
306 16 dgisselq
        const std::string &getdefn(void) {
307 13 dgisselq
                return m_value;
308
        }
309 34 dgisselq
 
310
        bool    hasargs(void) {
311
                return (m_args.size()>0);
312
        }
313
 
314
        void    reduce(void) {
315
                if (m_reduced)
316
                        return;
317
 
318
                // fprintf(stderr, "Reducing %s ( %s ) \n", m_name.c_str(), m_args.c_str());
319
                std::vector     alist;
320
                int i=0, bg, en;
321
                do {
322
                        if ((m_args[i] == ',')||(m_args[i] == '('))
323
                                i++;
324
                        while((m_args[i])&&(isspace(m_args[i])))
325
                                i++;
326
                        bg = i;
327
                        while((m_args[i])&&(
328
                                (isalpha(m_args[i]))
329
                                ||(m_args[i]==':')
330
                                ||(m_args[i]=='_')
331
                                ||(isdigit(m_args[i]))))
332
                                i++;
333
                        en = i;
334
                        while((m_args[i])&&(isspace(m_args[i])))
335
                                i++;
336
 
337
                        alist.push_back(m_args.substr(bg,en-bg));
338
                        // printf("Found argument %2ld of %s: \'%s\'\n",
339
                                // alist.size(),
340
                                // m_name.c_str(),
341
                                // m_args.substr(bg,en-bg).c_str());
342
                } while((m_args[i])&&(m_args[i] == ','));
343
 
344
                assert(m_args[i] == ')');
345
 
346
                // Now that we know our arguments, lets look for these
347
                // arguments in our macro definition
348
                std::string     building;
349
                i = 0;
350
                while(m_value[i]) {
351
                        int     nxti = m_value.size(), nxtv;
352
                        for(int a=0; a
353
                                const char *ptr;
354
                                ptr = strstr(m_value.c_str()+i, alist[a].c_str());
355
                                while((ptr)&&(ptr-m_value.c_str() < nxti)) {
356
                                        int loc = ptr-m_value.c_str();
357
                                        const char *pre = ptr-1;
358
                                        const char *pst = ptr+alist[a].size();
359
                                        if ((loc < nxti)&&(
360
                                                (loc == i)
361
                                                ||((!isalpha(*pre)
362
                                                        &&(*pre != '_')
363
                                                        &&(*pre != ':')
364
                                                        &&(!isdigit(*pre)))))
365
                                                &&((*pst=='\0')
366
                                                ||(     (!isalpha(*pst))
367
                                                        &&(*pst != '_')
368
                                                        &&(*pst != ':')
369
                                                        &&(!isdigit(*pst)))))
370
                                        {
371
                                                        nxti = loc;
372
                                                        nxtv = a;
373
                                                        break;
374
                                        } else {
375
                                                ptr = strstr(m_value.c_str()+loc, alist[a].c_str());
376
                                                loc = ptr-m_value.c_str();
377
                                        }
378
                                }
379
                        }
380
 
381
                        if (nxti < m_value.size()) {
382
                                // Found an argument!!
383
                                SYMTABLE_ACTION act;
384
                                if (nxti > i) {
385
                                        act.m_type = -1;
386
                                        act.m_str = m_value.substr(i,nxti-i);
387
                                        // printf("ACTION: \'%s\'\n", act.m_str.c_str());
388
                                        m_actions.push_back(act);
389
                                }
390
                                act.m_type = nxtv;
391
                                act.m_str = "";
392
                                m_actions.push_back(act);
393
                                // printf("ACTION[%2d]: \'%s\'\n", nxtv, alist[nxtv].c_str());
394
 
395
                                i = nxti+alist[nxtv].size();
396
                        } else break; // No more arguments
397
                } if (i
398
                        SYMTABLE_ACTION act;
399
                        act.m_type = -1;
400
                        act.m_str = m_value.substr(i);
401
                        // printf("ACTION: \'%s\'\n", act.m_str.c_str());
402
                        m_actions.push_back(act);
403
                }
404
                m_reduced = true;
405
        }
406
 
407
        std::string expand(std::string args) {
408
                if (!m_reduced)
409
                        reduce();
410
                std::vector     alist;
411
                std::string     result;
412
 
413
                // printf("Expanding %s\n", args.c_str());
414
                int i=0, bg, en, nest=-1;
415
                do {
416
                        if ((args[i] == '(')||(args[i] == ',')) {
417
                                if (args[i] =='(')
418
                                        nest++;
419
                                i++;
420
                        }
421
                        while((args[i])&&(isspace(args[i])))
422
                                i++;
423
                        bg = i;
424
                        while((args[i])&&(args[i] != ',')&&((args[i] != ')')||(nest != 0))) {
425
                                if (args[i] == '(')
426
                                        nest++;
427
                                else if (args[i] == ')')
428
                                        nest--;
429
                                i++;
430
                        } en = i-1;
431
                        while((en>0)&&(isspace(args[en])))
432
                                en--;
433
                        alist.push_back(args.substr(bg,en+1-bg));
434
                        // printf("Argument %2ld of %s maps to \'%s\'\n",
435
                                // alist.size(),
436
                                // m_name.c_str(),
437
                                // args.substr(bg,en+1-bg).c_str());
438
                } while((args[i])&&(args[i] == ','));
439
 
440
                // printf("At end, args[i] = \'%s\'\n", &args[i]);
441
                assert(args[i] == ')');
442
 
443
                // printf("Filling in %ld actions\n", m_actions.size());
444
                for(i=0; i
445
                        if((m_actions[i].m_type >= 0)&&(m_actions[i].m_type < alist.size()))
446
                                result += alist[m_actions[i].m_type].c_str();
447
                        else if (m_actions[i].m_type < 0)
448
                                result += m_actions[i].m_str.c_str();
449
                        // else {
450
                                // fprintf(fout, "m_type = %d, size = %ld\n", m_actions[i].m_type, alist.size());
451
                        // }
452
                }
453
 
454
                return result;
455
        }
456 13 dgisselq
};
457
 
458
class   SYMBOL_TABLE {
459
private:
460
        typedef SYMTABLE_ENTRY  *TBLV;
461
        typedef std::list       TBLT;
462
 
463
        TBLT    m_tbl;
464
        TBLT::iterator  lookup(const char *str) {
465
                TBLT::iterator  i = m_tbl.begin();
466
                for(; (i!= m_tbl.end())&&(strcmp(str, (*i)->m_name.c_str())>0); i++)
467
                        ;
468
                if ((i != m_tbl.end())&&(strcmp(str, (*i)->m_name.c_str())==0))
469
                        return i;
470
                return m_tbl.end();
471
        }
472
 
473
public:
474
        SYMBOL_TABLE(void) {}
475
 
476
        void define(const char *str) {
477
                SYMTABLE_ENTRY  *v = new SYMTABLE_ENTRY(str);
478
                TBLT::iterator  i = m_tbl.begin();
479
                for(; (i!= m_tbl.end())&&(strcmp(str, (*i)->m_name.c_str())>0); i++)
480
                        ;
481
                m_tbl.insert(i, v);
482
 
483 34 dgisselq
                // fprintf(stderr, "SYMS::Defining SYMBOL: \'%s\'\n", str);
484 13 dgisselq
        }
485
 
486
        bool defined(const char *str) {
487
                TBLT::iterator  i = lookup(str);
488
                if (i==m_tbl.end())
489
                        return false;
490
                else
491
                        return true;
492
        }
493
 
494
 
495
        void    undefine(const char *str) {
496
                TBLT::iterator  i = lookup(str);
497
                if (i == m_tbl.end())
498
                        return;
499
                TBLV    v = (*i);
500
                m_tbl.erase(i);
501
                delete  v;
502
        }
503
 
504
        void    addmacro(const char *name, const char *str) {
505
                TBLT::iterator i = lookup(name);
506
                if (i == m_tbl.end()) {
507 46 dgisselq
                        fprintf(stderr, "ADDMACRO::INTERNAL ERR, \'%s\' NOT DEFINED!\n", name);
508 13 dgisselq
                } *(*i) += str;
509
        }
510
 
511
        void    addargs(const char *name, const char *str) {
512
                TBLT::iterator i = lookup(name);
513
                if (i == m_tbl.end()) {
514
                        fprintf(stderr, "INTERNAL ERR, %s NOT DEFINED!\n", name);
515
                } (*i)->setargs(str);
516
        }
517 34 dgisselq
        bool    hasargs(const char *name) {
518
                TBLT::iterator i = lookup(name);
519
                if (i == m_tbl.end()) {
520
                        return false;
521
                } return (*i)->hasargs();
522
        }
523 13 dgisselq
        const   char *getdefn(const char *name) {
524
                TBLT::iterator i = lookup(name);
525
                if (i == m_tbl.end()) {
526 46 dgisselq
                        fprintf(stderr, "GETDEFN::INTERNAL ERR, \'%s\' NOT DEFINED!\n", name);
527 13 dgisselq
                        return NULL;
528
                } (*i)->getdefn().c_str();
529
        }
530 34 dgisselq
 
531
        std::string     expand(const char *name, const char *ptr) {
532
                TBLT::iterator  i = lookup(name);
533
                if (i==m_tbl.end())
534
                        return std::string("");
535
                return (*i)->expand(std::string(ptr));
536
        }
537 13 dgisselq
 
538
};
539
 
540
SYMTABLE_ENTRY  *last = NULL;
541
SYMBOL_TABLE    syms;
542
std::string     last_define;
543
 
544 46 dgisselq
char    *stb_trim(const char *str) {
545
        // fprintf(stderr, "Checking whether %s needs to be expanded\n",str);
546
        char *dup = strdup(str), *chr;
547
        chr = strchr(dup, '(');
548
        if (chr != NULL)
549
                *chr = '\0';
550
        // fprintf(stderr, "\tLooking it up by the name \'%s\'\n", dup);
551
 
552
        // Now, let's trim our string
553
        char    *end = dup+strlen(dup)-1;
554
        while((*dup)&&(end>dup)&&(isspace(*end)))
555
                *end-- = '\0';
556
        return dup;
557
}
558
 
559 13 dgisselq
void    stb_define(const char *str) {
560 34 dgisselq
        /*
561
        if (last_define.size()>0) {
562
                fprintf(stderr, "LAST-DEFINE(%s): %s\n", last_define.c_str(),
563
                        stb_getdefn(last_define.c_str()));
564
        } */
565 46 dgisselq
        char    *alt = stb_trim(str);
566
        if (syms.defined(alt)) {
567 13 dgisselq
                fprintf(stderr, "WARNING!  Symbol \'%s\' is already defined!\n", str);
568
                syms.undefine(str);
569
        }
570
 
571 46 dgisselq
        syms.define(alt);
572
        last_define = alt;
573
        free(alt);
574 13 dgisselq
}
575
 
576
void    stb_args(const char *args) {
577
        syms.addargs(last_define.c_str(), args);
578
}
579
 
580
void    stb_macro(const char *value) {
581
        syms.addmacro(last_define.c_str(), value);
582
}
583
 
584
void    stb_addmacro(const char *value) {
585
        syms.addmacro(last_define.c_str(),value);
586
}
587
 
588
bool    stb_isdefined(const char *str) {
589 46 dgisselq
        char    *dup = stb_trim(str);
590
        bool r = (syms.defined(dup));
591
        free(dup);
592
        return r;
593 13 dgisselq
}
594
 
595
const char *stb_getdefn(const char *str) {
596 46 dgisselq
        char    *dup = stb_trim(str);
597
        const char      *r;
598
        r = syms.getdefn(dup);
599
        free(dup);
600
        return r;
601 13 dgisselq
}
602
 
603 34 dgisselq
bool    stb_current(const char *str) {
604
        return (strcmp(str, last_define.c_str())==0);
605
}
606
 
607
bool    stb_hasargs(const char *str) {
608 46 dgisselq
        char    *dup = stb_trim(str);
609
        bool r = (syms.hasargs(dup));
610
        // fprintf(stderr, "\t%s has %sarguments\n", dup, (r)?"":"no ");
611
        free(dup);
612
        return r;
613 34 dgisselq
}
614
 
615
std::string     stb_expand(const char *macro) {
616
        const   char    *ptr;
617
        std::string     str;
618
        ptr = strchr(macro, '(');
619
        assert(ptr);
620
        ptr--;
621
        while((ptr>macro)&&(isspace(*ptr)))
622
                ptr--;
623
        char *nam = strndup(macro, ptr+1-macro);
624
        ptr = strchr(ptr, '(');
625
        // fprintf(stderr, "Requesting an expansion of %s -- %s\n", nam, ptr);
626
        str = syms.expand(nam, ptr);
627
        free(nam);
628
 
629
        return str;
630
}
631
 
632
void    stb_expand(FILE *fout, const char *macro) {
633
        std::string     str = stb_expand(macro);
634
        fprintf(fout, "%s", str.c_str());
635
}
636
 
637 16 dgisselq
class   BUFSTACK {
638
public:
639
        FILE            *m_fp;
640
        char            *m_fname;
641
        int             m_lineno;
642
        BUFSTACK        *m_prev;
643
        YY_BUFFER_STATE m_bs;
644
 
645
        static  BUFSTACK        *curbs;
646
        static  const char      *curfilename;
647
 
648
        BUFSTACK(void) {
649
                m_fp = stdin;
650
 
651
                if (curbs)
652
                        curbs->m_lineno = yylineno;
653
                m_prev = curbs;
654
                // m_bs = yy_create_buffer(fp, YY_BUF_SIZE);
655
                m_fname = strdup("(stdin)");
656
                // yy_switch_to_buffer(m_bs);
657
                m_bs = NULL;
658
                curbs = this;
659
                m_lineno = 1;
660
                curfilename = m_fname;
661
 
662
                yyrestart(m_fp);
663
                yylineno = 1;
664
        }
665
 
666
        BUFSTACK(const char *fname) {
667
                m_fp = fopen(fname, "r");
668
                if (!m_fp) {
669 36 dgisselq
                        char *pathptr = getenv("ZIPINC");
670
                        if (!pathptr) {
671
                                fprintf(stderr, "Cannot open %s\n", fname);
672
                                perror("O/S Err:");
673
                                exit(-1);
674
                        } else {
675
                                char    *dptr, *colonp;
676
                                char *pathcpy = new char[strlen(pathptr)+8192];
677
                                strcpy(pathcpy, pathptr);
678
 
679
                                fprintf(stderr, "ZIPINC := %s\n", pathptr);
680
                                dptr = pathptr;
681
                                while((!m_fp)&&(NULL != (colonp = strchr(dptr, ':')))) {
682
                                        strncpy(pathcpy, dptr, colonp-pathptr);
683
                                        strcat(pathcpy, "/");
684
                                        strcat(pathcpy, fname);
685
                                        fprintf(stderr, "Looking for include file, %s\n", pathcpy);
686
                                        if (access(fname, R_OK)==0)
687
                                                m_fp = fopen(pathcpy, "r");
688
                                        dptr = colonp+1;
689
                                } if ((!m_fp)&&(*dptr)) {
690
                                        strcpy(pathcpy, dptr);
691
                                        strcat(pathcpy, "/");
692
                                        strcat(pathcpy, fname);
693
                                        fprintf(stderr, "Looking for include file, %s\n", pathcpy);
694
                                        m_fp = fopen(pathcpy, "r");
695
                                } if (!m_fp) {
696
                                        fprintf(stderr, "Cannot open %s\n", fname);
697
                                        perror("O/S Err:");
698
                                        exit(-1);
699
                                }
700
 
701
                                delete[] pathcpy;
702
                        }
703 16 dgisselq
                }
704
 
705
                if (curbs)
706
                        curbs->m_lineno = yylineno;
707
                m_prev = curbs;
708
                m_bs = yy_create_buffer(m_fp, YY_BUF_SIZE);
709
                m_fname = strdup(fname);
710
                yy_switch_to_buffer(m_bs);
711
                curbs = this;
712
                m_lineno = 1;
713
                curfilename = m_fname;
714
 
715
                yyrestart(m_fp);
716
                yylineno = 1;
717
        }
718
 
719
        ~BUFSTACK(void) {
720
                // fprintf(stderr, "DELETING(%s)\n", m_fname);
721
                fclose(m_fp);
722
                free(m_fname);
723
                if (m_bs)
724
                        yy_delete_buffer(m_bs);
725
                curbs = m_prev;
726
 
727
                if (curbs) {
728
                        yy_switch_to_buffer(curbs->m_bs);
729
                        yylineno = curbs->m_lineno;
730
                        curfilename = curbs->m_fname;
731
                }
732
        }
733
 
734
        void    mark(void) {
735
                FILE    *fp = yyout;
736
                if (!fp) fp = stdout;
737
                fprintf(fp, "#line %d \"%s\"\n", yylineno, m_fname);
738
        }
739
 
740 26 dgisselq
        void    markline(void) {
741
                FILE    *fp = yyout;
742
                if (!fp) fp = stdout;
743
                fprintf(fp, "#line %d\n", yylineno);
744
        }
745
 
746 16 dgisselq
        static  void pop(void) {
747
                // fprintf(stderr, "POP! (%s)\n", curbs->m_fname);
748
                if (curbs)
749
                        delete curbs;
750
        }
751
};
752
 
753
BUFSTACK *BUFSTACK::curbs = NULL;
754
const char *BUFSTACK::curfilename = NULL;
755
 
756
int             last_marked_line = -1;
757
const char      *last_marked_file = NULL;
758
void    mark_line(void) {
759
        if ((yylineno != last_marked_line+1)||(BUFSTACK::curfilename != last_marked_file))
760 26 dgisselq
                if (BUFSTACK::curfilename == last_marked_file)
761
                        BUFSTACK::curbs->markline();
762
                else BUFSTACK::curbs->mark();
763 16 dgisselq
        last_marked_line = yylineno;
764
        last_marked_file = BUFSTACK::curfilename;
765
}
766
 
767
int     end_of_file(void) {
768
        BUFSTACK::pop();
769
        return (BUFSTACK::curbs == NULL);
770
}
771
 
772
void    pushf(const char *fname) {
773
        BUFSTACK        *bs = new BUFSTACK(fname);
774
}
775
 
776 13 dgisselq
int main(int argc, char **argv) {
777 16 dgisselq
        yylineno = 1;
778
        if (argc < 2) { // Stdin only
779
                BUFSTACK::curbs = new BUFSTACK();
780
                yylex();
781
        } else {
782
                for(int argn=1; argn
783
                        BUFSTACK        *bs = new BUFSTACK(argv[argn]);
784
                        mark_line();
785
                        yylex();
786
                        // delete       bs;
787
                }
788
        }
789 26 dgisselq
 
790
        return 0;
791 13 dgisselq
}
792
 

powered by: WebSVN 2.1.0

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