ICCItem *fnLinkedList (CEvalContext *pCtx, ICCItem *pArguments, DWORD dwData) // fnLinkedList // // Handles linked-list specific functions // // (lnkAppend linked-list item) -> list // (lnkRemove linked-list index) -> list // (lnkRemoveNil linked-list) -> list // (lnkReplace linked-list index item) -> list // // HACK: This function has different behavior depending on the first // argument. If the first argument is a variable holding a linked list, // then the variable contents will be changed. If the variable holds Nil, // then the variable contents are not changed. In all cases, the caller // should structure the call as: (setq ListVar (lnkAppend ListVar ...)) // in order to handle all cases. { CCodeChain *pCC = pCtx->pCC; ICCItem *pArgs; ICCItem *pList; CCLinkedList *pLinkedList; ICCItem *pResult; // Evaluate the arguments if (dwData == FN_LINKEDLIST_APPEND) pArgs = pCC->EvaluateArgs(pCtx, pArguments, CONSTLIT("lv")); else if (dwData == FN_LINKEDLIST_REMOVE_NIL) pArgs = pCC->EvaluateArgs(pCtx, pArguments, CONSTLIT("l")); else pArgs = pCC->EvaluateArgs(pCtx, pArguments, CONSTLIT("liv")); if (pArgs->IsError()) return pArgs; // Get the linked list pList = pArgs->GetElement(0); if (pList->GetClass()->GetObjID() == OBJID_CCLINKEDLIST) pLinkedList = (CCLinkedList *)pList->Reference(); else if (pList->IsNil()) { pList = pCC->CreateLinkedList(); if (pList->IsError()) { pArgs->Discard(pCC); return pList; } pLinkedList = (CCLinkedList *)pList; } else { pArgs->Discard(pCC); return pCC->CreateError(CONSTLIT("Linked-list expected:"), NULL); } // Do the right thing switch (dwData) { case FN_LINKEDLIST_APPEND: { ICCItem *pItem = pArgs->GetElement(1); ICCItem *pError; pLinkedList->Append(pCC, pItem, &pError); if (pError->IsError()) { pLinkedList->Discard(pCC); pResult = pError; } else { pError->Discard(pCC); pResult = pLinkedList; } break; } case FN_LINKEDLIST_REMOVE: { int iIndex = pArgs->GetElement(1)->GetIntegerValue(); // Make sure we're in range if (iIndex < 0 || iIndex >= pLinkedList->GetCount()) { pLinkedList->Discard(pCC); pResult = pCC->CreateError(CONSTLIT("Index out of range:"), pArgs->GetElement(1)); } else { pLinkedList->RemoveElement(pCC, iIndex); pResult = pLinkedList; } break; } case FN_LINKEDLIST_REMOVE_NIL: { // Iterate over all elements and remove any elements that are Nil int iIndex = 0; while (iIndex < pLinkedList->GetCount()) { if (pLinkedList->GetElement(iIndex)->IsNil()) pLinkedList->RemoveElement(pCC, iIndex); else iIndex++; } // Done pResult = pLinkedList; break; } case FN_LINKEDLIST_REPLACE: { int iIndex = pArgs->GetElement(1)->GetIntegerValue(); ICCItem *pItem = pArgs->GetElement(2); // Make sure we're in range if (iIndex < 0 || iIndex >= pLinkedList->GetCount()) { pLinkedList->Discard(pCC); pResult = pCC->CreateError(CONSTLIT("Index out of range:"), pArgs->GetElement(1)); } else { pLinkedList->ReplaceElement(pCC, iIndex, pItem); pResult = pLinkedList; } break; } default: ASSERT(FALSE); return NULL; } // Done pArgs->Discard(pCC); return pResult; }
ICCItem *CCodeChain::EvaluateArgs (CEvalContext *pCtx, ICCItem *pArgs, const CString &sArgValidation) // EvaluateArgs // // Evaluate arguments and validate their types { ICCItem *pArg; ICCItem *pNew; ICCItem *pError; CCLinkedList *pEvalList; char *pValidation; int i; BOOL bNoEval; // If the argument list if quoted, then it means that the arguments // have already been evaluated. This happens if we've been called by // (apply). bNoEval = pArgs->IsQuoted(); // Create a list to hold the results pNew = CreateLinkedList(); if (pNew->IsError()) return pNew; pEvalList = dynamic_cast<CCLinkedList *>(pNew); // Start parsing at the beginning pValidation = sArgValidation.GetPointer(); // If there is a '*' in the validation, figure out // how many arguments it represents int iVarArgs = Max(0, pArgs->GetCount() - (sArgValidation.GetLength() - 1)); // Loop over each argument for (i = 0; i < pArgs->GetCount(); i++) { ICCItem *pResult; pArg = pArgs->GetElement(i); // If we're processing variable args, see if we're done if (*pValidation == '*') { if (iVarArgs == 0) pValidation++; else iVarArgs--; } // Evaluate the item. If the arg is 'q' or 'u' then we // don't evaluate it. if (bNoEval || *pValidation == 'q' || *pValidation == 'u') pResult = pArg->Reference(); // If the arg is 'c' then we don't evaluate unless it is // a lambda expression (or an identifier) else if (*pValidation == 'c' && !pArg->IsLambdaExpression() && !pArg->IsIdentifier()) pResult = pArg->Reference(); // Evaluate else { pResult = Eval(pCtx, pArg); // We don't want to return on error because some functions // might want to pass errors around if (*pValidation != 'v' && *pValidation != '*') { if (pResult->IsError()) { pEvalList->Discard(this); return pResult; } } } // Check to see if the item is valid switch (*pValidation) { // We expect a function... case 'f': { if (!pResult->IsFunction()) { pError = CreateError(LITERAL("Function expected"), pResult); pResult->Discard(this); pEvalList->Discard(this); return pError; } break; } // We expect a numeral... // // NOTE: We treat integer the same a numeral because it's not always // clear to the user when they've created a double or an integer. // It is up to the actual function to use the integer or double // value appropriately. case 'i': case 'n': { if (!pResult->IsDouble() && !pResult->IsInteger()) { pError = CreateError(LITERAL("Numeral expected"), pResult); pResult->Discard(this); pEvalList->Discard(this); return pError; } break; } // We expect a double... case 'd': { if (!pResult->IsDouble()) { pError = CreateError(LITERAL("Double expected"), pResult); pResult->Discard(this); pEvalList->Discard(this); return pError; } break; } // We expect a vEctor... case 'e': { if (!(pResult->GetValueType() == ICCItem::Vector)) { pError = CreateError(LITERAL("Vector expected"), pResult); pResult->Discard(this); pEvalList->Discard(this); return pError; } break; } // We expect a linked list case 'k': { if (pResult->GetClass()->GetObjID() != OBJID_CCLINKEDLIST) { pError = CreateError(LITERAL("Linked-list expected"), pResult); pResult->Discard(this); pEvalList->Discard(this); return pError; } break; } // We expect a list case 'l': { if (!pResult->IsList()) { pError = CreateError(LITERAL("List expected"), pResult); pResult->Discard(this); pEvalList->Discard(this); return pError; } break; } // We expect an identifier case 's': case 'q': { if (!pResult->IsIdentifier()) { pError = CreateError(LITERAL("Identifier expected"), pResult); pResult->Discard(this); pEvalList->Discard(this); return pError; } break; } // We expect an atom table case 'x': { if (!pResult->IsAtomTable()) { pError = CreateError(LITERAL("Atom table expected"), pResult); pResult->Discard(this); pEvalList->Discard(this); return pError; } break; } // We expect a symbol table case 'y': { if (!pResult->IsSymbolTable()) { pError = CreateError(LITERAL("Symbol table expected"), pResult); pResult->Discard(this); pEvalList->Discard(this); return pError; } break; } // We expect anything case 'c': case 'u': case 'v': break; // We expect any number of anythings... case '*': break; // Too many arguments case '\0': { pError = CreateError(LITERAL("Too many arguments"), NULL); pResult->Discard(this); pEvalList->Discard(this); return pError; } default: ASSERT(FALSE); } // Add the result to the list pEvalList->Append(*this, pResult); pResult->Discard(this); // Next validation sequence (note that *pValidation can never // be '\0' because we return above if we find it) if (*pValidation != '*') pValidation++; } // Make sure we have enough arguments if (*pValidation != '\0' && *pValidation != '*') { pError = CreateError(LITERAL("Insufficient arguments"), NULL); pEvalList->Discard(this); return pError; } // Return the evaluation list return pEvalList; }