/// <summary> /// Creates a new Operator. /// </summary> private MorestachioOperator(string operatorText, OperatorTypes operatorType, bool isBinaryOperator, OperatorPlacement placement) { OperatorText = operatorText; OperatorType = operatorType; IsBinaryOperator = isBinaryOperator; Placement = placement; }
// INITIALIZATION //_________________________________________________________________________________________ /// <summary> /// Creates a new operator instance. /// </summary> /// <param name="token"> The token that corresponds to this operator. If the operator consists /// of multiple tokens (e.g. ?:) then this is the first token in the sequence. </param> /// <param name="precedence"> An integer that indicates the order of evaluation. Higher /// precedence operators are evaluated first. </param> /// <param name="placement"> A value that indicates where operands are allowed. </param> /// <param name="associativity"> Gets a value that indicates whether multiple operators of this /// type are grouped left-to-right or right-to-left. </param> /// <param name="type"> The type of operator: this decides what algorithm to use to calculate the result. </param> /// <param name="secondaryToken"> The second token in the sequence. </param> /// <param name="rhsPrecedence"> The precedence for the secondary or tertiary operand. </param> private Operator(Token token, int precedence, OperatorPlacement placement, OperatorAssociativity associativity, OperatorType type, Token secondaryToken = null, int rhsPrecedence = -1) { if (token == null) { throw new ArgumentNullException(nameof(token)); } if (token == secondaryToken) { throw new ArgumentException("Token and secondaryToken must be different."); } if ((placement & (OperatorPlacement.HasLHSOperand | OperatorPlacement.HasRHSOperand)) == 0) { throw new ArgumentException("An operator must take at least one operand."); } if (secondaryToken == null && (placement & OperatorPlacement.HasSecondaryRHSOperand) != 0) { throw new ArgumentException("HasSecondaryRHSOperand can only be specified along with a secondary token."); } if (secondaryToken == null && (placement & OperatorPlacement.InnerOperandIsOptional) != 0) { throw new ArgumentException("InnerOperandIsOptional can only be specified along with a secondary token."); } if ((placement & OperatorPlacement.InnerOperandIsOptional) != 0 && (placement & OperatorPlacement.HasRHSOperand) == 0) { throw new ArgumentException("InnerOperandIsOptional can only be specified along with HasRHSOperand."); } this.Token = token; this.HasLHSOperand = (placement & OperatorPlacement.HasLHSOperand) != 0; this.HasRHSOperand = (placement & OperatorPlacement.HasRHSOperand) != 0; this.Associativity = associativity; this.Type = type; this.SecondaryToken = secondaryToken; this.HasSecondaryRHSOperand = (placement & OperatorPlacement.HasSecondaryRHSOperand) != 0; this.InnerOperandIsOptional = (placement & OperatorPlacement.InnerOperandIsOptional) != 0; this.Precedence = this.SecondaryPrecedence = this.TertiaryPrecedence = precedence; if (rhsPrecedence >= 0 && this.HasRHSOperand) { this.SecondaryPrecedence = rhsPrecedence; } if (rhsPrecedence >= 0 && this.HasSecondaryRHSOperand) { this.TertiaryPrecedence = rhsPrecedence; } // Every operator instance is stored in a list for retrieval by the parser. allOperators.Add(this); }
private static MorestachioOperator UnaryOperator(string operatorText, OperatorTypes type, OperatorPlacement placement = OperatorPlacement.Right) { return(new MorestachioOperator(operatorText, type, false, placement)); }
// INITIALIZATION //_________________________________________________________________________________________ /// <summary> /// Creates a new operator instance. /// </summary> /// <param name="token"> The token that corresponds to this operator. If the operator consists /// of multiple tokens (e.g. ?:) then this is the first token in the sequence. </param> /// <param name="precedence"> An integer that indicates the order of evaluation. Higher /// precedence operators are evaluated first. </param> /// <param name="placement"> A value that indicates where operands are allowed. </param> /// <param name="associativity"> Gets a value that indicates whether multiple operators of this /// type are grouped left-to-right or right-to-left. </param> /// <param name="type"> The type of operator: this decides what algorithm to use to calculate the result. </param> /// <param name="secondaryToken"> The second token in the sequence. </param> /// <param name="rhsPrecedence"> The precedence for the secondary or tertiary operand. </param> private Operator(Token token, int precedence, OperatorPlacement placement, OperatorAssociativity associativity, OperatorType type, Token secondaryToken = null, int rhsPrecedence = -1) { if (token == null) throw new ArgumentNullException("token"); if (token == secondaryToken) throw new ArgumentException("Token and secondaryToken must be different."); if ((placement & (OperatorPlacement.HasLHSOperand | OperatorPlacement.HasRHSOperand)) == 0) throw new ArgumentException("An operator must take at least one operand."); if (secondaryToken == null && (placement & OperatorPlacement.HasSecondaryRHSOperand) != 0) throw new ArgumentException("HasSecondaryRHSOperand can only be specified along with a secondary token."); if (secondaryToken == null && (placement & OperatorPlacement.InnerOperandIsOptional) != 0) throw new ArgumentException("InnerOperandIsOptional can only be specified along with a secondary token."); if ((placement & OperatorPlacement.InnerOperandIsOptional) != 0 && (placement & OperatorPlacement.HasRHSOperand) == 0) throw new ArgumentException("InnerOperandIsOptional can only be specified along with HasRHSOperand."); this.Token = token; this.HasLHSOperand = (placement & OperatorPlacement.HasLHSOperand) != 0; this.HasRHSOperand = (placement & OperatorPlacement.HasRHSOperand) != 0; this.Associativity = associativity; this.Type = type; this.SecondaryToken = secondaryToken; this.HasSecondaryRHSOperand = (placement & OperatorPlacement.HasSecondaryRHSOperand) != 0; this.InnerOperandIsOptional = (placement & OperatorPlacement.InnerOperandIsOptional) != 0; this.Precedence = this.SecondaryPrecedence = this.TertiaryPrecedence = precedence; if (rhsPrecedence >= 0 && this.HasRHSOperand) this.SecondaryPrecedence = rhsPrecedence; if (rhsPrecedence >= 0 && this.HasSecondaryRHSOperand) this.TertiaryPrecedence = rhsPrecedence; // Every operator instance is stored in a list for retrieval by the parser. allOperators.Add(this); }
/// <summary> /// Creates a new operator instance. /// </summary> /// <param name="token"> The token that corresponds to this operator. If the operator consists /// of multiple tokens (e.g. ?:) then this is the first token in the sequence. </param> /// <param name="precedence"> An integer that indicates the order of evaluation. Higher /// precedence operators are evaluated first. </param> /// <param name="placement"> A value that indicates where operands are allowed. </param> /// <param name="associativity"> Gets a value that indicates whether multiple operators of this /// type are grouped left-to-right or right-to-left. </param> /// <param name="type"> The type of operator: this decides what algorithm to use to calculate the result. </param> /// <param name="secondaryToken"> The second token in the sequence. </param> /// <param name="rhsPrecedence"> The precedence for the secondary or tertiary operand. </param> private Operator(Token token, int precedence, OperatorPlacement placement, OperatorAssociativity associativity, OperatorType type, Token secondaryToken, int rhsPrecedence) { if (token == null) { throw new ArgumentNullException("token"); } if (token == secondaryToken) { throw new ArgumentException("Token and secondaryToken must be different."); } if ((placement & (OperatorPlacement.HasLHSOperand | OperatorPlacement.HasRHSOperand)) == 0) { throw new ArgumentException("An operator must take at least one operand."); } if (secondaryToken == null && (placement & OperatorPlacement.HasSecondaryRHSOperand) != 0) { throw new ArgumentException("HasSecondaryRHSOperand can only be specified along with a secondary token."); } if (secondaryToken == null && (placement & OperatorPlacement.InnerOperandIsOptional) != 0) { throw new ArgumentException("InnerOperandIsOptional can only be specified along with a secondary token."); } if ((placement & OperatorPlacement.InnerOperandIsOptional) != 0 && (placement & OperatorPlacement.HasRHSOperand) == 0) { throw new ArgumentException("InnerOperandIsOptional can only be specified along with HasRHSOperand."); } this.Token = token; this.HasLHSOperand = (placement & OperatorPlacement.HasLHSOperand) != 0; this.HasRHSOperand = (placement & OperatorPlacement.HasRHSOperand) != 0; this.Associativity = associativity; this.Type = type; this.SecondaryToken = secondaryToken; this.HasSecondaryRHSOperand = (placement & OperatorPlacement.HasSecondaryRHSOperand) != 0; this.InnerOperandIsOptional = (placement & OperatorPlacement.InnerOperandIsOptional) != 0; this.Precedence = this.SecondaryPrecedence = this.TertiaryPrecedence = precedence; if (rhsPrecedence >= 0 && this.HasRHSOperand) { this.SecondaryPrecedence = rhsPrecedence; } if (rhsPrecedence >= 0 && this.HasSecondaryRHSOperand) { this.TertiaryPrecedence = rhsPrecedence; } // If prefix: if (HasLHSOperand) { token.PostfixOperator = this; } else { token.PrefixOperator = this; } if (SecondaryToken != null) { // Update the secondary token, this time with post/pre inverted: if (HasRHSOperand) { SecondaryToken.PostfixOperator = this; } else { SecondaryToken.PrefixOperator = this; } if (InnerOperandIsOptional) { SecondaryToken.PrefixOperator = this; } } Arity = (this.HasLHSOperand ? 1 : 0) + (this.HasRHSOperand ? 1 : 0) + (this.HasSecondaryRHSOperand ? 1 : 0); }
private Operator(Token token, int precedence, OperatorPlacement placement, OperatorAssociativity associativity, OperatorType type, Token secondaryToken) : this(token, precedence, placement, associativity, type, token, -1) { }
// INITIALIZATION //_________________________________________________________________________________________ private Operator(Token token, int precedence, OperatorPlacement placement, OperatorAssociativity associativity, OperatorType type) : this(token, precedence, placement, associativity, type, null, -1) { }