-
Notifications
You must be signed in to change notification settings - Fork 3
/
LnMain.cpp
273 lines (245 loc) · 7.97 KB
/
LnMain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
/*
* Copyright 2024 Rochus Keller <mailto:[email protected]>
*
* This file is part of the Luon language project.
*
* The following is the license that applies to this copy of the
* file. For a license to use the file under conditions
* other than those described here, please email to [email protected].
*
* GNU General Public License Usage
* This file may be used under the terms of the GNU General Public
* License (GPL) versions 2.0 or 3.0 as published by the Free Software
* Foundation and appearing in the file LICENSE.GPL included in
* the packaging of this file. Please review the following information
* to ensure GNU General Public Licensing requirements will be met:
* http://www.fsf.org/licensing/licenses/info/GPLv2.html and
* http://www.gnu.org/copyleft/gpl.html.
*/
#include <QCoreApplication>
#include <QFile>
#include <QStringList>
#include <QtDebug>
#include <QFileInfo>
#include <QDir>
#include <QElapsedTimer>
#include <LnPpLexer.h>
#include <LnParser2.h>
#include <LnLexer.h>
#include <LnToken.h>
#include <LnValidator.h>
#include <QBuffer>
#include <QCommandLineParser>
class Lex2 : public Ln::Scanner2
{
public:
QString sourcePath;
Ln::PpLexer lex;
Ln::Token next()
{
return lex.nextToken();
}
Ln::Token peek(int offset)
{
return lex.peekToken(offset);
}
QString source() const { return sourcePath; }
};
static QByteArray getModuleName(const QString& file)
{
Ln::Lexer lex;
lex.setStream(file);
Ln::Token t = lex.nextToken();
while( t.isValid() && t.d_tokenType != Ln::Tok_MODULE )
t = lex.nextToken();
if( t.d_tokenType == Ln::Tok_MODULE )
{
t = lex.nextToken();
if( t.d_tokenType == Ln::Tok_ident )
return t.d_val;
}
return QByteArray();
}
struct ModuleSlot
{
Ln::Import imp;
QString file;
Ln::Declaration* decl;
ModuleSlot():decl(0) {}
ModuleSlot( const Ln::Import& i, const QString& f, Ln::Declaration* d):imp(i),file(f),decl(d){}
};
static bool operator==(const Ln::Import& lhs, const Ln::Import& rhs)
{
if( lhs.path != rhs.path )
return false;
if( lhs.metaActuals.size() != rhs.metaActuals.size() )
return false;
for( int i = 0; i < lhs.metaActuals.size(); i++ )
{
if( lhs.metaActuals[i]->kind != rhs.metaActuals[i]->kind )
return false;
if( lhs.metaActuals[i]->type != rhs.metaActuals[i]->type )
return false;
if( lhs.metaActuals[i]->val != rhs.metaActuals[i]->val )
return false;
}
return true;
}
class Manager : public Ln::Importer {
public:
typedef QList<ModuleSlot> Modules;
Modules modules;
QList<QDir> searchPath;
QString rootPath;
Manager() {}
~Manager() {
Modules::const_iterator i;
for( i = modules.begin(); i != modules.end(); ++i )
Ln::Declaration::deleteAll((*i).decl);
}
ModuleSlot* find(const Ln::Import& imp)
{
for(int i = 0; i < modules.size(); i++ )
{
if( modules[i].imp.equals(imp) )
return &modules[i];
}
return 0;
}
QByteArray modulePath( const QByteArrayList& path )
{
return path.join('$');
}
QByteArray moduleSuffix( const Ln::MetaActualList& ma )
{
// TODO: this is an intermediate solution assuming everything is built from sources in full everytime.
if( ma.isEmpty() )
return QByteArray();
else
return "$" + QByteArray::number(modules.size());
}
static inline void report(const QString& file, const Ln::RowCol& pos, const QString& msg)
{
QTextStream out(stdout);
out << QFileInfo(file).fileName() << ":" << pos.d_row << ":" << pos.d_col << ": " << msg << endl;
}
Ln::Declaration* loadModule( const Ln::Import& imp )
{
ModuleSlot* ms = find(imp);
if( ms != 0 )
return ms->decl;
QString file = toFile(imp);
if( file.isEmpty() )
{
qCritical() << "cannot find source file of module" << imp.path.join('.');
modules.append(ModuleSlot(imp,QString(),0));
return 0;
}
// immediately add it so that circular module refs lead to an error
modules.append(ModuleSlot(imp,file,0));
ms = &modules.back();
Lex2 lex;
lex.sourcePath = file; // to keep file name if invalid
lex.lex.setStream(file);
if( imp.metaActuals.isEmpty() )
qDebug() << "**** compiling" << QFileInfo(file).fileName();
else
qDebug() << "**** instantiating" << QFileInfo(file).fileName();
//Ln::MilEmitter e(&r);
Ln::AstModel mdl;
Ln::Parser2 p(&mdl,&lex);
p.RunParser();
Ln::Declaration* module = 0;
if( !p.errors.isEmpty() )
{
foreach( const Ln::Parser2::Error& e, p.errors )
report(e.path, e.pos, e.msg);
}else
{
module = p.takeResult();
Ln::Validator v(&mdl, this);
if( !v.validate(module, imp) )
{
foreach( const Ln::Validator::Error& e, v.errors )
report(e.path, e.pos, e.msg);
Ln::Declaration::deleteAll(module);
module = 0;
ms->imp = Ln::Import();
}else
{
}
}
// TODO: uniquely extend the name of generic module instantiations
ms->decl = module;
return module;
}
QString toFile(const Ln::Import& imp)
{
const QString path = imp.path.join('/') + ".luon";
foreach( const QDir& dir, searchPath )
{
const QString tmp = dir.absoluteFilePath(path);
if( QFile::exists(tmp) )
return tmp;
}
if( imp.importer )
{
// if the file is not in the search path, look in the directory of the caller assuming
// that the required module path is relative to the including module
QFileInfo info( imp.importer->data.value<Ln::ModuleData>().source );
const QString tmp = info.absoluteDir().absoluteFilePath(path);
if( QFile::exists(tmp) )
return tmp;
}
return QString();
}
};
static void process(const QStringList& files, const QStringList& searchPaths, bool run, bool dump)
{
int ok = 0;
int all = 0;
QElapsedTimer timer;
timer.start();
foreach( const QString& file, files )
{
Manager mgr;
QFileInfo info(file);
mgr.rootPath = info.absolutePath();
mgr.searchPath.append(info.absoluteDir());
for( int i = 0; i < searchPaths.size(); i++ )
{
const QString path = searchPaths[i];
mgr.searchPath.append(path);
}
Ln::Import imp;
imp.path.append(Ln::Token::getSymbol(info.baseName().toUtf8()));
Ln::Declaration* module = mgr.loadModule(imp); // recursively compiles all imported files
all += mgr.modules.size();
foreach( const ModuleSlot& m, mgr.modules )
ok += m.decl ? 1 : 0;
}
Ln::AstModel::cleanupGlobals();
qDebug() << "#### finished with" << ok << "files ok of total" << all << "files" << "in" << timer.elapsed() << " [ms]";
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QCommandLineParser cp;
cp.setApplicationDescription("Luon compiler");
cp.addHelpOption();
cp.addVersionOption();
cp.addPositionalArgument("main", "the main module of the application");
QCommandLineOption sp("I", "add a path where to look for modules", "path");
cp.addOption(sp);
QCommandLineOption run("r", "run in interpreter");
cp.addOption(run);
QCommandLineOption dump("d", "dump MIL code");
cp.addOption(dump);
cp.process(a);
const QStringList args = cp.positionalArguments();
if( args.isEmpty() )
return -1;
const QStringList searchPaths = cp.values(sp);
process(args, searchPaths, cp.isSet(run), cp.isSet(dump));
return 0;
}