Beispiel #1
0
bool MatchSubList::ArgumentMatches(LispEnvironment& aEnvironment,
                                          LispPtr& aExpression,
                                          LispPtr* arguments) const
{
  if (!aExpression->SubList())
    return false;

  LispIterator iter(aExpression);
  LispObject * pObj = iter.getObj();

  if (!pObj)
      throw LispErrInvalidArg();

  LispPtr * pPtr = pObj->SubList();

  if (!pPtr)
      throw LispErrNotList();

  iter = *pPtr;

  const LispInt iNrMatchers = iMatchers.size();
  for (LispInt i=0;i<iNrMatchers;i++,++iter)
  {
    if (!iter.getObj())
      return false;
    if (!iMatchers[i]->ArgumentMatches(aEnvironment,*iter,arguments))
      return false;
  }
  if (iter.getObj())
    return false;
  return true;
}
Beispiel #2
0
LispObject* operator+(const LispObjectAdder& left, const LispObjectAdder& right)
{
  LispObject* trav = left.iPtr;
  while (!!trav->Nixed())
  {
        trav = trav->Nixed();
  }
  trav->Nixed() = (right.iPtr);
  return left.iPtr;
}
Beispiel #3
0
LispObject LispFunctions::cdr(const VectorOfLispObjects &lispObjects)
{
    LispObject lvalue = lispObjects[0];
    if (lvalue.valuesSize() < 2)
    {
        return LispObject::kNil;
    }
    LispObject result(lvalue);
    result.clear();
    return result;
}
Beispiel #4
0
void InternalApplyString(LispEnvironment& aEnvironment, LispPtr& aResult,
                 const LispString* aOperator,LispPtr& aArgs)
{
    if (!InternalIsString(aOperator))
        throw LispErrNotString();

    LispObject *head =
        LispAtom::New(aEnvironment, *SymbolName(aEnvironment, *aOperator));
    head->Nixed() = (aArgs);
    LispPtr body(LispSubList::New(head));
    aEnvironment.iEvaluator->Eval(aEnvironment, aResult, body);
}
Beispiel #5
0
LispObject LispFunctions::append(const VectorOfLispObjects &lispObjects)
{
    LispObject result(TokenType::T_LIST);
    LispObject head = lispObjects[0];
    LispObject tail = lispObjects[1];
    VectorOfLispObjects::const_iterator it = tail.objects().begin();
    for (; it != tail.objects().end(); ++it)
    {
        result.objects().push_back(*it);
    }
    return result;
}
LispObject LispComparisonOperators::greaterThan(const VectorOfLispObjects &lispObjects)
{
    VectorOfLispObjects::const_iterator it = lispObjects.begin();
    LispObject first = *it++;
    long result = std::atol(first.value().c_str());
    for (; it != lispObjects.end(); ++it)
    {
        LispObject current = *it;
        if (result <= std::atol(current.value().c_str()))
        {
            return LispObject::kFalse;
        }
    }
    return LispObject::kTrue;
}
Beispiel #7
0
int LispObject::Equal(LispObject& aOther)
{
    // next line handles the fact that either one is a string
    if (String() != aOther.String())
        return 0;  // return false

    //So, no strings.
    LispPtr *iter1 = SubList();
    LispPtr *iter2 = aOther.SubList();
    assert(!!iter1 && !!iter2);

    // check all elements in sublist
    while (!!(*iter1) && !!(*iter2))
    {
        if (! (*iter1)->Equal(*(*iter2) ))
            return 0;
      iter1 = &(*iter1)->Nixed();
      iter2 = &(*iter2)->Nixed();
    }
    //One list longer than the other?
    if (!(*iter1) && !(*iter2))
        return 1;
    return 0;
}
Beispiel #8
0
LispObject LispFunctions::nullPointer(const VectorOfLispObjects &lispObjects)
{
    LispObject lvalue = lispObjects[0];
    return lvalue.isEmpty() ? LispObject::kTrue : LispObject::kFalse;
}
Beispiel #9
0
LispObject LispFunctions::length(const VectorOfLispObjects &lispObjects)
{
    LispObject lvalue = lispObjects[0];
    return LispObject(TokenType::T_NUMBER, std::to_string(lvalue.valuesSize()));
}
Beispiel #10
0
const YacasParamMatcherBase* YacasPatternPredicateBase::MakeParamMatcher(LispEnvironment& aEnvironment, LispObject* aPattern)
{
    if (!aPattern)
        return nullptr;

    if (aPattern->Number(aEnvironment.Precision()))
        return new MatchNumber(aPattern->Number(aEnvironment.Precision()));

    // Deal with atoms
    if (aPattern->String())
        return new MatchAtom(aPattern->String());

    // Else it must be a sublist
    if (aPattern->SubList())
    {
        // See if it is a variable template:
        LispPtr* sublist = aPattern->SubList();
        assert(sublist);

        LispInt num = InternalListLength(*sublist);

        // variable matcher here...
        if (num>1)
        {
            LispObject* head = (*sublist);
            if (head->String() == aEnvironment.HashTable().LookUp("_"))
            {
                LispObject* second = head->Nixed();
                if (second->String())
                {
                    LispInt index = LookUp(second->String());

                    // Make a predicate for the type, if needed
                    if (num>2)
                    {
                        LispPtr third;

                        LispObject* predicate = second->Nixed();
                        if (predicate->SubList())
                        {
                            InternalFlatCopy(third, *predicate->SubList());
                        }
                        else
                        {
                            third = (second->Nixed()->Copy());
                        }

                        LispObject* last = third;
                        while (!!last->Nixed())
                            last = last->Nixed();

                        last->Nixed() = LispAtom::New(aEnvironment, *second->String());

                        iPredicates.push_back(LispPtr(LispSubList::New(third)));
                    }
                    return new MatchVariable(index);
                }
            }
        }

        std::vector<const YacasParamMatcherBase*> matchers;
        matchers.reserve(num);
        LispIterator iter(*sublist);
        for (LispInt i = 0; i < num; ++i, ++iter) {
            matchers.push_back(MakeParamMatcher(aEnvironment,iter.getObj()));
            assert(matchers[i]);
        }
        return new MatchSubList(std::move(matchers));
    }

    return nullptr;
}