From 3f74fc17cee81774ddcc29c0a3560f81f7b60b7d Mon Sep 17 00:00:00 2001 From: Adrian Thurston Date: Thu, 27 Jul 2017 12:11:13 -0400 Subject: added a read_reduce statement to call postfix reducer Triggers the generation of the reducer. Accepts a stream. --- src/bytecode.c | 17 +++++++++++++++++ src/bytecode.h | 1 + src/colm.lm | 2 ++ src/consinit.cc | 2 +- src/loadcolm.cc | 22 ++++++++++++++++++---- src/loadinit.cc | 2 +- src/parser.cc | 8 ++++---- src/parser.h | 2 +- src/parsetree.h | 6 +++++- src/pdacodegen.cc | 2 +- src/print.c | 3 +++ src/program.h | 2 +- src/reduce.cc | 14 +++++++++----- src/synthesis.cc | 37 +++++++++++++++++++++++++++++++++++-- 14 files changed, 99 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/bytecode.c b/src/bytecode.c index ee44b6ef..0aea71ef 100644 --- a/src/bytecode.c +++ b/src/bytecode.c @@ -762,6 +762,23 @@ again: break; } + case IN_READ_REDUCE: { + half_t generic_id; + half_t reducer_id; + read_half( generic_id ); + read_half( reducer_id ); + + stream_t *stream = vm_pop_stream(); + + debug( prg, REALM_BYTECODE, "IN_READ_REDUCE %hd %hd\n", generic_id, reducer_id ); + + prg->rtd->read_reduce( prg, reducer_id, stream ); + + vm_push_tree( 0 ); + + break; + } + /* * LOAD_GLOBAL */ diff --git a/src/bytecode.h b/src/bytecode.h index 7e76a824..3d47bd8b 100644 --- a/src/bytecode.h +++ b/src/bytecode.h @@ -209,6 +209,7 @@ typedef unsigned char uchar; #define IN_GET_VLIST_MEM_BKT 0x5c #define IN_CONS_REDUCER 0x76 +#define IN_READ_REDUCE 0x69 #define IN_DONE 0x78 diff --git a/src/colm.lm b/src/colm.lm index ca8a9ab9..a1a237fd 100644 --- a/src/colm.lm +++ b/src/colm.lm @@ -13,6 +13,7 @@ lex token PRINT_DUMP / 'print_dump' / token PARSE / 'parse' / token REDUCE / 'reduce' / + token READ_REDUCE /'read_reduce'/ token PARSE_TREE / 'parse_tree' / token PARSE_STOP / 'parse_stop' / token CONS / 'construct' | 'cons' / @@ -634,6 +635,7 @@ def code_factor | [PARSE_TREE opt_capture type_ref opt_field_init accumulate] :ParseTree | [PARSE_STOP opt_capture type_ref opt_field_init accumulate] :ParseStop | [REDUCE id type_ref opt_field_init accumulate] :Reduce +| [READ_REDUCE id type_ref opt_field_init accumulate] :ReadReduce | [CONS opt_capture type_ref opt_field_init constructor] :Cons | [MATCH var_ref pattern] :Match | [string] :String diff --git a/src/consinit.cc b/src/consinit.cc index 60e2639c..fe40f5d5 100644 --- a/src/consinit.cc +++ b/src/consinit.cc @@ -799,7 +799,7 @@ void ConsInit::parseInput( StmtList *stmtList ) /* Parse the above list. */ LangExpr *parseExpr = parseCmd( internal, false, false, objField, - typeRef, 0, list, true, false, "" ); + typeRef, 0, list, true, false, false, "" ); LangStmt *parseStmt = LangStmt::cons( internal, LangStmt::ExprType, parseExpr ); stmtList->append( parseStmt ); } diff --git a/src/loadcolm.cc b/src/loadcolm.cc index cd2200f5..d49a5177 100644 --- a/src/loadcolm.cc +++ b/src/loadcolm.cc @@ -1800,7 +1800,7 @@ struct LoadColm ConsItemList *list = walkAccumulate( codeFactor.accumulate() ); expr = parseCmd( codeFactor.PARSE().loc(), false, false, objField, - typeRef, init, list, used, false, "" ); + typeRef, init, list, used, false, false, "" ); break; } case code_factor::ParseTree: { @@ -1812,7 +1812,7 @@ struct LoadColm ConsItemList *list = walkAccumulate( codeFactor.accumulate() ); expr = parseCmd( codeFactor.PARSE_TREE().loc(), true, false, objField, - typeRef, init, list, used, false, "" ); + typeRef, init, list, used, false, false, "" ); break; } case code_factor::ParseStop: { @@ -1824,7 +1824,7 @@ struct LoadColm ConsItemList *list = walkAccumulate( codeFactor.accumulate() ); expr = parseCmd( codeFactor.PARSE_STOP().loc(), false, true, objField, - typeRef, init, list, used, false, "" ); + typeRef, init, list, used, false, false, "" ); break; } case code_factor::Reduce: { @@ -1838,7 +1838,21 @@ struct LoadColm ConsItemList *list = walkAccumulate( codeFactor.accumulate() ); expr = parseCmd( codeFactor.REDUCE().loc(), false, false, 0, - typeRef, init, list, used, true, reducer ); + typeRef, init, list, used, true, false, reducer ); + break; + } + case code_factor::ReadReduce: { + /* The reducer name. */ + String reducer = codeFactor.id().data(); + + /* The type we are parsing. */ + type_ref typeRefTree = codeFactor.type_ref(); + TypeRef *typeRef = walkTypeRef( typeRefTree ); + FieldInitVect *init = walkOptFieldInit( codeFactor.opt_field_init() ); + ConsItemList *list = walkAccumulate( codeFactor.accumulate() ); + + expr = parseCmd( codeFactor.READ_REDUCE().loc(), false, false, 0, + typeRef, init, list, used, true, true, reducer ); break; } case code_factor::Cons: { diff --git a/src/loadinit.cc b/src/loadinit.cc index 9b5d8658..ee4fb4a6 100644 --- a/src/loadinit.cc +++ b/src/loadinit.cc @@ -331,7 +331,7 @@ void LoadInit::consParseStmt( StmtList *stmtList ) /* Parse the above list. */ LangExpr *parseExpr = parseCmd( internal, false, false, objField, - typeRef, 0, list, true, false, "" ); + typeRef, 0, list, true, false, false, "" ); LangStmt *parseStmt = LangStmt::cons( internal, LangStmt::ExprType, parseExpr ); stmtList->append( parseStmt ); } diff --git a/src/parser.cc b/src/parser.cc index 2ff0e98e..07dd57ae 100644 --- a/src/parser.cc +++ b/src/parser.cc @@ -691,7 +691,7 @@ LangExpr *BaseParser::send( const InputLoc &loc, LangVarRef *varRef, ConsItemList *list, bool eof ) { ParserText *parserText = ParserText::cons( loc, - curNspace(), list, true, false, "" ); + curNspace(), list, true, false, false, "" ); pd->parserTextList.append( parserText ); return LangExpr::cons( LangTerm::consSend( loc, varRef, @@ -702,7 +702,7 @@ LangExpr *BaseParser::sendTree( const InputLoc &loc, LangVarRef *varRef, ConsItemList *list, bool eof ) { ParserText *parserText = ParserText::cons( loc, - curNspace(), list, true, false, "" ); + curNspace(), list, true, false, false, "" ); pd->parserTextList.append( parserText ); return LangExpr::cons( LangTerm::consSendTree( loc, varRef, @@ -711,7 +711,7 @@ LangExpr *BaseParser::sendTree( const InputLoc &loc, LangVarRef *varRef, LangExpr *BaseParser::parseCmd( const InputLoc &loc, bool tree, bool stop, ObjectField *objField, TypeRef *typeRef, FieldInitVect *fieldInitVect, - ConsItemList *list, bool used, bool reduce, const String &reducer ) + ConsItemList *list, bool used, bool reduce, bool read, const String &reducer ) { LangExpr *expr = 0; @@ -733,7 +733,7 @@ LangExpr *BaseParser::parseCmd( const InputLoc &loc, bool tree, bool stop, used = true; ParserText *parserText = ParserText::cons( loc, curNspace(), - list, used, reduce, reducer ); + list, used, reduce, read, reducer ); pd->parserTextList.append( parserText ); LangTerm::Type langTermType = stop ? LangTerm::ParseStopType : ( tree ? diff --git a/src/parser.h b/src/parser.h index 40dc777c..0b02e447 100644 --- a/src/parser.h +++ b/src/parser.h @@ -127,7 +127,7 @@ struct BaseParser ConsItemList *list, bool eof ); LangExpr *parseCmd( const InputLoc &loc, bool tree, bool stop, ObjectField *objField, TypeRef *typeRef, FieldInitVect *fieldInitVect, ConsItemList *list, - bool used, bool reduce, const String &reducer ); + bool used, bool reduce, bool read, const String &reducer ); PatternItemList *consPatternEl( LangVarRef *varRef, PatternItemList *list ); PatternItemList *patternElNamed( const InputLoc &loc, LangVarRef *varRef, NamespaceQual *nspaceQual, const String &data, RepeatType repeatType ); diff --git a/src/parsetree.h b/src/parsetree.h index 4ebc8201..e8225fa1 100644 --- a/src/parsetree.h +++ b/src/parsetree.h @@ -1866,7 +1866,8 @@ struct ParserText { static ParserText *cons( const InputLoc &loc, Namespace *nspace, ConsItemList *list, - bool used, bool reduce, const String &reducer ) + bool used, bool reduce, bool read, + const String &reducer ) { ParserText *p = new ParserText; p->loc = loc; @@ -1878,6 +1879,7 @@ struct ParserText p->parse = true; p->used = used; p->reduce = reduce; + p->read = read; p->reducer = reducer; p->reducerId = -1; return p; @@ -1892,6 +1894,7 @@ struct ParserText bool parse; bool used; bool reduce; + bool read; String reducer; int reducerId; @@ -3094,6 +3097,7 @@ struct LangTerm UniqueType *evaluateConstruct( Compiler *pd, CodeVect &code ) const; void parseFrag( Compiler *pd, CodeVect &code, int stopId ) const; UniqueType *evaluateParse( Compiler *pd, CodeVect &code, bool tree, bool stop ) const; + UniqueType *evaluateReadReduce( Compiler *pd, CodeVect &code ) const; void evaluateSendStream( Compiler *pd, CodeVect &code ) const; void evaluateSendParser( Compiler *pd, CodeVect &code, bool strings ) const; UniqueType *evaluateSend( Compiler *pd, CodeVect &code ) const; diff --git a/src/pdacodegen.cc b/src/pdacodegen.cc index 2a088f89..c275ab42 100644 --- a/src/pdacodegen.cc +++ b/src/pdacodegen.cc @@ -450,7 +450,7 @@ void PdaCodeGen::writeRuntimeData( colm_sections *runtimeData, struct pda_tables "struct pda_run *pda_run, int id );\n" "int " << objectName << "_reducer_need_ign( program_t *prg, " "struct pda_run *pda_run );\n" - "void " << objectName << "_read_reduce( program_t *prg, int reducer );\n" + "void " << objectName << "_read_reduce( program_t *prg, int reducer, stream_t *stream );\n" "\n"; out << diff --git a/src/print.c b/src/print.c index ad5eefb4..f1c8a058 100644 --- a/src/print.c +++ b/src/print.c @@ -740,5 +740,8 @@ void colm_postfix_tree_file( program_t *prg, tree_t **sp, struct stream_impl *im }; colm_print_tree_args( prg, sp, &print_args, tree ); + + //struct stream_impl *impl = (struct stream_impl*) args->arg; + fflush( impl->file ); } diff --git a/src/program.h b/src/program.h index e3f747e0..3a7400bf 100644 --- a/src/program.h +++ b/src/program.h @@ -112,7 +112,7 @@ struct colm_sections void (*init_need)(); int (*reducer_need_tok)( program_t *prg, struct pda_run *pda_run, int id ); int (*reducer_need_ign)( program_t *prg, struct pda_run *pda_run ); - void (*read_reduce)( program_t *prg, int reducer ); + void (*read_reduce)( program_t *prg, int reducer, stream_t *stream ); }; struct heap_list diff --git a/src/reduce.cc b/src/reduce.cc index 4b6fa230..01f42d0f 100644 --- a/src/reduce.cc +++ b/src/reduce.cc @@ -43,7 +43,7 @@ void Compiler::writeCommitStub() "int " << objectName << "_reducer_need_ign( program_t *prg, " "struct pda_run *pda_run ) { return COLM_RN_BOTH; }\n" "\n" - "void " << objectName << "_read_reduce( program_t *prg, int reducer ) {}\n" + "void " << objectName << "_read_reduce( program_t *prg, int reducer, stream_t *stream ) {}\n" ; } @@ -438,6 +438,9 @@ void Compiler::writeCommit() "#include \n" "\n" "#include \n" + "#include \n" + "#include \n" + "\n" "using std::endl;\n" "\n" "#include \"reducer.h\"\n" @@ -626,7 +629,7 @@ void Compiler::writeCommit() /* READ REDUCE */ *outStream << - "extern \"C\" void " << objectName << "_read_reduce( program_t *prg, int reducer )\n" + "extern \"C\" void " << objectName << "_read_reduce( program_t *prg, int reducer, stream_t *stream )\n" "{\n" " switch ( reducer ) {\n"; @@ -634,7 +637,7 @@ void Compiler::writeCommit() Reduction *reduction = *r; *outStream << " case " << reduction->id << ":\n" - " ((" << reduction->name << "*)prg->red_ctx)->read_reduce_forward( prg );\n" + " ((" << reduction->name << "*)prg->red_ctx)->read_reduce_forward( prg, stream->impl->file );\n" " break;\n"; } @@ -685,9 +688,10 @@ void Compiler::writeCommit() " *dest = 0;\n" "}\n" "\n" - "void " << reduction->name << "::read_reduce_forward( program_t *prg )\n" + "void " << reduction->name << "::read_reduce_forward( program_t *prg, FILE *file )\n" "{\n" - " std::ifstream in( \"postfix.txt\" );\n" + " __gnu_cxx::stdio_filebuf fbuf( file, std::ios::in|std::ios::out|std::ios::app );\n" + " std::iostream in( &fbuf );\n" " std::string type, tok, text;\n" " long _id, line, column, byte, prod_num, children;\n" " read_reduce_node sentinal;\n" diff --git a/src/synthesis.cc b/src/synthesis.cc index c839a9cb..aa140794 100644 --- a/src/synthesis.cc +++ b/src/synthesis.cc @@ -1404,9 +1404,42 @@ void LangTerm::parseFrag( Compiler *pd, CodeVect &code, int stopId ) const } } +UniqueType *LangTerm::evaluateReadReduce( Compiler *pd, CodeVect &code ) const +{ + UniqueType *parserUT = typeRef->uniqueType; + UniqueType *targetUT = parserUT->generic->elUt; + + /* Should be one arg and it should be a stream. */ + + /* Assign bind ids to the variables in the replacement. */ + for ( ConsItemList::Iter item = *parserText->list; item.lte(); item++ ) { + switch ( item->type ) { + case ConsItem::LiteralType: { + break; + } + case ConsItem::InputText: { + break; + } + case ConsItem::ExprType: { + item->expr->evaluate( pd, code ); + break; + }} + } + + code.append( IN_READ_REDUCE ); + code.appendHalf( parserUT->generic->id ); + code.appendHalf( parserText->reducerId ); + + return targetUT; +} + UniqueType *LangTerm::evaluateParse( Compiler *pd, CodeVect &code, bool tree, bool stop ) const { + if ( parserText->reduce && parserText->read ) { + return evaluateReadReduce( pd, code ); + } + UniqueType *parserUT = typeRef->uniqueType; UniqueType *targetUT = parserUT->generic->elUt; @@ -1493,7 +1526,7 @@ UniqueType *LangTerm::evaluateParse( Compiler *pd, CodeVect &code, code.appendWord( mapEl->value ); break; } - case ConsItem::ExprType: + case ConsItem::ExprType: { UniqueType *ut = item->expr->evaluate( pd, code ); if ( ut->typeId == TYPE_TREE && ut->langEl == pd->voidLangEl ) { @@ -1515,7 +1548,7 @@ UniqueType *LangTerm::evaluateParse( Compiler *pd, CodeVect &code, code.append( IN_TREE_TO_STR_TRIM ); } break; - } + }} if ( isStream ) { if ( pd->revertOn ) -- cgit v1.2.1