diff options
author | Adrian Thurston <thurston@complang.org> | 2012-07-23 09:13:01 -0400 |
---|---|---|
committer | Adrian Thurston <thurston@complang.org> | 2012-07-23 09:13:01 -0400 |
commit | a90ea7d6eb03019b38ebd7a2ed0902f83e1fa736 (patch) | |
tree | eae0300e9faba75bab01115afe810f59ddeb5ff4 /src | |
parent | e17612e7f67d4e44f6889d36f21021303154fb3a (diff) | |
download | colm-a90ea7d6eb03019b38ebd7a2ed0902f83e1fa736.tar.gz |
renamed Defintion to Production and added a static constructor
Diffstat (limited to 'src')
-rw-r--r-- | src/closure.cc | 2 | ||||
-rw-r--r-- | src/compiler.cc | 30 | ||||
-rw-r--r-- | src/declare.cc | 16 | ||||
-rw-r--r-- | src/lmparse.kl | 17 | ||||
-rw-r--r-- | src/parsedata.h | 73 | ||||
-rw-r--r-- | src/pdabuild.cc | 27 | ||||
-rw-r--r-- | src/pdacodegen.h | 10 | ||||
-rw-r--r-- | src/pdagraph.h | 8 | ||||
-rw-r--r-- | src/resolve.cc | 2 | ||||
-rw-r--r-- | src/synthesis.cc | 6 |
10 files changed, 91 insertions, 100 deletions
diff --git a/src/closure.cc b/src/closure.cc index 37b0e259..f587ea0e 100644 --- a/src/closure.cc +++ b/src/closure.cc @@ -31,7 +31,7 @@ using std::endl; using std::cerr; void Compiler::lr0BringInItem( PdaGraph *pdaGraph, PdaState *dest, PdaState *prodState, - PdaTrans *expandFrom, Definition *prod ) + PdaTrans *expandFrom, Production *prod ) { /* We use dot sets for finding unique states. In the future, should merge * dots sets with the stateSet pointer (only need one of these). */ diff --git a/src/compiler.cc b/src/compiler.cc index 1e955fd7..e3676768 100644 --- a/src/compiler.cc +++ b/src/compiler.cc @@ -1091,10 +1091,9 @@ LangEl *Compiler::makeRepeatProd( Namespace *nspace, const String &repeatName, prodElList1->append( factor1 ); prodElList1->append( factor2 ); - Definition *newDef1 = new Definition( InputLoc(), + Production *newDef1 = Production::cons( InputLoc(), prodName, prodElList1, false, 0, - prodList.length(), prodName->defList.length(), - Definition::Production ); + prodList.length(), prodName->defList.length() ); prodName->defList.append( newDef1 ); prodList.append( newDef1 ); @@ -1102,10 +1101,9 @@ LangEl *Compiler::makeRepeatProd( Namespace *nspace, const String &repeatName, /* Build the second production of the repeat. */ ProdElList *prodElList2 = new ProdElList; - Definition *newDef2 = new Definition( InputLoc(), + Production *newDef2 = Production::cons( InputLoc(), prodName, prodElList2, false, 0, - prodList.length(), prodName->defList.length(), - Definition::Production ); + prodList.length(), prodName->defList.length() ); prodName->defList.append( newDef2 ); prodList.append( newDef2 ); @@ -1130,10 +1128,9 @@ LangEl *Compiler::makeListProd( Namespace *nspace, const String &listName, Names prodElList1->append( factor1 ); prodElList1->append( factor2 ); - Definition *newDef1 = new Definition( InputLoc(), + Production *newDef1 = Production::cons( InputLoc(), prodName, prodElList1, false, 0, - prodList.length(), prodName->defList.length(), - Definition::Production ); + prodList.length(), prodName->defList.length() ); prodName->defList.append( newDef1 ); prodList.append( newDef1 ); @@ -1145,10 +1142,9 @@ LangEl *Compiler::makeListProd( Namespace *nspace, const String &listName, Names ProdElList *prodElList2 = new ProdElList; prodElList2->append( factor3 ); - Definition *newDef2 = new Definition( InputLoc(), + Production *newDef2 = Production::cons( InputLoc(), prodName, prodElList2, false, 0, - prodList.length(), prodName->defList.length(), - Definition::Production ); + prodList.length(), prodName->defList.length() ); prodName->defList.append( newDef2 ); prodList.append( newDef2 ); @@ -1168,10 +1164,9 @@ LangEl *Compiler::makeOptProd( Namespace *nspace, const String &optName, Namespa ProdEl *factor1 = new ProdEl( ProdEl::ReferenceType, InputLoc(), 0, false, typeRef1, 0 ); prodElList1->append( factor1 ); - Definition *newDef1 = new Definition( InputLoc(), + Production *newDef1 = Production::cons( InputLoc(), prodName, prodElList1, false, 0, - prodList.length(), prodName->defList.length(), - Definition::Production ); + prodList.length(), prodName->defList.length() ); prodName->defList.append( newDef1 ); prodList.append( newDef1 ); @@ -1179,10 +1174,9 @@ LangEl *Compiler::makeOptProd( Namespace *nspace, const String &optName, Namespa /* Build the second production of the repeat. */ ProdElList *prodElList2 = new ProdElList; - Definition *newDef2 = new Definition( InputLoc(), + Production *newDef2 = Production::cons( InputLoc(), prodName, prodElList2, false, 0, - prodList.length(), prodName->defList.length(), - Definition::Production ); + prodList.length(), prodName->defList.length() ); prodName->defList.append( newDef2 ); prodList.append( newDef2 ); diff --git a/src/declare.cc b/src/declare.cc index 342ffd65..214b5e04 100644 --- a/src/declare.cc +++ b/src/declare.cc @@ -120,7 +120,7 @@ void Compiler::addProdRedObjectVar( ObjectDef *localFrame, LangEl *nonTerm ) localFrame->insertField( el->name, el ); } -void Compiler::addProdLHSLoad( Definition *prod, CodeVect &code, long &insertPos ) +void Compiler::addProdLHSLoad( Production *prod, CodeVect &code, long &insertPos ) { ObjField *lhsField = prod->redBlock->localFrame->findField("lhs"); assert( lhsField != 0 ); @@ -135,7 +135,7 @@ void Compiler::addProdLHSLoad( Definition *prod, CodeVect &code, long &insertPos insertPos += loads.length(); } -void Compiler::addPushBackLHS( Definition *prod, CodeVect &code, long &insertPos ) +void Compiler::addPushBackLHS( Production *prod, CodeVect &code, long &insertPos ) { CodeBlock *block = prod->redBlock; @@ -175,7 +175,7 @@ void Compiler::addProdRHSVars( ObjectDef *localFrame, ProdElList *prodElList ) } } -void Compiler::addProdRHSLoads( Definition *prod, CodeVect &code, long &insertPos ) +void Compiler::addProdRHSLoads( Production *prod, CodeVect &code, long &insertPos ) { CodeVect loads; long elPos = 0; @@ -208,10 +208,9 @@ void GenericType::declare( Compiler *pd, Namespace *nspace ) LangEl *prodName = langEl; assert( prodName->type == LangEl::NonTerm ); - Definition *newDef = new Definition( InputLoc(), prodName, + Production *newDef = Production::cons( InputLoc(), prodName, emptyList, false, 0, - pd->prodList.length(), prodName->defList.length(), - Definition::Production ); + pd->prodList.length(), prodName->defList.length() ); prodName->defList.append( newDef ); pd->prodList.append( newDef ); @@ -265,10 +264,9 @@ void Namespace::declare( Compiler *pd ) LangEl *prodName = lel; assert( prodName->type == LangEl::NonTerm ); - Definition *newDef = new Definition( loc, prodName, + Production *newDef = Production::cons( loc, prodName, emptyList, false, 0, - pd->prodList.length(), prodName->defList.length(), - Definition::Production ); + pd->prodList.length(), prodName->defList.length() ); prodName->defList.append( newDef ); pd->prodList.append( newDef ); diff --git a/src/lmparse.kl b/src/lmparse.kl index e9019f9d..50d09221 100644 --- a/src/lmparse.kl +++ b/src/lmparse.kl @@ -366,24 +366,17 @@ opt_prec: nonterm define_prod { - Definition *definition; + Production *definition; }; define_prod: '[' prod_el_list ']' opt_commit opt_reduce_code opt_prec final { - const InputLoc &loc = $1->loc; - //const String &name = curDefineId; ProdElList *prodElList = curProdElList; - bool commit = $4->commit; - CodeBlock *redBlock = $5->codeBlock; - LangEl *predOf = $6->predOf; - //Namespace *nspace = namespaceStack.top(); - - Definition *newDef = new Definition( loc, 0/*prodName*/, - prodElList, commit, redBlock, - pd->prodList.length(), 0, Definition::Production ); - newDef->predOf = predOf; + Production *newDef = Production::cons( $1->loc, 0, + prodElList, $4->commit, $5->codeBlock, + pd->prodList.length(), 0 ); + newDef->predOf = $6->predOf; pd->prodList.append( newDef ); diff --git a/src/parsedata.h b/src/parsedata.h index f5e00b24..d961d7f9 100644 --- a/src/parsedata.h +++ b/src/parsedata.h @@ -70,7 +70,7 @@ inline long makeReduceCode( long reduction, bool isShiftReduce ) struct ProdEl; struct ProdElList; struct PdaLiteral; -struct Definition; +struct Production; /* A pointer to this is in PdaRun, but it's specification is not known by the * runtime code. The runtime functions that access it are defined in @@ -79,8 +79,8 @@ struct Bindings : public Vector<ParseTree*> {}; -struct DefListEl { Definition *prev, *next; }; -struct LelDefListEl { Definition *prev, *next; }; +struct DefListEl { Production *prev, *next; }; +struct LelDefListEl { Production *prev, *next; }; typedef Vector< LangEl* > LangElVect; typedef Vector< ProdEl* > FactorVect; @@ -110,21 +110,31 @@ struct PredDecl typedef DList<PredDecl> PredDeclList; /* Graph dictionary. */ -struct Definition +struct Production : public DefListEl, public LelDefListEl { - enum Type { Production }; - - Definition( const InputLoc &loc, LangEl *prodName, ProdElList *prodElList, - bool prodCommit, CodeBlock *redBlock, int prodId, int prodNum, Type type ) + Production() : - loc(loc), prodName(prodName), prodElList(prodElList), - prodCommit(prodCommit), redBlock(redBlock), prodId(prodId), prodNum(prodNum), - type(type), fsm(0), fsmLength(0), uniqueEmptyLeader(0), + loc(loc), prodName(0), prodElList(0), prodCommit(false), redBlock(0), + prodId(0), prodNum(0), fsm(0), fsmLength(0), uniqueEmptyLeader(0), isLeftRec(false), localFrame(0), lhsField(0), predOf(0), collectIgnoreRegion(0) {} + static Production* cons( const InputLoc &loc, LangEl *prodName, ProdElList *prodElList, + bool prodCommit, CodeBlock *redBlock, int prodId, int prodNum ) + { + Production *p = new Production; + p->loc = loc; + p->prodName = prodName; + p->prodElList = prodElList; + p->prodCommit = prodCommit; + p->redBlock = redBlock; + p->prodId = prodId; + p->prodNum = prodNum; + return p; + } + InputLoc loc; LangEl *prodName; ProdElList *prodElList; @@ -134,7 +144,6 @@ struct Definition int prodId; int prodNum; - Type type; PdaGraph *fsm; int fsmLength; @@ -159,7 +168,7 @@ struct Definition struct CmpDefById { - static int compare( Definition *d1, Definition *d2 ) + static int compare( Production *d1, Production *d2 ) { if ( d1->prodId < d2->prodId ) return -1; @@ -172,18 +181,18 @@ struct CmpDefById /* Map dotItems to productions. */ -typedef BstMap< int, Definition*, CmpOrd<int> > DotItemIndex; -typedef BstMapEl< int, Definition*> DotItemIndexEl; +typedef BstMap< int, Production*, CmpOrd<int> > DotItemIndex; +typedef BstMapEl< int, Production*> DotItemIndexEl; struct DefList : - public DListMel<Definition, DefListEl> + public DListMel<Production, DefListEl> {}; /* A vector of production vectors. Each non terminal can have many productions. */ struct LelDefList : - public DListMel<Definition, LelDefListEl> + public DListMel<Production, LelDefListEl> {}; /* A set of machines made during a closure round. */ @@ -246,7 +255,7 @@ struct LangEl : public DListEl<LangEl> LelDefList defList; TokenDef *tokenDef; - Definition *rootDef; + Production *rootDef; LangEl *termDup; LangEl *eofLel; @@ -324,7 +333,7 @@ struct ProdEl struct ProdElList : public DList<ProdEl> { - PdaGraph *walk( Compiler *pd, Definition *prod ); + PdaGraph *walk( Compiler *pd, Production *prod ); }; /* This should be renamed. It is a literal string in a type reference. */ @@ -726,7 +735,7 @@ struct Compiler void lalr1AddFollowSets( PdaGraph *pdaGraph, LangElSet &parserEls ); void lr0BringInItem( PdaGraph *pdaGraph, PdaState *dest, PdaState *prodState, - PdaTrans *expandFrom, Definition *prod ); + PdaTrans *expandFrom, Production *prod ); void lr0InvokeClosure( PdaGraph *pdaGraph, PdaState *state ); void lr0CloseAllStates( PdaGraph *pdaGraph ); @@ -734,10 +743,10 @@ struct Compiler void reduceActions( PdaGraph *pdaGraph ); - bool makeNonTermFirstSetProd( Definition *prod, PdaState *state ); + bool makeNonTermFirstSetProd( Production *prod, PdaState *state ); void makeNonTermFirstSets(); - bool makeFirstSetProd( Definition *prod, PdaState *state ); + bool makeFirstSetProd( Production *prod, PdaState *state ); void makeFirstSets(); int findIndexOff( PdaTables *pdaTables, PdaGraph *pdaGraph, PdaState *state, int &currLen ); @@ -746,13 +755,13 @@ struct Compiler bool noPreIgnore, bool noPostIgnore ); PdaState *followProd( PdaState *tabState, PdaState *prodState ); void findFollow( AlphSet &result, PdaState *overTab, - PdaState *overSrc, Definition *parentDef ); + PdaState *overSrc, Production *parentDef ); void pdaActionOrder( PdaGraph *pdaGraph, LangElSet &parserEls ); void pdaOrderFollow( LangEl *rootEl, PdaState *tabState, PdaTrans *tabTrans, PdaTrans *srcTrans, - Definition *parentDef, Definition *definition, long &time ); + Production *parentDef, Production *definition, long &time ); void pdaOrderProd( LangEl *rootEl, PdaState *tabState, - PdaState *srcState, Definition *parentDef, long &time ); + PdaState *srcState, Production *parentDef, long &time ); void analyzeMachine( PdaGraph *pdaGraph, LangElSet &parserEls ); void makeProdFsms(); @@ -781,9 +790,9 @@ struct Compiler void addProdRedObjectVar( ObjectDef *localFrame, LangEl *langEl ); void addProdObjects(); - void addProdRHSLoads( Definition *prod, CodeVect &code, long &insertPos ); - void addProdLHSLoad( Definition *prod, CodeVect &code, long &insertPos ); - void addPushBackLHS( Definition *prod, CodeVect &code, long &insertPos ); + void addProdRHSLoads( Production *prod, CodeVect &code, long &insertPos ); + void addProdLHSLoad( Production *prod, CodeVect &code, long &insertPos ); + void addPushBackLHS( Production *prod, CodeVect &code, long &insertPos ); void prepGrammar(); void parsePatterns(); @@ -835,7 +844,7 @@ struct Compiler void resolvePreEof( TokenRegion *region ); void resolveRootBlock(); void resolveTranslateBlock( LangEl *langEl ); - void resolveReductionCode( Definition *prod ); + void resolveReductionCode( Production *prod ); void resolveParseTree(); void resolveGenericTypes(); @@ -847,8 +856,8 @@ struct Compiler void compileRootBlock(); void compileTranslateBlock( LangEl *langEl ); void findLocalTrees( CharSet &trees ); - void makeProdCopies( Definition *prod ); - void compileReductionCode( Definition *prod ); + void makeProdCopies( Production *prod ); + void compileReductionCode( Production *prod ); void initGenericTypes(); void removeNonUnparsableRepls(); void compileByteCode(); @@ -928,7 +937,7 @@ struct Compiler PdaState *actionDestState; DefSetSet prodSetSet; - Definition **prodIdIndex; + Production **prodIdIndex; AlphSet literalSet; PatternList patternList; diff --git a/src/pdabuild.cc b/src/pdabuild.cc index 3d1c6ab0..8fd1f11e 100644 --- a/src/pdabuild.cc +++ b/src/pdabuild.cc @@ -97,7 +97,7 @@ LangEl::LangEl( Namespace *nspace, const String &name, Type type ) { } -PdaGraph *ProdElList::walk( Compiler *pd, Definition *prod ) +PdaGraph *ProdElList::walk( Compiler *pd, Production *prod ) { PdaGraph *prodFsm = new PdaGraph(); PdaState *last = prodFsm->addState(); @@ -358,7 +358,7 @@ void Compiler::makeProdFsms() } /* Make the final state specific prod id to prod id mapping. */ - prodIdIndex = new Definition*[prodList.length()]; + prodIdIndex = new Production*[prodList.length()]; for ( DefList::Iter prod = prodList; prod.lte(); prod++ ) prodIdIndex[prod->prodId] = prod; } @@ -368,7 +368,7 @@ void Compiler::makeProdFsms() * from the table, taking only the characters on which the parent is reduced. * */ void Compiler::findFollow( AlphSet &result, PdaState *overTab, - PdaState *overSrc, Definition *parentDef ) + PdaState *overSrc, Production *parentDef ) { if ( overSrc->isFinState() ) { assert( overSrc->transMap.length() == 0 ); @@ -445,8 +445,8 @@ void Compiler::trySetTime( PdaTrans *trans, long code, long &time ) /* Go down a defintiion and then handle the follow actions. */ void Compiler::pdaOrderFollow( LangEl *rootEl, PdaState *tabState, - PdaTrans *tabTrans, PdaTrans *srcTrans, Definition *parentDef, - Definition *definition, long &time ) + PdaTrans *tabTrans, PdaTrans *srcTrans, Production *parentDef, + Production *definition, long &time ) { /* We need the follow from tabState/srcState over the defintion we are * currently processing. */ @@ -559,7 +559,7 @@ void Compiler::addRegion( PdaState *tabState, PdaTrans *tabTrans, #endif void Compiler::pdaOrderProd( LangEl *rootEl, PdaState *tabState, - PdaState *srcState, Definition *parentDef, long &time ) + PdaState *srcState, Production *parentDef, long &time ) { assert( srcState->dotSet.length() == 1 ); if ( tabState->dotSet2.find( srcState->dotSet[0] ) ) @@ -1074,10 +1074,9 @@ void Compiler::wrapNonTerminals() for ( LelList::Iter lel = langEls; lel.lte(); lel++ ) { /* Make a single production used when the lel is a root. */ ProdElList *prodElList = makeProdElList( lel ); - lel->rootDef = new Definition( InputLoc(), rootLangEl, + lel->rootDef = Production::cons( InputLoc(), rootLangEl, prodElList, false, 0, - prodList.length(), rootLangEl->defList.length(), - Definition::Production ); + prodList.length(), rootLangEl->defList.length() ); prodList.append( lel->rootDef ); rootLangEl->defList.append( lel->rootDef ); @@ -1087,7 +1086,7 @@ void Compiler::wrapNonTerminals() } } -bool Compiler::makeNonTermFirstSetProd( Definition *prod, PdaState *state ) +bool Compiler::makeNonTermFirstSetProd( Production *prod, PdaState *state ) { bool modified = false; for ( TransMap::Iter trans = state->transMap; trans.lte(); trans++ ) { @@ -1173,7 +1172,7 @@ void Compiler::printNonTermFirstSets() } } -bool Compiler::makeFirstSetProd( Definition *prod, PdaState *state ) +bool Compiler::makeFirstSetProd( Production *prod, PdaState *state ) { bool modified = false; for ( TransMap::Iter trans = state->transMap; trans.lte(); trans++ ) { @@ -1277,10 +1276,8 @@ void Compiler::insertUniqueEmptyProductions() char name[20]; sprintf(name, "U%li", prodList.length()); LangEl *prodName = addLangEl( this, rootNamespace, name, LangEl::NonTerm ); - Definition *newDef = new Definition( InputLoc(), prodName, - 0 /* FIXME new VarDef( name, 0 )*/, - false, 0, prodList.length(), prodName->defList.length(), - Definition::Production ); + Production *newDef = Production::cons( InputLoc(), prodName, + 0, false, 0, prodList.length(), prodName->defList.length() ); prodName->defList.append( newDef ); prodList.append( newDef ); diff --git a/src/pdacodegen.h b/src/pdacodegen.h index 8e5e7a3a..99043b6d 100644 --- a/src/pdacodegen.h +++ b/src/pdacodegen.h @@ -44,11 +44,11 @@ struct PdaCodeGen void writeTokenIds(); void writeLangEls(); - void writeReference( Definition *prod, char *data ); - void writeUndoReference( Definition *prod, char *data ); - void writeFinalReference( Definition *prod, char *data ); - void writeFirstLocate( Definition *prod ); - void writeRhsLocate( Definition *prod ); + void writeReference( Production *prod, char *data ); + void writeUndoReference( Production *prod, char *data ); + void writeFinalReference( Production *prod, char *data ); + void writeFirstLocate( Production *prod ); + void writeRhsLocate( Production *prod ); void defineRuntime(); void writeRuntimeData( RuntimeData *runtimeData, PdaTables *pdaTables ); diff --git a/src/pdagraph.h b/src/pdagraph.h index dc11b3e1..742fb047 100644 --- a/src/pdagraph.h +++ b/src/pdagraph.h @@ -48,7 +48,7 @@ struct PdaTrans; struct PdaState; struct PdaGraph; struct TokenDef; -struct Definition; +struct Production; struct LangEl; struct TokenRegion; @@ -101,11 +101,11 @@ struct ClosureQueueListEl { PdaState *prev, *next; }; typedef DListMel< PdaState, ClosureQueueListEl > StateClosureQueue; typedef DList<PdaTrans> TransClosureQueue; -typedef BstSet< Definition*, CmpOrd<Definition*> > DefSet; -typedef CmpTable< Definition*, CmpOrd<Definition*> > CmpDefSet; +typedef BstSet< Production*, CmpOrd<Production*> > DefSet; +typedef CmpTable< Production*, CmpOrd<Production*> > CmpDefSet; typedef BstSet< DefSet, CmpDefSet > DefSetSet; -typedef Vector< Definition* > DefVect; +typedef Vector< Production* > DefVect; typedef BstSet< long, CmpOrd<long> > AlphSet; struct ExpandToEl diff --git a/src/resolve.cc b/src/resolve.cc index 31dd3883..4e848ca7 100644 --- a/src/resolve.cc +++ b/src/resolve.cc @@ -559,7 +559,7 @@ void Compiler::resolveTranslateBlock( LangEl *langEl ) block->resolve( this ); } -void Compiler::resolveReductionCode( Definition *prod ) +void Compiler::resolveReductionCode( Production *prod ) { CodeBlock *block = prod->redBlock; block->resolve( this ); diff --git a/src/synthesis.cc b/src/synthesis.cc index c9bff5a6..ed05317c 100644 --- a/src/synthesis.cc +++ b/src/synthesis.cc @@ -2634,7 +2634,7 @@ ObjField *Compiler::makeLineEl() void Compiler::initTokenObjects( ) { - /* Make a default object Definition. */ + /* Make a default object Production. */ tokenObj = new ObjectDef( ObjectDef::BuiltinType, "token", nextObjectId++ ); ObjField *dataEl = makeDataEl(); @@ -2684,7 +2684,7 @@ void Compiler::findLocalTrees( CharSet &trees ) } } -void Compiler::makeProdCopies( Definition *prod ) +void Compiler::makeProdCopies( Production *prod ) { int pos = 0; for ( ProdElList::Iter pel = *prod->prodElList; pel.lte(); pel++, pos++) { @@ -2695,7 +2695,7 @@ void Compiler::makeProdCopies( Definition *prod ) } } -void Compiler::compileReductionCode( Definition *prod ) +void Compiler::compileReductionCode( Production *prod ) { CodeBlock *block = prod->redBlock; |