/// <summary> /// Parses the specified text converting it into a expression action. /// </summary> /// <param name="text">The text to parse.</param> /// <returns></returns> public Expression <Func <decimal> > Parse(string text) { var body = language.Parse(text); body = ExpressionConversions.Convert(body, typeof(decimal)); return(Expression.Lambda <Func <decimal> >(body)); }
/// <summary> /// Parses the specified text converting it into an expression. The expression can take a single parameter /// </summary> /// <typeparam name="T">the type of the parameter.</typeparam> /// <param name="text">The text to parse.</param> /// <returns></returns> public Expression <Func <T, decimal> > Parse <T>(string text) { var parameters = new[] { Expression.Parameter(typeof(T)) }; var body = language.Parse(text, parameters); body = ExpressionConversions.Convert(body, typeof(decimal)); return(Expression.Lambda <Func <T, decimal> >(body, parameters)); }
/// <summary> /// Applies the bracket operands. Executes the expressionBuilder with all the operands in the brackets. /// </summary> /// <param name="bracketOpen">The operator that opened the bracket.</param> /// <param name="bracketOperands">The list of operands within the brackets.</param> /// <param name="bracketClose">The operator that closed the bracket.</param> /// <param name="state">The current parse state.</param> /// <exception cref="FunctionArgumentCountException">When the number of opreands does not match the number of arguments</exception> /// <exception cref="FunctionArgumentTypeException">When argument Type does not match the type of the expression</exception> /// <exception cref="OperationInvalidException">When an error occured while executing the expressionBuilder</exception> public override void ApplyBracketOperands(Operator bracketOpen, Stack <Operand> bracketOperands, Operator bracketClose, ParseState state) { var operandSource = StringSegment.Encompass(bracketOperands.Select(x => x.SourceMap)); var functionArguments = bracketOperands.Select(x => x.Expression); //if we have been given specific argument types validate them if (ArgumentTypes != null) { var expectedArgumentCount = ArgumentTypes.Count; if (expectedArgumentCount != bracketOperands.Count) { throw new FunctionArgumentCountException( operandSource, expectedArgumentCount, bracketOperands.Count); } functionArguments = bracketOperands.Zip(ArgumentTypes, (o, t) => { try { return(ExpressionConversions.Convert(o.Expression, t)); } catch (InvalidOperationException) { //if we cant convert to the argument type then something is wrong with the argument //so we will throw it up throw new FunctionArgumentTypeException(o.SourceMap, t, o.Expression.Type); } }); } var functionSourceMap = StringSegment.Encompass(bracketOpen.SourceMap, operandSource); var functionArgumentsArray = functionArguments.ToArray(); Expression output; try { output = ExpressionBuilder(functionArgumentsArray); } catch (Exception ex) { throw new OperationInvalidException(functionSourceMap, ex); } if (output != null) { state.Operands.Push(new Operand(output, functionSourceMap)); } }