Exemplo n.º 1
0
static int opr_handler(struct calc *calc)
{
	assert(calc != NULL);
	char c = *calc->cur;
	opr_advance(calc);
	/* 如果操作符栈是空栈的话就压栈 */
	if (STACK_IS_EMPTY(calc->opr)) {
		OPR_PUSH(c);
		return 0;
	}

	/* 如果操作符栈非空, 且该操作符优先级高于栈顶运算符就压栈 */
	char top_c = 0;
	if (OPR_TOP(top_c) < 0) {
		fprintf(stderr, "Error: opr top failed.\n");
		return -1;
	}
	
	while(1) {
		if (opr2level(c) > opr2level(top_c) || STACK_IS_EMPTY(calc->opr)) {
			OPR_PUSH(c);
			return 0;
		}
		if (do_operate(calc) < 0) {
			fprintf(stderr, "Error: do operate failed.\n");
			return -1;
		}
		if ( !STACK_IS_EMPTY(calc->opr) && OPR_TOP(top_c) < 0) {
			fprintf(stderr, "Error: while opr top failed.\n");
			return -1;
		}
	}
	return 0;
}
Exemplo n.º 2
0
static int opr_handler(struct calc *calc)
{
	assert(calc != NULL);
	char c = *calc->cur;
	int zero = 0;
	if (calc->pre_flag == F_NUM) {
		calc->pre_flag = F_OPR;
	} else if (calc->pre_flag == F_OPR) {
		if (c == '-') {
			NUM_PUSH(zero);
			OPR_PUSH(c);
			calc->pre_flag = F_OPR;
			return 0;
		} else if (c == '+') {
			return 0;
		}
	} else {
		fprintf(stderr, "unknow character\n");
		return -2;
	}

	/* 如果操作符栈是空栈的话就压栈 */
	if (STACK_IS_EMPTY(calc->opr)) {
		OPR_PUSH(c);
		return 0;
	}

	/* 如果操作符栈非空, 且该操作符优先级高于栈顶运算符就压栈 */
	char top_c = 0;
	if (OPR_TOP(top_c) < 0) {
		fprintf(stderr, "Error: opr top failed.\n");
		return -1;
	}
	
	while(1) {
		if (opr2level(c) > opr2level(top_c) || STACK_IS_EMPTY(calc->opr)) {
			OPR_PUSH(c);
			return 0;
		}
		if (do_operate(calc) < 0) {
			fprintf(stderr, "Error: do operate failed.\n");
			return -1;
		}
		if ( !STACK_IS_EMPTY(calc->opr) && OPR_TOP(top_c) < 0) {
			fprintf(stderr, "Error: while opr top failed.\n");
			return -1;
		}
	}
	return 0;
}
Exemplo n.º 3
0
static num_t parser(struct calc *calc, const char *expr)
{
	assert(calc != NULL);
	assert(expr != NULL && expr != '\0');
	
	calc->cur = expr;
	for (; *calc->cur != '\0'; calc->cur++) {
		switch(*calc->cur) {
			case '0' ... '9': num_handler(calc);break;
			case '-':
			case '+':
			case '*':
			case '/':
			case '%': opr_handler(calc);break;
			case ' ':
			case '\t':
			case '\n': break;
			case '(': left_bracket_handler(calc);break;
			case ')': right_bracket_handler(calc);break;
			default: fprintf(stderr, "Error: Unknow character ");return -1;
		}
	}
	while(!STACK_IS_EMPTY(calc->opr))
		do_operate(calc);
	num_t res = 0;
	NUM_POP(res);
	return res;
}
Exemplo n.º 4
0
void stackFree(Stack *stack)
{
	LOG_PROLOG();
	if (stack != NULL) {
		while (!STACK_IS_EMPTY(stack)) {
			void *element = stackPop(stack);
			if (element != NULL) {
				my_free(element);
				element = NULL;
			}
			else {
				LOG_ERROR("Trying to free NULL pointer popped from stack");
			}
		}
		freeAtomicStampedReference(stack->top);
		stack->elementSize = 0;
		stack->numberOfElements = 0;
		my_free(stack->top);
		stack->top = NULL;
	}
	else {
		LOG_ERROR("Trying to free NULL stack pointer");
	}
	LOG_EPILOG();
}
Exemplo n.º 5
0
/*Busca y almacena en el arbol un nuevo camino a un spot libre
*/
void network_newFreeSpot	(AbbNet net){
	/*Precondicion: network_newFreeSpot() se tiene que llamar  despues de haber agregado un elemento*/
	networkNode * pivot;
	networkNode * ancestor;
	pivot = STACK_TOP(net->freeSpot);
	STACK_POP(net->freespot);
	assert(pivot->left == Leaf);
	if(pivote->right != Leaf){
		if(!STACK_IS_EMPTY(net->freeSpot)){
			ancestor = STACK_TOP(net->freeSpot);
			/*mientras el hijo derecho del ancestro es el pivote*/
			while(networkNode_compare(ancestor->right, pivot) && STACK_SIZE(net->freeSpot) > 1 ){
				/*sigo subiendo en el arbol*/
				pivot = ancestor;
				STACK_POP(net->freespot);
				ancestor = STACK_TOP(net->freeSpot);
			}
			/*pude haber salido por que el stack esta vacion o porque ancestor->right != pivot 
			 * si sali por que el ancestro->right es diferente al pivote, entonces me voy por el
			 * el hijo derecho del ancestro todo a la izquierda.
			 * si sali porque llegue a la copa del arbol me voo todo a la izq a iniciar un nivel del arbol nuevo
			*/
			if(!networkNode_compare(ancestor->right, pivot){
				ancestro = ancestro->right;
			}
			while(ancestor->left != Leaf){
				STACK_ADD(net->freeSpot, ancestor);
				ancestor = ancestor->left;
			}
		}
	}
Exemplo n.º 6
0
static void pop(UScriptRun *scriptRun)
{
    if (STACK_IS_EMPTY(scriptRun)) {
        return;
    }
    
    if (scriptRun->fixupCount > 0) {
        scriptRun->fixupCount -= 1;
    }
    
    scriptRun->pushCount -= 1;
    scriptRun->parenSP = DEC1(scriptRun->parenSP);
    
    /* If the stack is now empty, reset the stack
       pointers to their initial values.
     */
    if (STACK_IS_EMPTY(scriptRun)) {
        scriptRun->parenSP = -1;
    }
}
Exemplo n.º 7
0
void
gfxScriptItemizer::pop()
{
    if (STACK_IS_EMPTY()) {
        return;
    }

    if (fixupCount > 0) {
        fixupCount -= 1;
    }

    pushCount -= 1;
    parenSP = DEC1(parenSP);
  
    /* If the stack is now empty, reset the stack
       pointers to their initial values.
     */
    if (STACK_IS_EMPTY()) {
        parenSP = -1;
    }
}
Exemplo n.º 8
0
/*  pop an elPTR from stack
	precondition: stack is not empty
	add ERROR CHECKING
*/
DDT STACK_POP ( TYPE_STACK * thisStack ) {
    if ( DEBUG ) {
        printf ( ">>in STACK_POP ( thisStack=%p)\n", thisStack ) ;
    }
    DDT popElement = NULL ;
    if ( !STACK_IS_EMPTY ( thisStack) ) {
        popElement = ( DDT ) thisStack -> _STACK_DATA [ --( thisStack -> _STACK_TOP ) ] ;
    }
    else {
        printf ( "ERROR tried to pop and empty stack at %p\n" , thisStack ) ;
    } ;
    if ( DEBUG ) {
        printf ( "<<leaving STACK_POP, returning %p\n" , popElement ) ;
    }
    return ( popElement ) ;
}