コード例 #1
0
ファイル: hw1p3.c プロジェクト: carmi/comp356
/**
 * Menu function that handles the event when someone clicks "Animate sorting"
 * or "Exit." In the case of the former, we will set up the subwindows.
 *
 * @param value - the menu item clicked.
 */
void mymenu(int value) {
    if (value == 1) {
        mergesort_counter = 0; quicksort_counter = 0;
        xs_mergesort = generate_random_list();
        xs_quicksort = malloc(xs_size * sizeof(int));
        cp_array(xs_mergesort, xs_quicksort, xs_size);
        
        // push onto the mergesort stack
        mergesort_stack = make_stack();
        int *initial_array = malloc(xs_size * sizeof(int));
        cp_array(xs_mergesort, initial_array, xs_size);
        Node *initial_node = make_node(initial_array, xs_size, unsorted);
        initial_node->start_index = 0;
        initial_node->end_index = xs_size - 1;
        push(mergesort_stack, initial_node);

        // push onto the quicksort stack
        quicksort_stack = make_stack();
        Sublist *initial_list = make_sublist(0, xs_size - 1);        
        push(quicksort_stack, initial_list);

        recreate_subwindows();
        
        glutIdleFunc(update_lists);
        
        in_intro = false;
    }
    if (value == 2)
        exit(0);
}
コード例 #2
0
ファイル: symbol.c プロジェクト: fjames86/fpoly
void symbol_init (int n) {

	if (n < SYMBOL_MINIMUM_STACK) {
		symbol_pool = make_stack (SYMBOL_MINIMUM_STACK);
	} else {
		symbol_pool = make_stack (n);
	}

	symbol_nil = intern ("NIL");
}
コード例 #3
0
ファイル: eval.c プロジェクト: zbenjamin/vmscheme
// eval_sequence may return an object with a refcount of zero.
// This is because it's the interface to the interpreter and so the
// return value may be the result of a computation that only has
// one reference inside the created vm context.  On the other hand
// it may return an object with multiple references (if the object
// returned is referenced in the passed environment, for example).
struct object *
eval_sequence(struct pair *forms, struct environment *env)
{
  struct code *prog = compile(forms);
  INC_REF(&prog->obj);
  struct codeptr *pc = make_codeptr(prog, 0);
  INC_REF(&pc->obj);
  struct stack *stk = make_stack(1024);
  // push magic "end of instructions" return address
  stack_push(stk, NULL);
  stack_push(stk, &env->obj);
  INC_REF(&env->obj);
  struct vm_context *ctx = make_vm_context(pc, stk, env);
  INC_REF(&ctx->obj);
  struct vm_context **pctx = &ctx;

  eval_instructions(pctx);
  struct object *value = stack_pop((*pctx)->stk);

  // decrement the refcount if it's positive, but don't deallocate
  // the object
  if (value->refcount > 0) {
    --(value->refcount);
  }

  DEC_REF(&prog->obj);
  assert(stack_empty((*pctx)->stk));
  DEC_REF(&ctx->obj);
  return value;
}
コード例 #4
0
ファイル: AN386.c プロジェクト: LADSoft/Simple-MSIL-Compiler
/*  Assums the dseg is the next higher seg value from the cseg	*/
void loadds(void)
{
    gen_code(op_push, makesegreg(DS), 0);
    gen_code(op_push, makesegreg(CS), 0);
    gen_codes(op_add, 2, make_stack(0), make_immed(8));
    gen_code(op_pop, makesegreg(DS), 0);
}
コード例 #5
0
int main(void) {
  int buf[MAX_BUF] = {0,};

  if( !input("input.txt", buf) ) {
    fprintf(stderr, "ERROR: file open error or buffer overflow\n");
    delete_all_memory();
    exit(EXIT_FAILURE);
  }
  if(!make_stack(buf)) {
    fprintf(stderr, "ERROR: memory allocation failed\n");
    delete_all_memory();
    exit(EXIT_FAILURE);
  }
  if(!make_queue(buf)) {
    fprintf(stderr, "ERROR: memory allocation failed\n");
    delete_all_memory();
    exit(EXIT_FAILURE);
  }
  if(!make_circle(buf)) {
    fprintf(stderr, "ERROR: memory allocation failed or Queue size overflow\n");
    delete_all_memory();
    exit(EXIT_FAILURE);
  }
  output();

  exit(EXIT_SUCCESS);
}
コード例 #6
0
ファイル: main.c プロジェクト: zbenjamin/vmscheme
int
main(int argc, char* argv[])
{
  init_symbol_table();
  init_builtin_types();
  init_global_env();
  init_singleton_objects();
  init_primitive_procs();

  struct vm_context *global_ctx = make_vm_context(NULL, NULL,
                                                  global_env);
  INC_REF(&global_ctx->obj);
  struct vm_context **pctx = &global_ctx;
  struct object *value;
  value = load("prelude.scm", pctx);
  YIELD_OBJ(value);

  init_compiler();

  value = load("stage2.scm", pctx);
  YIELD_OBJ(value);

  struct vm_context *repl_ctx;
  repl_ctx = make_vm_context(NULL, make_stack(1024),
                             make_environment(global_env));
  INC_REF(&repl_ctx->obj);
  pctx = &repl_ctx;

  struct object *ret = env_lookup(global_env, "initial-repl");
  assert(ret->type->code == PROCEDURE_TYPE);
  struct procedure *repl = container_of(ret, struct procedure, obj);
  apply_and_run(repl, NIL, pctx);

  return 0;
}
コード例 #7
0
ファイル: match.c プロジェクト: jff/mathspad
static int lex_start(char *text)
{
    mtext = (unsigned char*) text;
    make_stack();
    stackpos=0;
    lex_pos=0;
    return 0;
}
コード例 #8
0
ファイル: queue.c プロジェクト: kennyyu/cs50section
queue *make_queue() {
  queue *qu = malloc(sizeof(queue));
  if (!qu)
    return NULL;
  qu->stack_in = make_stack();
  if (!qu->stack_in) {
    free(qu);
    return NULL;
  }    
  qu->stack_out = make_stack();
  if (!qu->stack_out) {
    free_stack(qu->stack_in);
    free(qu);
    return NULL;
  }
  return qu;
}
コード例 #9
0
ファイル: sortstack.c プロジェクト: ShengYun/algorithms
int main() {
    stack_t *s = make_stack();
    s->push(s, 1)->push(s, 4)->push(s, 2)->push(s, 8);
    sort(s);
    
    while (!s->is_empty(s))
        printf("%3d", s->pop(s));
        
    
}
コード例 #10
0
ファイル: backtrace.cpp プロジェクト: pippijn/purplebee
static bool
backtrace_test ()
{
  free (resolve_symbol (puts));
  free (resolve_symbol (puts));
#if 0
  for (int i = 0; i < 10; i++)
    make_stack ();
#endif
  exit (0);
  return true;
}
コード例 #11
0
ファイル: parser.c プロジェクト: reginaldford/ccx
void prepare_to_parse(void)
{
	reset();
	scope_stack = make_stack(128,4,0);
	push(scope_stack,lobby);
	current_col_size = 0;
	current_node = cons(NULL,NULL);
	lexical_stack = malloc(sizeof(double));
	*lexical_stack=make_cd(LIVEQ,peek(scope_stack),current_node);
	on_string = false;
	on_comment = false;	
}
コード例 #12
0
ファイル: cilk_fiber-unix.cpp プロジェクト: WojciechMigda/gcc
cilk_fiber_sysdep::cilk_fiber_sysdep(std::size_t stack_size)
    : cilk_fiber(stack_size)
    , m_magic(magic_number)
{
    // Set m_stack and m_stack_base.
    make_stack(stack_size);

    // Get high-address of stack, with 32-bytes of spare space, and rounded
    // down to the nearest 32-byte boundary.
    const uintptr_t align_mask = 32 - 1;
    m_stack_base -= ((std::size_t) m_stack_base) & align_mask;
}
コード例 #13
0
ファイル: sortstack.c プロジェクト: ShengYun/algorithms
void sort(stack_t *s) {
    if (s == NULL || s->is_empty(s))
        return;
    
    int buf;
    stack_t *r = make_stack();
    
    while (!s->is_empty(s)) {
        buf = s->pop(s);
        while (!r->is_empty(r) && r->peak(r) > buf)
            s->push(s, r->pop(r));
        r->push(r, buf);
    }
    
    memcpy(s, r, sizeof(stack_t));
    destroy_stack(r);
}
コード例 #14
0
ファイル: machine.c プロジェクト: yoanncouillec/c-scheme
struct TERM * debruijn_to_term (struct DEBRUIJN * debruijn, struct STACK * stack) {
  switch (debruijn->type) {
  case TYPE_DEBRUIJN_INTEGER : {
    TRACE("integer");
    return make_term_integer(debruijn->integer.value);
    break;
  }
  case TYPE_DEBRUIJN_VARIABLE : {
    TRACE("variable");
    struct STACK * v = get_stack (stack, debruijn->variable.value);
    return make_term_variable (v->ident);
    break;
  }
  case TYPE_DEBRUIJN_QUOTE : {
    TRACE("quote");
    return make_term_quote (debruijn_to_term (debruijn->quote.value, make_stack()));
    break ; 
  }
  case TYPE_DEBRUIJN_ABSTRACTION : {
    TRACE("abstraction");
    char * var = gen_ident();
    struct STACK * s = set_stack(stack, var, NULL); // We just want to get ident
    return make_term_abstraction (var,debruijn_to_term(debruijn->abstraction.body, s));
    break ; 
  }
  case TYPE_DEBRUIJN_CLOSURE : {
    TRACE("closure");
    char * var = gen_ident();
    struct STACK * s = set_stack(stack, var, NULL); // We just want to get ident
    return make_term_closure (var,debruijn_to_term(debruijn->abstraction.body, s), NULL); // Shouldn't be NULL. Should be stack->env transformation
    break ; 
  }
  case TYPE_DEBRUIJN_APPLICATION : {
    TRACE("application");
    return make_term_application(debruijn_to_term(debruijn->application.left, stack),
				 debruijn_to_term(debruijn->application.right, stack));
    break;
  }
  default:{
    fprintf(stderr, "Unknown debruijn term type\n");
    exit(1);
  }
  }
}
コード例 #15
0
ファイル: stack.c プロジェクト: DRiKE/bspwm
void stack_insert_before(stacking_list_t *a, node_t *n)
{
	stacking_list_t *s = make_stack(n);
	if (a == NULL) {
		stack_head = stack_tail = s;
	} else {
		if (a->node == n)
			return;
		remove_stack_node(n);
		stacking_list_t *b = a->prev;
		if (b != NULL)
			b->next = s;
		s->prev = b;
		s->next = a;
		a->prev = s;
		if (stack_head == a)
			stack_head = s;
	}
}
コード例 #16
0
ファイル: continuation.c プロジェクト: zbenjamin/vmscheme
struct vm_context *
clone_context(struct vm_context *ctx)
{
  struct codeptr *new_pc = make_codeptr(ctx->pc->base,
                                        ctx->pc->offset);

  struct stack *new_stack = make_stack(ctx->stk->size);
  int i;
  for (i = 0; i < ctx->stk->top; ++i) {
    stack_push(new_stack, ctx->stk->elems[i]);
    if (ctx->stk->elems[i]) {
      INC_REF(ctx->stk->elems[i]);
    }
  }

  struct vm_context *new_ctx;
  new_ctx = make_vm_context(new_pc, new_stack, ctx->env);
  return new_ctx;
}  
コード例 #17
0
ファイル: stack.c プロジェクト: guyhughes/bspwm
void stack_insert_after(stacking_list_t *a, node_t *n)
{
    stacking_list_t *s = make_stack(n);
    if (a == NULL) {
        stack_head = stack_tail = s;
    } else {
        if (a->node == n) {
            free(s);
            return;
        }
        remove_stack_node(n);
        stacking_list_t *b = a->next;
        if (b != NULL)
            b->prev = s;
        s->next = b;
        s->prev = a;
        a->next = s;
        if (stack_tail == a)
            stack_tail = s;
    }
}
コード例 #18
0
void print_preorder_no_recursion(bst * root) {
	int isFirst = 1;
	bst * curr = NULL;
	stack * stack = make_stack();

	push(&stack, root);
	while(!stack_is_empty(stack)) {
		bst * curr = pop(&stack);
		if(is_empty(curr)) {
			continue;
		}
		if(!isFirst) {
			printf(", ");
		} else {
			isFirst = 0;
		}
		printf("%d->%d", curr->key, curr->value);
		push(&stack, curr->right);
		push(&stack, curr->left);
	}
	printf("\n");
}
コード例 #19
0
ファイル: fileop.c プロジェクト: teddokano/CaFE2
void save_stack( stack *trg_p, FILE *fp )
{
	stack			*stack_p;
	stack_item		*si_p;
	string_object	s;

	if ( trg_p )
		fprintf( fp, "\n{ " );

	if ( NULL == (stack_p	= make_stack()) )
	{
		cprintf( ERROR, CONT, "unrecoverable @ show_stack (1)\n" );
		exit ( 1 );
	}


	stack_rcopy( stack_p, trg_p );
	
	si_p		= stack_p->stack_top_p;

	while ( si_p )
	{
		if ( si_p->type == STACK )
		{
			save_stack( *((stack **)si_p->item_p), fp );
		}
		else
		{
			s	= ui_stack_item_to_string( si_p, -1, STRING_WITH_QUOTE );
			fprintf( fp, "%s ", s );
			dispose_string_object( s );
		}
		si_p	= si_p->next;
	}
	dispose_stack( stack_p );

	if ( trg_p )
		fprintf( fp, "k}\n" );
}
コード例 #20
0
void main(){
	stack s;
	make_stack(&s);
	push(1, &s);
	push(2, &s);
	push(3, &s);
	push(4, &s);
	push(5, &s);
	push(6, &s);
	push(7, &s);
	push(8, &s);
	push(9, &s);
	push(10, &s);
	//print_stack(&s);
	push(11, &s);
	//print_stack(&s);
	pop(&s);
	print_stack(&s);
	free_stack(&s);
	print_stack(&s);

}
コード例 #21
0
ファイル: main.c プロジェクト: johnbartholomew/smalisp
int main(int argc, char *argv[])
{
	int result = 0;
	ref_t val, answer, assoc, name;
	FILE *input_fl = stdin, *output_fl = stdout;
	clock_t start_time, end_time;
	
#define FREE_AND_RETURN(x) {result = x; goto free_and_return;}

	if (parse_command_line(argc, argv, cmd_opt_decls) == -1)
	{
		printf("%s\n\n", get_error());
		print_usage();
		FREE_AND_RETURN(1);
	}

	if (help_flag)
	{
		print_usage();
		FREE_AND_RETURN(0);
	}

	if (input_fname)
	{
		input_fl = fopen(input_fname, "r");
		if (input_fl == 0)
		{
			printf("Could not open input file %s\n", input_fname);
			FREE_AND_RETURN(1);
		}
	}

	if (output_fname)
	{
		output_fl = fopen(output_fname, "w");
		if (output_fl == 0)
		{
			printf("Could not open output file %s\n", output_fname);
			FREE_AND_RETURN(1);
		}
	}

	if (trace_file_fname)
	{
		trace_fl = fopen(trace_file_fname, "w");
		if (trace_fl == 0)
		{
			printf("Could not open trace file %s\n", trace_file_fname);
			FREE_AND_RETURN(1);
		}
	}

	assoc = make_stack(nil());
	register_gc_root(assoc);
	stack_enter(assoc);

	name = make_symbol("t", 0);
	stack_let(assoc, name, name);
	release_ref(&name);
	
	register_core_lib(assoc);

	REG_FN(exit, assoc);
	REG_FN(trace, assoc);
	REG_FN(profile, assoc);
	REG_NAMED_FN("no-trace", slfe_no_trace, assoc);
	REG_NAMED_FN("dump-stack", slfe_dump_stack, assoc);

	if (trace_fl)
		set_trace_file(trace_fl);

	start_time = clock();

	finished = 0;
	while (! finished)
	{
		if (input_fl == stdin)
			printf("> ");

		val = read(input_fl);
		answer = eval(val, assoc);
		release_ref(&val);
		if (!quiet_flag)
		{
			println(answer, output_fl);
			fflush(output_fl);
		}
		release_ref(&answer);
		collect_garbage();
	}

	stack_enter(nil());
	unregister_gc_root(assoc);
	release_ref(&assoc);

	collect_garbage();

	end_time = clock();

	if (trace_fl)
		fprintf(trace_fl, "Total time taken: %f seconds\n", (float)(end_time - start_time) / (float)CLOCKS_PER_SEC);

#undef FREE_AND_RETURN

free_and_return:
	if (trace_fl && stats_flag)
		fprintf(trace_fl, "Total symbol evals: %d; total stack switches: %d\n", symbol_eval_count, stack_switch_count);

	if (input_fl != stdin) fclose(input_fl);
	if (output_fl != stdout) fclose(output_fl);
	if (trace_fl) fclose(trace_fl);

	if (input_fname) X_FREE(input_fname);
	if (output_fname) X_FREE(output_fname);
	if (trace_file_fname) X_FREE(trace_file_fname);

	return result;
}
コード例 #22
0
ファイル: GSTMT386.C プロジェクト: BGCX261/zlmtank-svn-to-git
void genreturn(SNODE *stmt, int flag)
/*
 *      generate a return statement.
 */
{       AMODE    *ap,*ap1;
				int size;
        if( stmt != 0 && stmt->exp != 0 ) {
                initstack();
								if (currentfunc->tp->btp && currentfunc->tp->btp->type != bt_void && (currentfunc->tp->btp->type == bt_struct || currentfunc->tp->btp->type == bt_union)) {
									size = currentfunc->tp->btp->size;
									ap = gen_expr(stmt->exp,F_ALL,4);
									if (!(save_mask & 0x40))
										gen_push(ESI,am_dreg,0);                                  
									if (!(save_mask & 0x80))
										gen_push(EDI,am_dreg,0);                                  
									if (prm_linkreg) {
										ap1 = xalloc(sizeof(AMODE));
										ap1->preg = EBP;
										ap1->mode = am_indisp;
										if (currentfunc->pascaldefn && currentfunc->tp->lst.head && currentfunc->tp->lst.head != (SYM *) -1) 
											ap1->offset = makenode(en_icon,(char *)(currentfunc->tp->lst.head->value.i+((currentfunc->tp->lst.head->tp->size+3) &0xFFFFFFFCL)),0);
										else
											ap1->offset = makenode(en_icon,(char *)8,0);
									}
									else
										if (currentfunc->pascaldefn && currentfunc->tp->lst.head && currentfunc->tp->lst.head != (SYM *) -1) 
											ap1 = make_stack(-stackdepth-framedepth-currentfunc->tp->lst.head->value.i-((currentfunc->tp->lst.head->tp->size+3) & 0xfffffffcL));
										else
											ap1 = make_stack(-stackdepth-framedepth);
									gen_code(op_mov,4,makedreg(ESI),ap);		                  
									gen_code(op_mov,4,makedreg(EDI),ap1);
									gen_code(op_mov,4,makedreg(EAX),makedreg(EDI));
									gen_code(op_mov,4,makedreg(ECX),make_immed(size));  
									gen_code(op_cld,0,0,0);
									gen_code(op_rep,1,0,0);                                   
									gen_code(op_movsb,1,0,0);		                              
									if (!(save_mask & 0x80))
										gen_pop(EDI,am_dreg,0);
									if (!(save_mask & 0x40))
										gen_pop(ESI,am_dreg,0);
								}
								else {
									size = currentfunc->tp->btp->size;
  	              ap = gen_expr(stmt->exp,F_DREG | F_FREG,size);
									if (size > 4) {
										if (ap->mode != am_freg)
											gen_code(op_fld,size,ap,0);
									}
            	    else
              	  		if( ap->mode != am_dreg || ap->preg != 0 )
                	        gen_code(op_mov,size,makedreg(0),ap);
                }
								freeop(ap);
				}
				if (flag) {
	        if( retlab != -1 )
                gen_label(retlab);
          if( fsave_mask != 0 )
						diag("Float restore in return");
					if (!prm_linkreg && lc_maxauto)
						gen_code(op_add,4,makedreg(ESP),make_immed(lc_maxauto));
					if (currentfunc->intflag) {
						gen_code(op_popad,0,0,0);
						if (prm_linkreg && (lc_maxauto || currentfunc->tp->lst.head && currentfunc->tp->lst.head != (SYM *)-1)) {
							gen_code(op_leave,0,0,0);
						}
           	gen_code(op_iretd,0,0,0);
					}
					else {
           	if( save_mask != 0 )
							popregs(save_mask);
						if (prm_linkreg && (lc_maxauto || currentfunc->tp->lst.head && currentfunc->tp->lst.head != (SYM *)-1)) {
							gen_code(op_leave,0,0,0);
						}
						if (currentfunc->pascaldefn) {
							long retsize = 0;
							if (currentfunc->tp->lst.head && currentfunc->tp->lst.head != (SYM *)-1) {
								retsize = currentfunc->tp->lst.head->value.i+((currentfunc->tp->lst.head->tp->size + 3) & 0xfffffffcL);
								if (prm_linkreg)
									retsize -= 8;
							}		
							if (currentfunc->tp->btp && currentfunc->tp->btp->type != bt_void && (currentfunc->tp->btp->type == bt_struct || currentfunc->tp->btp->type == bt_union)) 
								retsize += 4;
							if (retsize) {
            		gen_code(op_ret,0,make_immed(retsize),0);
								return;
							}
						}		
            gen_code(op_ret,0,0,0);
					}
				}
        else {
					if (retlab == -1)
            retlab = nextlabel++;
          gen_code(op_jmp,0,make_label(retlab),0);
				}
}
コード例 #23
0
int main(int argc, char **argv) {
  FILE *f;
  char line[LINE_SIZE], *p;

  // open file passed as argument
  if (argc < 2 || !(f = fopen(argv[1], "r"))) {
    fprintf(stderr, "Unable to open file argument\n");
    return 1;
  }

  // read lines from file
  while (fgets(line, LINE_SIZE, f)) {
    // possibly remove the trailing '\n'
    if ((p = strchr(line, '\n'))) { *p = '\0'; }

    // skip empty lines
    if (line[0] == '\0') { continue; }

    /*********************/
    /*** DO LINE ***/
    /*********************/

    // keep stack of encountered symbols
    stack_struct* symbols = make_stack();

    int i; bool b = true;
    for (i = 0; i < strlen(line); i++) {
      char symbol = line[i];
      //printf("SYMBOL %c\n", symbol);
      if (open(symbol)) {
        // if open, push onto stack
        push(symbols, symbol);
      }
      else if (close(symbol)) {
        // if close, make sure it's valid
        b = (b && match(pop(symbols), symbol));
        // if invalid, exit loop
        if (!b) {
          break;
        }
      }
      else {
        printf("ERROR, unknown symbol.\n");
        return 1;
      }
    }

    if (b && (symbols->curr == 0)) {
      printf("True\n");
    } else {
      printf("False\n");
    }

    /**************************/
    /*** /END DO LINE ***/
    /**************************/

  }


  if (ferror(f)) {
    perror("I/O Error");
  }

  return 0;
}