Esempio n. 1
0
HuffNode* Huff_char(FILE* fptr)
{

  	char number = 0;
  	int count = 0;
	while(fscanf(fptr, "%c", &number) == 1)
	{
		count++;
		// printf("Number = %d \n", number);
		//printf("Number = %d \n", number);
		if(number == 10)
		{
			if(fscanf(fptr, "%c", &number) == 1)
			{
				// printf("InsecondNumber = %d \n", number);
				if(number == 48 || number == 49)
				{
					count++;
				}
				else
				{
					break;
				}
			}
		}
	}
	char * data = malloc(sizeof(char) * count);
	fseek(fptr, 0, SEEK_SET );
	int i = 0;
	for(i = 0; i < count ; i++)
	{
		if(fscanf(fptr, "%c", &data[i]) == 1)
		{
		
		}
		else
		{
			printf("ERROR getting values \n");
			break;
		}
	}
	//fclose(fptr);
	// printf("InsecondNumber1 = %d \n", data[0]);
	// printf("InsecondNumber1last = %d \n", data[count - 1]);
	// int number_of_value_1 = 0;
	// fscanf(fptr, "%d", &number_of_value_1);
	// printf("\nlength: %d\n", number_of_value_1);
		
	// printf("count = %d, dacount = %d", count, data[count]);
	int b = 0;
	Stack* hufftree = NULL;
	HuffNode* NewNode = NULL;
	HuffNode* Node1 = NULL;
	HuffNode* Node2 = NULL;
	while(b < count)	
	{
		if(data[b] == 49)
		{
			b++;
			
			NewNode = Huff_new(data[b]);

			hufftree = Stack_push(NewNode, hufftree); 
			//Add to stack push
		}
		
		if(data[b] == 48)
		{
		    Node1 = hufftree->top;
			hufftree = Stack_pop(hufftree);
			if(hufftree == NULL)
			{
				free(data);
				return Node1;
				
			}
			
			else
			{
				Node2 = hufftree->top;
				hufftree = Stack_pop(hufftree);
				NewNode = Huff_new(0);
				NewNode->right = Node1;
				NewNode->left = Node2;
				hufftree = Stack_push(NewNode, hufftree); 
			}
			// combine two nodes and put new node with left right there
			// pop the stack and combine the newest 2 huff nodes
			// and put this back into stack
			// manuallly end it
			// POP THE STACK TO GET ANOTHER NODE //node b
			// CREATE A NODE AS THE PARENT OF THE TWO NODES //node a and node b share a parent
			// pus the parent node to the stack
			// -> take latest 2 bulit leaf nodes and make them share the same leaf node//hint stack
		}
		b++;
		 // Huff_postOrderPrint(NewNode);
		 // printf("AAAAAAAAAAAAA and b = %d\n", b-1);
	}
	free(data);
	return Node1;
}
Esempio n. 2
0
void UnlambdaEval_eval(UnlambdaEval* self, char* xs){
  char x;
  Stack* stack;
  Memory * memory;

  stack = UnlambdaEval_getStack(self);
  memory = UnlambdaEval_getMemory(self);

  while (*xs){
    x = *xs;

    switch(x){
      case '`':
        Stack_push(stack, quote());
        break;

      case '.':
        xs++;
        Stack_push(stack, (print(*xs)));
        break;

      case 'r':
        Stack_push(stack, (print('\n')));
        break;

      case 'i':
        Stack_push(stack, identity());
        break;

      case 'k':
        Stack_push(stack, constant_function());
        break;

      case 's':
        Stack_push(stack, generalized_evaluation());
        break;

      default:
        break;
    }
    while (runnable()){
      if(World_getDebug(getWorld()))
        Stack_print(stack);
      UnlambdaEval_runOnce(self);
      Memory_mark(memory, stack);
      Memory_sweep(memory);
      if(World_getDebug(getWorld()))
        Memory_stat(memory);
    }
    xs++;
  }
  while (runnable()){
    if(World_getDebug(getWorld()))
      Stack_print(stack);
    UnlambdaEval_runOnce(self);
    Memory_mark(memory, stack);
    Memory_sweep(memory);
    if(World_getDebug(getWorld()))
      Memory_stat(memory);
  }

  return;
}
Esempio n. 3
0
HuffNode* bit_input(FILE* fptr)
{
	// uint8_t ptr;
	// size_t bytesnum = 1;
	// int count = 0;
	// int a = 0;
	// FILE * fh = fopen(filename, "r");
	if(fptr == NULL)
	{
		return NULL;
	}
	// // while(bytesnum == 1)
	// // {
		// // count++;
		 // // // printf("first = %d, bytesnbum = %d", ptr, bytesnum);
		// // bytesnum = fread(&ptr, 1, 1, fh);
		 // // // printf("first = %d, bytesnbum = %d", ptr, bytesnum);
		
	// // }
	// // fseek(fh, 0, SEEK_SET );
	// // uint8_t * bitdata = malloc(sizeof(uint8_t) * count);
	// // uint8_t * bitdata2 = bitdata;
	
	// // for( a = 0; a < count; a++)
	// // {
		// // bytesnum = fread(&bitdata[a], 1, 1, fh);
		// // printf("second = %d", bitdata[a]);
	// // }

	// // // printf("data = %d", ptr.data);
	// // //fclose(fh);
	
	// // free(bitdata);
	bitfile* bp = bitopen(fptr);
	Stack* hufftree = NULL;
	HuffNode* NewNode = NULL;
	HuffNode* Node1 = NULL;
	HuffNode* Node2 = NULL;
	if(bp == NULL)
	{
		printf("Failed to open file \n");
		return NULL;
	}
	int byte;
	int bit = 0;
	// fprintf(stderr, "Printinf to stderr just test '%s' \n", bit_filename); //./a.out 2>/dev/null 2 streams, error stream not going to be printed int that circumstance
	// int bit = 0;
	
	while((bit = getbit(bp)) >= 0)
	{
		//printf("%d \n", bit);
	
	//bit = getbit(bp);
		if(bit == 1)
		{
			byte = getbyte(bp);
			//printf("%c", byte);
			NewNode = Huff_new(byte);

			hufftree = Stack_push(NewNode, hufftree); 
			
		}
		else
		{
				Node1 = hufftree->top;
				hufftree = Stack_pop(hufftree);
				if(hufftree == NULL)
				{
					//free(data);
					bitclose(bp);
					return Node1;
					
				}
				
				else
				{
					Node2 = hufftree->top;
					hufftree = Stack_pop(hufftree);
					NewNode = Huff_new(0);
					NewNode->right = Node1;
					NewNode->left = Node2;
					hufftree = Stack_push(NewNode, hufftree); 
				}
			//getbit(bp);
		}
	}
	// while(byte = getByte(bp)) >= 0)
	// {
		// fprintf("%x \n", byte);
	// }
	
	bitclose(bp);
	return NULL;
}
Esempio n. 4
0
int main(int argc, char* argv[]){
	int c;

	// initialization
	sp = Stack_new();
	Fmt_register('D', AP_fmt);

	while ((c = getchar())  != EOF) 
		switch (c) {
			// cases
			case ' ': case '\t': case '\n': case '\f': case '\r':
				break;

			case '0': case '1': case '2': case '3': case '4':
			case '5': case '6': case '7': case '8': case '9': {
				char buf[512];
				// gather up digits into buf
				int i = 0;
				for (; c != EOF && isdigit(c); c = getchar(), i++)
					if (i < (int)sizeof (buf) - 1) buf[i] = c;
				if (i > (int)sizeof (buf) - 1) {
					i = (int)sizeof (buf) - 1;
					Fmt_fprint(stderr, "?integer constant exceeds %d digits\n", i);
				}
				buf[i] = 0;
				if (c != EOF)
					ungetc(c, stdin);

				Stack_push(sp, AP_fromstr(buf, 10, NULL));
				break;
			}

			case '+': {
				// pop x and y off the stack
				AP_T y = pop(), x = pop();

				Stack_push(sp, AP_add(x, y));

				// free x and y
				AP_free(&x);
				AP_free(&y);

				break;
			}

			case '-': {
				// pop x and y off the stack
				AP_T y = pop(), x = pop();

				Stack_push(sp, AP_sub(x, y));

				// free x and y
				AP_free(&x);
				AP_free(&y);

				break;
			}

			case '*': {
				// pop x and y off the stack
				AP_T y = pop(), x = pop();

				Stack_push(sp, AP_mul(x, y));

				// free x and y
				AP_free(&x);
				AP_free(&y);

				break;
			}

			case '/': {
				// pop x and y off the stack
				AP_T y = pop(), x = pop();

				if (AP_cmpi(y, 0) == 0) {
					Fmt_fprint(stderr, "?/ by 0\n");
					Stack_push(sp, AP_new(0));
				} else {
					Stack_push(sp, AP_div(x, y));
				}

				// free x and y
				AP_free(&x);
				AP_free(&y);

				break;
			}

			case '%': {
				// pop x and y off the stack
				AP_T y = pop(), x = pop();

				if (AP_cmpi(y, 0) == 0) {
					Fmt_fprint(stderr, "?%% by 0\n");
					Stack_push(sp, AP_new(0));
				} else {
					Stack_push(sp, AP_mod(x, y));
				}

				// free x and y
				AP_free(&x);
				AP_free(&y);

				break;
			}

			case '^': {
				// pop x and y off the stack
				AP_T y = pop(), x = pop();

				if (AP_cmpi(y, 0) <= 0) {
					Fmt_fprint(stderr, "?nonpositive power\n");
					Stack_push(sp, AP_new(0));
				} else {
					Stack_push(sp, AP_pow(x, y, NULL));
				}

				// free x and y
				AP_free(&x);
				AP_free(&y);

				break;
			}

			case 'd': {
				AP_T x = pop();
				Stack_push(sp, x);
				Stack_push(sp, AP_addi(x, 0));
				break;
			}

			case 'p': {
				AP_T x = pop();
				Fmt_print("%D\n", x);
				Stack_push(sp, x);
				break;
			}

			case 'f': {
				if (!Stack_empty(sp)) {
					Stack_T tmp = Stack_new();
					while (!Stack_empty(sp)) {
						AP_T x = pop();
						Fmt_print("%D\n", x);
						Stack_push(tmp, x);
					}
					while (!Stack_empty(tmp))
						Stack_push(sp, Stack_pop(tmp));
					Stack_free(&tmp);
				}
				break;
			}

			case '~': {
				AP_T x = pop();
				Stack_push(sp, AP_neg(x));
				AP_free(&x);
				break;
			}

			case 'c': 
				// clear the stack
				while (!Stack_empty(sp)) {
					AP_T x = Stack_pop(sp);
					AP_free(&x);
				}
				break;

			case 'q':
				// clean up end exit
				// clear the stack
				while (!Stack_empty(sp)) {
					AP_T x = Stack_pop(sp);
					AP_free(&x);
				}
				Stack_free(&sp);
				return EXIT_SUCCESS;

			default:
				if (isprint(c))
					Fmt_fprint(stderr, "?'%c'", c);
				else
					Fmt_fprint(stderr, "?'\\%03o'", c);
				Fmt_fprint(stderr, " is unimplemented\n");
				break;
		}

	// clean up end exit
	// clear the stack
	while (!Stack_empty(sp)) {
		AP_T x = Stack_pop(sp);
		AP_free(&x);
	}
	Stack_free(&sp);
	return EXIT_SUCCESS;
}
/*                  EVALUATE THE EXPRESSION:                         */
static void evaluate(Token_T* oTokens, int length) {
    Stack_T oStack1;
    Stack_T oStack2;
    int i;
    /* Create and use a Stack of tokens. */
    assert(oTokens != NULL);
    oStack1 = Stack_new();
    oStack2 = Stack_new();
    for (i = 0; i < length; i++) {
        if (Token_getType(oTokens[i]) == VALUE)
            Stack_push(oStack1, oTokens[i]);
        else if (Stack_isEmpty(oStack2))
            Stack_push(oStack2, oTokens[i]);
        else if (precedence(oTokens[i], Stack_top(oStack2)))
            Stack_push(oStack2, oTokens[i]);
        else { /*ERROR HANDLING?*/
            int result;
            Token_T value_1 = (Token_T)Stack_top(oStack1);
            Stack_pop(oStack1);
            Token_T operator = (Token_T)Stack_top(oStack2);
            Stack_pop(oStack2);
            Token_T value_2 = (Token_T)Stack_top(oStack1);
            Stack_pop(oStack1);
            switch (Token_getType(operator))
            {   /*LEFT TO RIGHT EVALUATION*/
            case MULT:
                result = Token_getValue(value_1) * Token_getValue(value_2);
                break;
            case DIV:
                result = Token_getValue(value_2) / Token_getValue(value_1);
                break;
            case PLUS:
                result = Token_getValue(value_1) + Token_getValue(value_2);
                break;
            case MINUS:
                result = Token_getValue(value_2) - Token_getValue(value_1);
                break;
            default:
                assert(0);
            }
            Stack_push(oStack1, Token_newToken(VALUE, result));
            Stack_push(oStack2, oTokens[i]);
        }
    }
    while (!Stack_isEmpty(oStack2)) { /*ERROR HANDLING? + MODULARITY MUCH????*/
        int result;
        Token_T value_1 = (Token_T)Stack_top(oStack1);
        Stack_pop(oStack1);
        Token_T operator = (Token_T)Stack_top(oStack2);
        Stack_pop(oStack2);
        Token_T value_2 = (Token_T)Stack_top(oStack1);
        Stack_pop(oStack1);
        switch (Token_getType(operator))
        {   /*LEFT TO RIGHT EVALUATION*/
        case MULT:
            result = Token_getValue(value_1) * Token_getValue(value_2);
            break;
        case DIV:
            result = Token_getValue(value_2) / Token_getValue(value_1);
            break;
        case PLUS:
            result = Token_getValue(value_1) + Token_getValue(value_2);
            break;
        case MINUS:
            result = Token_getValue(value_2) - Token_getValue(value_1);
            break;
        default:
            assert(1);
        }
        Stack_push(oStack1, Token_newToken(VALUE, result));
    }
    printf("%d\n", Token_getValue((Token_T)Stack_top(oStack1)));
    Stack_free(oStack1);
    Stack_free(oStack2);
}
Esempio n. 6
0
/**
 * @if conly
 * @memberof ASTNode_t
 * @endif
 */
LIBSBML_EXTERN
ASTNode_t *
SBML_parseFormula (const char *formula)
{
  long rule, state, action;

  ASTNode_t *node = NULL;

  FormulaTokenizer_t *tokenizer = NULL; 
  Stack_t            *stack     = NULL;
  Token_t            *token     = NULL;

  if (formula == NULL) return NULL;
  
  tokenizer = FormulaTokenizer_createFromFormula(formula);
  token     = FormulaTokenizer_nextToken(tokenizer);
  stack     = Stack_create(20);


  Stack_push(stack, (void *) START_STATE);

  while (1)
  {
    state  = (long) Stack_peek(stack);
    action = FormulaParser_getAction(state, token);

    if (action == ACCEPT_STATE)
    {
      node = Stack_peekAt(stack, 1);
      break;
    }

    else if (action == ERROR_STATE)
    {
      /**
       * Free ASTNodes on the Stack, skip the states.
       */
      while (Stack_size(stack) > 1)
      {
        Stack_pop(stack);
        ASTNode_free( Stack_pop(stack) );
      }

      node = NULL;
      break;
    }

    /**
     * Shift
     */
    else if (action > 0)
    {
      Stack_push( stack, ASTNode_createFromToken(token) );
      Stack_push( stack, (void *) action );

      Token_free(token);
      token = FormulaTokenizer_nextToken(tokenizer);
    }

    /**
     * Reduce
     */
    else if (action < 0)
    {
      rule  = -action;
      node  = FormulaParser_reduceStackByRule(stack, rule);
      state = (long) Stack_peek(stack);

      Stack_push( stack, node );
      Stack_push( stack, (void *) FormulaParser_getGoto(state, rule) );
    }
  }

  FormulaTokenizer_free(tokenizer);
  Stack_free(stack);
  Token_free(token);

  return node;
}
Esempio n. 7
0
int main(int argc, char *argv[]){
    struct Stack *stack = Stack_new(10);
    int i=0;

    Stack_push(stack, 5);
    Stack_push(stack, 50);
    Stack_push(stack, 1000);
    Stack_push(stack, 25);
    Stack_push(stack, 25);
    Stack_push(stack, 25);
    Stack_push(stack, 25);
    Stack_push(stack, 25);
    Stack_push(stack, 25);
    Stack_push(stack, 25);
    Stack_push(stack, 25);
    Stack_push(stack, 25);
    Stack_push(stack, 25);
    Stack_push(stack, 25);
    Stack_push(stack, 25);
    Stack_push(stack, 25);

    for(i=0; i < stack->pos; i++){
        printf("Popped %d from the stack (pos %d)\n", stack->store[i], i);
    }
    Stack_destroy(stack);
    return 0;
}
Esempio n. 8
0
File: thread.c Progetto: cli/bean
    assert(frame->method != NULL);
}

void* Stackframe_dispose(Stackframe* frame) {
    free(frame->localVars);
    free(frame);
    return NULL;
}

Stackframe* Stackframe_create_init_push(Thread* thread, Class* class, Method* method) {
    // Create stackframe for main method
    Stackframe *stackframe = malloc(sizeof(Stackframe));
    Stackframe_init(stackframe, method, class->ConstantPool);

    // and push it onto thread's invocation stack
    int ret = Stack_push(thread->frameStack, stackframe);
    assert(0 == ret); // check for unexpected stack overflow
    return stackframe;
}

int start_process(FILE* class_file)
{
    Method* mainMethod = NULL;
    Method* clinitMethod = NULL;

    /* Create main thread */
    vm->ThreadNum++;
    vm->Threads = (Thread *)realloc(0, sizeof(Thread));

    /* Initialize main thread */
    init_thread(&vm->Threads[0]);