Example #1
0
Expression* LocalParselet::parse(Parser* parser, Token token)
{
    UniquePtr<std::vector<Token>*> names = new std::vector<Token>;

    do
    {
        Token name = parser->Consume(TokenType::Name);
        names->push_back(name);
    }
    while (parser->MatchAndConsume(TokenType::Comma));

    parser->Consume(TokenType::Assign);//its possible this wont be here and it may just be a mentioning, but no assignment

    //handle multiple comma expressions
    UniquePtr<std::vector<Expression*>*> rights = new std::vector<Expression*>;
    do
    {
        Expression* right = parser->parseExpression(Precedence::ASSIGNMENT-1/*assignment prcedence -1 */);

        rights->push_back(right);
    }
    while (parser->MatchAndConsume(TokenType::Comma));

    parser->Consume(TokenType::Semicolon);

    return new LocalExpression(names.Release(), rights.Release());
}
Example #2
0
Expression* ForParselet::parse(Parser* parser, Token token)
{
    parser->Consume(TokenType::LeftParen);
    if (parser->LookAhead().type == TokenType::Local)
    {
        if (parser->LookAhead(1).type == TokenType::Name)
        {
            Token n = parser->LookAhead(2);
            if (n.type == TokenType::Name && n.text == "in")
            {
                //ok its a foreach loop
                parser->Consume();
                auto name = parser->Consume();
                parser->Consume();
                UniquePtr<Expression*> container = parser->parseExpression();
                parser->Consume(TokenType::RightParen);

                auto block = new ScopeExpression(parser->parseBlock());
                return new ForEachExpression(name, container.Release(), block);
            }
        }
    }

    UniquePtr<Expression*> initial = parser->ParseStatement(true);
    UniquePtr<Expression*> condition = parser->ParseStatement(true);
    UniquePtr<Expression*> increment = parser->parseExpression();

    parser->Consume(TokenType::RightParen);

    auto block = new ScopeExpression(parser->parseBlock());
    return new ForExpression(token, initial.Release(), condition.Release(), increment.Release(), block);
}
Example #3
0
Expression* IndexParselet::parse(Parser* parser, Expression* left, Token token)
{
    UniquePtr<Expression*> index = parser->parseExpression();
    parser->Consume(TokenType::RightBracket);

    return new IndexExpression(left, index.Release(), token);
}
Example #4
0
Expression* WhileParselet::parse(Parser* parser, Token token)
{
    parser->Consume(TokenType::LeftParen);

    UniquePtr<Expression*> condition = parser->parseExpression();

    parser->Consume(TokenType::RightParen);

    auto block = new ScopeExpression(parser->parseBlock());
    return new WhileExpression(token, condition.Release(), block);
}
Example #5
0
Expression* IfParselet::parse(Parser* parser, Token token)
{
    std::vector<Branch*> branches;
    //take parens
    parser->Consume(TokenType::LeftParen);
    UniquePtr<Expression*> ifcondition = parser->parseExpression();
    parser->Consume(TokenType::RightParen);

    BlockExpression* ifblock = parser->parseBlock(true);

    branches.push_back(new Branch(ifblock, ifcondition.Release()));

    Branch* Else = 0;
    while(true)
    {
        //look for elses
        if (parser->MatchAndConsume(TokenType::ElseIf))
        {
            //keep going
            parser->Consume(TokenType::LeftParen);
            UniquePtr<Expression*> condition = parser->parseExpression();
            parser->Consume(TokenType::RightParen);

            BlockExpression* block = parser->parseBlock(true);

            branches.push_back(new Branch(block, condition.Release()));
        }
        else if (parser->MatchAndConsume(TokenType::Else))
        {
            //its an else
            BlockExpression* block = parser->parseBlock(true);

            Else = new Branch(block, 0);
            break;
        }
        else
            break;//nothing else
    }

    return new IfExpression(token, std::move(branches), Else);
}
Example #6
0
Expression* SwapParselet::parse(Parser* parser, Expression* left, Token token)
{
    if (dynamic_cast<IStorableExpression*>(left) == 0)
        throw CompilerException(parser->filename, token.line, "SwapParselet: Left hand side must be a storable location!");

    UniquePtr<Expression*> right = parser->parseExpression(Precedence::ASSIGNMENT-1/*assignment prcedence -1 */);

    if (dynamic_cast<IStorableExpression*>((Expression*)right) == 0)
        throw CompilerException(parser->filename, token.line, "SwapParselet: Right hand side must be a storable location!");

    return new SwapExpression(left, right.Release());
}
Example #7
0
Expression* NameParselet::parse(Parser* parser, Token token)
{
    if (parser->MatchAndConsume(TokenType::LeftBracket))
    {
        //array index
        UniquePtr<Expression*> index = parser->parseExpression();
        parser->Consume(TokenType::RightBracket);

        return new IndexExpression(new NameExpression(token.text), index.Release(), token);
    }
    else
        return new NameExpression(token.text);
}
Example #8
0
Expression* CallParselet::parse(Parser* parser, Expression* left, Token token)
{
    UniquePtr<std::vector<Expression*>*> arguments = new std::vector<Expression*>;

    if (!parser->MatchAndConsume(TokenType::RightParen))
    {
        do
        {
            arguments->push_back(parser->parseExpression(Precedence::ASSIGNMENT));
        }
        while( parser->MatchAndConsume(TokenType::Comma));

        parser->Consume(TokenType::RightParen);
    }
    return new CallExpression(token, left, arguments.Release());
}
Example #9
0
Expression* GroupParselet::parse(Parser* parser, Token token)
{
    UniquePtr<Expression*> exp = parser->parseExpression();
    parser->Consume(TokenType::RightParen);
    return exp.Release();
}