Ejemplo n.º 1
0
        private static Symbol Delayed(Expression exp, Scope localContext)
        {
            Symbol       name     = exp.Args[0];
            StringSymbol variable = (StringSymbol)Substitute((StringSymbol)exp.Args[1], localContext);
            Expression   function;

            if (exp.Args[2] is Expression ex)
            {
                function = ex;
            }
            else if (exp.Args[2] is StringSymbol ss)
            {
                Symbol temp = ss;
                while (temp is StringSymbol)
                {
                    temp = Substitute((StringSymbol)temp, localContext);
                }

                function = (Expression)temp;
            }
            else
            {
                throw new Exception("Wrong body");
            }

            functionsDictionary[name.ToString()] = ComputeDelayed;
            customFunctions[name.ToString()]     = new Tuple <StringSymbol, Expression>(variable, function);
            Console.WriteLine($"{name}({variable}) is defined as {function}");
            return(exp);
        }
        public void FlatWorks()
        {
            var flat = new StringSymbol("FlatTest", Attributes.Flat);

            evaluateAndAssert(
                flat[flat[1], flat[2, flat[3, 4, flat[5]], 6], 7, 8],
                flat[1, 2, 3, 4, 5, 6, 7, 8]
                );
        }
Ejemplo n.º 3
0
        public void InitializeData(string objXml) //задаём свойство Data
        {
            Data = null;
            if (objXml.IndexOf("Data=\"") != -1) // если задано свойство Data, задаём его в объекте
            {
                GetStringProperties getString = new GetStringProperties();
                getString.GetStringPropertiesData(objXml);

                StringSymbol sb = new StringSymbol();
                sb.Characters(getString.ObjectXml);

                Data = sb.Symbol;
            }
        }
Ejemplo n.º 4
0
        protected override bool ExecuteTerminal(LexerContext ctx)
        {
            string text;
            bool   match = ctx.QueryRegex(out text, expr, count);

            if (match)
            {
                Symbol sym = new StringSymbol(Name, text);

                ctx.AddSymbol(sym);
                ctx.Consume(text.Length);
            }

            return(match);
        }
Ejemplo n.º 5
0
        public static object ParseInput(string jsonInput, Scope context)
        {
            dynamic a = JsonConvert.DeserializeObject <dynamic>(jsonInput);

            // Check if it is an Expression
            try
            {
                string temp = a.Action.ToString();
            }
            catch (RuntimeBinderException e)
            {
                try
                {
                    return(new Constant(Convert.ToDecimal(a.Value.ToString())));
                }
                catch (RuntimeBinderException ex)
                {
                    context.IndeterminateList.Add(new StringSymbol(a.Name.ToString()));
                    return(new StringSymbol(a.Name.ToString()));
                }
            }

            //Console.WriteLine(a.Action.Name.ToString());
            Symbol        action       = new StringSymbol(a.Action.Name.ToString());
            List <Symbol> argumentList = new List <Symbol>();

            for (int i = 0; i < Int32.MaxValue; i++)
            {
                try
                {
                    argumentList.Add(ParseInput(a.Args[i].ToString(), context));
                }
                catch (ArgumentOutOfRangeException e)
                {
                    break;
                }
            }


            return(new Expression(action, argumentList.ToArray()));
        }
Ejemplo n.º 6
0
        public static Expression ReplaceVariable(Expression exp, StringSymbol localVariable, Symbol value)
        {
            Console.Write($"Replacing {localVariable} with {value}... ");
            Scope localScope = new Scope();

            localScope.SymbolRules.Add(localVariable.Name, value);
            List <Symbol> newArgs = new List <Symbol>();

            foreach (var arg in exp.Args)
            {
                if (arg is Expression argExp)
                {
                    //if (argExp.Action.Equals(PredefinedSymbols.Set))
                    //{
                    //	newArgs.Add(argExp);
                    //}
                    //else
                    //{
                    newArgs.Add(ReplaceVariable(argExp, localVariable, value));
                    //}
                }
                else if (arg is StringSymbol symbol)
                {
                    newArgs.Add(Substitute(symbol, localScope));
                }
                else
                {
                    newArgs.Add(arg);
                }
            }

            Expression newExp = new Expression(exp.Action, newArgs.ToArray());

            Console.WriteLine($"Result: {newExp}");
            return(newExp);
        }
 public Symbol VisitSymbol(StringSymbol symbol) => symbol;
Ejemplo n.º 8
0
 private static Symbol Substitute(StringSymbol symbol, Scope localContext)
 {
     return(localContext.SymbolRules.TryGetValue(symbol.Name, out var resultSymbol) ? resultSymbol : symbol);
 }
Ejemplo n.º 9
0
 public Expression VisitSymbol(StringSymbol symbol) => null;
Ejemplo n.º 10
0
        private static void Main(string[] args)
        {
            string       filepath  = "../../../TaskExamples/input1.json";
            Scope        context   = new Scope();
            StreamReader sr        = new StreamReader(filepath);
            string       inputJson = sr.ReadToEnd();
            Expression   inputExp  = (Expression)(Parser.ParseInput(inputJson, context));

            sr.Close();
            Console.WriteLine($"Got expression: {inputExp}");

            //If we got Plot() operator -----------------------------------------------------
            if (inputExp.Action.Equals(Plot))
            {
                Expression plotExpression = (Expression)inputExp.Args[0];
                Constant   width          = (Constant)inputExp.Args[1];
                Constant   height         = (Constant)inputExp.Args[2];
                var        window         = new MainWindow(plotExpression, inputJson, width.Value, height.Value);
                window.ShowDialog();
            }
            //If we got Gcd() operator ------------------------------------------------------
            else if (inputExp.Action.Equals(Gcd))
            {
                Expression exp1 = (Expression)inputExp.Args[0];

                Expression minFunc = List[
                    Set["lest", GetPolynomialCoefficients[exp1]],
                    Set["minEl", First["lest"]],
                    Set["tempLest", "lest"],
                    While[Not[Equal[First["tempLest"], "null"]],
                          List[
                              If[Less[First["tempLest"], "minEl"],
                                 List[
                                     Set["minEl", First["tempLest"]]
                                 ],
                                 List[
                                     False
                                 ]
                              ],
                              Set["tempLest", Rest["tempLest"]]
                          ]
                    ],
                    "minEl"
                                     ];

                StringSymbol f1  = new StringSymbol("f1");
                Expression   alg = List[
                    //This part finds common symbol divisor
                    Set["lest", GetIndeterminateList[exp1]],
                    Set["firstTerm", 1],
                    Set["ETMP", exp1],
                    While[Not[Equal[First["lest"], "null"]],
                          List[
                              Set["cur", First["lest"]],
                              Delayed[f1, "cur", "ETMP"],
                              Set["zero-checker", f1[0]],
                              Set["power", 0],
                              While[Equal["zero-checker", 0],
                                    List[
                                        Set["ETMP", Divide["ETMP", "cur"]],
                                        Delayed[f1, "cur", "ETMP"],
                                        Set["zero-checker", f1[0]],
                                        Set["power", Sum["power", 1]]
                                    ]
                              ],
                              If[Equal["power", 1],
                                 Set["firstTerm", Mul["firstTerm", "cur"]],
                                 List[False]
                              ],
                              If[Greater["power", 1],
                                 Set["firstTerm", Mul["firstTerm", Pow["cur", "power"]]],
                                 List[False]
                              ],
                              Set["lest", Rest["lest"]]
                          ]
                    ],
                    //This part computes common number divisor
                    Set["lest", GetPolynomialCoefficients[exp1]],
                    Set["divisor", minFunc],
                    Set["commonDivisor", 1],
                    Set["tempLest", "lest"],
                    While[Not[Equal["divisor", 1]],
                          List[
                              Set["reminder", 0],
                              While[Not[Equal[First["tempLest"], "null"]],
                                    List[
                                        Set["reminder", Sum[Rem[First["tempLest"], "divisor"], "reminder"]],
                                        Set["tempLest", Rest["tempLest"]]
                                    ]
                              ],
                              If[Equal["reminder", 0],
                                 List[
                                     Set["commonDivisor", Mul["commonDivisor", "divisor"]],
                                     Set["tempLest", Divide["lest", "divisor"]],
                                     Set["divisor", 1]
                                 ],
                                 List[
                                     Set["tempLest", "lest"],
                                     Set["divisor", Sub["divisor", 1]]
                                 ]
                              ]
                          ]
                    ],
                    "commonDivisor",
                    Mul[Mul["firstTerm", "commonDivisor"], Divide["ETMP", "commonDivisor"]]
                                   ];

                Expression resultExpression = (Expression)alg.Evaluate(context);
                Expression beautifiedExp    = (Expression)exp1.Evaluate(context);
                string     link             = "http://fred-wang.github.io/mathml.css/mspace.js";
                string     script           = $"<script src=\"{link}\"></script>\r\n";
                System.IO.File.WriteAllText(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\index.html", script + Parser.ToMathML(Equal[beautifiedExp, resultExpression]));
                Console.WriteLine($"\n{exp1} = {resultExpression}");
            }
            //Otherwise ------------------------------------------------------------------
            else
            {
                Console.WriteLine($"\nThe result of {inputExp}: {inputExp.Evaluate(context)}");
            }
        }
Ejemplo n.º 11
0
 public HasAttributeChecker(StringSymbol attribute) =>
 public Constant VisitSymbol(StringSymbol symbol) => null;
Ejemplo n.º 13
0
 protected AbstractListFunctionImplementation(StringSymbol names) : base(names)
 {
 }
 public Symbol VisitSymbol(StringSymbol symbol) => _default(symbol);
 public string VisitSymbol(StringSymbol symbol) =>
 symbol.Name;
Ejemplo n.º 16
0
 public Symbol VisitSymbol(StringSymbol symbol) => Equals(symbol, variable) ? funcArgument : symbol;