Пример #1
0
/* Bind values to variables in the current scope according to the format of the argument list */
void BindArguments(ArgList arguments, VyObj values[], int num_args){
    int i;
    for(i = 0; i < num_args; i++){
        Param cur = arguments.params[i];
        /* Normal arguments can just be bound right away */
        if(!cur.rest){
            VariableBind(cur.name, values[i]);
        } else {
            VyObj list = Nil();
            int j;
            for(j = num_args - 1; j >= i; j--)
                list = Cons(values[j], list);
            VariableBind(cur.name, list);
            i = num_args;
        }
    }

    /* Finish filling in arguments that weren't passed */
    for(; i < arguments.num_params; i++){
        Param cur = arguments.params[i];
        if(cur.rest)
            VariableBind(cur.name, Nil());
        else {
            if(IsNone(cur.default_value))
                cur.default_value = Nil();
            VariableBind(cur.name, cur.default_value);
        }
    }
}
Пример #2
0
/**
 * Parse out the token stream.
 *
 * @param parser   Not used.
 * @param in       The input token stream.
 * @param elements The elements.
 * @return the element that was parsed.
 */
Element Function_::Parse_( Parser const& /* parser */
                         , TokenStream& in
                         , std::vector<Element> const& elements) const
{
    in.Consume();

    std::shared_ptr<Function_> f(new Function_()); 

    size_t const NUMBER_OF_ARGS = (elements.empty()) ? 0 : elements.size() - 1; 

    std::vector<Variable> parms;
    bool isAVariable  = false;
    for(size_t i=0; i<NUMBER_OF_ARGS; ++i)
    {
        Variable v = CastToVariable(elements[i], isAVariable);
        if (isAVariable)
        {
            parms.push_back(v);
        }
    }
    f->SetArguments(parms);
    
    if (false == elements.empty())
    {
        f->SetBody( elements[elements.size() - 1] );
    }
    else
    {
        f->SetBody(Nil());
    }

    return Function(f, SourceLocation());
}
Пример #3
0
 Element InterpretString_(String str)
 {
     Element out = Nil();
     if (false == str.Value().empty())
     {
         out = String(str.Value().substr(1)); 
     }
     return out;
 }
Пример #4
0
 Element InterpretContainer_(Container container)
 {
     Element out = Nil();
     if (container.NumberOfElements() > 0)
     {
         out = container.Get(0);
     } 
     return out;
 } 
Пример #5
0
 Element InterpretSuperString_(SuperString str)
 {
     Element out = Nil();
     std::string const INPUT(str.Value());
     if (false == INPUT.empty())
     {
         StringHolder temp = str.GetStringHolder();
         out = SuperString(StringHolder(temp, 1, INPUT.size() - 1));
     }
     return out;
 }
Пример #6
0
int EvalArguments(List * arguments, ContextBindings * contextBindings, Term ** error) {
	List result = NULL;
	Pair * current = NULL;
	Term * i = NULL;
	while(i = Iterate(arguments)) {
		Pair * next = AllocatePair();
		next->first = EvalRecursive(i, contextBindings);
		if (terError == next->first->tag) {
			*error = next->first;
			return 0;
		}
		next->second = Nil();
		if (NULL != current) {
			current->second = AllocateTerm(terPair);
			current->second->pair = next;
		}
		current = next;
		if (NULL == result)
			result = current;
	}
	*arguments = result;
	return 1;
}
Пример #7
0
Element Nil_::Lex_(CharacterStream& in) const
{
    in.Consume();
    return Nil(Nil_::instance, SourceLocation());
}
Пример #8
0
/**
 * @return a wrapper around the 1 nil object in the system.
 */
Element Nil_::Interpret_(Environment&)
{
    return Nil(Nil_::instance, SourceLocation());
}
Пример #9
0
 static type call(Sequence& seq)
 {
     return type(
         segmented_begin_impl<Sequence, Nil>::call(seq, Nil()));
 }
Пример #10
0
Element TailRecurse_::Interpret_(Environment& )
{
    return Nil(); 
}
Пример #11
0
/**
 * Interpret the condition, if the condition type is true then we go ahead 
 * and return the interpreted value of the true_result. Otherwise return 
 * false.
 */
Element Case_::Interpret_(Environment& environment)
{
    #ifdef PRINT_STATS
    CALL_COUNT += 1;
    #endif

    if (this->elements.empty())
    {
        return Nil();
    }

    size_t predictedNextIndex = static_cast<size_t>(this->perceptrons.Predict());

    Element result;
    Element conditionResult = this->condition.Interpret(environment);
    bool foundIt = false;

    if (predictedNextIndex < this->elements.size())
    {
        
        CaseElement caseElement = this->elements[predictedNextIndex];
        Element id = caseElement.id.Interpret(environment);    
        if (id.ToString() == conditionResult.ToString())
        {
            result = caseElement.value.Interpret(environment);
            this->perceptrons.Learn(static_cast<unsigned char>(predictedNextIndex));
    
            #ifdef PRINT_STATS
            SUCCESS_COUNT += 1;
            std::cout << "Case_::Interpret(" << SUCCESS_COUNT << "/" << CALL_COUNT << "): " << ((SUCCESS_COUNT / CALL_COUNT) * 100.0) << "% accuracy" << std::endl;
            #endif
            return result;
        } 
    }


    // If our guess was incorrect, go through each value and find the
    // right value.
    size_t const ELEMENTS_SIZE = this->elements.size();
    for(size_t i=0; i<ELEMENTS_SIZE; ++i)
    {
        CaseElement& ce = this->elements[i];
        
        Element id = ce.id.Interpret(environment);
        if (id.ToString() == conditionResult.ToString())
        {
            foundIt = true;
            result = ce.value.Interpret(environment);
            this->perceptrons.Learn((i >= (sizeof(unsigned char) * 8)) ? 0 : static_cast<unsigned char>(i));
            break;
        }
    } 
    if (false == foundIt)
    {
        this->perceptrons.Learn(0);
    }
 
    #ifdef PRINT_STATS
    std::cout << "Case_::Interpret(" << SUCCESS_COUNT << "/" << CALL_COUNT << "): " << ((SUCCESS_COUNT / CALL_COUNT) * 100) << "% accuracy" << std::endl;
    #endif

    return result;
}
Пример #12
0
/**
 * This interpret method is only invoked by a processing element. This is 
 * a way to call back into a builtin operation to allow it to finish processing
 * while other operations can be completed.
 *
 * You should only need to override this IF your operation takes a long time
 * AND can continue asynchronously.
 *
 * @param processing  -- The processing element (contains a key)
 * @param environment -- The environment under which this is processing still.
 */
Element Builtin_::Interpret_( Processing const&
                            , Environment& )
{
    return Nil();
}