Beispiel #1
0
        public void UpdateSubProgramTest()
        {
            bool   pass              = true;
            string ValidLOOPString   = @"LOOP 0,0,0 
END_LOOP";
            string InvalidLOOPString = @"IF 0,0,0 
ELSE
END_IF";

            try
            {
                HLProgram        hpValid   = new HLProgram(ValidLOOPString);
                HLProgram        hpInvalid = new HLProgram(InvalidLOOPString);
                ProgramViewModel pvm       = new ProgramViewModel(new HLProgram());

                pvm.InsertSubProgram(0, hpInvalid);
                pvm.UpdateSubProgram(0, hpValid);
                pass = true;
            }
            catch (InstructionException ex)
            {
                Console.WriteLine(ex + ": Invalid");
                pass = false;
            }
            Assert.IsTrue(pass);
        }
        public void FindEndIfTest()
        {
            string IfBlock = @"IF 0,0,0
ELSE
	IF 0,0,0
	ELSE
	END_IF
END_IF";

            int       ValidifIndex = 0, inValidifIndex = 0;
            HLProgram p = new HLProgram(IfBlock);

            try
            {
                int i = p.FindEndIf(ValidifIndex);
                Assert.AreNotEqual(-1, i);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Assert.Fail("Invalid if index :", inValidifIndex);
            }

            try
            {
                int i = p.FindEndIf(inValidifIndex);
                Assert.AreNotEqual(-1, i);
            }
            catch (IfUnmatchedException ex)
            {
                Console.WriteLine(ex.ToString());
                Assert.Fail("Invalid if index :", inValidifIndex);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Insert a new instruction at specified index and update the content.
        /// </summary>
        /// <param name="index">The zero-base index at which the new instruction should be inserted.</param>
        /// <param name="opCode">The opcode of the instruction.</param>
        public void InsertNewInstruction(int index, string opCode)
        {
            Instruction newIns = Instruction.CreatDefaultFromOpcode(opCode);

            if (newIns != null)
            {
                if (opCode == Instruction.IF || opCode == Instruction.LOOP)
                {
                    // Add HLProgram for IF and LOOP.
                    pvm.InsertSubProgram(index, HLProgram.GetDefaultIfLoopBlock(newIns));
                }
                else
                {
                    // Add single Instruction.
                    pvm.InsertInstruction(index, newIns);
                }
                UpdateContent();
                listViewProgram.SelectedIndex = index;

                // To ensure that the selected item is visible.
                listViewProgram.ScrollIntoView(listViewProgram.SelectedItem);

                if (Properties.Settings.Default.PopupWindowForNewIns)
                {
                    ShowParamWindow(index);
                }
            }
        }
        public void FindEndLoopTest()
        {
            string    IfBlock = @"LOOP 0,0,0
LOOP 0,0,0
END_LOOP
END_LOOP";
            int       ValidLoopIndex = 0, inValidLoopIndex = 1;
            HLProgram p = new HLProgram(IfBlock);

            try
            {
                int i = p.FindEndLoop(ValidLoopIndex);
                Assert.AreNotEqual(-1, i);
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
                Assert.Fail("Invalid LOOP index :", inValidLoopIndex);
            }

            try
            {
                int i = p.FindEndLoop(inValidLoopIndex);
                Assert.AreNotEqual(-1, i);
            }
            catch (LoopUnmatchedException ex)
            {
                Console.WriteLine(ex.ToString());
                Assert.Fail("Invalid LOOP index :", inValidLoopIndex);
            }
        }
Beispiel #5
0
        public void GetHLProgramTest()
        {
            HLProgram        hp  = new HLProgram();
            ProgramViewModel pvm = new ProgramViewModel(new HLProgram());

            hp = pvm.GetHLProgram();
            Assert.IsNotNull(hp);
        }
Beispiel #6
0
 /// <summary>
 /// Update the SubProgram starting from specified index in ViewModel.
 /// </summary>
 /// <param name="index">The zero-base index at which the sub-program should be updated. </param>
 /// <param name="newSubProgram">The new sub-program.</param>
 public void UpdateSubProgram(int index, HLProgram newSubProgram)
 {
     // Update the pointer in ViewModel to the new position and add the new newSubProgram to
     // the end of program. We don't need to remove the previous sub-program since they will
     // no longer be indexed.
     this[index] = program.Count;
     program.Add(newSubProgram);
 }
        public void GetIfLoopBlockTest(string Startifloop)
        {
            string    inputBlock  = Startifloop.Trim();
            HLProgram ActualBlock = HLProgram.GetDefaultIfLoopBlock(new Instruction(inputBlock));

            Console.WriteLine(ActualBlock);
            Assert.IsNotNull(ActualBlock);
        }
Beispiel #8
0
        public void GetSubProgramTest()
        {
            ProgramViewModel pvm = new ProgramViewModel(new HLProgram());

            pvm.InsertInstruction(0, Instruction.CreatDefaultFromOpcode(Instruction.DRIVE));
            HLProgram hp = pvm.GetSubProgram(0);

            Assert.IsNull(hp);
        }
Beispiel #9
0
        private void PasteExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            int newIndex = listViewProgram.SelectedIndex;

            if (newIndex < 0)
            {
                newIndex = pvm.Count;
            }

            string    subProgramString = Clipboard.GetText();
            HLProgram subProgram       = new HLProgram(subProgramString);

            pvm.InsertSubProgram(newIndex, subProgram);

            UpdateContent();
        }
Beispiel #10
0
 /// <summary>
 /// Show dialog for IF or LOOP to modify the sub-program.
 /// </summary>
 /// <param name="program">The program being modified, including IF ELSE END_IF and LOOP END_LOOP Instructions.</param>
 /// <param name="owner">The owner Window.</param>
 /// <returns>The modified HLProgram.</returns>
 public static HLProgram ShowDialog(HLProgram program, Window owner)
 {
     if (program[0].opcode == Instruction.IF)
     {
         IfWindow dlg = new IfWindow();
         dlg.Owner      = owner;
         dlg.SubProgram = program;
         dlg.ShowDialog();
         return(dlg.SubProgram);
     }
     else if (program[0].opcode == Instruction.LOOP)
     {
         LoopWindow dlg = new LoopWindow();
         dlg.Owner      = owner;
         dlg.SubProgram = program;
         dlg.ShowDialog();
         return(dlg.SubProgram);
     }
     return(null);
 }
Beispiel #11
0
        /// <summary>
        /// Get the HLProgram for furthur processing, like translating.
        /// </summary>
        /// <returns></returns>
        public HLProgram GetHLProgram()
        {
            HLProgram result = new HLProgram();

            for (int pvmIndex = 0; pvmIndex < this.Count(); pvmIndex++)
            {
                Instruction ins = program[this[pvmIndex]];

                if (ins.opcode == Instruction.IF || ins.opcode == Instruction.LOOP)
                {
                    result.Add(GetSubProgram(pvmIndex));
                }
                else
                {
                    // Single instruction.
                    result.Add(GetInstruction(pvmIndex));
                }
            }
            return(result);
        }
Beispiel #12
0
        /// <summary>
        /// Get the sub-program pointed by the pointer at specified index.
        /// </summary>
        /// <param name="index">The index of pointer.</param>
        /// <returns>The sub-program pointed by the pointer.</returns>
        public HLProgram GetSubProgram(int pvmIndex)
        {
            int         programIndex = this[pvmIndex];
            Instruction ins          = program[programIndex];

            if (ins.opcode == Instruction.IF)
            {
                return(program.SubProgram(programIndex, program.FindEndIf(programIndex)));
            }
            else if (ins.opcode == Instruction.LOOP)
            {
                return(program.SubProgram(programIndex, program.FindEndLoop(programIndex)));
            }
            else
            {
                // A HLProgram with only one instruction.
                HLProgram result = new HLProgram();
                result.Add(ins);
                return(result);
            }
        }
Beispiel #13
0
 public ProgramViewModel(HLProgram program)
 {
     this.program = program;
     for (int i = 0; i < program.Count; i++)
     {
         if (program[i].opcode == Instruction.IF)
         {
             base.Add(i);
             i = program.FindEndIf(i);
         }
         else if (program[i].opcode == Instruction.LOOP)
         {
             base.Add(i);
             i = program.FindEndLoop(i);
         }
         else
         {
             base.Add(i);
         }
     }
 }
Beispiel #14
0
        public void InsertSubProgramTest()
        {
            bool   pass          = true;
            string ValidifString = @"IF 0,0,0 
ELSE
END_IF";

            try
            {
                HLProgram        hp  = new HLProgram(ValidifString);
                ProgramViewModel pvm = new ProgramViewModel(new HLProgram());
                pvm.InsertSubProgram(0, hp);
                pass = true;
            }
            catch (InvalidProgramException ex)
            {
                Console.WriteLine(ex + ": Invalid");
                pass = false;
            }
            Assert.IsTrue(pass);
        }
Beispiel #15
0
        /// <summary>
        /// pop up the parameter window
        /// </summary>
        /// <param name="index">index in pvm</param>
        private void ShowParamWindow(int index)
        {
            // The Ins under modification
            Instruction selectedIns = pvm.GetInstruction(index);

            if (selectedIns.opcode == Instruction.IF || selectedIns.opcode == Instruction.LOOP)
            {
                HLProgram subProgram = pvm.GetSubProgram(index);

                // invoke the dialog
                HLProgram result = DialogInvoker.ShowDialog(subProgram, Window.GetWindow(this));

                pvm.UpdateSubProgram(index, result);
            }
            else
            {
                Instruction result = DialogInvoker.ShowDialog(selectedIns, Window.GetWindow(this));
                pvm.UpdateInstruction(index, result);
            }

            UpdateContent();
        }
Beispiel #16
0
        public static void TranslateToC(HLProgram program)
        {
            string cCode = Translator.Translate(program);

            CodeGenerator.GenerateCSource(MicrocontrollerTemplate, MicrocontrollerOutputFile, cCode);
        }
Beispiel #17
0
 /// <summary>
 /// Add new sub-program at specified index.
 /// </summary>
 /// <param name="index">The zero-base index at which the new sub-program should be inserted. </param>
 /// <param name="newIns">The new sub-program.</param>
 public void InsertSubProgram(int index, HLProgram newSubProgram)
 {
     this.Insert(index, program.Count);
     program.Add(newSubProgram);
 }