public void SetArguments() { var parser = new ExpressionParser(); if (useScaledTForPath || useScaledTForScale) { eScaledT = parser.EvaluateExpression(scaledT).ToDelegate("t"); _ = eScaledT(0); } if (!useKeyframePath) { ePathX = parser.EvaluateExpression(pathX).ToDelegate(useScaledTForPath ? "T" : "t"); ePathY = parser.EvaluateExpression(pathY).ToDelegate(useScaledTForPath ? "T" : "t"); _ = ePathX(0); _ = ePathY(0); } if (!useKeyframeScale) { eScaleX = parser.EvaluateExpression(scaleX).ToDelegate(useScaledTForScale ? "T" : "t"); eScaleY = parser.EvaluateExpression(scaleY).ToDelegate(useScaledTForScale ? "T" : "t"); _ = eScaleX(0); _ = eScaleY(0); } }
/// <summary> /// Parses a Angular comment directive /// </summary> /// <param name="commentText">Comment text</param> /// <param name="directiveNameHandler">Directive name handler</param> /// <param name="expressionHandler">Binding expression handler</param> public static void ParseCommentDirective(string commentText, DirectiveNameDelegate directiveNameHandler, ExpressionDelegate expressionHandler) { Match match = _ngCommentDirectiveRegex.Match(commentText); if (match.Success) { var innerContext = new InnerMarkupParsingContext(commentText); var context = new MarkupParsingContext(innerContext); GroupCollection groups = match.Groups; Group directiveNameGroup = groups["directiveName"]; int directiveNamePosition = directiveNameGroup.Index; string originalDirectiveName = directiveNameGroup.Value; string normalizedDirectiveName = NormalizeDirectiveName(originalDirectiveName); innerContext.IncreasePosition(directiveNamePosition); directiveNameHandler?.Invoke(context, originalDirectiveName, normalizedDirectiveName); Group expressionGroup = groups["expression"]; if (expressionGroup.Success) { int expressionPosition = expressionGroup.Index; string expression = expressionGroup.Value.Trim(); innerContext.IncreasePosition(expressionPosition - directiveNamePosition); expressionHandler?.Invoke(context, expression); } } }
/// <summary> /// Parses a Angular class directive /// </summary> /// <param name="className">Class name</param> /// <param name="directiveNameHandler">Directive name handler</param> /// <param name="expressionHandler">Binding expression handler</param> /// <param name="semicolonHandler">Semicolon handler</param> public static void ParseClassDirective(string className, DirectiveNameDelegate directiveNameHandler, ExpressionDelegate expressionHandler, SemicolonDelegate semicolonHandler) { MatchCollection ngClassDirectiveMatches = _ngClassDirectiveRegex.Matches(className); if (ngClassDirectiveMatches.Count > 0) { var innerContext = new InnerMarkupParsingContext(className); var context = new MarkupParsingContext(innerContext); int currentPosition = 0; foreach (Match ngClassDirectiveMatch in ngClassDirectiveMatches) { GroupCollection groups = ngClassDirectiveMatch.Groups; Group directiveNameGroup = groups["directiveName"]; int directiveNamePosition = directiveNameGroup.Index; string originalDirectiveName = directiveNameGroup.Value; string normalizedDirectiveName = NormalizeDirectiveName(originalDirectiveName); innerContext.IncreasePosition(directiveNamePosition - currentPosition); currentPosition = directiveNamePosition; if (directiveNameHandler != null) { directiveNameHandler(context, originalDirectiveName, normalizedDirectiveName); } Group expressionGroup = groups["expression"]; if (expressionGroup.Success) { int expressionPosition = expressionGroup.Index; string expression = expressionGroup.Value.Trim(); innerContext.IncreasePosition(expressionPosition - currentPosition); currentPosition = expressionPosition; if (expressionHandler != null) { expressionHandler(context, expression); } } Group semicolonGroup = groups["semicolon"]; if (semicolonGroup.Success) { int semicolonPosition = semicolonGroup.Index; innerContext.IncreasePosition(semicolonPosition - currentPosition); currentPosition = semicolonPosition; if (semicolonHandler != null) { semicolonHandler(context); } } } } }
public override void Initialize (IFormulaParent _parent) { base.Initialize (_parent); ExpressionParser parser = new ExpressionParser (); Expression expression = parser.EvaluateExpression (mFormulaText); ApplyFormula = expression.ToDelegate(new List<string>(mParameters.Keys).ToArray()); }
public static int[] Map(this int[] numbers, ExpressionDelegate expressionDelegate) { int[] result = new int[numbers.Length]; for (int i = 0; i < result.Length; i++) { result[i] = expressionDelegate(numbers[i]); } return(result); }
public static int[] Map(this int[] array, ExpressionDelegate expressionDelegate) { int[] result = new int[array.Length]; // for each number, using delegate, return its mapped value for (int i = 0; i < array.Length; i++) { result[i] = expressionDelegate(array[i]); } return(result); }
//Set the xExpr delegate with the proper string. eg : "sin(x) + cos(y)" public void SetExprX(string expression) { try { xExpr = Expression.Parse(expression).ToDelegate(parameters.ToArray()); } catch (ExpressionParser.ParseException exception)//Should an error occur, set the expression equal to 0. { Error = exception.ToString(); xExpr = Expression.Parse("x").ToDelegate(parameters.ToArray()); } }
//Set the zExpr delegate with the proper string. eg : "sin(x) + cos(y)" public void SetExprZ(string expression) { try { zExpr = Expression.Parse(expression).ToDelegate(parameters.ToArray()); } catch (ExpressionParser.ParseException exception) { Error = exception.ToString(); zExpr = Expression.Parse("x").ToDelegate(parameters.ToArray()); } }
/// <summary> /// Parses a Angular class directive /// </summary> /// <param name="className">Class name</param> /// <param name="directiveNameHandler">Directive name handler</param> /// <param name="expressionHandler">Binding expression handler</param> /// <param name="semicolonHandler">Semicolon handler</param> public static void ParseClassDirective(string className, DirectiveNameDelegate directiveNameHandler, ExpressionDelegate expressionHandler, SemicolonDelegate semicolonHandler) { MatchCollection matches = _ngClassDirectiveRegex.Matches(className); int matchCount = matches.Count; if (matchCount > 0) { var innerContext = new InnerMarkupParsingContext(className); var context = new MarkupParsingContext(innerContext); int currentPosition = 0; for (int matchIndex = 0; matchIndex < matchCount; matchIndex++) { Match match = matches[matchIndex]; GroupCollection groups = match.Groups; Group directiveNameGroup = groups["directiveName"]; int directiveNamePosition = directiveNameGroup.Index; string originalDirectiveName = directiveNameGroup.Value; string normalizedDirectiveName = NormalizeDirectiveName(originalDirectiveName); innerContext.IncreasePosition(directiveNamePosition - currentPosition); currentPosition = directiveNamePosition; directiveNameHandler?.Invoke(context, originalDirectiveName, normalizedDirectiveName); Group expressionGroup = groups["expression"]; if (expressionGroup.Success) { int expressionPosition = expressionGroup.Index; string expression = expressionGroup.Value.Trim(); innerContext.IncreasePosition(expressionPosition - currentPosition); currentPosition = expressionPosition; expressionHandler?.Invoke(context, expression); } Group semicolonGroup = groups["semicolon"]; if (semicolonGroup.Success) { int semicolonPosition = semicolonGroup.Index; innerContext.IncreasePosition(semicolonPosition - currentPosition); currentPosition = semicolonPosition; semicolonHandler?.Invoke(context); } } } }
public object Invoke(object owner, params object[] parameters) { if (parameters.Length == 0) { return(ExpressionDelegate.DynamicInvoke(owner)); } object[] iuputParameters = new object[parameters.Length + 1]; iuputParameters.SetValue(owner, 0); for (int i = 1; i <= parameters.Length; i++) { iuputParameters.SetValue(parameters[i - 1], i); } return(ExpressionDelegate.DynamicInvoke(iuputParameters)); }
/// <summary> /// Parses a Knockout begin containerless comment /// </summary> /// <param name="commentText">Comment text</param> /// <param name="expressionHandler">Binding expression handler</param> public static void ParseBeginContainerlessComment(string commentText, ExpressionDelegate expressionHandler) { Match match = _koBeginContainerlessCommentRegex.Match(commentText); if (match.Success) { var innerContext = new InnerMarkupParsingContext(commentText); var context = new MarkupParsingContext(innerContext); Group expressionGroup = match.Groups["expression"]; int expressionPosition = expressionGroup.Index; string expression = expressionGroup.Value.TrimEnd(null); innerContext.IncreasePosition(expressionPosition); expressionHandler?.Invoke(context, expression); } }
private void OnExpressionChanged(String expression) { MethodInfo method = null; try { String unfriendlyExpression = expression; foreach (var key in parameters.Keys) { String name = key as String; if (name != null) { // Regular expression that matches the name of the parameter and won't // match if that parameters is a substring of something else. It will match // the preceeding and succeeding character as well. String regexp = String.Format(@"(?<![\.a-zA-Z0-9_]){0}(?![a-zA-Z0-9_])", name); // Replace string that puts the preceeding and suceeding characters matched // and changes the name of the variable to a reference in the paramter dict. String replaceWith = String.Format("(({0})((IDictionary)p[0])[\"{1}\"])", parameters[key].GetType().FullName, key); // Replace the parameter. unfriendlyExpression = Regex.Replace(unfriendlyExpression, regexp, replaceWith); } } method = ReflectionHelper.CompileCSharpMethod(unfriendlyExpression, typeof(T)); } catch { GearsetResources.Console.Log("Gearset", "Error while compiling CSExpression."); GearsetResources.Console.Log("Gearset", "Invalid Expression: {0}", expression); } if (method != null) { ExpressionMethod = (ExpressionDelegate)Delegate.CreateDelegate(typeof(ExpressionDelegate), method); this.expression = expression; } }
/// <summary> /// Parses a Knockout begin containerless comment /// </summary> /// <param name="commentText">Comment text</param> /// <param name="expressionHandler">Binding expression handler</param> public static void ParseBeginContainerlessComment(string commentText, ExpressionDelegate expressionHandler) { Match koBeginContainerlessCommentMatch = _koBeginContainerlessCommentRegex.Match(commentText); if (koBeginContainerlessCommentMatch.Success) { var innerContext = new InnerMarkupParsingContext(commentText); var context = new MarkupParsingContext(innerContext); Group expressionGroup = koBeginContainerlessCommentMatch.Groups["expression"]; int expressionPosition = expressionGroup.Index; string expression = expressionGroup.Value.TrimEnd(); innerContext.IncreasePosition(expressionPosition); if (expressionHandler != null) { expressionHandler(context, expression); } } }
private void BuildPlot(ExpressionDelegate fun) { deleteLines(); foreach (BoxCollider2D dot in graphDots) { Destroy(dot); } graphDots.Clear(); double w = Math.Abs(to - from); double sw = cam.ScreenToWorldPoint(new Vector3(Screen.width, 0f, 0f)).x; double xPrev = from + sw - 10 * w / 20; if (fun != null) { double yPrev = fun(-10) / 20 * w; for (double x = -10 + step; x < 10; x += step) { double ty = fun(x) / 20 * w; double tx = from + sw + x * w / 20; if (Math.Abs(ty) < w / 2 && Math.Abs(yPrev) < w / 2 && goodNumbers(tx, ty, xPrev, yPrev)) buildSegment(tx, ty, xPrev, yPrev); xPrev = tx; yPrev = ty; } } else { for (double x = -10 + step; x < 10; x += step) { double tx = from + sw + x * w / 20; if (goodNumbers(tx, xPrev)) buildSegment(tx, 0, xPrev, 0); xPrev = tx; } } }
/// <summary> /// Parses a Angular comment directive /// </summary> /// <param name="commentText">Comment text</param> /// <param name="directiveNameHandler">Directive name handler</param> /// <param name="expressionHandler">Binding expression handler</param> public static void ParseCommentDirective(string commentText, DirectiveNameDelegate directiveNameHandler, ExpressionDelegate expressionHandler) { Match ngCommentDirectiveMatch = _ngCommentDirectiveRegex.Match(commentText); if (ngCommentDirectiveMatch.Success) { var innerContext = new InnerMarkupParsingContext(commentText); var context = new MarkupParsingContext(innerContext); GroupCollection groups = ngCommentDirectiveMatch.Groups; Group directiveNameGroup = groups["directiveName"]; int directiveNamePosition = directiveNameGroup.Index; string originalDirectiveName = directiveNameGroup.Value; string normalizedDirectiveName = NormalizeDirectiveName(originalDirectiveName); innerContext.IncreasePosition(directiveNamePosition); if (directiveNameHandler != null) { directiveNameHandler(context, originalDirectiveName, normalizedDirectiveName); } Group expressionGroup = groups["expression"]; if (expressionGroup.Success) { int expressionPosition = expressionGroup.Index; string expression = expressionGroup.Value.Trim(); innerContext.IncreasePosition(expressionPosition - directiveNamePosition); if (expressionHandler != null) { expressionHandler(context, expression); } } } }
public static float numDerivative(float t, ExpressionDelegate expressionDelegate) //Calculate the numerical derivative with 0.001. { return((float)((expressionDelegate(0.001 + t) - expressionDelegate(t - 0.001)) / (2 * 0.001))); }
public ParExpression(ExpressionDelegate expression) { this.value = expression; }
//int debug_iter = 0; //Attempts to parse an expression. Sets invalidity flags and updates the status display //fieldNumber: X=1, Y=2, Z=3 public void ValidateExpression(int fieldNumber) { string expr; //Set the field we're looking at switch (fieldNumber) { case 1: expr = expressionXInput.text; //Debug.Log("Validation Field number" + fieldNumber + ". X equation input='" + expr + "'"); break; case 2: expr = expressionYInput.text; //Debug.Log("Validation Field number" + fieldNumber + ". Y equation input='" + expr + "'"); break; case 3: expr = expressionZInput.text; //Debug.Log("Validation Field number" + fieldNumber + ". Z equation input='" + expr + "'"); break; default: //shouldn't happen //Debug.Log("fieldNumber using default."); expr = expressionXInput.text; break; } //debug_iter += 1; //Debug.Log("Validating: Current expr at (" + debug_iter + "): <" + expr + ">"); //Validate try { //First check for empty expression if (expr == "") { //Debug.Log("Empty string detected. Current expr at (" + debug_iter + "): <" + expr + ">"); throw new EmptyExpressionException("Please enter all equations."); } ExpressionParser parser = new ExpressionParser(); //First, read in the user parameters as constants and give it test values int i = 1; foreach (KeyValuePair <string, double> p in parameters) { //each test value should be unique and nonzero to avoid divide by zero parser.AddConst(p.Key, () => 0.00013 * (i * 17)); i++; } //Next, try to evaluate the expression Expression tempExpr = parser.EvaluateExpression(expr); //If no ParseException was thrown, expression parsed correctly //This is where I'd check for custom functions, if that was handled. //Now let's check the parameters of the Expression foreach (KeyValuePair <string, Parameter> p in tempExpr.Parameters) { if (!(variablesAvailable.Contains(p.Key))) { throw new UnknownIdentiferException("Unknown identifier '" + p.Key + "'."); } } //Now for an invocation test: if (order == 1) { ExpressionDelegate deleg = tempExpr.ToDelegate("x", "y", "z", "t"); double[] values = { 0.125778, 0.13456928, 0.2944782, 0.41698793 }; deleg.Invoke(values); } else //order == 2 { ExpressionDelegate deleg = tempExpr.ToDelegate("x", "x'", "y", "y'", "z", "z'", "t"); double[] values = { 0.155926, 0.13562489, 0.2990285, 0.412947352, 1.8308463, 1.0329475, 2.7386352 }; deleg.Invoke(values); } //If no exception, then expression is validated. //Debug.Log("Expression validated: fieldNumber=" + fieldNumber); valid[(fieldNumber - 1)] = true; //All good in this expression. Check the other invalid flags CheckInvalidFlags(); UpdateStatusDisplay(validationStatusDisplay.text); } catch (ExpressionParser.ParseException ex) { //validation unsuccessful due to parsing error valid[(fieldNumber - 1)] = false; UpdateStatusDisplay(ex.Message); } catch (UnknownIdentiferException ex) { //validation unsuccessful due to unknown identifier valid[(fieldNumber - 1)] = false; UpdateStatusDisplay(ex.Message); } catch (EmptyExpressionException ex) { //validation unsuccessful due to empty expression valid[(fieldNumber - 1)] = false; UpdateStatusDisplay(ex.Message); } catch (Exception ex) { //validation unsuccessful on invocation test valid[(fieldNumber - 1)] = false; UpdateStatusDisplay("Unknown error. Check inputs."); Debug.Log(ex.Message); } }
/// <summary> /// Set a method that resolves to a RAW SQL expression. /// </summary> /// <param name="expression">The expression resolver.</param> /// <returns>The <see cref="SqlBatchConfigBuilder"/>.</returns> public SqlBatchConfigBuilder ParentKey(ExpressionDelegate expression) { SqlBatchConfig.ParentKeyExpression = expression; return(this); }
/// <summary> /// Set a method that resolves to a RAW SQL expression. /// </summary> /// <param name="expression">The expression resolver.</param> /// <returns>The <see cref="SqlColumnConfigBuilder"/>.</returns> public SqlColumnConfigBuilder Expression(ExpressionDelegate expression) { SqlColumnConfig.Expression = expression; return(this); }
public static object OperateTernary(bool result, ExpressionDelegate x, ExpressionDelegate y) { return(result ? x() : y()); }
public static object OperateTernary(bool result, ExpressionDelegate x, ExpressionDelegate y) { return result ? x() : y(); }