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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
/*
* Copyright 2006-2012 Adrian Thurston <thurston@complang.org>
*/
#include <iostream>
#include <string>
#include <errno.h>
#include "parser.h"
#include "config.h"
#include "avltree.h"
#include "compiler.h"
#include "parser.h"
#include "global.h"
#include "input.h"
#include "loadinit.h"
#include "if1.h"
#include "colm/colm.h"
using std::string;
extern RuntimeData colm_object;
void LoadInit::walkProdElList( String defName, ProdElList *list, prod_el_list &prodElList )
{
if ( prodElList.ProdElList() != 0 ) {
prod_el_list RightProdElList = prodElList.ProdElList();
walkProdElList( defName, list, RightProdElList );
}
if ( prodElList.ProdEl() != 0 ) {
prod_el El = prodElList.ProdEl();
String typeName = El.Id().text().c_str();
ObjectField *captureField = 0;
if ( El.OptName().Name() != 0 ) {
/* Has a capture. */
String fieldName = El.OptName().Name().text().c_str();
captureField = ObjectField::cons( internal,
ObjectField::RhsNameType, 0, fieldName );
}
else {
/* Default the capture to the name of the type. */
String fieldName = typeName;
if ( strcmp( fieldName, defName ) == 0 )
fieldName = "_" + defName;
captureField = ObjectField::cons( internal,
ObjectField::RhsNameType, 0, fieldName );
}
RepeatType repeatType = RepeatNone;
if ( El.OptRepeat().Star() != 0 )
repeatType = RepeatRepeat;
ProdEl *prodEl = prodElName( internal, typeName,
NamespaceQual::cons( curNspace() ),
captureField, repeatType, false );
appendProdEl( list, prodEl );
}
}
void LoadInit::walkProdList( String defName, LelDefList *outProdList, prod_list &prodList )
{
if ( prodList.ProdList() != 0 ) {
prod_list RightProdList = prodList.ProdList();
walkProdList( defName, outProdList, RightProdList );
}
ProdElList *outElList = new ProdElList;
prod_el_list prodElList = prodList.Prod().ProdElList();
walkProdElList( defName, outElList, prodElList );
String name;
if ( prodList.Prod().OptName().Name() != 0 )
name = prodList.Prod().OptName().Name().text().c_str();
bool commit = prodList.Prod().OptCommit().Commit() != 0;
Production *prod = BaseParser::production( internal, outElList, name, commit, 0, 0 );
prodAppend( outProdList, prod );
}
LexFactor *LoadInit::walkLexFactor( lex_factor &lexFactor )
{
LexFactor *factor = 0;
if ( lexFactor.Literal() != 0 ) {
String litString = lexFactor.Literal().text().c_str();
Literal *literal = Literal::cons( internal, litString, Literal::LitString );
factor = LexFactor::cons( literal );
}
if ( lexFactor.Id() != 0 ) {
String id = lexFactor.Id().text().c_str();
factor = lexRlFactorName( id, internal );
}
else if ( lexFactor.Expr() != 0 ) {
lex_expr LexExpr = lexFactor.Expr();
LexExpression *expr = walkLexExpr( LexExpr );
LexJoin *join = LexJoin::cons( expr );
factor = LexFactor::cons( join );
}
else if ( lexFactor.Low() != 0 ) {
String low = lexFactor.Low().text().c_str();
Literal *lowLit = Literal::cons( internal, low, Literal::LitString );
String high = lexFactor.High().text().c_str();
Literal *highLit = Literal::cons( internal, high, Literal::LitString );
Range *range = Range::cons( lowLit, highLit );
factor = LexFactor::cons( range );
}
return factor;
}
LexFactorNeg *LoadInit::walkLexFactorNeg( lex_factor_neg &lexFactorNeg )
{
if ( lexFactorNeg.FactorNeg() != 0 ) {
lex_factor_neg Rec = lexFactorNeg.FactorNeg();
LexFactorNeg *recNeg = walkLexFactorNeg( Rec );
LexFactorNeg *factorNeg = LexFactorNeg::cons( recNeg, LexFactorNeg::CharNegateType );
return factorNeg;
}
else {
lex_factor LexFactorTree = lexFactorNeg.Factor();
LexFactor *factor = walkLexFactor( LexFactorTree );
LexFactorNeg *factorNeg = LexFactorNeg::cons( factor );
return factorNeg;
}
}
LexFactorRep *LoadInit::walkLexFactorRep( lex_factor_rep &lexFactorRep )
{
LexFactorRep *factorRep = 0;
if ( lexFactorRep.Star() != 0 ) {
lex_factor_rep Rec = lexFactorRep.FactorRep();
LexFactorRep *recRep = walkLexFactorRep( Rec );
factorRep = LexFactorRep::cons( internal, recRep, 0, 0, LexFactorRep::StarType );
}
else if ( lexFactorRep.Plus() != 0 ) {
lex_factor_rep Rec = lexFactorRep.FactorRep();
LexFactorRep *recRep = walkLexFactorRep( Rec );
factorRep = LexFactorRep::cons( internal, recRep, 0, 0, LexFactorRep::PlusType );
}
else {
lex_factor_neg LexFactorNegTree = lexFactorRep.FactorNeg();
LexFactorNeg *factorNeg = walkLexFactorNeg( LexFactorNegTree );
factorRep = LexFactorRep::cons( factorNeg );
}
return factorRep;
}
LexFactorAug *LoadInit::walkLexFactorAug( lex_factor_rep &lexFactorRep )
{
LexFactorRep *factorRep = walkLexFactorRep( lexFactorRep );
return LexFactorAug::cons( factorRep );
}
LexTerm *LoadInit::walkLexTerm( lex_term &lexTerm )
{
if ( lexTerm.Term() != 0 ) {
lex_term Rec = lexTerm.Term();
LexTerm *leftTerm = walkLexTerm( Rec );
lex_factor_rep LexFactorRepTree = lexTerm.FactorRep();
LexFactorAug *factorAug = walkLexFactorAug( LexFactorRepTree );
LexTerm *term = LexTerm::cons( leftTerm, factorAug, LexTerm::ConcatType );
return term;
}
else {
lex_factor_rep LexFactorRepTree = lexTerm.FactorRep();
LexFactorAug *factorAug = walkLexFactorAug( LexFactorRepTree );
LexTerm *term = LexTerm::cons( factorAug );
return term;
}
}
LexExpression *LoadInit::walkLexExpr( lex_expr &LexExprTree )
{
if ( LexExprTree.Expr() != 0 ) {
lex_expr Rec = LexExprTree.Expr();
LexExpression *leftExpr = walkLexExpr( Rec );
lex_term lexTerm = LexExprTree.Term();
LexTerm *term = walkLexTerm( lexTerm );
LexExpression *expr = LexExpression::cons( leftExpr, term, LexExpression::OrType );
return expr;
}
else {
lex_term lexTerm = LexExprTree.Term();
LexTerm *term = walkLexTerm( lexTerm );
LexExpression *expr = LexExpression::cons( term );
return expr;
}
}
bool walkNoIgnore( opt_ni OptNi )
{
return OptNi.Ni() != 0;
}
void LoadInit::walkTokenList( token_list &tokenList )
{
if ( tokenList.TokenList() != 0 ) {
token_list RightTokenList = tokenList.TokenList();
walkTokenList( RightTokenList );
}
if ( tokenList.TokenDef() != 0 ) {
token_def tokenDef = tokenList.TokenDef();
String name = tokenDef.Id().text().c_str();
ObjectDef *objectDef = ObjectDef::cons( ObjectDef::UserType, name, pd->nextObjectId++ );
lex_expr LexExpr = tokenDef.Expr();
LexExpression *expr = walkLexExpr( LexExpr );
LexJoin *join = LexJoin::cons( expr );
bool leftNi = walkNoIgnore( tokenDef.LeftNi() );
bool rightNi = walkNoIgnore( tokenDef.RightNi() );
defineToken( internal, name, join, objectDef, 0, false, leftNi, rightNi );
}
if ( tokenList.IgnoreDef() != 0 ) {
ignore_def IgnoreDef = tokenList.IgnoreDef();
ObjectDef *objectDef = ObjectDef::cons( ObjectDef::UserType, String(), pd->nextObjectId++ );
lex_expr LexExpr = IgnoreDef.Expr();
LexExpression *expr = walkLexExpr( LexExpr );
LexJoin *join = LexJoin::cons( expr );
defineToken( internal, String(), join, objectDef, 0, true, false, false );
}
}
void LoadInit::walkLexRegion( item &LexRegion )
{
pushRegionSet( internal );
token_list tokenList = LexRegion.TokenList();
walkTokenList( tokenList );
popRegionSet();
}
void LoadInit::walkDefinition( item &define )
{
prod_list ProdList = define.ProdList();
String name = define.DefId().text().c_str();
LelDefList *defList = new LelDefList;
walkProdList( name, defList, ProdList );
NtDef *ntDef = NtDef::cons( name, curNspace(), curStruct(), false );
ObjectDef *objectDef = ObjectDef::cons( ObjectDef::UserType, name,
pd->nextObjectId++ );
cflDef( ntDef, objectDef, defList );
}
void LoadInit::consParseStmt( StmtList *stmtList )
{
/* Parse the "start" def. */
NamespaceQual *nspaceQual = NamespaceQual::cons( curNspace() );
TypeRef *typeRef = TypeRef::cons( internal, nspaceQual,
String("start"), RepeatNone );
/* Pop argv, this yields the file name . */
CallArgVect *popArgs = new CallArgVect;
QualItemVect *popQual = new QualItemVect;
popQual->append( QualItem( QualItem::Arrow, internal, String( "argv" ) ) );
LangVarRef *popRef = LangVarRef::cons( internal, 0,
curLocalFrame->rootScope, popQual, String("pop") );
LangExpr *pop = LangExpr::cons( LangTerm::cons( InputLoc(), popRef, popArgs ) );
/* Construct a literal string 'r', for second arg to open. */
ConsItem *modeConsItem = ConsItem::cons( internal,
ConsItem::InputText, String("r") );
ConsItemList *modeCons = new ConsItemList;
modeCons->append( modeConsItem );
LangExpr *modeExpr = LangExpr::cons( LangTerm::cons( internal, modeCons ) );
/* Call open. */
QualItemVect *openQual = new QualItemVect;
LangVarRef *openRef = LangVarRef::cons( internal,
0, curLocalFrame->rootScope, openQual, String("open") );
CallArgVect *openArgs = new CallArgVect;
openArgs->append( new CallArg(pop) );
openArgs->append( new CallArg(modeExpr) );
LangExpr *open = LangExpr::cons( LangTerm::cons( InputLoc(), openRef, openArgs ) );
/* Construct a list containing the open stream. */
ConsItem *consItem = ConsItem::cons( internal, ConsItem::ExprType, open );
ConsItemList *list = ConsItemList::cons( consItem );
/* Will capture the parser to "P" */
ObjectField *objField = ObjectField::cons( internal,
ObjectField::UserLocalType, 0, String("P") );
/* Parse the above list. */
LangExpr *parseExpr = parseCmd( internal, false, false, objField, typeRef, 0, list );
LangStmt *parseStmt = LangStmt::cons( internal, LangStmt::ExprType, parseExpr );
stmtList->append( parseStmt );
}
void LoadInit::consExportTree( StmtList *stmtList )
{
QualItemVect *qual = new QualItemVect;
LangVarRef *varRef = LangVarRef::cons( internal, 0,
curLocalFrame->rootScope, qual, String("P") );
LangExpr *expr = LangExpr::cons( LangTerm::cons( internal,
LangTerm::VarRefType, varRef ) );
NamespaceQual *nspaceQual = NamespaceQual::cons( curNspace() );
TypeRef *typeRef = TypeRef::cons( internal, nspaceQual, String("start"), RepeatNone );
ObjectField *program = ObjectField::cons( internal,
ObjectField::StructFieldType, typeRef, String("ColmTree") );
LangStmt *programExport = exportStmt( program, LangStmt::AssignType, expr );
stmtList->append( programExport );
}
void LoadInit::consExportError( StmtList *stmtList )
{
QualItemVect *qual = new QualItemVect;
LangVarRef *varRef = LangVarRef::cons( internal, 0,
curLocalFrame->rootScope, qual, String("error") );
LangExpr *expr = LangExpr::cons( LangTerm::cons( internal,
LangTerm::VarRefType, varRef ) );
NamespaceQual *nspaceQual = NamespaceQual::cons( curNspace() );
TypeRef *typeRef = TypeRef::cons( internal, nspaceQual, String("str"), RepeatNone );
ObjectField *program = ObjectField::cons( internal,
ObjectField::StructFieldType, typeRef, String("ColmError") );
LangStmt *programExport = exportStmt( program, LangStmt::AssignType, expr );
stmtList->append( programExport );
}
void LoadInit::go( long activeRealm )
{
LoadInit::init();
StmtList *stmtList = new StmtList;
const char *argv[3];
argv[0] = "load-init";
argv[1] = inputFileName;
argv[2] = 0;
colm_program *program = colm_new_program( &colm_object );
colm_set_debug( program, 0x1 );
colm_run_program( program, 2, argv );
/* Extract the parse tree. */
start Start = ColmTree( program );
if ( Start == 0 ) {
gblErrorCount += 1;
std::cerr << inputFileName << ": parse error" << std::endl;
return;
}
/* Walk the list of items. */
_repeat_item ItemList = Start.ItemList();
while ( !ItemList.end() ) {
item Item = ItemList.value();
if ( Item.DefId() != 0 )
walkDefinition( Item );
else if ( Item.TokenList() != 0 )
walkLexRegion( Item );
ItemList = ItemList.next();
}
colm_delete_program( program );
consParseStmt( stmtList );
consExportTree( stmtList );
consExportError( stmtList );
pd->rootCodeBlock = CodeBlock::cons( stmtList, 0 );
}
|