Ejemplo n.º 1
0
//the first element in the list is the key into the map
DelimitedSourcePtr
CopyCacheDSMap::get_( ExpressionList & list )
{
    ExpressionListItr elb;
    ExpressionListItr ele;
	char_ptr_list l;
    for( elb=list->begin(), ele=list->end(); elb!=ele ; ++elb )
    {
		l.push_back((*elb)->getCharPtr());
	}
	
	char_ptr keyp = ptr_strdup_ptr_list(l, "|", false);
	const char * key = keyp.get();
    dsmap_t::iterator i=d_->m_.find( key );
    dsmap_t::iterator e=d_->m_.end();
    if( i==e )
    {
		clearStale();
		d_->list_.push_back(keyp);
        DelimitedSourcePtr ds(d_->adsm_->get(list));
        d_->m_.insert( pair< const char *, DelimitedSourcePtr >(key,ds));
		return ds;
    }
	else
	{
    	return (*i).second ;
    }
}
CommandResult ExpressionInterpreter::interpretCommand(string userInput)
{
    CommandResult result = CommandSuccess;
    string text;
    stringstream ss;
    ss << userInput;

    ExpressionList currentState;
    currentState.push_back(mpRoot);
    ExpressionList nextState;

    while (result == CommandSuccess && !ss.eof())
    {
        ss >> text;
        transform(text.begin(), text.end(), text.begin(), ::tolower);

        ExpressionList::const_iterator iter = currentState.begin();
        ExpressionList::const_iterator end = currentState.end();
        for (; iter != end; ++iter)
        {
            Expression* expr = *iter;
            ExpressionList exprNextList = expr->getNextExpressionClosure(text);
            nextState.splice(nextState.end(), exprNextList);
        }

        if (nextState.size() > 0)
        {
            currentState = nextState;
            nextState.clear();
        }
        else
        {
            mErrorText = "'" + text + "' not recognized.";
            result = CommandInvalid;
        }
    }

    //remove impossible expressions in the final state before checking for ambiguity
    nextState.clear();
    ExpressionList::const_iterator iter = currentState.begin();
    ExpressionList::const_iterator end = currentState.end();
    for (; iter != end; ++iter)
    {
        Expression* expr = *iter;
        if (expr->isExecutable())
        {
            nextState.push_back(expr);
        }
        else
        {
            ExpressionList children = expr->getNextExpressions();

            bool flag = false;

            ExpressionList::const_iterator iter = children.begin();
            ExpressionList::const_iterator end = children.end();
            for (; iter != end; ++iter)
            {
                if ((*iter)->getName()[0] == '[')
                {
                    flag = true;
                }
            }

            if (flag || children.size() == 0)
            {
                nextState.push_back(expr);
            }
        }
    }

    currentState = nextState;

    if (currentState.size() != 1)
    {
        mErrorText = "'" + text + "' ambiguous or incomplete.";
        result = CommandInvalid;
    }

    //run command if executable and non-ambiguous
    if (result == CommandSuccess)
    {
        Expression* expr = *(currentState.begin());

        ExpressionList executables = expr->getClosureExecutables(false);
        if (executables.size() == 1)
        {
            ilmErrorTypes initResult = ilm_init();
            if (ILM_SUCCESS != initResult)
            {
                mErrorText = ILM_ERROR_STRING(initResult);
                result = CommandExecutionFailed;
            }
            else
            {
                Expression* exec = executables.front();
                exec->execute();
                ilm_commitChanges();
                ilm_destroy();
            }
        }
        else if (executables.size() == 0)
        {
            mErrorText = "command is incomplete.";
            result = CommandIncomplete;
        }
        else
        {
            mErrorText = "command is ambiguous.";
            result = CommandIncomplete;
        }
    }

    return result;
}