public DictionaryHooker(OptimizingStackMachine machine, IDictionary<string, double> source = null)
 {
     Machine = machine;
     if(source != null)
         Source = source;
     else
         Source = new Dictionary<string, double>();
 }
Esempio n. 2
0
        private string TryCalculate(ITreeNode <object> assign_expr, Dictionary <string, double> varsValues)
        {
            List <string> commands = Parser.ExampleLang.Lang.Compile((from a in assign_expr where a.Current is Token t select(Token) a.Current).ToList(),
                                                                     new ReportParser(assign_expr));
            Dictionary <string, double> toSend = new Dictionary <string, double>(varsValues);

            foreach (string varName in from a in assign_expr where a.Current is Token tk && tk.Type == Lexer.ExampleLang.VAR && !toSend.ContainsKey(tk.Value) select((Token)a.Current).Value)
            {
                toSend[varName] = double.NaN;
            }
            OptimizingStackMachine localStackMachine = new OptimizingStackMachine(toSend);

            try
            {
                localStackMachine.Execute(commands);
            }
            catch { return(null); }
            return(localStackMachine.GetLastValueStack());
        }
        public ReportParser Optimize(ReportParser compiledCode)
        {
            if(!compiledCode.IsSuccess)
                throw new OptimizingException("Входное дерево компиляции построено не верно!");
            if(compiledCode.Compile == null)
                throw new OptimizingException("Вызовите compiledCode.Compile() перед началом.");
            ITreeNode<object> treeCompileForCheckVars = compiledCode.Compile.CloneCompileTree();

            var assignOpTokens = from a in treeCompileForCheckVars
            where a.Current is Token token && token.Type == Lexer.ExampleLang.ASSIGN_OP
            select (Token)a.Current;

            foreach(Token t in assignOpTokens)
                t.Value = t.Id + " =";

            IList<string> commands = Parser.ExampleLang.Lang.Compile((from a in treeCompileForCheckVars where a.Current is Token t select (Token)a.Current).ToList(), new ReportParser(treeCompileForCheckVars));
            
            foreach(Token t in assignOpTokens)
                t.Value = "=";

            
            HashSet<ulong> TokensToRemove = new OptimizingStackMachine().MyExecute(commands);

            ITreeNode<object> output = compiledCode.Compile.CloneCompileTree();

            HashSet<ITreeNode<object>> RPCToRemove = new HashSet<ITreeNode<object>>(
                from a in output
                where a.Current is ParserToken
                    && a.Count == 3
                    && a[1].Current is Token token
                    && TokensToRemove.Contains(token.Id)
                select a);
            Console.WriteLine($"Remove need:\n{string.Join("\n", RPCToRemove)}");
            ParserToken none = new ParserToken(new Nonterminal("none", (a, b, c) => {}, RuleOperator.NONE), RuleOperator.NONE);
            output.ReplaceWhere(
                (a) => RPCToRemove.Contains(a),
                () => new TreeNode<object>(none));

            return new ReportParser(output);
        }