From ba0569b4a80459e7d05f63115c89073eb9637537 Mon Sep 17 00:00:00 2001 From: Adrian Thurston Date: Fri, 27 Dec 2019 11:52:41 +0200 Subject: Created a common superclass for the export classes Every class representing Colm types has a program and tree pointer and some common functions. Moving this out allows us to pass the common base to the repeat iterator and eliminate the first argument to the iterator. --- colm/colmex.h | 36 ++++++++++++++++++++++++++++++++++-- colm/exports.cc | 33 +++------------------------------ colm/loadfinal.cc | 38 +++++++++++++++++++------------------- colm/loadinit.cc | 2 +- test/rlparse.d/load.cc | 24 ++++++++++++------------ test/rlparse.d/reducer.cc | 2 +- 6 files changed, 70 insertions(+), 65 deletions(-) diff --git a/colm/colmex.h b/colm/colmex.h index 5786b0a7..2abc7b2f 100644 --- a/colm/colmex.h +++ b/colm/colmex.h @@ -8,13 +8,45 @@ #include #include +inline void appendString( colm_print_args *args, const char *data, int length ) +{ + std::string *str = (std::string*)args->arg; + *str += std::string( data, length ); +} + +inline std::string printTreeStr( colm_program *prg, colm_tree *tree, bool trim ) +{ + std::string str; + struct indent_impl indent = { -1, 0 }; + colm_print_args printArgs = { &str, 1, 0, trim, &indent, &appendString, + &colm_print_null, &colm_print_term_tree, &colm_print_null }; + colm_print_tree_args( prg, colm_vm_root(prg), &printArgs, tree ); + return str; +} + +struct ExportTree +{ + ExportTree( colm_program *prg, colm_tree *tree ) + : __prg(prg), __tree(tree) {} + + std::string text() { return printTreeStr( __prg, __tree, true ); } + colm_location *loc() { return colm_find_location( __prg, __tree ); } + std::string text_notrim() { return printTreeStr( __prg, __tree, false ); } + std::string text_ws() { return printTreeStr( __prg, __tree, false ); } + colm_data *data() { return __tree->tokdata; } + operator colm_tree *() { return __tree; } + + colm_program *__prg; + colm_tree *__tree; +}; + /* Non-recursive tree iterator. Runs an in-order traversal and when it finds a * search target it yields it and then resumes searching the next child. It * does not go into what it finds. This iterator can be used to search lists, * regardless if they are left-recursive or right-recursive. */ -template struct RepeatIter +template struct RepeatIter { - RepeatIter( const RootType &root ) + RepeatIter( const ExportTree &root ) : prg(root.__prg), search_id(SearchType::ID) diff --git a/colm/exports.cc b/colm/exports.cc index cc040d57..58482ad1 100644 --- a/colm/exports.cc +++ b/colm/exports.cc @@ -64,26 +64,6 @@ void Compiler::generateExports() "#include \n" "\n"; - out << - "inline void appendString( colm_print_args *args, const char *data, int length )\n" - "{\n" - " std::string *str = (std::string*)args->arg;\n" - " *str += std::string( data, length );\n" - "}\n" - "\n"; - - out << - "inline std::string printTreeStr( colm_program *prg, colm_tree *tree, bool trim )\n" - "{\n" - " std::string str;\n" - " struct indent_impl indent = { -1, 0 };\n" - " colm_print_args printArgs = { &str, 1, 0, trim, &indent, &appendString, \n" - " &colm_print_null, &colm_print_term_tree, &colm_print_null };\n" - " colm_print_tree_args( prg, colm_vm_root(prg), &printArgs, tree );\n" - " return str;\n" - "}\n" - "\n"; - /* Declare. */ for ( LelList::Iter lel = langEls; lel.lte(); lel++ ) { if ( lel->isEOF ) @@ -102,25 +82,18 @@ void Compiler::generateExports() openNameSpace( out, lel->nspace ); out << "struct " << lel->fullName << "\n"; + out << " : public ExportTree\n"; out << "{\n"; out << " static const int ID = " << lel->id << ";\n"; - out << " std::string text() { return printTreeStr( __prg, __tree, true ); }\n"; - out << " colm_location *loc() { return colm_find_location( __prg, __tree ); }\n"; - out << " std::string text_notrim() { return printTreeStr( __prg, __tree, false ); }\n"; - out << " std::string text_ws() { return printTreeStr( __prg, __tree, false ); }\n"; - out << " colm_data *data() { return __tree->tokdata; }\n"; - out << " operator colm_tree *() { return __tree; }\n"; - out << " colm_program *__prg;\n"; - out << " colm_tree *__tree;\n"; if ( mainReturnUT != 0 && mainReturnUT->langEl == lel ) { out << " " << lel->fullName << - "( colm_program *prg ) : __prg(prg), __tree(returnVal(prg)) {\n"; + "( colm_program *prg ) : ExportTree( prg, returnVal(prg) ) {\n"; out << " }\n"; } out << " " << lel->fullName << - "( colm_program *prg, colm_tree *tree ) : __prg(prg), __tree(tree) {\n"; + "( colm_program *prg, colm_tree *tree ) : ExportTree( prg, tree ) {\n"; out << "}\n"; diff --git a/colm/loadfinal.cc b/colm/loadfinal.cc index 01101973..a6496f15 100644 --- a/colm/loadfinal.cc +++ b/colm/loadfinal.cc @@ -314,7 +314,7 @@ struct LoadColm StmtList *retList = new StmtList; /* Walk the list of statements. */ - RepeatIter<_repeat_statement, statement> ri( langStmtList.StmtList() ); + RepeatIter ri( langStmtList.StmtList() ); while ( !ri.end() ) { statement Statement = ri.value(); @@ -385,7 +385,7 @@ struct LoadColm ObjectDef *objectDef = ObjectDef::cons( ObjectDef::UserType, String(), pd->nextObjectId++ ); - RepeatIter<_repeat_var_def, var_def> varDefIter( varDefList ); + RepeatIter varDefIter( varDefList ); while ( !varDefIter.end() ) { ObjectField *varDef = walkVarDef( varDefIter.value(), @@ -534,7 +534,7 @@ struct LoadColm { PatternItemList *list = new PatternItemList; - RepeatIter<_repeat_sq_cons_data, sq_cons_data> sqConsDataIter( sqConsDataList ); + RepeatIter sqConsDataIter( sqConsDataList ); while ( !sqConsDataIter.end() ) { String consData = unescape( sqConsDataIter.value().text().c_str() ); @@ -561,7 +561,7 @@ struct LoadColm { ConsItemList *list = new ConsItemList; - RepeatIter<_repeat_sq_cons_data, sq_cons_data> sqConsDataIter( sqConsDataList ); + RepeatIter sqConsDataIter( sqConsDataList ); while ( !sqConsDataIter.end() ) { String consData = unescape( sqConsDataIter.value().text().c_str() ); @@ -589,7 +589,7 @@ struct LoadColm { PatternItemList *list = new PatternItemList; - RepeatIter<_repeat_litpat_el, litpat_el> litpatElIter( litpatElList ); + RepeatIter litpatElIter( litpatElList ); while ( !litpatElIter.end() ) { PatternItemList *tail = walkLitpatEl( litpatElIter.value(), patternVarRef ); @@ -613,7 +613,7 @@ struct LoadColm { PatternItemList *list = new PatternItemList; - RepeatIter<_repeat_pattern_el, pattern_el> patternElIter( patternElList ); + RepeatIter patternElIter( patternElList ); while ( !patternElIter.end() ) { PatternItemList *tail = walkPatternEl( patternElIter.value(), patternVarRef ); @@ -783,7 +783,7 @@ struct LoadColm String lit = ""; _repeat_sq_cons_data sqConsDataList = Include.SqConsDataList(); - RepeatIter<_repeat_sq_cons_data, sq_cons_data> sqConsDataIter( sqConsDataList ); + RepeatIter sqConsDataIter( sqConsDataList ); while ( !sqConsDataIter.end() ) { colm_data *data = sqConsDataIter.value().data(); @@ -1465,7 +1465,7 @@ struct LoadColm { ConsItemList *list = new ConsItemList; - RepeatIter<_repeat_lit_cons_el, lit_cons_el> litConsElIter( litConsElList ); + RepeatIter litConsElIter( litConsElList ); while ( !litConsElIter.end() ) { ConsItemList *extension = walkLitConsEl( litConsElIter.value(), consTypeRef ); @@ -1526,7 +1526,7 @@ struct LoadColm { ConsItemList *list = new ConsItemList; - RepeatIter<_repeat_cons_el, cons_el> consElIter( consElList ); + RepeatIter consElIter( consElList ); while ( !consElIter.end() ) { ConsItemList *extension = walkConsEl( consElIter.value(), consTypeRef ); @@ -1601,7 +1601,7 @@ struct LoadColm { ConsItemList *list = new ConsItemList; - RepeatIter<_repeat_lit_string_el, lit_string_el> litStringElIter( litStringElList ); + RepeatIter litStringElIter( litStringElList ); while ( !litStringElIter.end() ) { ConsItemList *extension = walkLitStringEl( litStringElIter.value() ); @@ -1657,7 +1657,7 @@ struct LoadColm { ConsItemList *list = new ConsItemList; - RepeatIter<_repeat_string_el, string_el> stringElIter( stringElList ); + RepeatIter stringElIter( stringElList ); while ( !stringElIter.end() ) { ConsItemList *extension = walkStringEl( stringElIter.value() ); @@ -1733,7 +1733,7 @@ struct LoadColm { ConsItemList *list = new ConsItemList; - RepeatIter<_repeat_lit_accum_el, lit_accum_el> litAccumElIter( litAccumElList ); + RepeatIter litAccumElIter( litAccumElList ); while ( !litAccumElIter.end() ) { ConsItemList *extension = walkLitAccumEl( litAccumElIter.value() ); @@ -1789,7 +1789,7 @@ struct LoadColm { ConsItemList *list = new ConsItemList; - RepeatIter<_repeat_accum_el, accum_el> accumElIter( accumElList ); + RepeatIter accumElIter( accumElList ); while ( !accumElIter.end() ) { ConsItemList *extension = walkAccumEl( accumElIter.value() ); @@ -1857,7 +1857,7 @@ struct LoadColm { FieldInitVect *list = new FieldInitVect; - RepeatIter<_repeat_field_init, field_init> fieldInitIter( fieldInitList ); + RepeatIter fieldInitIter( fieldInitList ); while ( !fieldInitIter.end() ) { walkFieldInit( list, fieldInitIter.value() ); @@ -2517,7 +2517,7 @@ struct LoadColm _repeat_struct_item structItemList = structDef.ItemList(); - RepeatIter<_repeat_struct_item, struct_item> structItemIter( structItemList ); + RepeatIter structItemIter( structItemList ); while ( !structItemIter.end() ) { walkStructItem( structItemIter.value() ); @@ -2608,7 +2608,7 @@ struct LoadColm void walkRedItemList( _repeat_host_item itemList, ReduceTextItemList &list ) { - RepeatIter<_repeat_host_item, host_item> itemIter( itemList ); + RepeatIter itemIter( itemList ); while ( !itemIter.end() ) { walkRedItem( itemIter.value(), list ); @@ -2659,7 +2659,7 @@ struct LoadColm void walkReductionList( _repeat_reduction_item itemList ) { - RepeatIter<_repeat_reduction_item, reduction_item> itemIter( itemList ); + RepeatIter itemIter( itemList ); while ( !itemIter.end() ) { walkReductionItem( itemIter.value() ); @@ -2855,7 +2855,7 @@ struct LoadColm void walkNamespaceItemList( _repeat_namespace_item itemList, StmtList *stmtList ) { /* Walk the list of items. */ - RepeatIter<_repeat_namespace_item, namespace_item> itemIter( itemList ); + RepeatIter itemIter( itemList ); while ( !itemIter.end() ) { walkNamespaceItem( itemIter.value(), stmtList ); itemIter.next(); @@ -2867,7 +2867,7 @@ struct LoadColm StmtList *stmtList = new StmtList; /* Walk the list of items. */ - RepeatIter<_repeat_root_item, root_item> rootItemIter( rootItemList ); + RepeatIter rootItemIter( rootItemList ); while ( !rootItemIter.end() ) { walkRootItem( rootItemIter.value(), stmtList ); rootItemIter.next(); diff --git a/colm/loadinit.cc b/colm/loadinit.cc index 027a1018..65e7e062 100644 --- a/colm/loadinit.cc +++ b/colm/loadinit.cc @@ -394,7 +394,7 @@ void LoadInit::go( long activeRealm ) /* Walk the list of items. */ _repeat_item ItemList = Start.ItemList(); - RepeatIter<_repeat_item, item> itemIter( ItemList ); + RepeatIter itemIter( ItemList ); while ( !itemIter.end() ) { item Item = itemIter.value(); diff --git a/test/rlparse.d/load.cc b/test/rlparse.d/load.cc index 3d6e297b..234c3f7a 100644 --- a/test/rlparse.d/load.cc +++ b/test/rlparse.d/load.cc @@ -319,7 +319,7 @@ struct LoadRagel { ruby_inline::_repeat_block_item BlockItemList = RubyInlineBlock._repeat_block_item(); - RepeatIter BlockItemIter( BlockItemList ); + RepeatIter BlockItemIter( BlockItemList ); while ( !BlockItemIter.end() ) { loadBlockItem( inlineList, BlockItemIter.value() ); BlockItemIter.next(); @@ -364,7 +364,7 @@ struct LoadRagel { InlineList *inlineList = new InlineList; ruby_inline::_repeat_expr_item ExprItemList = InlineExpr._repeat_expr_item(); - RepeatIter ExprItemIter( ExprItemList ); + RepeatIter ExprItemIter( ExprItemList ); while ( !ExprItemIter.end() ) { InlineItem *inlineItem = loadExprItem( ExprItemIter.value() ); inlineList->append( inlineItem ); @@ -533,7 +533,7 @@ struct LoadRagel { ocaml_inline::_repeat_block_item BlockItemList = OCamlInlineBlock._repeat_block_item(); - RepeatIter BlockItemIter( BlockItemList ); + RepeatIter BlockItemIter( BlockItemList ); while ( !BlockItemIter.end() ) { loadBlockItem( inlineList, BlockItemIter.value() ); BlockItemIter.next(); @@ -579,7 +579,7 @@ struct LoadRagel InlineList *inlineList = new InlineList; ocaml_inline::_repeat_expr_item ExprItemList = InlineExpr._repeat_expr_item(); - RepeatIter ExprItemIter( ExprItemList ); + RepeatIter ExprItemIter( ExprItemList ); while ( !ExprItemIter.end() ) { InlineItem *inlineItem = loadExprItem( ExprItemIter.value() ); inlineList->append( inlineItem ); @@ -748,7 +748,7 @@ struct LoadRagel { crack_inline::_repeat_block_item BlockItemList = CrackInlineBlock._repeat_block_item(); - RepeatIter BlockItemIter( BlockItemList ); + RepeatIter BlockItemIter( BlockItemList ); while ( !BlockItemIter.end() ) { loadBlockItem( inlineList, BlockItemIter.value() ); BlockItemIter.next(); @@ -794,7 +794,7 @@ struct LoadRagel InlineList *inlineList = new InlineList; crack_inline::_repeat_expr_item ExprItemList = InlineExpr._repeat_expr_item(); - RepeatIter ExprItemIter( ExprItemList ); + RepeatIter ExprItemIter( ExprItemList ); while ( !ExprItemIter.end() ) { InlineItem *inlineItem = loadExprItem( ExprItemIter.value() ); inlineList->append( inlineItem ); @@ -2298,7 +2298,7 @@ struct LoadRagel inputItem->writeArgs.push_back( Cmd.text() ); - RepeatIter WordIter( WordList ); + RepeatIter WordIter( WordList ); while ( !WordIter.end() ) { inputItem->writeArgs.push_back( WordIter.value().text() ); WordIter.next(); @@ -2472,7 +2472,7 @@ struct LoadRagel void loadImportList( _repeat_import ImportList ) { - RepeatIter<_repeat_import, import> ImportIter( ImportList ); + RepeatIter ImportIter( ImportList ); while ( !ImportIter.end() ) { loadImport( ImportIter.value() ); ImportIter.next(); @@ -2602,7 +2602,7 @@ struct LoadRagel ragel::_repeat_statement StmtList = RagelStart._repeat_statement(); - RepeatIter StmtIter( StmtList ); + RepeatIter StmtIter( StmtList ); while ( !StmtIter.end() ) { bool stop = loadStatement( StmtIter.value() ); if ( stop ) @@ -2706,7 +2706,7 @@ struct LoadRagel c_host::_repeat_section SectionList = Start.SectionList(); if ( SectionList ) { - RepeatIter SectionIter( SectionList ); + RepeatIter SectionIter( SectionList ); while ( !SectionIter.end() ) { loadSection( SectionIter.value(), targetMachine, searchMachine ); SectionIter.next(); @@ -2715,7 +2715,7 @@ struct LoadRagel else if ( Start.RSectionList() ) { ruby_host::_repeat_section RSectionList = Start.RSectionList(); if ( RSectionList ) { - RepeatIter RSectionIter( RSectionList ); + RepeatIter RSectionIter( RSectionList ); while ( !RSectionIter.end() ) { loadSection( RSectionIter.value(), targetMachine, searchMachine ); RSectionIter.next(); @@ -2725,7 +2725,7 @@ struct LoadRagel else { ocaml_host::_repeat_section OSectionList = Start.OSectionList(); if ( OSectionList ) { - RepeatIter OSectionIter( OSectionList ); + RepeatIter OSectionIter( OSectionList ); while ( !OSectionIter.end() ) { loadSection( OSectionIter.value(), targetMachine, searchMachine ); OSectionIter.next(); diff --git a/test/rlparse.d/reducer.cc b/test/rlparse.d/reducer.cc index b3bd32ba..21bee96e 100644 --- a/test/rlparse.d/reducer.cc +++ b/test/rlparse.d/reducer.cc @@ -248,7 +248,7 @@ void TopLevel::loadImport( std::string fileName ) return; } - RepeatIter<_repeat_import, import> ImportIter( ImportList ); + RepeatIter ImportIter( ImportList ); while ( !ImportIter.end() ) { import Import = ImportIter.value(); -- cgit v1.2.1