public void pushItemTest()
        {
            ProgramStack stack = new ProgramStack();

            stack.PushItem(new StackItem(10));
            Assert.AreEqual(stack.StackItems.Count, 1);
        }
示例#2
0
        private static List <ProgramStack> ParseInputs(string[] inputs)
        {
            List <ProgramStack> programStacks = new List <ProgramStack>();
            string pattern = @"(\w+) \((\d+)\)( -> )?(.*)?";
            Regex  regex   = new Regex(pattern);

            foreach (string input in inputs)
            {
                Match match = regex.Match(input);

                ProgramStack prog = new ProgramStack
                {
                    ProgramName = match.Groups[1].Value,
                    Weight      = int.Parse(match.Groups[2].Value)
                };

                if (!string.IsNullOrWhiteSpace(match.Groups[4].Value))
                {
                    string[] dependents = match.Groups[4].Value.Split(new string[] { ", " }, StringSplitOptions.RemoveEmptyEntries);
                    foreach (string dependent in dependents)
                    {
                        prog.DependentPrograms.Add(dependent);
                    }
                }

                programStacks.Add(prog);
            }

            return(programStacks);
        }
        public void popItemTest()
        {
            ProgramStack stack = new ProgramStack();

            stack.PushItem(new StackItem(10));
            Assert.AreEqual(stack.PopItem(), 10);
        }
示例#4
0
        private static void FindMisbalancedNode(List <ProgramStack> programStacks, ProgramStack baseProg)
        {
            var selectedProg = programStacks.First(x => x.ProgramName == baseProg.ProgramName);

            if (selectedProg.DependentWeights.Any() && selectedProg.DependentWeights.Distinct().Count() != 1)
            {
                Console.WriteLine($"dependent nodes of {selectedProg.ProgramName} don't balance");

                var differentProgValue = selectedProg.DependentWeights.GroupBy(x => x).First(x => x.Count() == 1).Key;
                var differentProg      = programStacks.First(x => x.TotalWeight == differentProgValue && selectedProg.DependentPrograms.Contains(x.ProgramName));

                Console.WriteLine($"{differentProg.ProgramName} is the node that doesn't balance");

                var distinctWeights    = selectedProg.DependentWeights.GroupBy(x => x);
                int differenceInWeight = distinctWeights.First().Key;
                foreach (var weight in distinctWeights.Skip(1))
                {
                    differenceInWeight -= weight.Key;
                }

                Console.WriteLine($"It's weight should be {differentProg.Weight - differenceInWeight}");

                FindMisbalancedNode(programStacks, differentProg);
            }
            else
            {
                Console.WriteLine($"Dependent programs of {selectedProg.ProgramName} are balanced.");
            }
        }
        private void Debugger(ProgramStack ProgramStack)
        {
            lvDebug_FS.Items.Clear();
            lvDebug_BS.Items.Clear();
            foreach (ListViewItem lvi in lvDebug_Runnables.Items)
            {
                if (lvi.Tag is IDE.Annotation)
                {
                    ((IDE.Annotation)lvi.Tag).FirstRunnable.Annotation.ListViewItem = null;
                }
            }
            if ((ProgramStack.FunctionStack == null) || (ProgramStack.FunctionStack.BlockStack == null) || (ProgramStack.FunctionStack.BlockStack.Block != lvDebug_Runnables.Tag))
            {
                lvDebug_Runnables.Items.Clear();
                lvDebug_Runnables.Tag = null;
            }
            dgvDebug_Block.Rows.Clear();
            tvDebug_Variables.Nodes.Clear();

            if (ProgramStack.FunctionStack == null)
            {
                tsDebug_Stop.Enabled    = false;
                tsDebug_StepOut.Enabled = false;
                if (ProgramStack.Debugger.DebuggerCallback != null)
                {
                    PopulateVariableRoot(ProgramStack.Tree.TreeRoot, "Tree", tvDebug_Variables.Nodes);
                    MessageBox.Show(ProgramStack.Success ? "Success!" : "Failure!");
                }

                _ps = null;

                return;
            }
            else
            {
                tsDebug_Stop.Enabled    = true;
                tsDebug_StepOut.Enabled = true;
            }

            //Show Tree
            PopulateVariableRoot(ProgramStack.Tree.TreeRoot, "Tree", tvDebug_Variables.Nodes);

            //Fill function stack
            FunctionStackFrame fs;

            for (fs = ProgramStack.FunctionStack; fs != null; fs = fs.Previous)
            {
                lvDebug_FS.Items.Add(fs.Function.Name).Tag = fs;
            }

            if (ProgramStack.FunctionStack == null)
            {
                return;
            }

            //Fill block stack
            lvDebug_FS_ItemActivate(null, null);
        }
示例#6
0
 /// <summary>
 /// Clears down the run environment.
 /// </summary>
 public void Clear()
 {
     ProgramStack.Clear();
     DefinedFunctions.Clear();
     ContinueLineNumber    = null;
     OnErrorGotoLineNumber = null;
     LastErrorNumber       = 0;
     LastErrorLine         = null;
 }
        public Interpreter()
        {
            Stack = new ProgramStack();
            alive = true;
            Strict = false;
            CurrentLine = 0;
            CurrentBlock = null;
            file = null;

            System.Console.WriteLine("Toast Interpreter prototype by Max Foster\n");
        }
        //Debugger views
        private void DebugStep(ProgramStack.DebuggerOptions.eBreakOn BreakOn)
        {
            if (_ps == null)
            {
                _ps = new ProgramStack();
                _ps.Debugger.BreakOn          = BreakOn;
                _ps.Debugger.DebuggerCallback = Debugger;

                _ps.Source = tbRun_Source.Text;
                _ps.RunFunction(_ide.ExecutionTree.Functions[tsDebug_Functions.Text]);
            }
            else
            {
                tsDebug_Stop.Enabled    = true;
                tsDebug_StepOut.Enabled = true;
                _ps.Debugger.BreakOn    = BreakOn;
                _ps.RunUntilBreakPoint();
            }
        }
        private void tsDebug_Run_Click(object sender, EventArgs e)
        {
            if (_ps == null)
            {
                _ps = new ProgramStack();
                _ps.Debugger.BreakOn          = ProgramStack.DebuggerOptions.eBreakOn.Breakpoints;
                _ps.Debugger.DebuggerCallback = Debugger;

                _ps.Source = _run_source;
                if (!_ps.RunFunction(_ide.ExecutionTree.Functions[tsDebug_Functions.Text]))
                {
                    return;
                }
            }

            tsDebug_Stop.Enabled    = false;
            tsDebug_StepOut.Enabled = false;
            _ps.Debugger.BreakOn    = ProgramStack.DebuggerOptions.eBreakOn.Breakpoints;
            _ps.RunUntilBreakPoint();
        }
示例#10
0
        public static void Execute()
        {
            string[] inputs = File.ReadAllLines(@"C:\Work\Misc Projects\AdventOfCode\AdventOfCode\AdventOfCode\2017\Data\day07_full.txt");

            List <ProgramStack> programStacks = ParseInputs(inputs);

            List <ProgramStack> progsWithDependencies = programStacks.Where(x => x.DependentPrograms.Any()).ToList();

            ProgramStack baseProg = new ProgramStack();

            foreach (var prog in progsWithDependencies)
            {
                if (!progsWithDependencies.Any(x => x.DependentPrograms.Contains(prog.ProgramName)))
                {
                    baseProg = prog;
                }
            }

            int baseProgWeight = CalculateWeights(baseProg, ref programStacks);

            FindMisbalancedNode(programStacks, baseProg);
        }
        //Run
        private void butRun_Run_Click(object sender, EventArgs e)
        {
            _run_ps = new ProgramStack();

            _run_ps.Source = _run_source;
            Buffers.RootBuffer.Clear();
            Buffers.NodeBuffer.Clear();
            Buffers.FunctionStackFrameBuffer.Clear();
            Buffers.BlockStackFrameBuffer.Clear();
            var rv = _run_ps.RunFunction(_ide.ExecutionTree.Functions[cbRun_Function.Text]);

            if (rv)
            {
                ssMain_RunStatus.Text = "Running...";
                int duration = Environment.TickCount;
                rv       = _run_ps.RunUntilBreakPoint();
                duration = Environment.TickCount - duration;
                ssMain_RunStatus.Text = string.Format("{0} ({1}ms/{2}ops={3}. Reused: roots {4}%, nodes {5}%, functions {6}%, blocks {7}%)", (rv ? "Success!" : "Failure!"), duration, _run_ps.ExecutionsCount, Math.Round(((double)_run_ps.ExecutionsCount / duration), 0), Math.Round(Buffers.RootBuffer.Reusability() * 100), Math.Round(Buffers.NodeBuffer.Reusability() * 100), Math.Round(Buffers.FunctionStackFrameBuffer.Reusability() * 100), Math.Round(Buffers.BlockStackFrameBuffer.Reusability() * 100));
            }

            cbRun_ShowTree_CheckedChanged(null, null);
        }
示例#12
0
        private static int CalculateWeights(ProgramStack prog, ref List <ProgramStack> progs)
        {
            var selectedProg = progs.First(x => x.ProgramName == prog.ProgramName);

            if (selectedProg.DependentPrograms.Any())
            {
                int weight = selectedProg.Weight;
                foreach (var dp in selectedProg.DependentPrograms)
                {
                    var p           = progs.First(x => x.ProgramName == dp);
                    int weightToAdd = CalculateWeights(p, ref progs);
                    selectedProg.DependentWeights.Add(weightToAdd);
                    weight += weightToAdd;
                }
                selectedProg.TotalWeight = weight;
            }
            else
            {
                return(selectedProg.Weight);
            }

            return(selectedProg.TotalWeight);
        }
        private void tsDebug_Run_Click(object sender, EventArgs e)
        {
            if (_ps == null)
            {
                _ps = new ProgramStack();
                _ps.Debugger.BreakOn = ProgramStack.DebuggerOptions.eBreakOn.Breakpoints;
                _ps.Debugger.DebuggerCallback = Debugger;

                _ps.Source = _run_source;
                if (!_ps.RunFunction(_ide.ExecutionTree.Functions[tsDebug_Functions.Text]))
                    return;
            }

            tsDebug_Stop.Enabled = false;
            tsDebug_StepOut.Enabled = false;
            _ps.Debugger.BreakOn = ProgramStack.DebuggerOptions.eBreakOn.Breakpoints;
            _ps.RunUntilBreakPoint();
        }
        private void Debugger(ProgramStack ProgramStack)
        {
            lvDebug_FS.Items.Clear();
            lvDebug_BS.Items.Clear();
            foreach (ListViewItem lvi in lvDebug_Runnables.Items)
                if (lvi.Tag is IDE.Annotation)
                    ((IDE.Annotation)lvi.Tag).FirstRunnable.Annotation.ListViewItem = null;
            if ((ProgramStack.FunctionStack == null) || (ProgramStack.FunctionStack.BlockStack == null) || (ProgramStack.FunctionStack.BlockStack.Block != lvDebug_Runnables.Tag))
            {
                lvDebug_Runnables.Items.Clear();
                lvDebug_Runnables.Tag = null;
            }
            dgvDebug_Block.Rows.Clear();
            tvDebug_Variables.Nodes.Clear();

            if (ProgramStack.FunctionStack == null)
            {
                tsDebug_Stop.Enabled = false;
                tsDebug_StepOut.Enabled = false;
                if (ProgramStack.Debugger.DebuggerCallback != null)
                {
                    PopulateVariableRoot(ProgramStack.Tree.TreeRoot, "Tree", tvDebug_Variables.Nodes);
                    MessageBox.Show(ProgramStack.Success ? "Success!" : "Failure!");
                }

                _ps = null;

                return;
            }
            else
            {
                tsDebug_Stop.Enabled = true;
                tsDebug_StepOut.Enabled = true;
            }

            //Show Tree
            PopulateVariableRoot(ProgramStack.Tree.TreeRoot, "Tree", tvDebug_Variables.Nodes);

            //Fill function stack
            FunctionStackFrame fs;
            for (fs = ProgramStack.FunctionStack ; fs != null ; fs = fs.Previous)
                lvDebug_FS.Items.Add(fs.Function.Name).Tag = fs;

            if (ProgramStack.FunctionStack == null)
                return;

            //Fill block stack
            lvDebug_FS_ItemActivate(null, null);
        }
        //Debugger views
        private void DebugStep(ProgramStack.DebuggerOptions.eBreakOn BreakOn)
        {
            if (_ps == null)
            {
                _ps = new ProgramStack();
                _ps.Debugger.BreakOn = BreakOn;
                _ps.Debugger.DebuggerCallback = Debugger;

                _ps.Source = tbRun_Source.Text;
                _ps.RunFunction(_ide.ExecutionTree.Functions[tsDebug_Functions.Text]);
            }
            else
            {
                tsDebug_Stop.Enabled = true;
                tsDebug_StepOut.Enabled = true;
                _ps.Debugger.BreakOn = BreakOn;
                _ps.RunUntilBreakPoint();
            }
        }
        public void ProgramStackTest()
        {
            ProgramStack stack = new ProgramStack();

            Assert.IsInstanceOfType(stack, typeof(ProgramStack));
        }
        //Run
        private void butRun_Run_Click(object sender, EventArgs e)
        {
            _run_ps = new ProgramStack();

            _run_ps.Source = _run_source;
            Buffers.RootBuffer.Clear();
            Buffers.NodeBuffer.Clear();
            Buffers.FunctionStackFrameBuffer.Clear();
            Buffers.BlockStackFrameBuffer.Clear();
            var rv = _run_ps.RunFunction(_ide.ExecutionTree.Functions[cbRun_Function.Text]);
            if (rv)
            {
                ssMain_RunStatus.Text = "Running...";
                int duration = Environment.TickCount;
                rv = _run_ps.RunUntilBreakPoint();
                duration = Environment.TickCount - duration;
                ssMain_RunStatus.Text = string.Format("{0} ({1}ms/{2}ops={3}. Reused: roots {4}%, nodes {5}%, functions {6}%, blocks {7}%)", (rv ? "Success!" : "Failure!"), duration, _run_ps.ExecutionsCount, Math.Round(((double)_run_ps.ExecutionsCount / duration), 0), Math.Round(Buffers.RootBuffer.Reusability() * 100), Math.Round(Buffers.NodeBuffer.Reusability() * 100), Math.Round(Buffers.FunctionStackFrameBuffer.Reusability() * 100), Math.Round(Buffers.BlockStackFrameBuffer.Reusability() * 100));
            }

            cbRun_ShowTree_CheckedChanged(null, null);
        }