inline bool cast_array(STATE, CallFrame* call_frame) { // Use stack_top and not stack_pop because we may need // to preserve and reread the value from the stack below. Object* t1 = stack_top(); if(t1->nil_p()) { t1 = Array::create(state, 0); } else if(Tuple* tup = try_as<Tuple>(t1)) { t1 = Array::from_tuple(state, tup); } else if(!rubinius::kind_of<Array>(t1)) { Object* recv = G(type); Arguments args(G(sym_coerce_to_array), recv, 1, &t1); Dispatch dispatch(G(sym_coerce_to_array)); Object* res = dispatch.send(state, args); // If the send still doesn't produce an array, wrap // the value in one. if(res && !rubinius::kind_of<Array>(res)) { Array* ary = Array::create(state, 1); // Don't read t1 here, it's not GC safe because we called // a method. ary->set(state, 0, stack_top()); t1 = ary; } else { t1 = res; } } (void)stack_pop(); // Remove original value CHECK_AND_PUSH(t1); }
//two args: exps & tail_context static cellpoint eval_sequence(void) { stack_push(&vars_stack, args_ref(1)); args_push(stack_top(&vars_stack)); reg = last_exp(); while (is_false(reg)){ args_push(stack_top(&vars_stack)); reg = first_exp(); //eval the exp which isn't the last one with tail_context is a_false args_push(a_false); args_push(reg); reg = eval(); //renew the remain exps args_push(stack_pop(&vars_stack)); reg = rest_exps(); stack_push(&vars_stack, reg); args_push(stack_top(&vars_stack)); reg = last_exp(); } //get the last expression args_push(stack_pop(&vars_stack)); reg = first_exp(); //eval the last expression with the second argument (tail_context) args_push(args_ref(2)); args_push(reg); reg = eval(); args_pop(2); return reg; }
//one arg: body static cellpoint eval_lambda_body(void) { stack_push(&vars_stack, args_ref(1)); args_push(stack_top(&vars_stack)); reg = last_exp(); while (is_false(reg)){ args_push(stack_top(&vars_stack)); reg = first_exp(); //eval the exp which isn't the last one with tail_context is a_false args_push(a_false); args_push(reg); reg = eval(); //renew the remain exps args_push(stack_pop(&vars_stack)); reg = rest_exps(); stack_push(&vars_stack, reg); args_push(stack_top(&vars_stack)); reg = last_exp(); } //get the last expression args_push(stack_pop(&vars_stack)); reg = first_exp(); //eval the last expression with the tail_context is a_true args_push(a_true); args_push(reg); reg = eval(); args_pop(1); return reg; }
//one arg: exp static cellpoint letstar_2_nested_lets(void) { //get the reverse list of bindings list args_push(args_ref(1)); reg = letstar_bindings(); args_push(reg); reg = reverse(); stack_push(&vars_stack, reg); //get body of let* expression args_push(args_ref(1)); reg = letstar_body(); //create nested lets if (is_true(is_null(stack_top(&vars_stack)))){ args_push(reg); args_push(stack_pop(&vars_stack)); reg = make_let(); }else { while (is_false(is_null(stack_top(&vars_stack)))){ check_bindings("let*", car(stack_top(&vars_stack)), args_ref(1)); args_push(reg); args_push(cons(car(stack_top(&vars_stack)), NIL)); reg = make_let(); reg = cons(reg, NIL); //renews bingdings stack_push(&vars_stack, cdr(stack_pop(&vars_stack))); } stack_pop(&vars_stack); reg = car(reg); } args_pop(1); return reg; }
int main(int argc, char *argv[]) { int max = 5; stack_init(max); assert(stack_isempty()); stack_push(1); stack_push(2); stack_push(3); assert(stack_count() == 3); assert(stack_top() == 3); stack_push(3); assert(stack_count() == 3); assert(stack_top() == 3); stack_push(2); assert(stack_count() == 3); assert(stack_top() == 3); stack_push(4); stack_push(5); stack_push(5); // stack full, ok to push a duplicate item. nl(); printf("count: %d \n", stack_count()); return 0; }
struct media_entity * media_entity_graph_walk_next(struct media_entity_graph *graph) { if (stack_top(graph) == NULL) return NULL; while (link_top(graph) < stack_top(graph)->num_links) { struct media_entity *entity = stack_top(graph); struct media_link *link = &entity->links[link_top(graph)]; struct media_entity *next; if (!(link->flags & MEDIA_LNK_FL_ENABLED)) { link_top(graph)++; continue; } next = media_entity_other(entity, link); if (next == stack_peek(graph)) { link_top(graph)++; continue; } link_top(graph)++; stack_push(graph, next); } return stack_pop(graph); }
int main(void) { stack_t stack; stack_init(&stack); printf("Pushing items: "); int i; for (i = 0; i < 10; i++) { if (!stack_push(&stack, i)) { fprintf(stderr, "Can't push another item!\n"); return EXIT_FAILURE; } printf("%d ", stack_top(&stack)); } if (___sl_get_nondet_int()) { printf("\nPoping items: "); while (!stack_empty(&stack)){ printf("%d ", stack_top(&stack)); stack_pop(&stack); } printf("\n"); } stack_clear(&stack); return 0; }
int main(void) { int value = 10, v = 11, i; Student s1 = {12, "student1", "addr1"},s2 = {13, "student2","a1"}, s3 = {15, "stu3", "address3"}, *sptr; pStack ps = stack_new(sizeof(Student) + 20); pStack p = stack_new(sizeof(int)); stack_push(p, &value); stack_push(p, &v); printf("%d\n", *((int*)stack_top(p))); stack_pop(p); printf("%d\n", *((int*)stack_top(p))); stack_push(ps, &s1); stack_push(ps, &s2); sptr = (pStudent)stack_top(ps); printf("no: %d, name: %s\n", sptr->no, sptr->addr); stack_pop(ps); stack_push(ps, &s3); sptr = (pStudent)stack_top(ps); printf("no: %d, name: %s\n", sptr->no, sptr->addr); stack_free(ps); for (i = 0; i < 100; i++) { stack_push(p, &i); } for (i = 0; i < 100; i++) { printf("%d ", *((int*)stack_top(p))); stack_pop(p); } stack_free(p); exit(EXIT_SUCCESS); }
char *expr_evaluate(expr_t *expression, char *variables) { token_stack_t *numbers_stack = NULL; char *postfix = malloc(sizeof(char) * (strlen(expression->postfix) + 1)); strcpy(postfix, expression->postfix); char **tokens = NULL; int tokens_count = strtoktoa(postfix, " ", &tokens); /* iterate over each token in postfix expression */ int i; for (i = 0; i < tokens_count; ++i) { char *token = tokens[i]; /* if 'token' is an operator */ op_t *op = op_find_by_str(token); if (op) { char *tmp_str = NULL; double a = 0.0, b = 0.0; /* get first value from numbers stack */ tmp_str = stack_top(numbers_stack); a = atof(tmp_str); free(tmp_str); stack_pop(&numbers_stack); /* get second value from numbers stack if operator is binary */ if (!(op->unary)) { tmp_str = stack_top(numbers_stack); b = atof(tmp_str); free(tmp_str); stack_pop(&numbers_stack); } /* put the result on stack */ tmp_str = malloc(sizeof(char) * (max_token_len + 1)); snprintf(tmp_str, max_token_len, "%g", (*op->eval)(b, a)); stack_push(&numbers_stack, tmp_str); free(tmp_str); } /* otherwise replace 'token' with its value from 'variables' */ else { char *new_token = var_evaluate(variables, token); if (new_token) { stack_push(&numbers_stack, new_token); free(new_token); } else { /* TODO: return error message */ return NULL; } } } free(tokens); free(postfix); return stack_top(numbers_stack); }
double rpn_eval(char * fileName, double x) { double a,b,c; int i = 0; char *word; FILE * fd = fopen(fileName, "r"); if (fd == NULL) { printf("Could not open file %s\n", fileName); exit(1); } while((word = nextword(fd)) != NULL) { if (strcmp(word, "x")==0) { stack_push(x); continue; } else if (strcmp(word, "+")==0) { stack_push(stack_pop() + stack_pop()); } else if (strcmp(word, "-")==0) { a = stack_pop(); b = stack_pop(); stack_push(b - a); } else if (strcmp(word, "*")==0) { a = stack_pop(); b = stack_pop(); stack_push(a * b); } else if (strcmp(word, "/")==0) { a = stack_pop(); b = stack_pop(); stack_push(b / a); } else if (strcmp(word, "sin")==0) stack_push(sin(stack_pop())); else if (strcmp(word, "cos")==0) stack_push(cos(stack_pop())); else if (strcmp(word, "log")==0) stack_push(log(stack_pop())); else if (strcmp(word, "exp")==0) stack_push(exp(stack_pop())); else if (strcmp(word, "pow")==0) { a = stack_pop(); b = stack_pop(); stack_push(pow(b,a)); } else { stack_push(atof(word)); } // stack_print();printf("\nWord: %s\n",word); } //printf("%s\n",word); if (stack_top() > 1) { printf("Elements remain in the stack\n"); exit(1); } //else if (stac // printf("Stack Underflow\n"); exit(1); //} if (stack_top() == 1) return stack_pop(); //else { printf("Stack Underflow\n"); exit(1); } }
static int _kthread_create(kthread_t * thread, int gfp_flags, void * (*start_routine)(void *), void * args) { *thread = _kmalloc_kthread(); if(*thread) { if(stack_alloc(&((*thread)->stack), 1024, gfp_flags)) { void* stack_p = stack_top(&((*thread)->stack)); cpu_state_build(&((*thread)->cpu_state), start_routine, args, (void*)(stack_p), &_exited_kthread); } else { _free_kthread(*thread); *thread = NULL; return -1; } } return 0; }
struct symbol_table_s *context_local() { /*assert(!stack_empty(&stack));*/ if (stack_empty(&stack)) return 0; return (struct symbol_table_s *) stack_top(&stack); }
/* 队首 */ int queue_front (QUEUE* queue) { if (stack_empty (&queue->os)) while (! stack_empty (&queue->is)) stack_push (&queue->os, stack_pop (&queue->is)); return stack_top (&queue->os); }
int main(void) { int a[10]; int i; int data; rand_a(a, 10); show_a(a, 10); stack_t *st = stack_create(sizeof(int), NULL); printf("====== push ======\n"); for (i = 0; i < 10; i++) stack_push(st, &a[i]); printf("====== top ======\n"); stack_top(st, &data); printf("top : %d\n", data); printf("====== pop ======\n"); //while (!stack_isempty(st)) { stack_pop(st, &data); printf("%d ", data); } putchar('\n'); stack_destroy(st); return 0; }
static inline enum bt_btr_status handle_state(struct bt_btr *btr) { enum bt_btr_status status = BT_BTR_STATUS_OK; BT_LOGV("Handling state: btr-addr=%p, state=%s", btr, btr_state_string(btr->state)); switch (btr->state) { case BTR_STATE_NEXT_FIELD: status = next_field_state(btr); break; case BTR_STATE_ALIGN_BASIC: status = align_type_state(btr, btr->cur_basic_field_type, BTR_STATE_READ_BASIC_BEGIN); break; case BTR_STATE_ALIGN_COMPOUND: status = align_type_state(btr, stack_top(btr->stack)->base_type, BTR_STATE_NEXT_FIELD); break; case BTR_STATE_READ_BASIC_BEGIN: status = read_basic_begin_state(btr); break; case BTR_STATE_READ_BASIC_CONTINUE: status = read_basic_continue_state(btr); break; case BTR_STATE_DONE: break; } BT_LOGV("Handled state: btr-addr=%p, status=%s", btr, bt_btr_status_string(status)); return status; }
void main(void) { double opn1, opn2; /* working operands */ char s[LINE_SIZE]; /* input line buffer */ char opr; /* working operator */ char *p; /* buffer cursor */ printf("\nPrefix Form Evaluator"); printf("\n====================="); printf("\n\nInput --> "); gets(s); initial(); /* initial stack */ for (p = s; *p != '\0'; p++) /* for each character */ if (is_opr(*p)) /* is is an operator ? */ push_opr(*p); /* YES, push. */ else if (isdigit(*p)) { /* is it an operand ? */ opn2 = *p - '0'; /* YES, convert to double */ while (stack_top() == OPERAND) { opn1 = pop_opn(); /* for all of its */ opr = pop_opr(); /* buddy, compute */ opn2 = compute(opr, opn1, opn2); } push_opn(opn2); /* then push the result */ } printf("\nResult = %lf", pop_opn()); }
void __cyg_profile_func_exit(void *this_fn, void *call_site) { printf("exit func <= %p\n", this_fn); struct defer_func_ctx *ctx = NULL; printf("in funcexit: stack top is [%d]\n", stack_top()); while ((ctx = stack_pop()) != NULL) { printf("params count is [%d]\n", ctx->params_count); if (ctx->params_count == 0) { printf("defer pop function: [%p]\n", ctx->ctx.zp.df); ctx->ctx.zp.df(); } else if (ctx->params_count == 1) { printf("defer pop function: [%p]\n", ctx->ctx.op.df); ctx->ctx.op.df(ctx->ctx.op.p1); } else if (ctx->params_count == 2) { printf("in exit: df = %p\n", (ctx->ctx).tp.df); printf("in exit: p1 = %p\n", (ctx->ctx).tp.p1); printf("in exit: p2 = %p\n", (ctx->ctx).tp.p2); printf("defer pop function: [%p]\n", ctx->ctx.tp.df); ctx->ctx.tp.df(ctx->ctx.tp.p1, ctx->ctx.tp.p2); } } }
int main(void) { int a[10]; int i; stack_t *st; int data; rand_a(a, 10); show_a(a, 10); st = stack_create(sizeof(int), 10, NULL); printf("=== push ===\n"); for (i = 0; i < st->max; i++ ) stack_push(st, &a[i]); int top; stack_top(st, &top); printf("top is %d\n", top); printf("=== pop ===\n"); while (!stack_isempty(st)) { stack_pop(st, &data); printf("%2d ", data); } putchar('\n'); return 0; }
int main(void) { int method, val; Node *head; head=NULL; puts("Input a method(1 and a value to push, 0 to pop, 2 to show the top) or -1 to quit:"); while(scanf("%d", &method)) { switch(method) { case -1: while(head!=NULL){ stack_pop(&head); } return 0; case 0: stack_pop(&head); break; case 1: scanf("%d", &val); stack_push(&head, val); break; case 2: stack_top(head); break; default: puts("Error Method!!"); break; } stack_display(head); } }
static void do_state_dot(char **pscan) { if (is_digit(**pscan)){ //deal with real, this for extend ; } else if (**pscan == '.'){ ++*pscan; if (**pscan != '.'){ printf("Error: illegally uses '.' -- READ\n"); do_input_error(pscan); } else{ ++*pscan; if (is_delimiter(**pscan) || **pscan == '\0'){ reg = make_symbol("..."); current_state = stack_pop(&state_stack); if (current_state == STATE_QUOTE){ reg = cons(reg, NIL); reg = cons(make_symbol("quote"), reg); current_state = stack_pop(&state_stack); } stack_push(&parser_stack, reg); } else{ printf("Error: illegally uses '.' -- READ\n"); do_input_error(pscan); } } } else if (is_delimiter(**pscan) && stack_top(&state_stack) == STATE_LIST){ stack_push(&parser_stack, make_symbol(".")); current_state = stack_pop(&state_stack); } else{ printf("Error: illegally uses '.' -- READ\n"); do_input_error(pscan); } }
Status push_operator(const Operator *operator, Stack **operands, Stack **operators) { if (!operator) { return ERROR_SYNTAX; } Status status = OK; while (*operators && status == OK) { const Operator *stack_operator = stack_top(*operators); if (operator->arity == OPERATOR_UNARY || operator->precedence < stack_operator->precedence || (operator->associativity == OPERATOR_RIGHT && operator->precedence == stack_operator->precedence)) { break; } status = apply_operator(stack_pop(operators), operands); } stack_push(operators, operator); return status; }
//two args: exp & tail_context static cellpoint eval_or(void) { reg = cdr(args_ref(1)); if (is_true(is_null(reg))){ args_pop(2); return a_false; } stack_push(&vars_stack, reg); while (is_false(is_null(cdr(reg)))){ //evals the expressions which aren't the last expression args_push(a_false); args_push(car(reg)); reg = eval(); if (is_true(reg)){ args_pop(2); return reg; } //renews the remaining expressions stack_push(&vars_stack, cdr(stack_pop(&vars_stack))); reg = stack_top(&vars_stack); } //evals the last expressions args_push(args_ref(2)); args_push(car(stack_pop(&vars_stack))); reg = eval(); args_pop(2); return reg; }
void transfer_rest_of_in_out_stack() { while (!stack_empty(in_out_stack)) { stack_push(postfix_output_stack, stack_top(in_out_stack)); stack_pop(in_out_stack); } }
/* 测试用例 */ int main (void) { STACK stack; stack_init (&stack); int i; for (i = 0; i < 10; ++i) stack_push (&stack, i); printf ("%u\n", stack_size (&stack)); while (! stack_empty (&stack)) printf ("%d ", stack_pop (&stack)); printf ("\n"); /* * 输入任意行字符串,按!结束输入, * 将之前输入的字符串逆向打印 */ char text[1024]; for (;;) { gets (text); if (! strcmp (text, "!")) break; stack_push (&stack, (int)strcpy (malloc ( (strlen(text)+1)*sizeof(text[0])),text)); } while (! stack_empty (&stack)) { puts ((char const*)stack_top (&stack)); free ((void*)stack_pop (&stack)); } stack_deinit (&stack); return 0; }
//one arg: arglst cellpoint vector(void) { int len, i=0; args_push(args_ref(1)); len = get_integer(list_len()); reg = make_vector(len, NIL); stack_push(&vars_stack, args_ref(1)); while (is_false(is_null(stack_top(&vars_stack)))){ vector_set(reg, i, car(stack_top(&vars_stack))); ++i; stack_push(&vars_stack, cdr(stack_pop(&vars_stack))); } stack_pop(&vars_stack); args_pop(1); return reg; }
void test_stack_top(void) { test_start(); struct stack *a = NULL; fail_on_false(stack_empty(&a), "Stack is not empty here"); fail_on_false(stack_top(&a) == NULL, "Top of the stack is not NULL"); const char *HELLO = "hello"; halt_on_true(stack_push(&a, (void *) HELLO), "Failed to push HELLO") fail_on_true(stack_empty(&a), "Stack is empty here"); fail_on_true(stack_top(&a) == NULL, "Top of the stack is NULL"); fail_on_false(stack_top(&a) == HELLO, "The top does not point to HELLO"); stack_pop(&a); test_end(); }
int main () { STACK * stack = NULL; char cmd[256]; printf("push [number] -- Push a number onto stack\n" "pop -- Pop a number from stack\n" "peek -- Peek the number from stack top\n" "exit -- Exit this program\n"); while(1) { // "%255s" limits max string length to 255 bytes // it prevents buffer overflow // but it may modify 256 bytes of memory (with '\0' terminated) scanf("%255s", cmd); if (strcmp(cmd, "push") == 0) { // push int k; scanf("%d", &k); stack = stack_push(stack, k); } else if (strcmp(cmd, "pop") == 0) { // pop if (stack != NULL) { stack = stack_pop(stack); } else { puts("error: stack is empty"); } } else if (strcmp(cmd, "peek") == 0) { // peek if (stack != NULL) { printf("top = %d\n", stack_top(stack)); } else { puts("error: stack is empty"); } } else if (strcmp(cmd, "exit") == 0) { // output stack puts("exit"); while (stack) { printf("top = %d\n", stack_top(stack)); stack = stack_pop(stack); } break; } } return 0; }
void test_stack_push_pop(void) { test_start(); struct stack *st = NULL; void *A = (void *) 0x4354523; void *B = (void *) 0x0918401; void *C = (void *) 0x0809481; void *D = (void *) 0x9084019; halt_on_true(stack_push(&st, A), "Failed to push A"); fail_on_true(stack_empty(&st), "Stack is empty, but A is there"); fail_on_false(stack_depth(&st) == 1, "Stack's depth is not 1"); fail_on_false(stack_top(&st) == A, "Top value is not A"); halt_on_true(stack_push(&st, B), "Failed to push B"); fail_on_true(stack_empty(&st), "Stack is empty, but A, B is there"); fail_on_false(stack_depth(&st) == 2, "Stack's depth is not 2"); fail_on_false(stack_top(&st) == B, "Top value is not B"); halt_on_false(stack_pop(&st) == B, "Popped value is not B"); fail_on_true(stack_empty(&st), "Stack is empty, but A is there"); fail_on_false(stack_depth(&st) == 1, "Stack's depth is not 1"); fail_on_false(stack_top(&st) == A, "Top value is not A"); halt_on_true(stack_push(&st, C), "Failed to push C"); fail_on_true(stack_empty(&st), "Stack is empty, but C, C is there"); fail_on_false(stack_depth(&st) == 2, "Stack's depth is not 2"); fail_on_false(stack_top(&st) == C, "Top value is not C"); halt_on_true(stack_push(&st, D), "Failed to push D"); fail_on_true(stack_empty(&st), "Stack is empty, but D, D is there"); fail_on_false(stack_depth(&st) == 3, "Stack's depth is not 3"); fail_on_false(stack_top(&st) == D, "Top value is not D"); fail_on_false(stack_pop(&st) == D, "Popped value is not D"); fail_on_true(stack_empty(&st), "Stack is empty, but A, C is there"); fail_on_false(stack_pop(&st) == C, "Popped value is not C"); fail_on_true(stack_empty(&st), "Stack is empty, but A is there"); fail_on_false(stack_pop(&st) == A, "Popped value is not A"); fail_on_false(stack_empty(&st), "Stack is not empty"); fail_on_false(stack_pop(&st) == NULL, "Popped value is not NULL"); test_end(); }
void minStackPush(MinStack *stack, int element) { stack_push(&stack->s, element); if (stack_empty(&stack->m)) { stack_push(&stack->m, element); return; } int top = stack_top(&stack->m); stack_push(&stack->m, element<top?element:top); }
static inline Tl_var *decode_dest(void *ptr) { if ((prep_frame == NULL) || ((ta_table[current_line].f != assign_i) && (ta_table[current_line].f != assign_d) && (ta_table[current_line].f != assign_str))) return decode_var(ptr, stack_top(frame_stack)); return decode_var(ptr, prep_frame); }