// allows us to evaluate user defined functions Value evalApply(Value expr, Frame *env) { // evaluate function Value f = eval(expr.consValue->car, env); if( (f.type != closureType) && (f.type != primitiveType) ){ return evaluationError("first argument of a function applcation did not evaluate to a proceedure"); } // evaluate arguments Vector args; initVector(&args,8); int i = 1; Value curr = getNth(expr.consValue, i); while(curr.type != openType){ Value *storedVal = (Value *)malloc(sizeof(Value)); (*storedVal) = valdup(eval(curr,env)); if((*storedVal).type == openType){ //This is an error void *pointerToAVal = NULL; for(int j = 0; j < i-2; j++){ getVectorItem(&args, j, &pointerToAVal); freeValue(*((Value *)pointerToAVal)); free((Value *)pointerToAVal); } free(storedVal); return evaluationError(NULL); } insertVectorItem(&(args), i-1, (void *)storedVal); i++; curr = getNth(expr.consValue, i); } // apply the function Value ret = apply(f, args); cleanupVector(&args); return ret; }
// Adds bindings to a frame. Used in let* bool addBindingsStar(Frame *theEnv, Value bindings) { // add things ConsCell *cc = bindings.consValue; int i = 0; while(cc){ if( cc->car.type != consType){ return false; } ConsCell *binding = cc->car.consValue; Value caar = getNth(binding,0); Value cadar = getNth(binding,1); if( (caar.type == openType) || (cadar.type == openType) ){ return false; } Value *storedVal = (Value *)malloc(sizeof(Value)); (*storedVal) = valdup(eval(cadar,theEnv)); if( (*storedVal).type == openType){ free(storedVal); return false; } insertVectorItem(&(theEnv->names), i, (void *)strdup(caar.symbolValue)); insertVectorItem(&(theEnv->values), i, (void *)storedVal); cc = cc->cdr.consValue; i++; } return true; }
// Evaluates a define expression and makes a new frame that will point to the frame that is given to it // as a parameter Value evalDefine(Value tree, Frame *env){ // Get the bindings and the body Value val = getNth(tree.consValue,0); Value expr = getNth(tree.consValue,1); // Check for errors if( (val.type == openType) || (expr.type == openType) ){ return evaluationError("not enough arguments given to a \'define\'"); } if(val.type != symbolType){ return evaluationError("first argument given to \'define\' was not a symbol"); } // End error checking //eval expr and add this binding Value *storedVal = (Value *)malloc(sizeof(Value)); (*storedVal) = valdup(eval(expr,env)); if((*storedVal).type == openType){ free(storedVal); return evaluationError("second argument given to \'define\' could not be evaluated"); } insertVectorItem(&(env->names), 0, (void *)strdup(val.symbolValue)); insertVectorItem(&(env->values), 0, (void *)storedVal); return voidVal(); }
// Evaluates a let* expression and makes a new frame that will point to the frame that is given to it // as a parameter Value evalLetStar(Value tree, Frame *env){ if(tree.type != consType){ return evaluationError("improper use of \'let*\'"); } if(!tree.consValue){ return evaluationError("no arguments given to \'let*\'"); } // Get the bindings and the body Value bindings = getNth(tree.consValue,0); Value body = getNth(tree.consValue,1); if( (body.type == openType) || (bindings.type == openType) ){ return evaluationError("not enough arguments given to a \'let*\'"); } // Make a new frame from the bindings list Frame *newEnv = makeFrame(env); insertVectorItem(envGarbage, envGarbage->size, (void *)newEnv); if(!addBindingsStar(newEnv, bindings)){ return evaluationError("poorly formed \'let*\'"); } return eval(body, newEnv); }
TEST_F(order_statistic_treeTest, Rank) { auto ost = dyset.getObj(); // test order_statistic_tree Rank try { ost->getNth(0); EXPECT_TRUE(false); } catch (...) { EXPECT_TRUE(true); } EXPECT_EQ(ost->getNth(1), 0); try { ost->getNth(this->N+1); EXPECT_TRUE(false); } catch (...) { EXPECT_TRUE(true); } EXPECT_EQ(ost->getNth(this->N), this->N-1); }
TEST_F(SBTTest, Rank) { auto sbt = dyset.getObj(); // test SBT Rank try { sbt->getNth(0); EXPECT_TRUE(false); } catch (...) { EXPECT_TRUE(true); } EXPECT_EQ(sbt->getNth(1), 0); try { sbt->getNth(this->N+1); EXPECT_TRUE(false); } catch (...) { EXPECT_TRUE(true); } EXPECT_EQ(sbt->getNth(this->N), this->N-1); }
int main(void) { struct item *start = NULL; int choice, number, n; do { printf("1. Insert element into a linked list\n"); printf("2. Find nth element in the linked list\n"); printf("Enter choice\n"); scanf("%d", &choice); switch(choice) { case 1: printf("Enter the number to be inserted\n"); scanf("%d", &number); start = insertAtEnd(start, number); break; case 2: printf("Enter the value of n\n"); scanf("%d", &n); number = getNth(start, n); printf("The %dth element in the linked list is %d\n", n, number); break; case 3: break; default: printf("Invalid choice\n"); } }while(choice != 3); return 0; }
/* recurseVariables - recurses the references variable token and returns its final value dumpLast - if true (default) returns the final value, if false, it returns an array/string containing the final value noRecurse - if false (default) recurses all the way through the references, otherwise only removes ONE layer of indirection */ stutskInteger recurseVariables(Token & newToken, bool norecurse, bool dumpLast) { if (newToken.tokenType == T_VARIABLE) { TokenMap::iterator iter; iter = newToken.data.asVariable.context->variables.find (newToken.data.asVariable.name); if (iter != newToken.data.asVariable.context->variables.end()) { vector<stutskInteger> origIndex = newToken.index; newToken = iter->second; if (newToken.tokenType == T_VARIABLE) if (!norecurse) recurseVariables(newToken); for (vector<stutskInteger>::iterator it = origIndex.begin(); it != origIndex.end(); ++it) { if ((!dumpLast) && (it+1==origIndex.end())) return *it; getNth(newToken, *it); } if (newToken.tokenType == T_VARIABLE) if (!norecurse) recurseVariables(newToken); } else { stringstream ExceptionText; ExceptionText << "Variable '" << newToken.data.asVariable.name << "' not defined in context"; throw StutskException(ET_ERROR, ExceptionText.str()); } } return 0; }
Dsymbol *ScopeDsymbol::getNth(Array *members, size_t nth, size_t *pn) { if (!members) return NULL; size_t n = 0; for (size_t i = 0; i < members->dim; i++) { Dsymbol *s = (Dsymbol *)members->data[i]; AttribDeclaration *a = s->isAttribDeclaration(); if (a) { s = getNth(a->decl, nth - n, &n); if (s) return s; } else if (n == nth) return s; else n++; } if (pn) *pn += n; return NULL; }
// Evaluates a cond expression. Evaluates the cadr of the first thing with a car that evaluates to true Value evalCond(Value tree, Frame *env) { int i = 0; // Get the bindings and the body Value val = getNth(tree.consValue, i); // Check for errors if(val.type == openType){ return evaluationError("no arguments given to a \'cond\'"); } // loop through the tree and return the while(val.type != openType){ ///////////// if( val.type != consType){ return evaluationError("poorly formed argument given to a \'cond\'"); } ConsCell *condCell = val.consValue; if( !condCell ){ return evaluationError("non test-value pair given to a \'cond\'"); } Value caar = getNth(condCell,0); Value cadar = getNth(condCell,1); ///////////// i++; Value conditional = eval(caar, env); if(conditional.type != booleanType){ return evaluationError("Non boolean value given to cond statement"); } if(conditional.booleanValue == true){ return eval(cadar, env); } val = getNth(tree.consValue, i); } return voidVal(); // or deal with else case //\\//\\//\\//\\//\\//\\//\\ // Value ret = eval(val, env); // while(val.type != openType){ // ret = eval(val, env); // if(ret.type == openType) break; // i++; // val = getNth(tree.consValue, i); // } // return ret; }
int main() { struct node *head=NULL; push(&head,7); push(&head,8); push(&head,71); push(&head,9); push(&head,17); getNth(head,9); }
//Returns the nth item in the ConsCell* chain for convenience. // This encodes errors as open parens. Value getNth(ConsCell* cc, int n){ if(cc == NULL){ //This is an error. return evaluationError(NULL); } if(n>0){ return getNth(cc->cdr.consValue, n-1); }else{ return cc->car; } }
// Evaluates a define expression and makes a new frame that will point to the frame that is given to it // as a parameter Value evalBegin(Value tree, Frame *env){ int i = 0; // Get the bindings and the body Value val = getNth(tree.consValue, i); // Returns void if no arguments are given if(val.type == openType){ return voidVal(); } Value ret; while(val.type != openType){ ret = eval(val, env); if(ret.type == openType) return evaluationError(NULL); i++; val = getNth(tree.consValue, i); } return ret; }
// Evaluates an If expression Value evalIf(Value tree, Frame *env){ Value test = getNth(tree.consValue,0); Value trueExpr = getNth(tree.consValue,1); Value falseExpr = getNth(tree.consValue,2); if( (test.type == openType) || (trueExpr.type == openType) || (falseExpr.type == openType) ){ return evaluationError("not enough arguments given to an \'if\'"); } Value testEv = eval(test,env); if(testEv.type != booleanType){ //Bad. return evaluationError("first expression in an \'if\' did not evaluate to a boolean"); } if(testEv.booleanValue){ return eval(trueExpr,env); }else{ return eval(falseExpr,env); } }
// Adds bindings to a frame. Used in letrec bool addBindingsRec(Frame *theEnv, Value bindings) { // add things ConsCell *cc = bindings.consValue; int i = 0; while(cc){ if( cc->car.type != consType){ return false; } ConsCell *binding = cc->car.consValue; Value caar = getNth(binding,0); Value cadar = falseVal(); if( (caar.type == openType) || (cadar.type == openType) ){ return false; } Value *storedVal = (Value *)malloc(sizeof(Value)); (*storedVal) = valdup(cadar); insertVectorItem(&(theEnv->names), i, (void *)strdup(caar.symbolValue)); insertVectorItem(&(theEnv->values), i, (void *)storedVal); cc = cc->cdr.consValue; i++; } cc = bindings.consValue; i = 0; while(cc){ ConsCell *binding = cc->car.consValue; Value caar = getNth(binding,0); Value cadar = getNth(binding,1); if( (caar.type == openType) || (cadar.type == openType) ){ return false; } Value evaled = eval(cadar,theEnv); if( (evaled).type == openType){ return false; } void *theEggman = NULL; getVectorItem(&(theEnv->values), i, &theEggman); *(Value *) theEggman = evaled; cc = cc->cdr.consValue; i++; } return true; }
// Evaluates a lambda statement and returns a new closure. Value evalLambda(Value tree, Frame *env){ //Get the args passed to the lambda Value formals = getNth(tree.consValue,0); Value body = getNth(tree.consValue,1); if( (formals.type == openType) || (body.type == openType) ){ return evaluationError("not enough arguments given to a \'lambda\'"); } if(formals.type != consType){ return evaluationError("first argument given to a \'lambda\' was not a list"); } Closure closey; closey.env = env; int i = 1; Value curr = getNth(formals.consValue, i); while(curr.type != openType){ if(curr.type != symbolType){ return evaluationError("formal parameters given to a \'lambda\' were not symbols"); } i++; curr = getNth(formals.consValue, i); } closey.body = valdup(body); closey.formals = valdup(formals); Closure *storedClosure = (Closure *)malloc(sizeof(Closure)); (*storedClosure) = closey; insertVectorItem(closureGarbage, closureGarbage->size, (void *)storedClosure); Value ret; ret.type = closureType; ret.closureValue = storedClosure; return ret; }
// Allows us to apply userdefined functions Value apply(Value function, Vector args) { if (function.type == primitiveType) { void *pointerToAVal = NULL; for(int i = 0; i < args.size; i++){ getVectorItem(&args, i, &pointerToAVal); insertVectorItem(valGarbage, valGarbage->size, pointerToAVal); } return function.primitiveValue(args); } if (function.type == closureType) { Frame *newEnv = makeFrame(function.closureValue->env); Value formals = function.closureValue->formals; //check length Value shouldBeLast = getNth(formals.consValue, args.size-1); //should work if(args.size == 0) shouldBeLast.type = closeType; Value shouldNotExist = getNth(formals.consValue, args.size); //should fail if( (shouldBeLast.type == openType) || (shouldNotExist.type != openType) ){ return evaluationError("expected different number of args than recieved"); } // bind formals to actuals for(int i = 0; i < args.size; i++){ Value sym = getNth(formals.consValue, i); void *storedVal = NULL; getVectorItem(&args, i, &storedVal); insertVectorItem(&(newEnv->names), 0, (void *)strdup(sym.symbolValue)); insertVectorItem(&(newEnv->values), 0, (void *)storedVal); } insertVectorItem(envGarbage, envGarbage->size, (void *)newEnv); return eval(function.closureValue->body, newEnv); } // Else, error return evaluationError("apply called on non-function"); }
Dsymbol *ScopeDsymbol::getNth(Dsymbols *members, size_t nth, size_t *pn) { if (!members) return NULL; size_t n = 0; for (size_t i = 0; i < members->dim; i++) { Dsymbol *s = (*members)[i]; AttribDeclaration *a = s->isAttribDeclaration(); TemplateMixin *tm = s->isTemplateMixin(); TemplateInstance *ti = s->isTemplateInstance(); if (a) { s = getNth(a->decl, nth - n, &n); if (s) return s; } else if (tm) { s = getNth(tm->members, nth - n, &n); if (s) return s; } else if (ti) ; else if (n == nth) return s; else n++; } if (pn) *pn += n; return NULL; }
bool PulseClusterer::isStronger(int idx, ClusterTag *old) { if (!isComplete() || (idx < 0 || idx > (int) clusterList.size())) Fatal(666); lock(); PulseClusterer *oldClust = old->holder->getPulse(); bool stronger = false; if (oldClust) { const PulseSignalHeader &oldpd = oldClust->getNth(old->index); const PulseSignalHeader &newpd = getNth(idx); stronger = (newpd.sig.path.power > oldpd.sig.path.power); } unlock(); return (stronger); }
int main(){ int option = 999; char *menuStr = getScrOp(); while(option!=11){ printf("%sEnter an Option :",menuStr); scanf("%d",&option); switch(option){ case 1: insert(); break; case 10: display(); break; case 2: InsertBegin(); break; case 3: InsertAnywhere(); break; case 11: printf("Exited."); break; case 4: deleteNode(); break; case 5: countIteratively(); break; case 6: getNth(); break; case 7: searchIteratively(); break; case 8: searchRecursively(head,getInput()); break; case 9: swapByLinks(); break; default : printf("Enter an valid option"); } } return 0; }
main() { calcFib(); int j; while(scanf("%d\n",&j)==1) //for(int j =1; j <=5000;j++) { getNth(j); } }
int main(void){ Node *myList = NULL; Node *dupList = NULL; //Node *nn= newNode(4); Node *randomList =NULL; append(&myList, 10); //append(&myList, 15); printList(myList); myList= buildOneTwoThree(); if(myList != NULL){ printList(myList); printf("\nmyList length = %d; ", linkLength(myList)); } push(&myList, 0); push(&(myList->next), 42); printList(myList); append(&myList,5); printList(myList); dupList = copyList(myList); printList(dupList); changeToNull(&dupList); printList(dupList); dupList=addAtHead(); printList(dupList); dupList= buildWithSpecialCase(); printList(dupList); dupList=buildWithDummyNode(); printList(dupList); printf("\n List has %d 5.", countTest(dupList,5)); printf("\n 3rd Element of list = %d", getNth(dupList, 3)); //deleteList(&dupList); insertNthTest(); //sortedInsert(&dupList, nn); printList(dupList); // random list insertNth(&randomList, 0, 13); insertNth(&randomList, 1, 13); insertNth(&randomList, 2, 27); insertNth(&randomList, 3, 55); insertNth(&randomList, 4, 55); printList(randomList); removeDuplicates(randomList); printList(randomList); //insertSort(&randomList); //printList(randomList); //testAppendList(); //testFrontBackSplit(); return 0; }
int main(int argc, char* argv[]) { NodePtr head = NULL; NodePtr second = NULL; NodePtr third = NULL; head = malloc(sizeof(Node)); second = malloc(sizeof(Node)); third = malloc(sizeof(Node)); assert(head != NULL); assert(second != NULL); assert(third != NULL); head = buildList(head, second, third); NodePtr newNode; newNode = malloc(sizeof(Node)); newNode->data = 1; newNode->next = head; head = newNode; // now head points to list {1,2,3} printList(head); Push(&head, 13); Push(&head, 2); Push(&head, 1); Push(&head, 23); Push(&head, 35); printf("Before reverse:\n"); printList(head); Reverse(&head); printf("After reverse:\n"); printList(head); int count = countIntInList(head, 1); printf("count of %d in list is : %d\n", 1, count); printf("Third item in list is : %d\n", getNth(head, 3) ); #if 0 printf("Test code for append()\n"); NodePtr a; a = malloc(sizeof(a)); assert( a!= NULL); Push(&a, 100); Push(&a, 200); printf("Built list a :\n"); printList(a); NodePtr b; b = malloc(sizeof(a)); assert( b!= NULL); Push(&b, 300); Push(&b, 400); printf("Built list b :\n"); printList(b); //Delete(&a); Append(&a, &b); printf("After appending a to b :\n"); printList(a); free(head); #endif #if 0 free(second); free(third); free(newNode); free(a); free(b); #endif return 0; }
int main(){ int c, n, r; do { printf("0. Show\n"); printf("1. Append\n"); printf("2. Push\n"); printf("3. pop\n"); printf("4. Count Nodes\n"); printf("5. Get nth element\n"); printf("6. Delete List\n"); printf("7. Insert at nth index\n"); printf("8. InsertSort the List\n"); printf("9. FrontBackSplit\n"); printf("10. Remove Duplicates\n"); printf("11. Reverse the List\n"); printf("12. Alternating Split\n"); printf("13. Make a Dummy List\n"); printf("14. Shuffle Merge\n"); printf("15. EXIT\n"); printf("Your Choice : "); scanf("%d", &c); switch(c){ case 0 : show(); break; case 1 : printf("Enter a number : "); scanf("%d", &n); append(n); break; case 2 : printf("Enter a number : "); scanf("%d", &n); push(n); break; case 3 : if((r = pop()) > 0) printf("%d\n", r); else printf("List Empty\n"); break; case 4 : printf("%d\n", countNodes()); break; case 5 : printf("Enter the index : "); scanf("%d", &n); ((r = getNth(n)) >= 0) ? printf("%d\n", r) : printf("Index out of Range\n"); break; case 6 : deleteList(); break; case 7 : printf("Enter the index and the number : "); scanf("%d%d", &r, &n); if(insertNth(r, n) < 0) printf("Index out of Range. Aborting..."); break; case 8 : insertSort(); break; case 9 : frontBackSplit(); break; case 10: removeDuplicate(); break; case 11: reverse(); break; case 12: alternatingSplit(); break; case 13: makeDummyList(); break; case 14: shuffleMerge(); break; case 15: exit(0); default: printf("wrong Choice\n"); exit(0); } }while(c >= 0 && c <= 14); }