public Func <double, double> ParseFunction(string functions) { if (functions.Contains("sin")) { return(SinFunc(functions)); } if (functions.Contains("cos")) { return(CosFunc(functions)); } if (functions.Contains("+")) { var index = functions.IndexOf("+"); return(PlusFunc(index, functions)); } if (functions.Contains("-")) { var index = functions.IndexOf("-"); return(MinusFunc(index, functions)); } for (var i = 0; i < functions.Length; i++) { if (functions[i] == '*') { return(d => ParseFunction(functions.Substring(0, i))(d) * ParseFunction(functions.Substring(i + 1))(d)); } if (char.IsDigit(functions[i]) && i != functions.Length - 1 && char.IsLetter(functions[i + 1])) { return(d => double.Parse(functions.Substring(0, i + 1)) * ParseFunction(functions.Substring(i + 1))(d)); } } for (var i = 0; i < functions.Length; i++) { if (functions[i] == '^') { return(d => Math.Pow(d, double.Parse(functions[i + 1].ToString()))); } } for (var i = 0; i < functions.Length; i++) { if (char.IsDigit(functions[i])) { return(d => double.Parse(functions)); } else if (char.IsLetter(functions[i])) { return(d => d); } } return(d => 0); }
public Command(string name, string description, IReadOnlyList <Arg> arguments, ParseFunction parse) { Name = name; Description = description; Arguments = arguments; Parse = parse; }
/// <summary> /// Constructor. /// </summary> public FrontEndParseInput(AbsolutePath path, ParseFunction parse) { Contract.Requires(path.IsValid); Contract.Requires(parse != null); Path = path; Parse = parse; }
private static AstNode LeftAssocCombiner( string[] operators, ParseFunction termParser, TokenStream tokenStream) { AstNode leftTerm = termParser(tokenStream); while (tokenStream.HasMoreTokens && OneOf(tokenStream.Current.Content, operators)) { AstNode tokenNode = new AstNode(AstNodeType.Identifier, tokenStream.Current); tokenStream.Next(); AstNode rightTerm = termParser(tokenStream); leftTerm = new AstNode( AstNodeType.BinaryExpression, new AstNode[] { tokenNode, leftTerm, rightTerm }); } return leftTerm; }
public Func <double, double> MinusFunc(int index, string functions) { return(d => ParseFunction(functions.Substring(0, index))(d) - ParseFunction(functions.Substring(index + 1))(d)); }
//bool reset = false; /// <summary> /// Constructs an empty parse state. /// </summary> public ParseState() { Execute = InitialState; }
protected ParseState Next(ParseFunction next) { Execute = next; return(this); }
public static Func <InputField, OneOf <Success, Error <string> > > CreateInputFieldValidator <T>(ParseFunction <T> parse, Func <string> errorMessage) { return(field => { if (parse(field.text, out var _)) { return new Success(); } return new Error <string>(errorMessage()); }); }
public Parser(string name, ParseFunction <TSource, TResult> parse) { Name = name; _parse = parse; }
public Parser(ParseFunction <TSource, TResult> parse) : this(null, parse) { }