Ejemplo n.º 1
0
        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);
        }
Ejemplo n.º 2
0
 public Command(string name, string description, IReadOnlyList <Arg> arguments, ParseFunction parse)
 {
     Name        = name;
     Description = description;
     Arguments   = arguments;
     Parse       = parse;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Constructor.
        /// </summary>
        public FrontEndParseInput(AbsolutePath path, ParseFunction parse)
        {
            Contract.Requires(path.IsValid);
            Contract.Requires(parse != null);

            Path  = path;
            Parse = parse;
        }
Ejemplo n.º 4
0
 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;
 }
Ejemplo n.º 5
0
 public Func <double, double> MinusFunc(int index, string functions)
 {
     return(d => ParseFunction(functions.Substring(0, index))(d) -
            ParseFunction(functions.Substring(index + 1))(d));
 }
Ejemplo n.º 6
0
        //bool reset = false;

        /// <summary>
        /// Constructs an empty parse state.
        /// </summary>
        public ParseState()
        {
            Execute = InitialState;
        }
Ejemplo n.º 7
0
 protected ParseState Next(ParseFunction next)
 {
     Execute = next;
     return(this);
 }
Ejemplo n.º 8
0
    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());
        });
    }
Ejemplo n.º 9
0
 public Parser(string name, ParseFunction <TSource, TResult> parse)
 {
     Name   = name;
     _parse = parse;
 }
Ejemplo n.º 10
0
 public Parser(ParseFunction <TSource, TResult> parse)
     : this(null, parse)
 {
 }