void Pipe::_releaseViews() { LB_TS_THREAD( _pipeThread ); for( bool changed = true; changed; ) { changed = false; for( ViewHashIter i =_impl->views.begin(); i !=_impl->views.end(); ++i ) { View* view = i->second; view->commit(); if( view->getVersion() + 20 > view->getHeadVersion( )) continue; // release unused view to avoid memory leaks due to deltas piling up view->_pipe = 0; ClientPtr client = getClient(); client->unmapObject( view ); _impl->views.erase( i ); NodeFactory* nodeFactory = Global::getNodeFactory(); nodeFactory->releaseView( view ); changed = true; break; } } }
Node getArgs() const { Node args = this->lhs->getChild( 1 ); if ( args->hasName( SEQ ) ) return args; NodeFactory b; b.start( SEQ ); b.add( args ); b.end(); return b.build(); }
static void predicate( TokType sense, NodeFactory & ifunless, Node pred ) { if ( sense == tokty_if ) { ifunless.add( pred ); } else { // TODO: This is a bug? ifunless.start( "sysval" ); ifunless.put( "name", "not" ); ifunless.add( pred ); ifunless.end(); } }
Node ReadStateClass::readCompoundCore() { if ( this->cstyle_mode ) { NodeFactory stmnts; stmnts.start( SEQ ); while ( this->tryPeekToken( tokty_semi ) || not this->tryPeekCloser() ) { Node n = this->readSingleStmnt(); stmnts.add( n ); } stmnts.end(); return stmnts.build(); } else { return this->readStmnts(); } }
bool check_create() { NodeFactory factory; internals::loadInternals(factory); Node::ptr metro0 = factory.create("base.metro"); Node::ptr print0 = factory.create("base.print"); Node::ptr counter0 = factory.create("base.counter"); if (metro0.get() == 0) { std::cout << "Metro ptr is null" << std::endl; return false; } return true; }
Node ReadStateClass::readSingleStmnt( const bool top_level ) { if ( this->cstyle_mode ) { Item it = this->item_factory->read(); //cerr << "SINGLE " << tok_type_name( it->tok_type ) << endl; switch ( it->tok_type ) { case tokty_obrace: return this->readCompoundCoreCheck( tokty_cbrace ); case tokty_function: if ( this->cstyle_mode && this->item_factory->peek()->tok_type == tokty_oparen ) { return this->readLambda(); } else { return this->readDefinition(); } case tokty_if: return this->readIf( tokty_if, tokty_endif ); case tokty_for: return this->readFor(); case tokty_switch: return this->readSwitch(); case tokty_recordclass: return this->readRecordClass(); case tokty_dsemi: case tokty_semi: return makeEmpty(); default: this->item_factory->unread(); // --- Fall-through --- // } } // Fall thru! Node n = this->readOptEmptyExpr(); if ( this->tryToken( tokty_dsemi ) ) { NodeFactory f; f.start( ERASE ); f.add( n ); f.end(); return f.build(); } else if ( this->tryToken( tokty_semi ) ) { return n; } else { if ( this->item_factory->peek()->role.IsCloser() && not top_level ) { return n; } else { throw CompileTimeError( "Missing semi-colon?" ).culprit( "Token", this->item_factory->peek()->nameString() ); } } }
void Pipe::_flushViews() { LB_TS_THREAD( _pipeThread ); NodeFactory* nodeFactory = Global::getNodeFactory(); ClientPtr client = getClient(); for( ViewHashCIter i = _impl->views.begin(); i != _impl->views.end(); ++i ) { View* view = i->second; client->unmapObject( view ); view->_pipe = 0; nodeFactory->releaseView( view ); } _impl->views.clear(); }
std::pair<std::shared_ptr<Node>, boost::optional<Status>> createNode(const Status& originalStatus, Point p, Point d, const std::shared_ptr<Node>& base, NodeFactory& nodeFactory, const HeurCalculator& heurCalculator, const Checker& checker, Dumper* dumper) { Point pd = p+d, pmd = p-d; if (originalStatus.value(pd) != FieldType::floor || !originalStatus.reachable(pmd)) { return {}; } Status status(originalStatus); status.currentPos(p); if (heurCalculator.calculateStone(status, pd) < 0 || !status.moveStone(p, pd)) { return {}; } auto doCreateNode = [&]() { return nodeFactory.createNode( status, MoveDescriptor(p, d), base); }; if (pd != status.table().destination()) { if (!checker.check(status, pd)) { if (dumper) { dumper->reject(doCreateNode(), checker.errorMessage()); } return {}; } } return {doCreateNode(), std::move(status)}; }
Node ReadStateClass::readSwitchStmnts() { NodeFactory st; st.start( SEQ ); while ( not ( this->tryPeekToken( tokty_cbrace ) || this->tryPeekToken( tokty_case ) || this->tryPeekToken( tokty_default ) ) ) { Node x = this->readSingleStmnt(); st.add( x ); } st.end(); return st.build(); }
Node ReadStateClass::readEnvVar() { this->checkToken( tokty_obrace ); NodeFactory envvar; envvar.start( SYSAPP ); envvar.put( "name", "sysGetEnv" ); envvar.start( "constant" ); envvar.put( "type", "string" ); envvar.put( "value", this->item_factory->read()->nameString() ); envvar.end(); this->checkToken( tokty_cbrace ); envvar.end(); return envvar.build(); }
bool Pipe::_cmdDetachView( co::ICommand& cmd ) { co::ObjectICommand command( cmd ); LB_TS_THREAD( _pipeThread ); ViewHash::iterator i = _impl->views.find( command.get< uint128_t >( )); if( i != _impl->views.end( )) { View* view = i->second; _impl->views.erase( i ); NodeFactory* nodeFactory = Global::getNodeFactory(); nodeFactory->releaseView( view ); } return true; }
Node ReadStateClass::readImport() { NodeFactory imp; imp.start( "import" ); bool pervasive, qualified; readImportQualifiers( *this, pervasive, qualified ); imp.put( "pervasive", pervasive ? "true" : "false" ); imp.put( "qualified", qualified ? "true" : "false" ); readImportMatch( *this, imp ); this->checkToken( tokty_from ); string url = this->readPkgName(); imp.put( "from", url ); if ( this->tryName( "alias" ) ) { imp.put( "alias", this->readIdName() ); } if ( this->tryName( "into" ) ) { readImportInto( *this, imp ); } imp.end(); return imp.build(); }
Node ReadStateClass::readAnon( const std::string name ) { NodeFactory anon; if ( this->pattern_mode ) { anon.start( VAR ); } else { anon.start( CONSTANT ); anon.put( CONSTANT_TYPE, "absent" ); anon.put( CONSTANT_VALUE, "absent" ); anon.put( CONSTANT_WAS_ANON, name ); } anon.put( COMMENT, name ); anon.end(); return anon.build(); }
Node ReadStateClass::readQueryPrec( const int prec ) { Node e = this->readExprPrec( prec ); const std::string name = e->name(); if ( name == BIND || name == IN || name == FROM || name == WHERE || name == WHILE || name == ZIP || name == CROSS || name == OK || name == FINALLY || name == DO ) { return e; } else { NodeFactory where; where.start( WHERE ); where.start( OK ); where.end(); where.add( e ); where.end(); return where.build(); } }
void squash( NodeFactory acc, Node rhs ) { const std::string name = rhs->name(); if ( name == SEQ ) { int n = rhs->size(); for ( int i = 0; i < n; i++ ) { squash( acc, rhs->getChild( i ) ); } } else { acc.add( rhs ); } }
static void readTags( ReadStateClass & r, NodeFactory & imp, const char * prefix, const bool add_default ) { if ( r.tryToken( tokty_oparen ) ) { ItemFactory ifact = r.item_factory; if ( ifact->peek()->tok_type != tokty_cparen ) { for ( int i = 0; true; i++ ) { Item item = ifact->read(); ostringstream s; s << prefix << i; imp.put( s.str(), item->nameString() ); item = ifact->read(); if ( item->tok_type != tokty_comma ) { if ( item->tok_type != tokty_cparen ) throw CompileTimeError( "Expecting close parenthesis" ); break; } } } } else if ( add_default ) { imp.put( prefix, "public" ); } }
Node canonise( const int level, Node fn ) { const bool is_var = fn->hasName( VAR ); if ( is_var ) { if ( level == 0 ) { if ( this->name_needed ) this->badHeader(); return this->anon( fn ); } else if ( this->assume_no_name ) { return this->anon( fn ); } else { return fn; } } else if ( fn->hasName( SEQ ) && not this->name_needed ) { return this->anon( fn ); } else if ( fn->hasName( APP ) && fn->size() == 2 ) { NodeFactory b; b.start( APP ); Node n = this->canonise( level + 1, fn->getChild( 0 ) ); b.add( n ); b.start( SEQ ); squash( b, fn->getChild( 1 ) ); b.end(); b.end(); return b.build(); } else { throw this->badHeader(); } }
View* Pipe::getView( const co::ObjectVersion& viewVersion ) { LB_TS_THREAD( _pipeThread ); if( viewVersion.identifier == 0 ) return 0; View* view = _impl->views[ viewVersion.identifier ]; if( !view ) { NodeFactory* nodeFactory = Global::getNodeFactory(); view = nodeFactory->createView( 0 ); LBASSERT( view ); view->_pipe = this; ClientPtr client = getClient(); LBCHECK( client->mapObject( view, viewVersion )); _impl->views[ viewVersion.identifier ] = view; } view->sync( viewVersion.version ); return view; }
static Node makeSymbol( const std::string & name ) { NodeFactory sym; sym.start( "constant" ); sym.put( "type", "symbol" ); sym.put( "value", name ); sym.end(); return sym.build(); }
static Node makeIndex( Node lhs, Node rhs ) { NodeFactory index; index.start( SYSAPP ); index.put( SYSAPP_NAME, "index" ); index.add( rhs ); index.add( lhs ); index.end(); return index.build(); }
Node ReadStateClass::readMap( TokType closer ) { NodeFactory list; list.start( SYSAPP ); list.put( SYSAPP_NAME, "newMap" ); Node x = this->readStmntsCheck( closer ); list.add( x ); list.end(); return list.build(); }
Node ReadStateClass::readReturn() { NodeFactory ret; ret.start( ASSERT ); ret.put( ASSERT_TAILCALL, "true" ); Node n = this->readExpr(); ret.add( n ); ret.end(); return ret.build(); }
Node ReadStateClass::readOptExprPrec( int prec ) { ItemFactory ifact = this->item_factory; Node e = this->prefixProcessing(); if ( not e ) return Node(); //cerr << "starting postfix checking ... " << this->isPostfixAllowed() << endl; while ( this->isPostfixAllowed() ) { int q; //cerr << "peeking" << endl; Item it = ifact->peek(); if ( it->item_is_signed_num() ) { NodeFactory t; t.start( SYSAPP ); t.put( "name", "+" ); t.add( e ); if ( it->tok_type == tokty_int ) { t.start( "constant" ); t.put( "type", "int" ); t.put( "value", it->nameString() ); t.end(); } else { throw CompileTimeError( "Only integers supported so far" ).culprit( "Item", it->nameString() ); } t.end(); e = t.build(); ifact->drop(); } else if ( it->item_is_postfix() ) { q = it->precedence; if ( q >= prec ) break; ifact->drop(); e = this->postfixProcessing( e, it, q ); } else { break; } } return e; }
Node ReadStateClass::readPackage() { NodeFactory pkg; pkg.start( "package" ); string url = this->readPkgName(); pkg.put( "url", url ); this->checkToken( tokty_semi ); Node body = this->readStmntsCheck( tokty_endpackage ); pkg.add( body ); pkg.end(); return pkg.build(); }
Node anon( Node fn ) { NodeFactory b; b.start( APP ); b.start( VAR ); // But no name - anonymous. b.end(); b.start( SEQ ); squash( b, fn ); b.end(); b.end(); return b.build(); }
static Node makeCharSequence( Item item ) { int n = item->nameString().size(); if ( n == 0 ) { return makeEmpty(); } else { NodeFactory charseq; charseq.start( SEQ ); const std::string & s = item->nameString(); std::string::const_iterator iter = s.begin(); for ( iter = s.begin(); iter != s.end(); ++iter ) { charseq.start( CONSTANT ); charseq.put( CONSTANT_TYPE, "char" ); charseq.put( CONSTANT_VALUE, std::string() + *iter ); // WRONG charseq.end(); } charseq.end(); return charseq.build(); } }
Node ReadStateClass::readThrow() { NodeFactory panic; panic.start( "throw" ); panic.put( "event", this->readIdName() ); panic.put( "level", this->tryToken( tokty_bang ) ? "rollback" : this->tryToken( tokty_dbang ) ? "failover" : this->tryToken( tokty_panic ) ? "panic" : "escape" ); if ( this->tryPeekToken( tokty_oparen ) ) { Node e = this->readExpr(); panic.add( e ); } else { panic.start( SEQ ); panic.end(); } panic.end(); return panic.build(); }
void uncurry() { #ifdef DBG_COMMON2GNX cerr << "At start of uncurrying" << endl; cerr << " LHS: "; this->lhs->render( cerr ); cerr << endl; cerr << " RHS: "; this->rhs->render( cerr ); cerr << endl; #endif while ( this->isCurryd() ) { NodeFactory b; b.start( FN ); Node arg = this->lhs->getChild( 1 ); if ( arg->hasName( SEQ ) ) { b.add( arg ); } else { b.start( SEQ ); b.add( arg ); b.end(); } b.add( this->rhs ); b.end(); this->rhs = b.build(); this->lhs = this->lhs->getChild( 0 ); #ifdef DBG_COMMON2GNX cerr << "Uncurried" << endl; cerr << " LHS: "; this->lhs->render( cerr ); cerr << endl; cerr << " RHS: "; this->rhs->render( cerr ); cerr << endl; #endif } }
Node ReadStateClass::readVarVal( TokType fnc ) { NodeFactory bind; bind.start( BIND ); Node lhs = this->readExprPrec( prec_assign ); updateAsPattern( lhs, fnc == tokty_val ); bind.add( lhs ); this->checkToken( this->cstyle_mode ? tokty_equal : tokty_bind ); Node x = this->readExpr(); bind.add( x ); bind.end(); return bind.build(); }
Node ReadStateClass::readLambda() { #ifdef DBG_COMMON2GNX cerr << "LAMBDA" << endl; #endif Node ap = this->readCanonicalLambdaLHS( false ); #ifdef DBG_COMMON2GNX cerr << "Now check =>> is next token" << endl; #endif if ( not this->cstyle_mode ) this->checkToken( tokty_fnarrow ); Node body = this->readCompoundStmnts(); if ( not this->cstyle_mode ) this->checkToken( tokty_endfn ); Uncurry components( ap, body ); components.uncurry(); Node fun = components.getFun(); NodeFactory fn; fn.start( FN ); if ( fun && fun->hasAttribute( "name" ) ) { fn.put( FN_NAME, fun->attribute( "name" ) ); } { Node a = components.getArgs(); fn.add( a ); Node b = components.getBody(); fn.add( b ); } fn.end(); Node lambda = fn.build(); #ifdef DBG_COMMON2GNX cerr << "Built lambda: "; lambda->render( cerr ); cerr << endl; #endif return lambda; }