public override bool Equals(ExpressionBase otherBase) { var other = (otherBase as BinaryOperatorExpression); if (other == null) return false; if (other.Type != this.Type) return false; switch (this.Type) { case OperatorType.Add: case OperatorType.Multiply: if (this.Left == other.Left && this.Right == other.Right || this.Left == other.Right && this.Right == other.Left) return true; break; case OperatorType.Subtract: case OperatorType.Divide: case OperatorType.Power: if (this.Left.Equals(other.Left) && this.Right.Equals(other.Right)) return true; break; } return false; }
public override Boolean Replace(ExpressionBase old, ExpressionBase replacement, bool doRecursively) { var hasReplaced = false; if (Object.ReferenceEquals(Left, old)) { Left = replacement.Clone(); Left.Parent = this; hasReplaced |= true; } else if (doRecursively) { hasReplaced |= Left.Replace(old, replacement, true); } if (Object.ReferenceEquals(Right, old)) { Right = replacement.Clone(); Right.Parent = this; hasReplaced |= true; } else if (doRecursively) { hasReplaced |= Right.Replace(old, replacement, true); } return hasReplaced; }
public override bool Equals(ExpressionBase otherBase) { var other = (otherBase as VariableExpression); if (other == null) return false; return this.value == other.value; }
public override bool Equals(ExpressionBase otherBase) { var other = (otherBase as NumericExpression); if (other == null) return false; return (Math.Abs(this.Number - other.Number) < double.Epsilon); }
public override bool Equals(ExpressionBase otherBase) { var other = (otherBase as DelimiterExpression); if (other == null) return false; return this.Expression == other.Expression; }
public override bool Equals(ExpressionBase otherBase) { var other = (otherBase as ConstantExpression); if (other == null) return false; return this.Type == other.Type; }
//a/c + b/c = (a+b)/c public static ExpressionBase AddFractionWithCommonDenominatorRule(ExpressionBase expression, List<ExpressionBase> selection) { //If expression is a sum. VariadicOperatorExpression variadicExpression = expression as VariadicOperatorExpression; if (variadicExpression != null && variadicExpression.Type == OperatorType.Add) { BinaryOperatorExpression binaryOperand; List<ExpressionBase> terms = new List<ExpressionBase>(); ExpressionBase commonDenominator = null; //Foreach operand in sum. foreach (ExpressionBase operand in variadicExpression) { //If operand is a fraction. binaryOperand = operand as BinaryOperatorExpression; if (binaryOperand != null && binaryOperand.Type == OperatorType.Divide) { //If operand is the first fraction in sum if (terms.Count == 0) { //Add numerator to list of numerators. terms.Add(binaryOperand.Left.Clone()); //Set commonDenominator to operand's denominator. commonDenominator = binaryOperand.Right.Clone(); } else if (commonDenominator == binaryOperand.Right) { //Add fraction's numerator to list if its denominator equals commenDenominator. terms.Add(binaryOperand.Left.Clone()); } else { //Return null if an fraction's denominator does not equals commenDenominator. return null; } } else { //Return if operand is not a fraction. return null; } } if (terms.Count > 1) { //Initialize sum of all fractions' numerators. VariadicOperatorExpression SuggestionNumerator = new VariadicOperatorExpression(OperatorType.Add, terms[0], terms[1]); SuggestionNumerator.Add(terms.Skip(2).ToList()); //Return fraction with sum of all fractions' numerators and with their common denominator. return new BinaryOperatorExpression(SuggestionNumerator, commonDenominator, OperatorType.Divide); } } return null; }
protected BinaryExpression(ExpressionBase left, ExpressionBase right, OperatorType type) : base(type) { if (left == null) throw new ArgumentNullException("left"); if (right == null) throw new ArgumentNullException("right"); Left = left; Right = right; }
protected UnaryExpression(OperatorType type, ExpressionBase expression) : base(type) { if (type != OperatorType.Minus) throw new ArgumentException("Invalid Type: " + type, "type"); if (expression == null) throw new ArgumentNullException("expression"); Expression = expression; Expression.Parent = this; }
public ExpressionModel(string expression, Action<ExpressionModel> onChange, params ExpressionRule[] rules) { selectionParent = null; selection = new List<ExpressionBase>(); identities = new List<Identity>(); serializer = new ExpressionSerializer(); analyzer = new ExpressionAnalyzer(); this.expression = serializer.Deserialize(expression); foreach (ExpressionRule rule in rules) { analyzer.Add(rule); } OnChanged = onChange; callOnChanged(); }
public void ApplyIdentity(ExpressionBase identity) { ExpressionBase parent = Selected.Parent; if (parent == null) { expression = identity; } else { Selected.Replace(identity); } Selected.Parent = parent; UpdateSelection(); UnSelectAll(); UpdateIdentities(); }
public override bool Replace(ExpressionBase old, ExpressionBase replacement, bool doRecursively) { var hasReplaced = false; if (Object.ReferenceEquals(Expression, old)) { Expression = replacement.Clone(); Expression.Parent = this; hasReplaced |= true; } else if (doRecursively) { hasReplaced |= Expression.Replace(old, replacement, true); } return hasReplaced; }
// 4^2 = 16, 4/2 = 2 public static ExpressionBase CalculateBinaryRule(ExpressionBase expression, List<ExpressionBase> selection) { BinaryOperatorExpression binary = expression as BinaryOperatorExpression; if (binary != null) { int? leftNumber, rightNumber; leftNumber = FromNumeric(binary.Left); rightNumber = FromNumeric(binary.Right); //If left and right operands are numbers if (leftNumber != null && rightNumber != null) { //If expression is a power if (binary.Type == OperatorType.Power) { if ((int)rightNumber > 0) { //Calculate and return left^right int sum = 1; for (int n = 0; n < (int)rightNumber; n++) { sum *= (int)leftNumber; } return ToNumeric(sum); } else { return null; } } else if (binary.Type == OperatorType.Divide && (int)leftNumber % (int)rightNumber == 0) { //If expression is a fraction return left / right return ToNumeric((int)leftNumber / (int)rightNumber); } else { return null; } } } return null; }
public IWhereClauseConnectionBase Gte(string propertyName, object value) { _first = new ExpressionGreaterThanOrEqual(propertyName, value); return(this); }
public UnaryExpression(ExpressionBase innerExpression) { InnerExpression = innerExpression; }
public IWhereClauseConnectionBase Between(string propertyName, object min, object max) { _first = new ExpressionBetween(propertyName, min, max); return(this); }
public override bool Evaluate(InterpreterScope scope, out ExpressionBase result) { var leaderboard = new Leaderboard(); var stringExpression = GetStringParameter(scope, "title", out result); if (stringExpression == null) { return(false); } leaderboard.Title = stringExpression.Value; stringExpression = GetStringParameter(scope, "description", out result); if (stringExpression == null) { return(false); } leaderboard.Description = stringExpression.Value; leaderboard.Start = ProcessTrigger(scope, "start", out result); if (leaderboard.Start == null) { return(false); } leaderboard.Cancel = ProcessTrigger(scope, "cancel", out result); if (leaderboard.Cancel == null) { return(false); } leaderboard.Submit = ProcessTrigger(scope, "submit", out result); if (leaderboard.Submit == null) { return(false); } leaderboard.Value = ProcessValue(scope, "value", out result); if (leaderboard.Value == null) { return(false); } var format = GetStringParameter(scope, "format", out result); if (format == null) { return(false); } leaderboard.Format = Leaderboard.ParseFormat(format.Value); if (leaderboard.Format == ValueFormat.None) { result = new ParseErrorExpression(format.Value + " is not a supported leaderboard format", format); return(false); } var lowerIsBetter = GetBooleanParameter(scope, "lower_is_better", out result); if (lowerIsBetter == null) { return(false); } leaderboard.LowerIsBetter = lowerIsBetter.Value; var integerExpression = GetIntegerParameter(scope, "id", out result); if (integerExpression == null) { return(false); } leaderboard.Id = integerExpression.Value; var functionCall = scope.GetContext <FunctionCallExpression>(); if (functionCall != null && functionCall.FunctionName.Name == this.Name.Name) { leaderboard.SourceLine = functionCall.Location.Start.Line; } var context = scope.GetContext <AchievementScriptContext>(); Debug.Assert(context != null); context.Leaderboards.Add(leaderboard); return(true); }
public void Select(ExpressionBase expression) { expression.Selected = expression.Selected == false; UpdateSelection(); UpdateIdentities(); }
public IWhereClauseConnectionBase LTreeMatch(string propertyName, string value) { _first = new ExpressionLTreeMatch(propertyName, value); return(this); }
public ExpressionBase <T> Optimize(ExpressionBase <T> expression) => expression switch {
private static bool NormalizeNots(ref ExpressionBase expression, out ParseErrorExpression error) { error = null; // not a condition - don't need to worry about it var condition = expression as ConditionalExpression; if (condition == null) { return(true); } // not a not, just recurse if (condition.Operation != ConditionalOperation.Not) { var left = condition.Left; if (!NormalizeNots(ref left, out error)) { return(false); } var right = condition.Right; if (!NormalizeNots(ref right, out error)) { return(false); } if (!ReferenceEquals(left, condition.Left) || !ReferenceEquals(right, condition.Right)) { expression = new ConditionalExpression(left, condition.Operation, right); } return(true); } // found a not - eliminate it var operand = ((ConditionalExpression)expression).Right; // logical inversion condition = operand as ConditionalExpression; if (condition != null) { switch (condition.Operation) { case ConditionalOperation.Not: // !(!A) => A expression = condition.Right; break; case ConditionalOperation.And: // !(A && B) => !A || !B expression = new ConditionalExpression( new ConditionalExpression(null, ConditionalOperation.Not, condition.Left), ConditionalOperation.Or, new ConditionalExpression(null, ConditionalOperation.Not, condition.Right)); break; case ConditionalOperation.Or: // !(A || B) => !A && !B expression = new ConditionalExpression( new ConditionalExpression(null, ConditionalOperation.Not, condition.Left), ConditionalOperation.And, new ConditionalExpression(null, ConditionalOperation.Not, condition.Right)); break; default: throw new NotImplementedException("Unsupported condition operation"); } return(NormalizeNots(ref expression, out error)); } // comparative inversion var comparison = operand as ComparisonExpression; if (comparison != null) { // !(A == B) => A != B, !(A < B) => A >= B, ... expression = new ComparisonExpression( comparison.Left, ComparisonExpression.GetOppositeComparisonOperation(comparison.Operation), comparison.Right); return(NormalizeNots(ref expression, out error)); } // unsupported inversion error = new ParseErrorExpression("! operator cannot be applied to " + operand.Type, operand); return(false); }
public override bool BuildMacro(RichPresenceDisplayFunction.RichPresenceDisplayContext context, InterpreterScope scope, out ExpressionBase result) { var macro = GetStringParameter(scope, "macro", out result); if (macro == null) { return(false); } var expression = GetParameter(scope, "expression", out result); if (expression == null) { return(false); } var value = TriggerBuilderContext.GetValueString(expression, scope, out result); if (value == null) { return(false); } var functionCall = scope.GetContext <FunctionCallExpression>(); var valueFormat = GetValueFormat(macro.Value); context.RichPresence.AddValueField(functionCall, macro.Value, valueFormat); result = new StringConstantExpression(String.Format("@{0}({1})", macro.Value, value)); return(true); }
public override bool Evaluate(InterpreterScope scope, out ExpressionBase result) { var stringExpression = GetStringParameter(scope, "format_string", out result); if (stringExpression == null) { return(false); } var varargs = GetParameter(scope, "varargs", out result) as ArrayExpression; if (varargs == null) { if (!(result is ParseErrorExpression)) { result = new ParseErrorExpression("unexpected varargs", stringExpression); } return(false); } var builder = new StringBuilder(); var tokenizer = new PositionalTokenizer(Tokenizer.CreateTokenizer(stringExpression.Value)); while (tokenizer.NextChar != '\0') { var token = tokenizer.ReadTo('{'); builder.Append(token.ToString()); if (tokenizer.NextChar == '\0') { break; } var positionalTokenColumn = tokenizer.Column; tokenizer.Advance(); if (tokenizer.NextChar == '}') { result = new ParseErrorExpression("Empty parameter index", stringExpression.Location.Start.Line, stringExpression.Location.Start.Column + positionalTokenColumn, stringExpression.Location.Start.Line, stringExpression.Location.Start.Column + tokenizer.Column - 1); return(false); } var index = tokenizer.ReadNumber(); if (tokenizer.NextChar != '}') { result = new ParseErrorExpression("Invalid positional token", stringExpression.Location.Start.Line, stringExpression.Location.Start.Column + positionalTokenColumn, stringExpression.Location.Start.Line, stringExpression.Location.Start.Column + tokenizer.Column - 1); return(false); } tokenizer.Advance(); Int32 parameterIndex; if (!Int32.TryParse(index.ToString(), out parameterIndex) || parameterIndex < 0 || parameterIndex >= varargs.Entries.Count) { result = new ParseErrorExpression("Invalid parameter index: " + index.ToString(), stringExpression.Location.Start.Line, stringExpression.Location.Start.Column + positionalTokenColumn, stringExpression.Location.Start.Line, stringExpression.Location.Start.Column + tokenizer.Column - 1); return(false); } result = varargs.Entries[parameterIndex]; var functionCall = result as FunctionCallExpression; if (functionCall != null) { if (!functionCall.Evaluate(scope, out result)) { return(false); } } if (result.IsLiteralConstant) { result.AppendStringLiteral(builder); } else { result = new ParseErrorExpression("Cannot convert expression to string", result); return(false); } } result = new StringConstantExpression(builder.ToString()); return(true); }
private string BuildTooltip(ExpressionBase expression) { if (expression.Location.Start.Line > 0 && expression.Location.End.Line > 0) { var text = GetText(expression.Location.Start.Line, expression.Location.Start.Column, expression.Location.End.Line, expression.Location.End.Column + 1); var builder = new StringBuilder(); bool lastCharWasWhitespace = true; bool inComment = false; foreach (var c in text) { if (inComment) { inComment = (c != '\n'); } else if (Char.IsWhiteSpace(c)) { if (lastCharWasWhitespace) { continue; } builder.Append(' '); lastCharWasWhitespace = true; } else if (c == '/' && (builder.Length > 0 && builder[builder.Length - 1] == '/')) { inComment = true; builder.Length--; lastCharWasWhitespace = (builder.Length == 0 || builder[builder.Length - 1] == ' '); } else { builder.Append(c); lastCharWasWhitespace = false; } } if (lastCharWasWhitespace && builder.Length > 0) { builder.Length--; } return(builder.ToString()); } var tooltip = expression.ToString(); var index = tooltip.IndexOf(':'); if (index >= 0) { if (tooltip[index + 1] == ' ') { index++; } tooltip = tooltip.Substring(index + 1); } return(tooltip); }
public SubtractionExpression(ExpressionBase.IExpressionBase expr1, ExpressionBase.IExpressionBase expr2) { _expr1 = expr1; _expr2 = expr2; }
public void UnSelectAll() { for (int index = 0; index < selection.Count; index++) { selection[index].Selected = false; } selection.Clear(); identities.Clear(); selectionParent = null; callOnChanged(); }
public IWhereClauseConnectionBase Like(string propertyName, string value, bool ignoreCase) { _first = new ExpressionLike(propertyName, value, ignoreCase); return(this); }
internal WhereClauseOr(ExpressionBase firstxpressions) { Xpressions.Add(firstxpressions); }
public FunctionExpression(ExpressionBase expression, string function) { Expression = expression; Expression.Parent = this; Function = function.ToLower(); }
public IWhereClauseConnectionBase IsNotNull(string propertyName) { _first = new ExpressionIsNotNull(propertyName); return(this); }
public void UpdateIdentities() { UpdateSelection(); selectionParent = analyzer.GetCommonParent(selection); identities = analyzer.GetIdentities(selection); Console.WriteLine("Identities updated"); if (OnChanged != null) OnChanged(this); }
/// <summary> /// Constructor. /// </summary> /// <param name="expression">A C# expression that is evaluated to determine if the event should be traced or not.</param> public ExpressionFilter(string expression) { if (expression == null) throw new ArgumentNullException("expression"); compiledExpression = CompileExpression(expression); }
public SubtractionExpression(ExpressionBase expression1, ExpressionBase expression2) { this.expression1 = expression1; this.expression2 = expression2; }
public IWhereClauseConnectionBase Lt(string propertyName, object value) { _first = new ExpressionLowerThan(propertyName, value); return(this); }
public Not(ExpressionBase innerExpression) : base(innerExpression) { }
public ComputedQuestion(QLMemory memory, string name, string label, QType type, ExpressionBase expression) : base(memory, name, label, type) { _value = expression; }
public override bool Equals(ExpressionBase otherBase) { var other = (otherBase as FunctionExpression); if (other == null) return false; return this.Function == other.Function && this.Expression == other.Expression; }
protected ParseErrorExpression BuildTriggerCondition(TriggerBuilderContext context, InterpreterScope scope, ExpressionBase condition) { var builder = new ScriptInterpreterAchievementBuilder(); ExpressionBase result; if (!TriggerBuilderContext.ProcessAchievementConditions(builder, condition, scope, out result)) { return((ParseErrorExpression)result); } if (builder.AlternateRequirements.Count > 0) { return(new ParseErrorExpression(Name.Name + " does not support ||'d conditions", condition)); } if (builder.CoreRequirements.Count > 1) { var last = builder.CoreRequirements.Last(); foreach (var requirement in builder.CoreRequirements) { if (requirement.Type == RequirementType.None && !ReferenceEquals(requirement, last)) { requirement.Type = RequirementType.AndNext; } } } ParseErrorExpression error = ValidateSingleCondition(builder.CoreRequirements); if (error != null) { return(new ParseErrorExpression(error, condition)); } error = ModifyRequirements(builder); if (error != null) { return(error); } foreach (var requirement in builder.CoreRequirements) { context.Trigger.Add(requirement); } return(null); }
public AdditionExpression(ExpressionBase.IExpressionBase expr1, ExpressionBase.IExpressionBase expr2) { _expr1 = expr1; _expr2 = expr2; }
public AdditionExpression(ExpressionBase exp1, ExpressionBase exp2) { _expression1 = exp1; _expression2 = exp2; }
public Sub(ExpressionBase leftExpression, ExpressionBase rightExpression) : base(leftExpression, rightExpression) { }
public IWhereClauseConnectionBase Eq(string propertyName, object value) { _first = new ExpressionEqual(propertyName, value); return(this); }
public string Serialize(ExpressionBase expression) { return expression.ToString(); }
private static ExpressionBase Parse(string input) { return(ExpressionBase.Parse(new PositionalTokenizer(Tokenizer.CreateTokenizer(input)))); }
public void DeclareComputedValue(string name, ExpressionBase value) { _idsComputedValues[name] = value; }
public override bool ReplaceVariables(InterpreterScope scope, out ExpressionBase result) { return(Evaluate(scope, out result)); }
public override Boolean Replace(ExpressionBase old, ExpressionBase replacement, bool doRecursively) { return false; }
public Pos(ExpressionBase innerExpression) : base(innerExpression) { }
public DelimiterExpression(ExpressionBase expression) { Expression = expression; Expression.Parent = this; }
public Equals(ExpressionBase leftExpression, ExpressionBase rightExpression) : base(leftExpression, rightExpression) { }
public GrThEq(ExpressionBase leftExpression, ExpressionBase rightExpression) : base(leftExpression, rightExpression) { }
/// <summary> /// Set the new expression. /// </summary> /// <param name="newExpr">The new expression.</param> public void SwitchCurrentExpression(ExpressionBase newExpr) { var updateResult = true; // Whitespace insertion control at operators. if (newExpr is Operator) { var op = (Operator)newExpr; // TODO @ Parser: Still errors in whitespace insertion... if (CurrentExpr == null || (CurrentExpr is Operator && ((Operator)CurrentExpr).HasRightWhitespace)) { op.HasLeftWhitespace = false; op.HasRightWhitespace = false; } } // Control the behaviour of sub- and superscript. if (SubAndSuperscriptExpr != null) { if (SubAndSuperscriptType == CharType.Subscript) { if (SubAndSuperscriptExpr.SubScript == null) { SubAndSuperscriptExpr.SubScript = newExpr; return; } else { SubAndSuperscriptExpr = null; SubAndSuperscriptType = CharType.Unknown; } } else { if (SubAndSuperscriptExpr.SuperScript == null) { SubAndSuperscriptExpr.SuperScript = newExpr; return; } else { SubAndSuperscriptExpr = null; SubAndSuperscriptType = CharType.Unknown; } } } // Assign new expression to current expression. CurrentExpr = newExpr; // Handle command expressions. Stop adding expressions after the required count was reached. if (CommandExpr != null) { updateResult = false; CommandExpr.AddEpression(CurrentExpr); CurrentExpr = CommandExpr; if (CommandExpr.RequiredExpressionCount == 0) { // Handle fractions before they are finished. if (CommandExpr is Fraction) { FractionStackCounter--; } // Set the previous command expression or null. if (CommandExprStack.Count > 0) { CommandExpr = CommandExprStack.Pop(); } else { CommandExpr = null; } } } // Check, whether this expression is a composite expression and therefore needs input blocks. if (CurrentExpr is CompositeExpression && (CurrentExpr != CommandExpr) && ((CompositeExpression)CurrentExpr).RequiredExpressionCount > 0) { if (CommandExpr != null) { CommandExprStack.Push(CommandExpr); } CommandExpr = (CompositeExpression)CurrentExpr; // Handle the fraction stack. if (newExpr is Fraction) { FractionStackCounter++; ((Fraction)newExpr).IsSubFraction = FractionStackCounter > 1; } } // Break here, if needed. if (updateResult) { // Set the new result: // - If the last result was null, copy the new expression. // - Otherwise, try to cast to a block or create a new one. if (Result == null) { Result = CurrentExpr; } else { var block = (Result is Block) ? (Block)Result : new Block() { Expressions = { Result } }; block.Expressions.Add(CurrentExpr); Result = block; } } }
public Div(ExpressionBase leftExpression, ExpressionBase rightExpression) : base(leftExpression, rightExpression) { }
public IWhereClauseConnectionBase Any(string propertyName, params object[] value) { _first = new ExpressionAny(propertyName, value); return(this); }