std::unique_ptr<ExprAST> Parser::parsePrimary(s_cursor_t &it, const s_cursor_t &end) { switch (_curTok) { case tok_identifier: return parseIdentifierExpr(it, end); case tok_number: return parseNumberExpr(it, end); case '(': return parseParenExpr(it, end); default: return Error("unknown token when expecting an expression"); } }
ast::Expr *parsePrimary(TokenIt &it, const TokenIt &end) { Token *current = currentToken(it, end); EXPECT_ANY_TOKEN(current, errorE, "Expected a primary expression"); switch(current->type) { case Token::IDENTIFIER: return parseIdentifier(it, end); case Token::NUMBER: return parseLiteral(it, end); case Token::LPAREN: return parseParenExpr(it, end); default: return errorE("Unexpected token when expecting an expression"); } }
ExprAST* Parser::parsePrimary() { switch (lexer->token) { case Lexer::TOK_IDENTIFIER: return parseIdentifierExpr(); case Lexer::TOK_INTEGER: return parseIntegerExpr(); case Lexer::TOK_DOUBLE: return parseDoubleExpr(); case Lexer::TOK_STRING: return parseStringExpr(); case '(': return parseParenExpr(); default: return Error("Unknown token '" + lexer->TokToStr(lexer->token) + "' when expecting an expression"); } }
ExprAST* Parser::handleCondition() { std::vector<ExprAST*> body; ExprAST* condition = 0; if (lexer->token == Lexer::TOK_ELSE) lexer->NextToken(); // eat "else" if (lexer->token == Lexer::TOK_IF) { lexer->NextToken(); // eat if if (lexer->token != '(') return Error("Expected '('"); condition = parseParenExpr(); if (!condition) return 0; } if (lexer->token != '{') return Error("Expected '{' opening condition body"); lexer->NextToken(); // eat { while (lexer->token != '}') { body.push_back(handleTopLevelExpr()); if (lexer->token == Lexer::TOK_EOF) return Error("Expected }, got EOF"); } lexer->NextToken(); // eat } if (!condition) condition = new NumIntAST(1); // if no condition, assume if(1) { } [always true] if (lexer->token == Lexer::TOK_ELSE) return new ConditionAST(condition, body, (ConditionAST*)handleCondition()); return new ConditionAST(condition, body); }