Example #1
0
int FAO::GetPrecedence()
{
	if (cur_token_.GetType() == TokenType::TOKEN_OPERATION){
		return this->binary_op_precedence_[cur_token_.GetOperation()];
	}
	return -1;
}
Example #2
0
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;
}