Beispiel #1
0
CoutStatementNode * Parser::Cout(){
	CoutStatementNode * coutnode = new CoutStatementNode();
	Match(COUT_TOKEN);
	Match(INSERTION_TOKEN);
	ExpressionNode * expr = Expression();
	coutnode->AddCnode(expr);
	while(true){	
		Token next = scan->PeekNextToken();
		if(next.GetTokenType() == INSERTION_TOKEN){
			Match(next.GetTokenType());
			Token nNext = scan->PeekNextToken();
			if(nNext.GetTokenType() == ENDL_TOKEN){
				Match(ENDL_TOKEN);
				coutnode->AddCnode(NULL);
			}
			else{
				ExpressionNode * nexpr = Expression();
				coutnode->AddCnode(nexpr);
			}
		}
		else{
			if(next.GetTokenType() == ENDL_TOKEN){
				Match(ENDL_TOKEN);
				coutnode->AddCnode(NULL);
			}
			Match(SEMICOLIN_TOKEN);
			break;
		}
	}
	MSG("Creating a CoutStatementNode.");
	return coutnode;
}
Beispiel #2
0
Token Parser::Match(TokenType expectedType){
	Token curtoken = scan->GetNextToken();
	//cout << curtoken << endl;
	if(curtoken.GetTokenType() != expectedType)
	{
		cerr << "Error in the Parse::Match.\n";
		cerr << "Expected token type "<< gTokenTypeNames[expectedType] << ", but got type " << curtoken.GetTokenTypeName() << endl;
		exit(1);
	}
	MSG("\tSuccessfully matched token type: " << curtoken.GetTokenTypeName() << ". Lexeme: \"" << curtoken.GetLexeme() << "\"");
	return curtoken;
}
Beispiel #3
0
IfStatementNode * Parser::If(){
	g_maybe_upscope = true;
	Match(IF_TOKEN);
	Match(LPAREN_TOKEN);
	ExpressionNode * exp = Expression();
	Match(RPAREN_TOKEN);

	StatementGroupNode * stategroup;
	StatementGroupNode * stategroupelse;
	Token tt = scan->PeekNextToken();
	if(tt.GetTokenType() == LCURLY_TOKEN){
		Match(LCURLY_TOKEN);
		stategroup = StatementGroup();
		Match(RCURLY_TOKEN);
	}
	else{
		stategroup = StatementGroup();
	}

	TokenType next = scan->PeekNextToken().GetTokenType();
	if(next == ELSE_TOKEN)
	{
		Match(next);
		next = scan->PeekNextToken().GetTokenType();
		if(next == LCURLY_TOKEN){
			Match(LCURLY_TOKEN);
			stategroupelse = StatementGroup();
			Match(RCURLY_TOKEN);
		}
		else{
			stategroupelse = StatementGroup();
		}

	}
	else{
		stategroupelse = NULL;
	}
	IfStatementNode * ifs = new IfStatementNode(exp, stategroup, stategroupelse);
	g_maybe_upscope = false;
	g_varname.clear();
	return ifs;
}
vector<Token*>* CoreEvaluator::ShuntingOperations(vector<Token>* toUnSort)
{
    if (toUnSort == NULL) throw new exception();

    // vector
    vector<Token*>* SortedVector = new vector<Token*>();

    // queue
    queue<Token*>* toQueue = new queue<Token*>();

    // stack
    stack<Token*>* toStack = new stack<Token*>();

    // temp token
    Token* tempToken;

    // iterating token by token
    for (int i=0; i < toUnSort->size(); i++)
    {
        // 1 - reading at i
        tempToken = &toUnSort->at(i);

        // 2 - int, real or negative values
        if (isNumber(tempToken))
        {
            toQueue->push(tempToken);
        }

        // 4 - for log(v,b) and coma ERROR checking
        else if (tempToken->GetTokenType() ==  Comma) // exa: 3,4
        {


            if (toStack->empty())
            {
                cout << "Invalid expression entered." << endl;
								isValid = false;
                while (!toQueue->empty())
                {
                    toQueue->pop(); // cleaning the queue before printing the error
                }
                break;
            }

            Token* tempOpt = toStack->top();

            if (tempOpt->GetTokenType() == OpenPar) // exa: sin(3,4)
            {
                toStack->pop(); // getting the OpenPar out

                if (!toStack->empty())
                {
                    tempOpt = toStack->top(); // update top position
                }
                else
                {
                    cout << "Invalid expression entered." << endl;
										isValid = false;
                    while (!toQueue->empty())
                    {
                        toQueue->pop(); // cleaning the queue before printing the error
                    }
                    break;
                }

                if (tempOpt->GetTokenType() == sinFunc || tempOpt->GetTokenType() == cosFunc || tempOpt->GetTokenType() == tanFunc || tempOpt->GetTokenType() == PowerFunc || tempOpt->GetTokenType() == FactFunc || tempOpt->GetTokenType() == secFunc || tempOpt->GetTokenType() == cscFunc || tempOpt->GetTokenType() == cotFunc || tempOpt->GetTokenType() == lnFunc)
                {
                    cout << "Invalid expression entered." << endl;
										isValid = false;
                    while (!toQueue->empty())
                    {
                        toQueue->pop(); // cleaning the queue before printing the error
                    }
                    while (!toStack->empty())
                    {
                        toStack->pop(); // cleaning the queue before printing the error
                    }
                    break;
                }
                else // it is log
                {
                    Token* parth = new Token(OpenPar,"(");
                    toStack->push(parth); // adding the OpenPar back to stack
                    // adding and erassing the Comma
                    toStack->push(tempToken);
                    toStack->pop();
                }
            }
            else
            {
                // adding and erassing the Comma
                toStack->push(tempToken);
                toStack->pop();
            }
        }

        // 5 - an operator +,-,*,/,power ^, sin, cos, tan, factorial, log, root
        else if (isOperator(tempToken))
        {
            // if the stack is not empty
            if (!toStack->empty())
            {
                // 5.1 - while operator on the top of the stack
                Token* tempOpt = toStack->top();

                while (!toStack->empty() && isOperator(tempOpt) && ((tempToken->GetAssociativity() == Left && tempToken->GetPrecedence() <= tempOpt->GetPrecedence()) || (tempToken->GetAssociativity() == Right && tempToken->GetPrecedence() < tempOpt->GetPrecedence())))
                {
                    // 5.2
                    toQueue->push(toStack->top());
                    toStack->pop();
                    if (!toStack->empty())
                    {
                        tempOpt = toStack->top(); // update top position
                    }
                    else
                    {
                        break;
                    }
                }
            }
            // 5.4
            toStack->push(tempToken);
        }

        // 6 - openPar
        else if (tempToken->GetTokenType() == OpenPar)
        {
            toStack->push(tempToken);
        }

        // 7 - closePar
        else if (tempToken->GetTokenType() == ClosePar)
        {
            // 7.1
            Token* tempOpt = toStack->top();
            while (!toStack->empty() && tempOpt->GetTokenType() != OpenPar)
            {
                toQueue->push(toStack->top()); // pop onto the queue
                toStack->pop();
                tempOpt = toStack->top(); // update top position in the stack
            }
            // 7.4
            if(toStack->empty())
            {
                throw new logic_error("Mismatched parentheses.");
            }
            // 7.2
            toStack->pop(); // pop from the stack to nowhere
        }
        else
        {
            string str = tempToken->GetTokenStr();

            // clean memory

            //trow exception
            throw runtime_error( str + " is not a valid argument");
        }

    } // end of the for

    while(!toStack->empty())
    {
        if (toStack->top()->GetTokenType() == OpenPar || toStack->top()->GetTokenType() == ClosePar) // is parentheses
        {
            throw new logic_error("Mismatched parentheses.");
        }
        toQueue->push(toStack->top()); // pop onto the queue
        toStack->pop();
    }

    // passing token by token from the queue to the Sorted vector
    while (!toQueue->empty())
    {
        SortedVector->push_back(toQueue->front());
        toQueue->pop();
    }

    return SortedVector;
}