Пример #1
0
Expression *Parser::ParseAExpr() {
    Token* tk = lexer_->GetToken();
    lexer_->Advance();

    switch(tk->type()) {
    case Token::INT:
        return new ENum(tk, atoi(tk->TokenStr().c_str()));
    case Token::ID:
        return new EVar(tk, tk->TokenStr());
    case Token::PACK: {
        Advance(Token::LBRACE);
        Token* left = lexer_->GetToken();
        if (left->type() != Token::INT) {
            throw ParseException(left, "expect a INT here");
        }
        int tag = atoi(left->TokenStr().c_str());
        lexer_->Advance();

        Advance(Token::COMMA);

        Token* right = lexer_->GetToken();
        if (right->type() != Token::INT) {
            throw ParseException(right, "expect a INT here");
        }
        int arity = atoi(right->TokenStr().c_str());
        lexer_->Advance();

        Advance(Token::RBRACE);
        return new EConstructor(tk, tag, arity);
    }
    case Token::LPAREN: {
        lexer_->Advance();
        Expression* result = ParseExpr();
        Advance(Token::RPAREN);
        return result;
    }
    default:
        throw ParseException(tk, "Should Not Get Here");
    }
}
Пример #2
0
Expression *Parser::ParseCmpExpr() {
    // PlusExpr [ Op PlusExpr ] *
    Expression* current = ParsePlusExpr();
    while (IsCmpOperator(lexer_->GetToken()->type())) {
        Token* token = lexer_->GetToken();
        lexer_->Advance();

        Expression * right = ParsePlusExpr();
        current = new EApplication(token,
                                   new EApplication(token,
                                                    new EVar(token, token->TokenStr()),
                                                    current),
                                   right);
    }
    return current;
}
Пример #3
0
Expression *Parser::ParseAndExpr() {
    // CmpExpr [ and CmpExpr ] *
    Expression * current = ParseCmpExpr();
    while (lexer_->GetToken()->type() == Token::AND) {
        Token* token = lexer_->GetToken();
        lexer_->Advance();

        Expression * right = ParseCmpExpr();
        current = new EApplication(token,
                                   new EApplication(token,
                                                    new EVar(token, token->TokenStr()),
                                                    current),
                                   right);
    }
    return current;
}
Пример #4
0
Expression *Parser::ParsePlusExpr() {
    // FactorExpr [ +/- FactorExpr ] *
    Expression* current = ParseFactor();
    while (lexer_->GetToken()->type() == Token::PLUS ||
           lexer_->GetToken()->type() == Token::MINUS) {
        Token* token = lexer_->GetToken();
        lexer_->Advance();

        Expression * right = ParseFactor();
        current = new EApplication(token,
                                   new EApplication(token,
                                                    new EVar(token, token->TokenStr()),
                                                    current),
                                   right);
    }
    return current;
}
Пример #5
0
Expression *Parser::ParseFactor() {
    // SExpr [ *// SExpr ] *
    Expression* current = ParseSExpr();
    while (lexer_->GetToken()->type() == Token::STAR ||
           lexer_->GetToken()->type() == Token::DIVIDE) {
        Token* token = lexer_->GetToken();
        lexer_->Advance();

        Expression * right = ParseSExpr();
        current = new EApplication(token,
                                   new EApplication(token,
                                                    new EVar(token, token->TokenStr()),
                                                    current),
                                   right);
    }
    return current;
}
Пример #6
0
SuperCombinatorDef *Parser::ParseSCDef() {
    if (lexer_->GetToken()->type() != Token::ID) {
        throw ParseException(lexer_->GetToken(), "expected a function name");
    }

    Token* token = lexer_->GetToken();
    lexer_->Advance();

    SuperCombinatorDef::Symbol name = token->TokenStr();

    SuperCombinatorDef::SymbolList vars;
    while (lexer_->GetToken()->type() == Token::ID) {
        vars.push_back(lexer_->GetToken()->TokenStr());
        lexer_->Advance();
    }

    Advance(Token::ASSIGN);

    Expression* body = ParseExpr();
    return new SuperCombinatorDef(token, name, vars, body);
}