/// binoprhs /// ::= ('+' primary)* static ExprAST *ParseBinOpRHS(int ExprPrec, ExprAST *LHS) { // If this is a binop, find its precedence. while (1) { int TokPrec = GetTokPrecedence(); // If this is a binop that binds at least as tightly as the current binop, // consume it, otherwise we are done. if (TokPrec < ExprPrec) return LHS; // Okay, we know this is a binop. int BinOp = CurTok; getNextToken(); // eat binop // Parse the primary expression after the binary operator. ExprAST *RHS = ParsePrimary(); if (!RHS) return 0; // If BinOp binds less tightly with RHS than the operator after RHS, let // the pending operator take RHS as its LHS. int NextPrec = GetTokPrecedence(); if (TokPrec < NextPrec) { RHS = ParseBinOpRHS(TokPrec+1, RHS); if (RHS == 0) return 0; } // Merge LHS/RHS. LHS = new BinaryExprAST(BinOp, LHS, RHS); } }
/// expression /// ::= primary binoprhs /// static std::unique_ptr<ExprAST> ParseExpression() { auto LHS = ParsePrimary(); if (!LHS) return nullptr; return ParseBinOpRHS(0, std::move(LHS)); }
// binoprhs // ::= ('+' unary)* std::unique_ptr<CExprAST> CParser::ParseBinOpRHS(int precedence, std::unique_ptr<CExprAST> lhs) { // If this is a binary operation, find its precedence while (true) { int p = GetTokenPrecedence(); // If this is a binop that binds at least as tightly as the current binop, // consume it, otherwise we are done if (p < precedence) return lhs; int binop = m_CurrentToken; GetNextToken(); // eating that binop // Parsing unary expression after the binary operator auto rhs = ParseUnary(); if (!rhs) return nullptr; // If binop binds less tightly that the operator after rhs, // let the pending operator take rhs as it lhs int nextp = GetTokenPrecedence(); if (p < nextp) { rhs = ParseBinOpRHS(p + 1, std::move(rhs)); if (!rhs) return nullptr; } // Merge lhs/rhs lhs = std::make_unique<CBinaryExprAST>(binop, std::move(lhs), std::move(rhs)); } }
/// binoprhs /// ::= ('+' primary)* static std::unique_ptr<ExprAST> ParseBinOpRHS(int ExprPrec, std::unique_ptr<ExprAST> LHS) { // If this is a binop, find its precedence. while (1) { int TokPrec = GetTokPrecedence(); // If this is a binop that binds at least as tightly as the current binop, // consume it, otherwise we are done. if (TokPrec < ExprPrec) return LHS; // Okay, we know this is a binop. int BinOp = CurTok; getNextToken(); // eat binop // Parse the primary expression after the binary operator. auto RHS = ParsePrimary(); if (!RHS) return nullptr; // If BinOp binds less tightly with RHS than the operator after RHS, let // the pending operator take RHS as its LHS. int NextPrec = GetTokPrecedence(); if (TokPrec < NextPrec) { RHS = ParseBinOpRHS(TokPrec + 1, std::move(RHS)); if (!RHS) return nullptr; } // Merge LHS/RHS. LHS = helper::make_unique<BinaryExprAST>(BinOp, std::move(LHS), std::move(RHS)); } }
/* expression * ::= primary binoprhs * */ static shared_ptr<ExprAST> ParseExpression() { auto LHS = ParsePrimary(); if (!LHS) return 0; if (LHS->getType() == Ty_string) return LHS; return ParseBinOpRHS(1, LHS); }
/// expression /// ::= primary binoprhs /// std::unique_ptr<ExprAST> UDFParser::ParseExpression() { auto lhs = ParsePrimary(); if (!lhs) { return nullptr; } return ParseBinOpRHS(0, std::move(lhs)); }
// expression // ::= unary binoprhs // std::unique_ptr<CExprAST> CParser::ParseExpression() { auto lhs = ParseUnary(); if (!lhs) return nullptr; return ParseBinOpRHS(0, std::move(lhs)); }
ExprAST * FAO::ParseExpression() { ExprAST * lhs = ParsePrimary(); if (lhs == nullptr){ return nullptr; } return ParseBinOpRHS(0, lhs); }
// expression // ::= primary binoprhs // ::= primary binoprhs '(' arguments ')' // ::= block ExprAST *Parser::ParseExpression() { if (Lex.tokStr == "{") { return ParseBlock(); } else if (Lex.tok == tok_return) { Lex.gettok(); if (Lex.tokStr == "}" || Lex.tokStr == ";") { return new ReturnAST(Lex.tag(), 0); } else { ExprAST *v = ParseExpression(); return new ReturnAST(Lex.tag(), v); } } else { ExprAST *LHS = ParseBinOpLHS(); return ParseBinOpRHS(0, LHS); } }
/* binoprhs * ::= ('+' primary)* * */ static shared_ptr<ExprAST> ParseBinOpRHS(int lasprec, shared_ptr<ExprAST> LHS) { while (1) { int curprec = getTokPrecedence(); if (curprec < lasprec) return LHS; int op = CurTok; getNextToken(); //eat op auto RHS = ParsePrimary(); if (!RHS) return 0; int nextprec = getTokPrecedence(); if (curprec < nextprec) { RHS = ParseBinOpRHS(curprec + 1, RHS); if (!RHS) return 0; } LHS = shared_ptr<ExprAST>(new BinaryExprAST(op, LHS, RHS)); } }
// binoprhs // ::= ('+' primary)* // ::= functioncall ExprAST *Parser::ParseBinOpRHS(int ExprPrec, ExprAST *LHS) { while (1) { int tokPrec = getBinopPrec(Lex.tokStr); if (tokPrec < ExprPrec) { if (LHS == 0) throw new PIFError("Bad LHS."); return LHS; } if (Lex.tokStr == "(") { // this isn't really a binary expression, it's a function call LHS = ParseCall(LHS); if (LHS == 0) return 0; } else if (Lex.tokStr == ":") { // this isn't really a binary expression, it's a cast operation Lex.gettok(); TypeAST *Type = ParseType(); LHS = new CastExprAST(Lex.tag(), LHS, Type); } else if (Lex.tokStr == ".") { LHS = ParseDotMember(LHS); if (LHS == 0) return 0; } else { std::string binop = Lex.tokStr; Lex.gettok(); ExprAST *RHS = ParseBinOpLHS(); int nextPrec = getBinopPrec(Lex.tokStr); if (tokPrec < nextPrec) { RHS = ParseBinOpRHS(tokPrec + 1, RHS); if (!RHS) return 0; } if (LHS == 0) { if (binop == "@") { LHS = new DerefExprAST(Lex.tag(), RHS); } else { LHS = new UnaryExprAST(Lex.tag(), binop, RHS); } } else { LHS = new BinaryExprAST(Lex.tag(), binop, LHS, RHS); } } } }
/// binoprhs /// ::= ('+' primary)* std::unique_ptr<ExprAST> UDFParser::ParseBinOpRHS( int expr_prec, std::unique_ptr<ExprAST> lhs) { // If this is a binop, find its precedence. while (true) { int tok_prec = GetTokPrecedence(); // If this is a binop that binds at least as tightly as the current binop, // consume it, otherwise we are done. if (tok_prec < expr_prec) { return lhs; } // Okay, we know this is a binop. int bin_op = cur_tok_; GetNextToken(); // eat binop // Parse the primary expression after the binary operator. auto rhs = ParsePrimary(); if (!rhs) { return nullptr; } // If BinOp binds less tightly with rhs than the operator after rhs, let // the pending operator take rhs as its lhs. int next_prec = GetTokPrecedence(); if (tok_prec < next_prec) { rhs = ParseBinOpRHS(tok_prec + 1, std::move(rhs)); if (!rhs) { return nullptr; } } // Merge lhs/rhs. lhs = llvm::make_unique<BinaryExprAST>(bin_op, std::move(lhs), std::move(rhs)); } }
/* * Handle the rhs of a binary operation, where * binoprhs ::= (binop primary)* */ static std::unique_ptr<ExprAST> ParseBinOpRHS(int minExprPrec, std::unique_ptr<ExprAST> lhs) { while (1) { int tokPrec = GetTokPrecedence(); // token's precedence is too low to do anything with so we're done if (tokPrec < minExprPrec) { return lhs; } // otherwise, precedence it high enough so we should handle the binop int binOp = CurTok; getNextToken(); // advance past the binop // parse the primary expression after the operator auto rhs = ParsePrimary(); if (!rhs) { return nullptr; } // determine the direction of associativity int nextPrec = GetTokPrecedence(); if (tokPrec < nextPrec) { rhs = ParseBinOpRHS(tokPrec + 1, std::move(rhs)); if (!rhs) { return nullptr; } } // merge the lhs and rhs lhs = llvm::make_unique<BinaryExprAST>(binOp, std::move(lhs), std::move(rhs)); } }
ExprAST * FAO::ParseBinOpRHS(int Expc, ExprAST * lhs) { // If this is a binary operator,find its precedence while (true){ int TokPrec = GetPrecedence(); // If this is a binary operator,find its precedence at least as tightly as the current binary operator // consume it,otherwise we are done. if (TokPrec < Expc){ return lhs; } // Okay,we know this is binary operator char BinOp = cur_token_.GetOperation(); Advance(); // eat binary operator // Parse the primary expression after the binary operator ExprAST * rhs = ParsePrimary(); if (rhs == nullptr){ return nullptr; } // If binary operator less tightly with rhs than the operator after rhs,let // the pending operator than rhs as its lhs int NextPrec = GetPrecedence(); if (TokPrec < NextPrec){ rhs = ParseBinOpRHS(TokPrec + 1, rhs); if (rhs == nullptr){ return nullptr; } } // Merge lhs/rhs lhs = new BinaryOpExpr(BinOp, lhs, rhs); } return lhs; }
/// expression /// ::= primary binoprhs /// static ExprAST *ParseExpression() { ExprAST *LHS = ParsePrimary(); if (!LHS) return 0; return ParseBinOpRHS(0, LHS); }