* Bug 15630 fixed: now x(0:2)=1 and x=1;x(0:1,1) raise an error
[scilab.git] / scilab / modules / ast / src / cpp / types / types_tools.cpp
index 840a255..b10a1e4 100644 (file)
@@ -285,9 +285,20 @@ bool getScalarImplicitIndex(GenericType* _pRef, typed_list* _pArgsIn, std::vecto
     {
         ImplicitList* pIL = pIT->getAs<ImplicitList>();
         int sizeRef = _pRef->getSize();
-        index.push_back(evalute(pIL->getStart(), sizeRef));
-        index.push_back(evalute(pIL->getStep(), sizeRef));
-        index.push_back(evalute(pIL->getEnd(), sizeRef));
+        double start = evalute(pIL->getStart(), sizeRef);
+        double step = evalute(pIL->getStep(), sizeRef);
+        double end = evalute(pIL->getEnd(), sizeRef);
+
+        if ((start < 1 && step > 0) || (end < 1 & step < 0))
+        {
+            wchar_t szError[bsiz];
+            os_swprintf(szError, bsiz, _W("Invalid index.\n").c_str());
+            throw ast::InternalError(szError);
+        }
+
+        index.push_back(start);
+        index.push_back(step);
+        index.push_back(end);
     }
 
     return true;
@@ -376,6 +387,13 @@ bool getImplicitIndex(GenericType* _pRef, typed_list* _pArgsIn, std::vector<int>
 
                 std::vector<int> idx(size);
 
+                if ((start < 1 && step > 0) || (end < 1 & step < 0))
+                {
+                    wchar_t szError[bsiz];
+                    os_swprintf(szError, bsiz, _W("Invalid index.\n").c_str());
+                    throw ast::InternalError(szError);
+                }
+
                 double val = start - 1;
                 std::generate(idx.begin(), idx.end(), [&val, step]{ double s = val; val += step; return (int)s; });
 
@@ -511,9 +529,10 @@ int checkIndexesArguments(InternalType* _pRef, typed_list* _pArgsIn, typed_list*
             {
                 int size = pCurrentArg->getSize();
                 double* dbl = pCurrentArg->get();
+                double minIndex = _pRef==NULL || _pRef->isList()==false ? 1 : 0;
                 for (int j = 0; j < size; ++j)
                 {
-                    if (dbl[j] < 0)
+                    if (dbl[j] < minIndex)
                     {
                         if (pCurrentArg->isDeletable())
                         {
@@ -536,54 +555,71 @@ int checkIndexesArguments(InternalType* _pRef, typed_list* _pArgsIn, typed_list*
                 //: or $
                 if (_pRef == NULL)
                 {
-                    //not enough information to compute indexes.
-                    _pArgsOut->push_back(NULL);
-                    bUndefine = true;
-                    pIL->killMe();
-                    continue;
-                }
-                //evalute polynom with "MaxDim"
-                int iMaxDim = _pRef->getAs<GenericType>()->getVarMaxDim(i, iDims);
-#if defined(_SCILAB_DEBUGREF_)
-                Double* pdbl = new Double(iMaxDim);
-#else
-                Double dbl(iMaxDim);
-#endif
-                if (pIL->getStart()->isPoly())
-                {
-                    Polynom *poPoly = pIL->getStart()->getAs<types::Polynom>();
-#if defined(_SCILAB_DEBUGREF_)
-                    pIL->setStart(poPoly->evaluate(pdbl));
-#else
-                    pIL->setStart(poPoly->evaluate(&dbl));
-#endif
-                }
-                if (pIL->getStep()->isPoly())
-                {
-                    Polynom *poPoly = pIL->getStep()->getAs<types::Polynom>();
-#if defined(_SCILAB_DEBUGREF_)
-                    pIL->setStep(poPoly->evaluate(pdbl));
-#else
-                    pIL->setStep(poPoly->evaluate(&dbl));
-#endif
-                }
-                if (pIL->getEnd()->isPoly())
-                {
-                    Polynom *poPoly = pIL->getEnd()->getAs<types::Polynom>();
-#if defined(_SCILAB_DEBUGREF_)
-                    pIL->setEnd(poPoly->evaluate(pdbl));
-#else
-                    pIL->setEnd(poPoly->evaluate(&dbl));
-#endif
+                    if (pIL->getStep()->isDouble() &&
+                        ((getIndex(pIL->getStep()) > 0 && pIL->getStart()->isDouble() && getIndex(pIL->getStart()) < 1) ||
+                        (getIndex(pIL->getStep()) < 0 && pIL->getEnd()->isDouble() && getIndex(pIL->getEnd()) < 1)))
+                    {
+                        pCurrentArg = NULL;
+                    }
+                    else
+                    {
+                        //not enough information to compute indexes.
+                        _pArgsOut->push_back(NULL);
+                        bUndefine = true;
+                        pIL->killMe();
+                        continue;
+                    }
                 }
+                else
+                {        //evalute polynom with "MaxDim"
+                    int iMaxDim = _pRef->getAs<GenericType>()->getVarMaxDim(i, iDims);
+    #if defined(_SCILAB_DEBUGREF_)
+                    Double* pdbl = new Double(iMaxDim);
+    #else
+                    Double dbl(iMaxDim);
+    #endif
+                    if (pIL->getStart()->isPoly())
+                    {
+                        Polynom *poPoly = pIL->getStart()->getAs<types::Polynom>();
+    #if defined(_SCILAB_DEBUGREF_)
+                        pIL->setStart(poPoly->evaluate(pdbl));
+    #else
+                        pIL->setStart(poPoly->evaluate(&dbl));
+    #endif
+                    }
+                    if (pIL->getStep()->isPoly())
+                    {
+                        Polynom *poPoly = pIL->getStep()->getAs<types::Polynom>();
+    #if defined(_SCILAB_DEBUGREF_)
+                        pIL->setStep(poPoly->evaluate(pdbl));
+    #else
+                        pIL->setStep(poPoly->evaluate(&dbl));
+    #endif
+                    }
+                    if (pIL->getEnd()->isPoly())
+                    {
+                        Polynom *poPoly = pIL->getEnd()->getAs<types::Polynom>();
+    #if defined(_SCILAB_DEBUGREF_)
+                        pIL->setEnd(poPoly->evaluate(pdbl));
+    #else
+                        pIL->setEnd(poPoly->evaluate(&dbl));
+    #endif
+                    }
 
-#if defined(_SCILAB_DEBUGREF_)
-                pdbl->killMe();
+    #if defined(_SCILAB_DEBUGREF_)
+                    pdbl->killMe();
 #endif
+                }
             }
+            if (_pRef != NULL || pIL->isComputable() == true)
+            {
+                double start = getIndex(pIL->getStart());
+                double step = getIndex(pIL->getStep());
+                double end = getIndex(pIL->getEnd());
 
+                pCurrentArg = ((start < 1 && step > 0) || (end < 1 & step < 0)) ? NULL : pIL->extractFullMatrix()->getAs<Double>();
+            }
 
-            pCurrentArg = pIL->extractFullMatrix()->getAs<Double>();
             pIL->killMe();
         }
         else if (pIT->isString())