Example #1
0
 public override void WriteLine(string value)
 {
     base.WriteLine(value + Environment.NewLine);
     control.SelectionStart  = control.Text.Length;
     control.SelectionLength = 0;
     control.ScrollToCaret();
 }
Example #2
0
        protected override string Write(string value, bool newLine)
        {
            if (_textBox.InvokeRequired)
            {
                _textBox.BeginInvoke(new Func <string, bool, string>(Write), value, newLine);
                return(null);
            }
            else
            {
                if (newLine)
                {
                    value = value.Trim('.') + ".";
                }

                value = base.Write(value, newLine);

                lock (_textBox)
                {
                    _textBox.AppendText(value);
                    if (_scrollTextBox)
                    {
                        _textBox.ScrollToCaret();
                    }
                }

                return(value);
            }
        }
Example #3
0
 private static void TextBoxOutput(TextBoxBase textbox, string output)
 {
     textbox.AppendText(output);
     textbox.SelectionStart  = textbox.Text.Length;
     textbox.SelectionLength = 0;
     textbox.ScrollToCaret();
 }
Example #4
0
 private static void SetCaretAt(TextBoxBase textBox, int position)
 {
     textBox.Focus();
     textBox.SelectionStart  = position;
     textBox.SelectionLength = 0;
     textBox.ScrollToCaret();
 }
Example #5
0
 private void appendToLogControl(object txt, TextBoxBase box)
 {
     BeginInvoke(new MethodInvoker(delegate {
         box.Text           = box.Text + txt + "\r\n";
         box.SelectionStart = box.TextLength;
         box.ScrollToCaret();
     }));
 }
Example #6
0
 private void ScrollTextBox(TextBoxBase box)
 {
     Invoke(new MethodInvoker(delegate
     {
         box.Select(box.Text.Length, 1);
         box.ScrollToCaret();
     }));
 }
Example #7
0
 public override void Write(string message)
 {
     message = message.Replace("\r\n", "\n");
     message = message.Replace("\n", "\r\n");
     _textBox.BeginInvoke((Action) delegate {
         _textBox.AppendText(message);
         _textBox.ScrollToCaret();
     });
 }
Example #8
0
 public void WriteLog(string msg)
 {
     Builder.AppendLine(msg);
     textBox.Invoke((Action)(() => {
         textBox.Text = Builder.ToString();
         textBox.SelectionStart = textBox.Text.Length;
         textBox.ScrollToCaret();
     }));
 }
Example #9
0
        public override void Write(String text)
        {
            MethodInvoker action = delegate
            {
                _textBoxBase.AppendText(text);
                _textBoxBase.ScrollToCaret();
            };

            _form.BeginInvoke(action);
        }
        private void hbGet_Click(object sender, EventArgs e)
        {
            String url = hURL.Text.StartsWith("http://") ? hURL.Text : "http://" + hURL.Text;

            BindingOutputControl.Text += GetHTML(url, true);
            TextBoxBase tbb = BindingOutputControl as TextBoxBase;

            if (tbb != null)
            {
                tbb.Select(tbb.TextLength, 0);
                tbb.ScrollToCaret();
            }
        }
Example #11
0
        protected override void Append(LoggingEvent loggingEvent)
        {
            var logInfo = string.Empty;

            if (_textBox == null)
            {
                if (string.IsNullOrEmpty(FormName) || string.IsNullOrEmpty(TextBoxName))
                {
                    return;
                }
                Form form = Application.OpenForms[FormName];
                if (form == null)
                {
                    return;
                }
                TableLayoutPanel layoutPanel = form.Controls[TableLayoutPanelName] as TableLayoutPanel;
                if (layoutPanel == null)
                {
                    return;
                }
                Panel panel = layoutPanel.Controls[PanelName] as Panel;
                if (panel == null)
                {
                    return;
                }
                TabControl tabControl = panel.Controls[TabControlName] as TabControl;
                if (tabControl == null)
                {
                    return;
                }
                TabPage tabPage = tabControl.Controls[TabPageName] as TabPage;
                if (tabPage == null)
                {
                    return;
                }
                _textBox = tabPage.Controls[TextBoxName] as TextBox;
                if (_textBox == null)
                {
                    return;
                }
                form.FormClosed += (s, e) => _textBox = null;
            }

            _textBox.BeginInvoke((MethodInvoker) delegate
            {
                _textBox.AppendText(RenderLoggingEvent(loggingEvent));
                _textBox.Focus();
                _textBox.Select(_textBox.TextLength, 0);
                _textBox.ScrollToCaret();
            });
        }
Example #12
0
        /// <summary>
        /// </summary>
        /// <param name="textOutputControl"></param>
        /// <param name="someString"></param>
        private static void SetControlText
        (
            [CanBeNull] TextBoxBase textOutputControl,
            string someString)
        {
            if (textOutputControl != null)
            {
                textOutputControl.Text           = someString;
                textOutputControl.SelectionStart = textOutputControl.TextLength;
                textOutputControl.ScrollToCaret();

                textOutputControl.Refresh( );
            }
        }
 public void ScrollToCaret()
 {
     if (control.IsDisposed)
     {
         return;
     }
     if (control.InvokeRequired)
     {
         ScrollToCaretDelegate del = new ScrollToCaretDelegate(ScrollToCaret);
         control.Invoke(del);
     }
     else
     {
         control.ScrollToCaret();
     }
 }
Example #14
0
 /// <summary>
 /// Go to the begining of text box and scroll to.
 /// </summary>
 /// <param name="control"></param>
 public static void TextBoxGoUp(TextBoxBase control)
 {
     if (control.InvokeRequired)
     {
         control.Invoke(new MethodInvoker(() => TextBoxGoUp(control)));
         return;
     }
     try
     {
         control.SelectionLength = 0;
         control.SelectionStart  = 0;
         control.ScrollToCaret();
     }
     catch (ObjectDisposedException)
     {
     }
 }
Example #15
0
        /// <summary>
        /// Moves the caret to a specific line of a textbox
        /// </summary>
        /// <param name="t"></param>
        /// <param name="lineNumber"></param>
        public static void GoToLine(this TextBoxBase t, int lineNumber)
        {
            int i        = 1;
            int intStart = 0;
            int intEnd   = 0;

            foreach (Match m in Regex.Matches(t.Text, "^.*?$", RegexOptions.Multiline))
            {
                if (i == lineNumber)
                {
                    intStart = m.Index;
                    intEnd   = intStart + m.Length;
                    break;
                }
                i++;
            }

            t.Select(intStart, intEnd - intStart);
            t.ScrollToCaret();
            t.Focus();
        }
Example #16
0
        }//method

        private bool DoSearch(TextBoxBase textBox, string fragment, int start)
        {
            textBox.SelectionLength = 0;
            // Compile the regular expression.
            Regex r = new Regex(fragment, RegexOptions.IgnoreCase);
            // Match the regular expression pattern against a text string.
            Match m = r.Match(textBox.Text.Substring(start));

            if (m.Success)
            {
                int               i  = 0;
                Group             g  = m.Groups[i];
                CaptureCollection cc = g.Captures;
                Capture           c  = cc[0];
                textBox.SelectionStart  = c.Index + start;
                textBox.SelectionLength = c.Length;
                textBox.Focus();
                textBox.ScrollToCaret();
                return(true);
            }
            return(false);
        }//method
Example #17
0
 /// <summary>
 /// Scrolls the contents of the text box to the given position.
 /// The value is clamped to the length of the text.
 /// </summary>
 /// <param name="textBox"></param>
 /// <param name="position"></param>
 public static void ScrollTo(this TextBoxBase textBox, int position)
 {
     textBox.SelectionStart = MathHelpers.Clamp(position, 0, textBox.TextLength);
     textBox.ScrollToCaret();
 }
Example #18
0
 public void Select(int start, int length)
 {
     _tb.Select(start, length);
     _tb.ScrollToCaret();
 }
Example #19
0
 private void AppendText(string text)
 {
     _textBox.AppendText(text);
     _textBox.ScrollToCaret();
 }
Example #20
0
        public static void FastScrollToCaret(this TextBoxBase control)
        {
            if (control.IsHandleCreated)
            {
                var textLength = control.TextLength;

                if (textLength == 0)
                {
                    return;
                }
                bool   flag       = false;
                object iunknown   = null;
                var    iunkHandle = IntPtr.Zero;
                try
                {
                    if (SendMessage(new HandleRef(control, control.Handle), 1084, 0, out iunknown) != 0)
                    {
                        iunkHandle = Marshal.GetIUnknownForObject(iunknown);
                        if (iunkHandle != IntPtr.Zero)
                        {
                            var itextDocumentHandle = IntPtr.Zero;
                            var gUID = typeof(ITextDocument).GUID;
                            try
                            {
                                Marshal.QueryInterface(iunkHandle, ref gUID, out itextDocumentHandle);
                                var textDocument = Marshal.GetObjectForIUnknown(itextDocumentHandle) as ITextDocument;
                                if (textDocument != null)
                                {
                                    int start             = control.SelectionStart;
                                    int lineFromCharIndex = control.GetLineFromCharIndex(start);
                                    var textRange         = textDocument.Range(start, start + control.SelectionLength);
                                    textRange.ScrollIntoView(0);                                     // scroll to start of selection
                                    int num3 = (int)Win32.SendMessage(control.Handle, (Win32.WM) 206, IntPtr.Zero, IntPtr.Zero);
                                    if (num3 > lineFromCharIndex)
                                    {
                                        textRange.ScrollIntoView(32);
                                    }
                                    flag = true;
                                }
                            }
                            finally
                            {
                                if (itextDocumentHandle != IntPtr.Zero)
                                {
                                    Marshal.Release(itextDocumentHandle);
                                }
                            }
                        }
                    }
                }
                finally
                {
                    if (iunkHandle != IntPtr.Zero)
                    {
                        Marshal.Release(iunkHandle);
                    }
                }
                if (!flag)
                {
                    Win32.SendMessage(control.Handle, (Win32.WM) 183, IntPtr.Zero, IntPtr.Zero);
                    return;
                }
            }
            else
            {
                control.ScrollToCaret();
            }
        }
Example #21
0
 /// <summary>
 /// TextBoxの最終行に追加、スクロール、CRLFを付加しない
 /// </summary>
 /// <param name="ed"></param>
 /// <param name="msg"></param>
 public static void edAppendText(TextBoxBase ed, string msg)
 {
     ed.AppendText(msg);
     ed.ScrollToCaret();
     edBottomScroll(ed);
 }
 public static void ScrollToTop(TextBoxBase tb)
 {
     tb.SelectionStart = 0;
     tb.ScrollToCaret();
 }
Example #23
0
 /// <summary>
 /// Assembles the given code.
 /// </summary>
 /// <param name="outputPath">
 /// Path to output file.
 /// </param>
 /// <returns>
 /// Binary code of assembled program.
 /// </returns>
 public byte[] Assemble(string outputPath = "out.o")
 {
     try
     {
         if (!code.EndsWith("\n"))
         {
             code += "\r\n";
         }
         var provider = CSharpCodeProvider.CreateProvider("c#");
         var options  = new CompilerParameters();
         var assemblyContainingNotDynamicClass = Path.GetFileName(Assembly.GetExecutingAssembly().Location);
         options.ReferencedAssemblies.Add(assemblyContainingNotDynamicClass);
         string parserCode    = File.ReadAllText("Grammar/cs/" + ((SystemComponent)cpu).Name + "Parser.cs");
         string constantsCode = File.ReadAllText("Grammar/cs/" + ((SystemComponent)cpu).Name + "Constants.cs");
         string tokenizerCode = File.ReadAllText("Grammar/cs/" + ((SystemComponent)cpu).Name + "Tokenizer.cs");
         string analyzerCode  = File.ReadAllText("Grammar/cs/" + ((SystemComponent)cpu).Name + "Analyzer.cs");
         var    results       = provider.CompileAssemblyFromSource(options, new[] { parserCode, constantsCode, tokenizerCode, analyzerCode });
         if (results.Errors.Count > 0)
         {
             foreach (var error in results.Errors)
             {
                 File.AppendAllText("error.txt", error + "\n");
             }
             return(null);
         }
         else
         {
             Parser parser   = null;
             var    t        = results.CompiledAssembly.GetType("MultiArc_Compiler." + cpu.Name + "Parser", true);
             object instance = Activator.CreateInstance(t, new[] { new StringReader(code) });
             parser = (Parser)instance;
             try
             {
                 Node n = parser.Parse();
                 instructions.Clear();
                 labels.Clear();
                 symbolTable.Clear();
                 getInstructionsFromTree(n);
                 getOriginFromTree(n);
                 binaryCode = new byte[instructions.Count * cpu.Constants.MAX_BYTES]; // THIS MUST BE TESTED.
                 count      = 0;
                 separators.Clear();
                 separators = new LinkedList <int>();
                 // First passing:
                 for (int i = 0; i < instructions.Count; i++)
                 {
                     Instruction inst = cpu.Constants.GetInstruction(instructions.ElementAt(i).GetName());
                     if (labels.ContainsKey(instructions.ElementAt(i)))
                     {
                         Symbol symbol = new Symbol(labels[instructions.ElementAt(i)], 0, count, true);
                         if (symbolTable.Contains(symbol))
                         {
                             throw new Exception("Label already defined");
                         }
                         else
                         {
                             symbolTable.AddLast(symbol);
                         }
                     }
                     for (int j = inst.Mask.Length - 1; j >= 0; j--)
                     {
                         binaryCode[count + j] = inst.Mask[j];
                     }
                     LinkedList <AddressingMode> addrModes        = new LinkedList <AddressingMode>();
                     LinkedList <int>            argumentsIndexes = new LinkedList <int>();
                     for (int j = 0; j < instructions.ElementAt(i).GetChildCount(); j++)
                     {
                         AddressingMode am = null;
                         am = cpu.Constants.GetAddressingMode(instructions.ElementAt(i).GetChildAt(j).Name);
                         if (!object.ReferenceEquals(am, null))
                         {
                             addrModes.AddLast(am);
                             argumentsIndexes.AddLast(j);
                         }
                     }
                     for (int j = 0; j < inst.Arguments.Count; j++)
                     {
                         int argcodeValue = inst.Arguments.ElementAt(j).CodeValues[addrModes.ElementAt(j).Name];
                         int argcodeStart = inst.Arguments.ElementAt(j).CodeStarts[addrModes.ElementAt(j).Name];
                         int argcodeEnd   = inst.Arguments.ElementAt(j).CodeEnds[addrModes.ElementAt(j).Name];
                         int argcodeSize  = 1;
                         for (int k = argcodeStart; k >= argcodeEnd; k--)
                         {
                             if (k % 8 == 0 && k != argcodeEnd)
                             {
                                 argcodeSize++;
                             }
                         }
                         int argcodeCount = argcodeStart - argcodeEnd;
                         int byteCount    = argcodeSize - 1;
                         for (int k = argcodeStart; k >= argcodeEnd; k--)
                         {
                             int semiValue = (argcodeValue & (1 << argcodeCount)) << argcodeEnd % 8;
                             binaryCode[count + inst.Size - 1 - argcodeEnd / 8 - byteCount] |= (byte)((semiValue & (1 << (argcodeEnd % 8 + argcodeCount))) >> byteCount * 8); // This might be a problem.
                             if ((argcodeEnd + argcodeCount) % 8 == 0)
                             {
                                 byteCount--;
                             }
                             argcodeCount--;
                         }
                         AddressingMode am              = addrModes.ElementAt(j);
                         int            argumentIndex   = argumentsIndexes.ElementAt(j);
                         int            operandValue    = 0;
                         bool           shouldBeWritten = false;
                         if (am.OperandInValues)
                         {
                             shouldBeWritten = true;
                             string expr = "";
                             if (instructions.ElementAt(i).GetChildAt(argumentIndex).GetChildAt(0) is Production)
                             {
                                 expr = ((Production)instructions.ElementAt(i).GetChildAt(argumentIndex).GetChildAt(0)).Name;
                             }
                             else
                             {
                                 for (int l = 0; l < instructions.ElementAt(i).GetChildAt(argumentIndex).GetChildCount(); l++)
                                 {
                                     expr += ((Token)instructions.ElementAt(i).GetChildAt(argumentIndex).GetChildAt(l)).Image;
                                 }
                             }
                             if (am.Values.ContainsKey(expr.ToLower()))
                             {
                                 operandValue = am.Values[expr.ToLower()];
                             }
                         }
                         else if (am.OperandReadFromExpression)
                         {
                             Node node       = (instructions.ElementAt(i).GetChildAt(argumentIndex));
                             int  childCount = node.GetChildCount();
                             Node child      = node;
                             int  sign       = 1;
                             while (!(child.GetChildAt(0) is Token))
                             {
                                 child = child.GetChildAt(0);
                             }
                             for (int l = 0; l < node.GetChildCount() && !child.Name.Equals("DEC_NUMBER") && !child.Name.Equals("HEX_NUMBER") && !child.Name.Equals("OCT_NUMBER") && !child.Name.Equals("BIN_NUMBER") && !child.Name.Equals("IDENTIFIER"); l++)
                             {
                                 child = node.GetChildAt(l);
                                 if (l != 0 && node.GetChildAt(l - 1).Name.Equals("SIGN"))
                                 {
                                     if (((Token)node.GetChildAt(l - 1)).Image.Equals("+"))
                                     {
                                         sign = 1;
                                     }
                                     else
                                     {
                                         sign = -1;
                                     }
                                 }
                             }
                             if (child.Name.Equals("DEC_NUMBER"))
                             {
                                 operandValue    = Convert.ToInt32(((Token)child).Image.ToLower()) * sign;
                                 shouldBeWritten = true;
                             }
                             else if (child.Name.Equals("HEX_NUMBER"))
                             {
                                 operandValue    = Convert.ToInt32(((Token)child).Image.ToLower().Substring(0, ((Token)child).Image.Length - 1), 16) * sign;
                                 shouldBeWritten = true;
                             }
                             else if (child.Name.Equals("OCT_NUMBER"))
                             {
                                 operandValue    = Convert.ToInt32(((Token)child).Image.ToLower().Substring(0, ((Token)child).Image.Length - 1), 8) * sign;
                                 shouldBeWritten = true;
                             }
                             else if (child.Name.Equals("BIN_NUMBER"))
                             {
                                 operandValue    = Convert.ToInt32(((Token)child).Image.ToLower().Substring(0, ((Token)child).Image.Length - 1), 2) * sign;
                                 shouldBeWritten = true;
                             }
                             else if (child.Name.Equals("IDENTIFIER"))
                             {
                                 string label = ((Token)child).Image;
                                 operandValue = 0;
                                 foreach (Symbol s in symbolTable)
                                 {
                                     if (s.Label.ToLower().Equals(label))
                                     {
                                         operandValue = s.Offset;
                                         break;
                                     }
                                 }
                             }
                             if (am.OperandType.ToLower().Equals("relative"))
                             {
                                 operandValue = operandValue - inst.Size - count;
                             }
                         }
                         else if (am.OperandValueDefinedByUser)
                         {
                             Node node        = (instructions.ElementAt(i).GetChildAt(argumentIndex));
                             int  childCount  = node.GetChildCount();
                             Node child       = node;
                             bool labelNotYet = false;
                             int  sign        = 1;
                             while (!(child.GetChildAt(0) is Token))
                             {
                                 child = child.GetChildAt(0);
                             }
                             for (int l = 0; l < node.GetChildCount() && !child.Name.Equals("DEC_NUMBER") && !child.Name.Equals("HEX_NUMBER") && !child.Name.Equals("OCT_NUMBER") && !child.Name.Equals("BIN_NUMBER") && !child.Name.Equals("IDENTIFIER"); l++)
                             {
                                 child = node.GetChildAt(l);
                                 if (l != 0 && node.GetChildAt(l - 1).Name.Equals("SIGN"))
                                 {
                                     if (((Token)node.GetChildAt(l - 1)).Image.Equals("+"))
                                     {
                                         sign = 1;
                                     }
                                     else
                                     {
                                         sign = -1;
                                     }
                                 }
                             }
                             if (child.Name.Equals("DEC_NUMBER"))
                             {
                                 operandValue = Convert.ToInt32(((Token)child).Image.ToLower()) * sign;
                             }
                             else if (child.Name.Equals("HEX_NUMBER"))
                             {
                                 operandValue = Convert.ToInt32(((Token)child).Image.ToLower().Substring(0, ((Token)child).Image.Length - 1), 16) * sign;
                             }
                             else if (child.Name.Equals("OCT_NUMBER"))
                             {
                                 operandValue = Convert.ToInt32(((Token)child).Image.ToLower().Substring(0, ((Token)child).Image.Length - 1), 8) * sign;
                             }
                             else if (child.Name.Equals("BIN_NUMBER"))
                             {
                                 operandValue = Convert.ToInt32(((Token)child).Image.ToLower().Substring(0, ((Token)child).Image.Length - 1), 2) * sign;
                             }
                             else if (child.Name.Equals("IDENTIFIER"))
                             {
                                 string label = ((Token)child).Image;
                                 operandValue = 0;
                                 bool found = false;
                                 foreach (Symbol s in symbolTable)
                                 {
                                     if (s.Label.ToLower().Equals(label))
                                     {
                                         operandValue = s.Offset;
                                         found        = true;
                                         break;
                                     }
                                 }
                                 if (found == false)
                                 {
                                     labelNotYet = true;
                                 }
                             }
                             int    relativeOperandValue = operandValue - inst.Size - count;
                             string image = getExpression(instructions.ElementAt(i), "");
                             if (labelNotYet == false)
                             {
                                 operandValue    = am.GetOperandValue(image, count + inst.Size, relativeOperandValue, operandValue);
                                 shouldBeWritten = true;
                             }
                         }
                         if (shouldBeWritten == true)
                         {
                             int operandStart = inst.Arguments.ElementAt(j).OperandStarts[am.Name];
                             int operandEnd   = inst.Arguments.ElementAt(j).OperandEnds[am.Name];
                             int operandSize  = 1;
                             for (int k = operandStart; k >= operandEnd; k--)
                             {
                                 if (k % 8 == 0 && k != operandEnd)
                                 {
                                     operandSize++;
                                 }
                             }
                             int operandCount = operandStart - operandEnd;
                             byteCount = operandSize - 1;
                             for (int k = operandStart; k >= operandEnd; k--)
                             {
                                 int semiValue = (operandValue & (1 << operandCount)) << operandEnd % 8;
                                 binaryCode[count + inst.Size - 1 - operandEnd / 8 - byteCount] |= (byte)((semiValue & (1 << (operandEnd % 8 + operandCount))) >> byteCount * 8); // This might be a problem.
                                 if ((operandEnd + operandCount) % 8 == 0)
                                 {
                                     byteCount--;
                                 }
                                 operandCount--;
                             }
                         }
                     }
                     separators.AddLast(count);
                     count += inst.Size;
                 }
                 separators.AddLast(count);
                 count = 0;
                 // Second passing:
                 for (int i = 0; i < instructions.Count; i++)
                 {
                     Instruction inst = cpu.Constants.GetInstruction(instructions.ElementAt(i).GetName());
                     LinkedList <AddressingMode> addrModes        = new LinkedList <AddressingMode>();
                     LinkedList <int>            argumentsIndexes = new LinkedList <int>();
                     for (int j = 0; j < instructions.ElementAt(i).GetChildCount(); j++)
                     {
                         AddressingMode am = null;
                         am = cpu.Constants.GetAddressingMode(instructions.ElementAt(i).GetChildAt(j).Name);
                         if (!object.ReferenceEquals(am, null))
                         {
                             addrModes.AddLast(am);
                             argumentsIndexes.AddLast(j);
                         }
                     }
                     for (int j = 0; j < inst.Arguments.Count; j++)
                     {
                         AddressingMode am                 = addrModes.ElementAt(j);
                         int            argumentIndex      = argumentsIndexes.ElementAt(j);
                         int            operandValue       = 0;
                         bool           shouldBeOverwriten = false;
                         if (am.OperandReadFromExpression)
                         {
                             Node node       = (instructions.ElementAt(i).GetChildAt(argumentIndex));
                             int  childCount = node.GetChildCount();
                             Node child      = node;
                             while (!(child.GetChildAt(0) is Token))
                             {
                                 child = child.GetChildAt(0);
                             }
                             for (int l = 0; l < node.GetChildCount() && !child.Name.Equals("DEC_NUMBER") && !child.Name.Equals("HEX_NUMBER") && !child.Name.Equals("OCT_NUMBER") && !child.Name.Equals("BIN_NUMBER") && !child.Name.Equals("IDENTIFIER"); l++)
                             {
                                 child = node.GetChildAt(l);
                             }
                             if (child.Name.Equals("IDENTIFIER"))
                             {
                                 string label = ((Token)child).Image;
                                 operandValue       = 0;
                                 shouldBeOverwriten = true;
                                 bool found = false;
                                 foreach (Symbol s in symbolTable)
                                 {
                                     if (s.Label.ToLower().Equals(label))
                                     {
                                         operandValue = s.Offset;
                                         found        = true;
                                         break;
                                     }
                                 }
                                 if (found == false)
                                 {
                                     operandValue = Program.Mem.FirstFreeInRAM(am.Result.Size / 8);
                                     if (operandValue == -1)
                                     {
                                         throw new Exception("Memory full");
                                     }
                                     Symbol newSymbol = new Symbol(label, 0, operandValue, true);
                                     symbolTable.AddLast(newSymbol);
                                     Program.Mem.Allocate(operandValue, am.Result.Size / 8);
                                 }
                             }
                             if (am.OperandType.ToLower().Equals("relative"))
                             {
                                 operandValue = operandValue - inst.Size - count;
                             }
                         }
                         else if (am.OperandValueDefinedByUser)
                         {
                             Node node       = (instructions.ElementAt(i).GetChildAt(argumentIndex));
                             int  childCount = node.GetChildCount();
                             Node child      = node;
                             while (!(child.GetChildAt(0) is Token))
                             {
                                 child = child.GetChildAt(0);
                             }
                             for (int l = 0; l < node.GetChildCount() && !child.Name.Equals("DEC_NUMBER") && !child.Name.Equals("HEX_NUMBER") && !child.Name.Equals("OCT_NUMBER") && !child.Name.Equals("BIN_NUMBER") && !child.Name.Equals("IDENTIFIER"); l++)
                             {
                                 child = node.GetChildAt(l);
                             }
                             if (child.Name.Equals("IDENTIFIER"))
                             {
                                 string label = ((Token)child).Image;
                                 bool   found = false;
                                 shouldBeOverwriten = true;
                                 foreach (Symbol s in symbolTable)
                                 {
                                     if (s.Label.ToLower().Equals(label))
                                     {
                                         operandValue = s.Offset;
                                         found        = true;
                                         break;
                                     }
                                 }
                                 if (found == false)
                                 {
                                     operandValue = Program.Mem.FirstFree(Program.Mem.AuSize);
                                     Program.Mem.Allocate(operandValue, Program.Mem.AuSize); // This must be improved.
                                 }
                             }
                             int    relativeOperandValue = operandValue - inst.Size - count;
                             string image = getExpression(instructions.ElementAt(i), "");
                             operandValue = am.GetOperandValue(image, count + inst.Size, relativeOperandValue, operandValue);
                         }
                         if (shouldBeOverwriten == true)
                         {
                             int operandStart = inst.Arguments.ElementAt(j).OperandStarts[am.Name];
                             int operandEnd   = inst.Arguments.ElementAt(j).OperandEnds[am.Name];
                             int operandSize  = 1;
                             for (int k = operandStart; k >= operandEnd; k--)
                             {
                                 if (k % 8 == 0 && k != operandEnd)
                                 {
                                     operandSize++;
                                 }
                             }
                             int operandCount = operandStart - operandEnd;
                             int byteCount    = operandSize - 1;
                             for (int k = operandStart; k >= operandEnd; k--)
                             {
                                 int semiValue = (operandValue & (1 << operandCount)) << operandEnd % 8;
                                 binaryCode[count + inst.Size - 1 - operandEnd / 8 - byteCount] |= (byte)((semiValue & (1 << (operandEnd % 8 + operandCount))) >> byteCount * 8); // This might be a problem.
                                 if ((operandEnd + operandCount) % 8 == 0)
                                 {
                                     byteCount--;
                                 }
                                 operandCount--;
                             }
                         }
                     }
                     count += inst.Size;
                 }
                 output.Text += DateTime.Now.ToString() + " Compile successfull.\n";
                 output.ScrollToCaret();
                 byte[] ret = new byte[count];
                 for (int i = 0; i < count; i++)
                 {
                     ret[i] = binaryCode[i];
                 }
                 for (int i = 0; i < count; i += Program.Mem.AuSize)
                 {
                     byte[] toWrite = new byte[Program.Mem.AuSize];
                     for (int j = 0; j < Program.Mem.AuSize && i + j < count; j++)
                     {
                         toWrite[j] = ret[j + i];
                     }
                     Program.Mem[(uint)(i + origin)] = toWrite;
                 }
                 return(ret);
             }
             catch (ParserLogException ex)
             {
                 string o = "Error(s) in code existed. Compile unsuccessfull.\r\nList of errors:\r\n";
                 for (int j = 0; j < ex.Count; j++)
                 {
                     ParseException pe = ex[j];
                     o += (j + 1) + ": Syntax error " + '\'' + pe.ErrorMessage + '\'' + " in line " + pe.Line + " and column " + pe.Column + "\n";
                 }
                 output.Text += DateTime.Now.ToString() + " " + o;
                 output.ScrollToCaret();
                 return(null);
             }
         }
     }
     catch (Exception ex)
     {
         File.AppendAllText("error.txt", ex.ToString());
         return(null);
     }
 }
Example #24
0
 /// <summary>
 /// 设置光标到文本编辑框指定位置并选中指定长度
 /// </summary>
 /// <param name="tBox">文本框</param>
 /// <param name="index">光标其实位置</param>
 /// <param name="lenth">选中长度</param>
 public static void SetCursorToTextBoxBase(TextBoxBase tBox, int index, int lenth)
 {
     tBox.Focus();              //让文本框获取焦点
     tBox.Select(index, lenth); //设置光标选中的文本
     tBox.ScrollToCaret();      //滚动到控件光标处
 }
Example #25
0
 /// <summary>
 /// Method that executes binary code.
 /// </summary>
 public void Execute()
 {
     try
     {
         cpu.Constants.GetRegister("pc").Val = entryPoint;
         LinkedList <Instruction> instructions = new LinkedList <Instruction>();
         LinkedList <byte>        binary       = new LinkedList <byte>();
         Thread.BeginCriticalRegion();
         executing = true;
         Thread.EndCriticalRegion();
         Variables vars = new Variables();
         vars.SetVariable("working", true);
         while (true)
         {
             int pc = cpu.Constants.GetRegister("pc").Val;
             cpu.CheckForInterupts(vars);
             if (separators.Contains(pc) && pc != entryPoint)
             {
                 Instruction         inst = cpu.Constants.MatchInstruction(binary.ToArray());
                 InstructionRegister ir   = new InstructionRegister(binary.ToArray());
                 inst.ReadAddressingModes(binary.ToArray());
                 int[] operands = inst.FetchOperands(ir, cpu, vars);
                 int[] result   = inst.Execute(ir, cpu, vars, operands);
                 inst.StoreResult(ir, cpu, vars, result);
                 binary.Clear();
                 pc = cpu.Constants.GetRegister("pc").Val;
             }
             if (pc - entryPoint >= binary.Count() || (bool)vars.GetVariable("working") == false)
             {
                 Thread.BeginCriticalRegion();
                 executing = false;
                 Thread.EndCriticalRegion();
                 break;
             }
             //binary.AddLast(binaryCode[pc++]);
             //byte[] nextWord = Program.Mem[(uint)pc];
             byte[] nextWord = cpu.ReadFromMemory((uint)pc);
             for (int i = 0; i < nextWord.Length; i++)
             {
                 binary.AddLast(nextWord[i]);
             }
             pc++;
             cpu.Constants.GetRegister("pc").Val = pc;
         }
         system.EndWorking();
         output.Text += DateTime.Now.ToString() + " Code executed successfully.\n";
         output.ScrollToCaret();
     }
     catch (System.Reflection.TargetInvocationException ex)
     {
         writeToOutput(DateTime.Now + " Execution error: " + ex.InnerException.Message + " in " + ex.InnerException.TargetSite + "\n");
         File.AppendAllText("error.txt", ex.ToString());
         StopDebugging();
         system.EndWorking();
         endExecution();
     }
     catch (Exception ex)
     {
         writeToOutput(DateTime.Now + " Execution error: " + ex.Message + "\n");
         File.AppendAllText("error.txt", ex.ToString());
         StopDebugging();
         system.EndWorking();
         endExecution();
     }
 }
Example #26
0
 public void ScrollToCaret()
 {
     TextBox.ScrollToCaret();
 }