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

Subversion Repositories theia_gpu

[/] [theia_gpu/] [branches/] [beta_2.0/] [compiler/] [src/] [cp_compiler/] [Preprocessor.cpp] - Blame information for rev 216

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 216 diegovalve
#include "Preprocessor.h"
2
#include <iostream>
3
#include <fstream>
4
#include <map>
5
#include <vector>
6
using namespace std;
7
void find_and_replace( string& input_string,
8
                       const string& find_string,
9
                       const string& replace_string )
10
{
11
  if( find_string.empty()
12
      or find_string == replace_string
13
      or input_string.length() < find_string.length() )
14
  {
15
    return;
16
  }
17
 
18
  string output_string;
19
  output_string.reserve( input_string.length() );
20
  size_t last_pos = 0u;
21
  for( size_t new_pos = input_string.find( find_string );
22
       new_pos != string::npos;
23
       new_pos = input_string.find( find_string, new_pos ) )
24
  {
25
    bool did_replace = false;
26
    if( ( new_pos == 0u
27
          or not std::isalpha( input_string.at( new_pos - 1u ) ) )
28
        and ( new_pos + find_string.length() == input_string.length()
29
              or not std::isalpha( input_string.at( new_pos + find_string.length() ) ) ) )
30
    {
31
      output_string.append( input_string, last_pos, new_pos - last_pos );
32
      output_string.append( replace_string );
33
      did_replace = true;
34
    }
35
    new_pos += find_string.length();
36
    if( did_replace )
37
    {
38
      last_pos = new_pos;
39
    }
40
  }
41
  output_string.append( input_string, last_pos,
42
                        input_string.length() - last_pos );
43
 
44
  input_string.swap( output_string );
45
}
46
//----------------------------------------------------------------
47
Preprocessor::Preprocessor()
48
{
49
}
50
//----------------------------------------------------------------
51
Preprocessor::~Preprocessor()
52
{
53
}
54
//-----------------------------------------------------------------------
55
void Tokenize(const string& str,
56
                      vector<string>& tokens,
57
                      const string& delimiters = " ")
58
{
59
 
60
    string::size_type lastPos = str.find_first_not_of(delimiters, 0);
61
      string::size_type pos     = str.find_first_of(delimiters, lastPos);
62
 
63
    while (string::npos != pos || string::npos != lastPos)
64
    {
65
 
66
        tokens.push_back(str.substr(lastPos, pos - lastPos));
67
        lastPos = str.find_first_not_of(delimiters, pos);
68
        pos = str.find_first_of(delimiters, lastPos);
69
    }
70
}
71
//----------------------------------------------------------------
72
void Preprocessor::IncludeFile(  string aPath, vector<string> & NewFile )
73
{
74
        ifstream ifs;
75
        ifs.open(aPath.c_str());
76
        if (!ifs.is_open())
77
                throw string("Could not include file '") + aPath + "'";
78
 
79
        while( !ifs.eof())
80
        {
81
                        char Buffer[1024];
82
                        ifs.getline( Buffer,1024 );
83
                        string Line = Buffer;
84
                        ScanLine(Line,NewFile,ifs, false);
85
                        //      NewFile.push_back("//" + Line); 
86
        }
87
        ifs.close();
88
}
89
//----------------------------------------------------------------
90
void Preprocessor::ScanLine( string & Line , vector<string> & NewFile, ifstream & ifs, bool aAddLineToFile = true )
91
{
92
         //replace tabs with spaces
93
                 int pos = 0;
94
                 while ((pos = Line.find("\t")) != string::npos)
95
                                        Line.replace(pos,1," ");
96
 
97
                 if (Line.find("#") == string::npos)
98
                 {
99
 
100
                        NewFile.push_back(Line);
101
                        return;
102
                 }
103
 
104
                 if (Line.find("#macro") != string::npos)
105
                 {
106
                          if (aAddLineToFile)
107
                                NewFile.push_back("//" + Line);
108
                          PopulateMacroFunction(Line,ifs,NewFile);
109
                          return;
110
             }
111
 
112
                vector<string> Tokens;
113
                Tokenize(Line,Tokens," ");
114
                if (Tokens.size() != 0)
115
                {
116
 
117
                        if (Tokens[0] == "#define")
118
                        {
119
                                mMacros[ Tokens[1] ] = Tokens[2];
120
                                if (aAddLineToFile)
121
                                        NewFile.push_back("//" + Line);
122
                        }
123
 
124
                        if (Tokens[0] == "#include")
125
                        {
126
                                string IncludeFilaPath = Tokens[1];
127
 
128
                                while(IncludeFilaPath.find("\"") != string::npos) IncludeFilaPath.erase(IncludeFilaPath.find("\""),1);
129
                                IncludeFile( IncludeFilaPath, NewFile );
130
                        /*      ifstream ifs;
131
                                ifs.open(IncludeFilaPath.c_str());
132
                                if (!ifs.is_open())
133
                                        throw string("Could not include file '") + IncludeFilaPath + "'";
134
 
135
                                while( !ifs.eof())
136
                                {
137
                                        char Buffer[1024];
138
                                        ifs.getline( Buffer,1024 );
139
                                        string Line = Buffer;
140
                                        ScanLine(Line,NewFile,ifs);
141
                                //      NewFile.push_back("//" + Line);
142
                                }
143
                                ifs.close();    */
144
                        }
145
                }
146
}
147
//----------------------------------------------------------------
148
vector<string> Preprocessor::ScanFile(  std::ifstream & ifs )
149
{
150
        vector<string> NewFile;
151
        while (!ifs.eof())
152
        {
153
                 char Buffer[1024];
154
                 ifs.getline( Buffer,1024 );
155
                 string Line = Buffer;
156
                 ScanLine( Line,NewFile,ifs );
157
 
158
 
159
        }
160
 
161
 
162
        return NewFile;
163
}
164
//----------------------------------------------------------------
165
void Preprocessor::SubstitudeMacros( vector<string> & aFile, ofstream & ofs )
166
{
167
        //for (int i = 0; i < NewFile.size(); i++)
168
        int i = 0;
169
        for ( vector<string>::iterator I = aFile.begin(); (I != aFile.end() && i < aFile.size()); I++, i++)
170
        {
171
 
172
        //Now the macro literals
173
                for( map<string,string>::const_iterator it = mMacros.begin(); it != mMacros.end(); ++it )
174
                {
175
                        string Key = it->first;
176
                        string Value = it->second;
177
                        string Line = aFile[i];
178
                        if (Line.find("#define") != string::npos)
179
                        {
180
                                aFile[i] = Line;
181
                                continue;
182
                        }
183
                        find_and_replace(Line,Key,Value);
184
 
185
                        aFile[i] = Line;
186
                }
187
 
188
 
189
                //Now the macro functions
190
                for( map<string,TMacroFunction>::iterator it = mMacroFunctions.begin(); it != mMacroFunctions.end(); ++it )
191
                {
192
                        string Key = it->first;
193
                        TMacroFunction MacroFunction = it->second;
194
                        string Line =*I;
195
                        if (Line.find("//") != string::npos)
196
                                Line.erase(Line.find("//"),Line.length()-Line.find("//"));
197
                                //continue;
198
 
199
                        if (Line.find(Key) != string::npos)
200
                        {
201
                                int pos = 0;
202
                                 while ((pos = Line.find(" ")) != string::npos)
203
                                        Line.erase(pos,1);
204
                                  vector<string> Tokens;
205
                                Tokenize(Line,Tokens,"=");
206
 
207
                                MacroFunction.mReturnValue = Tokens[0];
208
                                vector<string> Tokens2;
209
                                string Tmp = Tokens[1];
210
                                Tokens.clear();
211
                                Tokenize(Tmp,Tokens,"(");
212
                                Tmp = Tokens[1];
213
                                 while ((pos = Tmp.find(")")) != string::npos)
214
                                         Tmp.erase(pos,1);
215
                                 while ((pos = Tmp.find(";")) != string::npos)
216
                                         Tmp.erase(pos,1);
217
                                 Tokens.clear();
218
                                 Tokenize(Tmp,Tokens,",");
219
                                 MacroFunction.mParamterValue = Tokens;
220
 
221
                                 vector<string> MacroString = MacroFunction.GetSubstitudedMacro();
222
                                 aFile[i] = "//" + aFile[i];
223
                                 I = aFile.begin() + i+1;
224
                                 aFile.insert(I,MacroString.begin(),MacroString.end());
225
 
226
                                 I = aFile.begin() + i;
227
                        }
228
                }
229
 
230
        //      cout <<  aFile[i].c_str() << std::endl;
231
                ofs << aFile[i].c_str() << std::endl;
232
        }
233
}
234
//----------------------------------------------------------------
235
void Preprocessor::Execute( string aFile )
236
{
237
        ifstream ifs;
238
        ifs.open(aFile.c_str());
239
        if (!ifs.good())
240
                std::cout << "Cannot open file" << aFile << "\n";
241
        cout << "Scanning file " << aFile << "\n";
242
        vector<string> NewFile = ScanFile( ifs );
243
        ifs.close();
244
        cout << "Preprocessing file " << aFile << "\n";
245
        ofstream ofs(string(aFile + ".preprocessed").c_str());
246
        SubstitudeMacros( NewFile, ofs );
247
        ofs.close();
248
}
249
//----------------------------------------------------------------
250
vector<string> TMacroFunction::GetSubstitudedMacro()
251
        {
252
                vector<string> ReturnString;
253
                for (int i = 0; i < mLines.size(); i++)
254
                {
255
 
256
                        string Line = mLines[i];
257
                        for (int j = 0; j < mParamterSymbol.size(); j++)
258
                        {
259
                                while(  Line.find(mParamterSymbol[j]) != string::npos )
260
                                        Line.replace(Line.find(mParamterSymbol[j]),mParamterSymbol[j].length(),mParamterValue[j]);
261
 
262
                                while(  Line.find(mReturnSymbol) != string::npos )
263
                                        Line.replace(Line.find(mReturnSymbol),mReturnSymbol.length(),mReturnValue);
264
 
265
                        }
266
 
267
                        ReturnString.push_back( Line );
268
                }
269
                return ReturnString;
270
        }
271
//----------------------------------------------------------------
272
void Preprocessor::PopulateMacroFunction(string aSignature, std::ifstream & ifs, vector<string> & aNewFile)
273
{
274
 
275
         //Get rid of spaces
276
         int pos = 0;
277
         while ((pos = aSignature.find(" ")) != string::npos)
278
                                 aSignature.erase(pos,1);
279
        //Delete the "#macro" string
280
        aSignature.erase(0,6);
281
        vector<string> Tokens;
282
        Tokenize(aSignature,Tokens,"=");
283
        TMacroFunction M;
284
        M.mReturnSymbol = Tokens[0];
285
 
286
        string Tmp = Tokens[1];
287
        Tokens.clear();
288
        Tokenize(Tmp,Tokens,"(");
289
        M.mName = Tokens[0];
290
        Tmp = Tokens[1];
291
        while ((pos = Tmp.find(")")) != string::npos)
292
                 Tmp.erase(pos,1);
293
         Tokens.clear();
294
         Tokenize(Tmp,Tokens,",");
295
         M.mParamterSymbol = Tokens;
296
 
297
         int MacroLineCount = 0;
298
         while (MacroLineCount < MAX_MACRO_FUNCTION_LINE_COUNT)
299
         {
300
                char Buffer[1024];
301
                 ifs.getline( Buffer,1024 );
302
                 string Line = Buffer;
303
             aNewFile.push_back("//" + Line);
304
                 if (Line.find("#endmacro") != string::npos)
305
                                 break;
306
                 M.mLines.push_back( Line );
307
                 MacroLineCount++;
308
         }
309
 
310
         mMacroFunctions[M.mName] = M;
311
         M.mLines.clear();
312
}
313
//----------------------------------------------------------------

powered by: WebSVN 2.1.0

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