Ejemplo n.º 1
0
void SymbolCheckVisitor::visit(ASTUnary& ast)
{
   visitChildren(ast);

   checkOperator(ast, ast.getPre());
   checkOperator(ast, ast.getPost());
}
Ejemplo n.º 2
0
bool ASTBuilder::match(int construct){
	if (nextToken() == "") //end of code reached unexpectedly
	{
		error("End of code reached unexpectedly");
		return false;
	}

	bool matched = false;
	switch(construct){
	case VAR_NAME:
		//cout << "check_VAR_NAME: " << nextToken() << endl;
		matched = name();
		break;
	case CONSTANT:
		//cout << "check_CONSTANT: " << nextToken() << endl;
		matched = integer();
		break;
	case PLUS:
		//cout << "check_PLUS: " << nextToken() <<endl;
		matched = checkOperator(PLUS);
		break;
	case MINUS:
		//cout << "check_MINUS: " << nextToken() <<endl;
		matched = checkOperator(MINUS);
		break;
	case TIMES:
		//cout << "check_TIMES: " << nextToken() <<endl;
		matched = checkOperator(TIMES);
		break;
	case OPEN_BRACKET:
		//cout << "check_OPEN_BRACKET: " << nextToken() <<endl;
		matched = checkOperator(OPEN_BRACKET);
		break;
	case CLOSE_BRACKET:
		//cout << "check_CLOSE_BRACKET: " << nextToken() <<endl;
		matched = checkOperator(CLOSE_BRACKET);
		break;
	}
	if(matched){
		consumeToken();
		return true;
	}
	else{
		error(nextToken());
		return false;
	}
}
Ejemplo n.º 3
0
bool InputOutputMaster::findLastOperator(int cStart, int cEnd, Printable** ppOutput) {
    int cPos, coName, cOrder;
    int bLeading = false;
    int cEndStore = cEnd;
    if (hTest[cStart] == '-' || hTest[cStart] == '+')bLeading = true;
    int last;
    //do{
    last = 0;
    cPos = cStart;
    cOrder = 0; //higher signifies would be done after in BODMAS order
    for (int cIter = cStart; cIter < cEndStore; cIter++) {
        int cAscii = (int) hTest [cIter];
        if (cAscii == 40) cIter = findClosingBracket(cIter, cEnd); //if (hTest[cIter]== '(') ignore brackets
        else if (checkOperator(cAscii, &coName, &cOrder)) cPos = cIter;
    }
    if ((cPos == cStart && !bLeading) || cPos == cEnd - 1) return false;
    cEndStore = cPos;
    //}
    if (cPos!=cStart&&checkOperator(hTest[cPos - 1], &coName, &last)) {
        cPos--;
    };

    Expression * peInput1;
    Expression * peInput2;
    peInput1 = NULL;
    if (cPos != cStart) {
        if (!(convertToPrintable(cStart, cPos, ppOutput)))
            return false;
        peInput1 = static_cast<Expression*> (*ppOutput);
    }
    if (!(convertToPrintable(cPos + 1, cEnd, ppOutput)))
        return false;
    peInput2 = static_cast<Expression*> (*ppOutput);
    *ppOutput = createBiInput(coName, peInput1, peInput2);
    if (*ppOutput == NULL)
        return false;
    return true;
}
Ejemplo n.º 4
0
bool TExtensionGLSL::visitAggregate(Visit, TIntermAggregate *node)
{
    checkOperator(node);

    return true;
}
Ejemplo n.º 5
0
bool TExtensionGLSL::visitUnary(Visit, TIntermUnary *node)
{
    checkOperator(node);

    return true;
}
Ejemplo n.º 6
0
int main() {

	char exitOperator = 'x';
	char cancelOperator = 'c';
	char addOperator = '+';
	char subtractOperator = '-';
	char multiplyOperator = '*';
	char divideOperator = '/';
	char squareOperator = 'r';
	char powerOperator = '^';
	char factorialOperator = '!';
	char simplifyOperator = 's';

	int numerator1, denominator1, numerator2, denominator2, resultNumerator,
			resultDenominator, exponent;
	double number1, number2, powerResult;
	char operator1 = '0', operator2, menuCharacter, character;

	loadOperatorsFromFile(exitOperator, cancelOperator, addOperator,
			subtractOperator, multiplyOperator, divideOperator, squareOperator,
			powerOperator, factorialOperator, simplifyOperator);
	bool exitMenu = false;
	do {
		menuCharacter = printMenu();

		switch (menuCharacter) {

		case '1':

			printOperators(exitOperator, cancelOperator, addOperator,
					subtractOperator, multiplyOperator, divideOperator,
					squareOperator, powerOperator, factorialOperator);

			cout << "Write an inline expresion:" << endl;

			do {

				cin.sync();
				checkFirstCharacter(character, number1);
				checkOperator(character, operator1, exitOperator);

				do {

					checkOperation(operator1, addOperator, number2, number1,
							subtractOperator, multiplyOperator, divideOperator,
							squareOperator, factorialOperator, powerOperator,
							exponent, powerResult, exitOperator, operator2,
							cancelOperator);
					if (operator2 == addOperator
							|| operator2 == subtractOperator
							|| operator2 == divideOperator
							|| operator2 == multiplyOperator
							|| operator2 == powerOperator
							|| operator2 == factorialOperator
							|| operator2 == squareOperator) {

						cin.get(operator1);
					}

				} while (operator1 != '\n' && operator1 != exitOperator);

			} while (operator2 != exitOperator);

			break;

		case '2':

			configureOperators(character, exitOperator, cancelOperator,
					addOperator, subtractOperator, multiplyOperator,
					divideOperator, squareOperator, powerOperator,
					factorialOperator);
			break;

		case '3':

			cout << endl << "Fractions mode" << endl << endl;

			printOperators(exitOperator, cancelOperator, addOperator,
					subtractOperator, multiplyOperator, divideOperator,
					squareOperator, powerOperator, factorialOperator);

			do {

				cout << "Write an inline fraction expresion:" << endl;
				cin.sync();

				character = cin.peek();

				if (isdigit(character)) {

					readFraction(numerator1, denominator1);

					if (numerator1 != 0 && denominator1 != 0) {
						cin.get(operator1);
					}

					else {
						operator1 = cancelOperator;
					}
				} else {
					cin.get(operator1);
				}

				do {
//TODO extract
					checkSimplifyOperation(operator1, simplifyOperator,
							numerator1, denominator1);

					if (operator1 == addOperator) {

						readFraction(numerator2, denominator2);
						addFractions(numerator1, denominator1, numerator2,
								denominator2, resultNumerator,
								resultDenominator);
						printFraction(resultNumerator, resultDenominator);
						numerator1 = resultNumerator;
						denominator1 = resultDenominator;
					}

					if (operator1 == subtractOperator) {

						readFraction(numerator2, denominator2);
						subtractFractions(numerator1, denominator1, numerator2,
								denominator2, resultNumerator,
								resultDenominator);
						printFraction(resultNumerator, resultDenominator);
						numerator1 = resultNumerator;
						denominator1 = resultDenominator;
					}

					if (operator1 == multiplyOperator) {

						readFraction(numerator2, denominator2);
						multiplyFractions(numerator1, denominator1, numerator2,
								denominator2, resultNumerator,
								resultDenominator);
						printFraction(resultNumerator, resultDenominator);
						numerator1 = resultNumerator;
						denominator1 = resultDenominator;
					}

					if (operator1 == divideOperator) {

						readFraction(numerator2, denominator2);
						divideFractions(numerator1, denominator1, numerator2,
								denominator2, resultNumerator,
								resultDenominator);
						printFraction(resultNumerator, resultDenominator);
						numerator1 = resultNumerator;
						denominator1 = resultDenominator;
					}

					if (operator1 == powerOperator) {

						cin >> exponent;

						powerOfFractions(numerator1, denominator1, exponent,
								resultNumerator, resultDenominator);
						numerator1 = resultNumerator;
						denominator1 = resultDenominator;
						printFraction(numerator1, denominator1);
					}

					if (operator1 != cancelOperator) {

						cin.get(operator1);

						operator2 = cin.peek();
						if (operator2 == cancelOperator) {
							numerator1 = 0;
							cout << numerator1 << endl;
							operator1 = exitOperator;
						}

						if (operator2 == simplifyOperator
								|| operator2 == addOperator
								|| operator2 == subtractOperator
								|| operator2 == divideOperator
								|| operator2 == multiplyOperator
								|| operator2 == powerOperator) {

							cin.get(operator1);

						}

					}

				} while (operator1 != '\n' && operator1 != exitOperator);

			} while (operator2 != exitOperator);

			break;

		case '4':
			saveConfigurationToFile(exitOperator, cancelOperator, addOperator,
					subtractOperator, multiplyOperator, divideOperator,
					squareOperator, powerOperator, factorialOperator,
					simplifyOperator);
			exitMenu = true;

			break;

		}

	} while (!exitMenu);

	return 0;
}
Ejemplo n.º 7
0
static ACCExpr *getExprList(ACCExpr *head, std::string terminator, bool repeatCurrentToken, bool preserveParen)
{
    bool parseState = false;
    ACCExpr *currentOperand = nullptr;
    ACCExpr *tok;
    ACCExpr *exprStack[MAX_EXPR_DEPTH];
    int exprStackIndex = 0;
#define TOP exprStack[exprStackIndex]
    TOP = nullptr;
    if (trace_expr)
        printf("[%s:%d] head %s\n", __FUNCTION__, __LINE__, head ? head->value.c_str() : "(nil)");
    if (head) {
        while ((tok = get1Token()) && tok->value != terminator) {
            if (trace_expr)
                printf("[%s:%d] parseState %d tok->value %s repeat %d\n", __FUNCTION__, __LINE__, parseState, tok->value.c_str(), repeatCurrentToken);
            if ((parseState = !parseState)) {    /* Operand */
                ACCExpr *unary = nullptr;
                ACCExpr *tnext = tok;
                if (repeatCurrentToken)
                    tok = head;
                else
                    tnext = get1Token();
                repeatCurrentToken = false;
                if ((tok->value == "-" || tok->value == "!") && !tok->operands.size()) { // unary '-'
                    unary = tok;
                    tok = tnext;
                    tnext = get1Token();
                    if (trace_expr)
                        printf("[%s:%d] unary '-' unary %p tok %p tnext %p\n", __FUNCTION__, __LINE__, (void *)unary, (void *)tok, (void *)tnext);
                }
                if (!checkOperand(tok->value) && !checkOperator(tok->value)) {
                    printf("[%s:%d] OPERAND CHECKFAILLLLLLLLLLLLLLL %s from %s\n", __FUNCTION__, __LINE__, tree2str(tok).c_str(), lexString.c_str());
                    exit(-1);
                }
                while (tnext && (isParen(tnext->value) || isIdChar(tnext->value[0]))) {
                    assert(isIdChar(tok->value[0]));
                    tok->operands.push_back(tnext);
                    tnext = get1Token();
                }
                repeatGet1Token = tnext;
                if (unary) {
                    unary->operands.push_back(tok);
                    tok = unary;
                }
                currentOperand = tok;
            }
            else {                        /* Operator */
                std::string L = TOP ? TOP->value : "", R = tok->value;
                if (!checkOperator(R)) {
                    printf("[%s:%d] OPERATOR CHECKFAILLLLLLLLLLLLLLL %s from %s\n", __FUNCTION__, __LINE__, R.c_str(), lexString.c_str());
                    exit(-1);
                }
                else if (!((L == R && L != "?") || (L == "?" && R == ":"))) {
                    if (TOP) {
                        int lprec = findPrec(L), rprec = findPrec(R);
                        if (lprec < rprec) {
                            exprStackIndex++;
                            TOP = nullptr;
                        }
                        else {
                            TOP->operands.push_back(currentOperand);
                            currentOperand = TOP;
                            while (exprStackIndex > 0 && lprec >= rprec) {
                                exprStackIndex--;
                                TOP->operands.push_back(currentOperand);
                                currentOperand = TOP;
                                L = TOP->value;
                                lprec = findPrec(L);
                            }
                        }
                    }
                    TOP = tok;
                }
                TOP->operands.push_back(currentOperand);
                currentOperand = nullptr;
            }
        }
        while (exprStackIndex != 0) {
            TOP->operands.push_back(currentOperand);
            currentOperand = TOP;
            exprStackIndex--;
        }
        if (currentOperand) {
            if (TOP)
                TOP->operands.push_back(currentOperand);
            else
                TOP = currentOperand;
        }
        if (TOP) {
            if (terminator != "")
                head->operands.push_back(TOP); // the first item in a recursed list
            else
                head = TOP;
        }
    }
    head = cleanupExpr(head, preserveParen);
    return head;
}