Beispiel #1
0
char* infixToPostfix(char const* str) {
	generic_stack* infixStack = stackAllocate(1);
	generic_stack* output = stackAllocate(1);
	char peek;
	for (; *str; ++str) {
		stackPeek(infixStack, &peek);
		if (*str == '(') {
			peek = '(';
			stackPush(infixStack, &peek);
		} else if (*str == ')') {
			while (peek != '(') {
				stackPop(infixStack, &peek);
				stackPush(output, &peek);
				stackPeek(infixStack, &peek);
			}
			//Discard the '('
			stackPop(infixStack, &peek);
		} else {
			while (!stackEmpty(infixStack) && precidence(peek) >= precidence(*str)) {
				stackPop(infixStack, &peek);
				stackPush(output, &peek);
				//Update the peeked
				stackPeek(infixStack, &peek);
			}
			stackPush(infixStack, str);
		}
	}

	while (!stackEmpty(infixStack)) {
		stackPop(infixStack, &peek);
		stackPush(output, &peek);
	}

	peek = '\0';
	stackPush(output, &peek);

	char* result = malloc(stackSize(output));
	memcpy(result, output->data, output->current);

	stackFree(infixStack);
	stackFree(output);

	return result;
}
Beispiel #2
0
void VM::addFrame(State *state, size_t slots, size_t fastSlots) {
    auto *frame = (CallFrame *)stackAllocate(state, sizeof(CallFrame));
    if (!frame) return;
    frame->m_above = state->m_frame;
    frame->m_count = slots;
    frame->m_slots = (Object **)stackAllocate(state, sizeof(Object *) * slots);
    if (!frame->m_slots) {
        stackFree(state, frame, sizeof *frame);
        return;
    }
    frame->m_fastSlotsCount = fastSlots;
    // don't need to be initialized since fast slots are not subjected to
    // traditional garbage collection.
    frame->m_fastSlots = (Object ***)stackAllocateUninitialized(state, sizeof(Object **) * fastSlots);
    if (!frame->m_fastSlots) {
        stackFree(state, frame->m_slots, sizeof(Object *) * slots);
        stackFree(state, frame, sizeof *frame);
        return;
    }
    state->m_frame = frame;
}
Beispiel #3
0
char* infixInsertExplicitConcatenation(char const* str) {

	generic_stack* output = stackAllocate(1);
	char temp;
	bool insertPlanned = false;

	for (; *str; str++) {
		if (*str == '[') {
			str = infixComputeBrackets(str, output);
			if (!str) {
				stackFree(output);
				return 0;
			}
			insertPlanned = true;
		} else {
			stackPush(output, str);
			if (!isOperator(*str)) {
				insertPlanned = true;
			} else if (*str == '|') {
				insertPlanned = false;
			}
		}
		if (insertPlanned && nextChar(str) != '\0' && (nextChar(str) == '(' || !isOperator(nextChar(str)))) {
			stackPush(output, "&");
			insertPlanned = false;
		}
	}

	temp = '\0';
	stackPush(output, &temp);

	char* result = malloc(strlen(output->data) + 1);
	strcpy(result, output->data);

	stackFree(output);
	return result;
}