📄 mainform.cs
字号:
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PLX;
namespace PLX
{
public partial class Form1 : Form
{
private string strDefaultFileName;
private static SourceCodeChildForm scChildForm;
private static OutPutChildForm OPChildForm;
private static MIDCode midCodeForm;
private static PLXAnalyzer plxAnalyzer;
public int lineNum;
public Form1()
{
InitializeComponent();
ShowOPChildForm();
ShowMIDCodeChildForm();
ShowSCChildForm();
}
//Open File
private void OpenFileOToolStripMenuItem_Click(object sender, EventArgs e)
{
if (ofgOpenFile.ShowDialog() == DialogResult.OK)
{
//clear three windows' older information
OPChildForm.Controls[0].Text = "";
midCodeForm.Controls[0].Text = "";
scChildForm.Controls["rtxtLineNum"].Text = "1\n";
strDefaultFileName = ofgOpenFile.FileName;
StreamReader sr = new StreamReader(strDefaultFileName,Encoding.GetEncoding("GB2312"));
ShowSCTextAndLineNum(ref sr);
//decide scan menu is enabled or disabled according to textbox's content
CanScan();
}
}
//Save File
private void SaveFileSToolStripMenuItem_Click(object sender, EventArgs e)
{
sfgSaveFile.FileName = strDefaultFileName;
if (sfgSaveFile.ShowDialog() == DialogResult.OK)
{
(scChildForm.Controls["rtxtSourceCode"] as RichTextBox).SaveFile(sfgSaveFile.FileName, RichTextBoxStreamType.PlainText);
}
}
private void ExitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
private void SourceCodeToolStripMenuItem_Click(object sender, EventArgs e)
{
ShowSCChildForm();
}
private void OutPutToolStripMenuItem_Click(object sender, EventArgs e)
{
ShowOPChildForm();
}
private void MIDCodeToolStripMenuItem_Click(object sender, EventArgs e)
{
ShowMIDCodeChildForm();
}
//Show SourceCode Window
private void ShowSCChildForm()
{
if (scChildForm == null || scChildForm.IsDisposed)
{
scChildForm = new SourceCodeChildForm();
scChildForm.MdiParent = this;
scChildForm.Show();
}
}
//Show OutPut Window
private void ShowOPChildForm()
{
if (OPChildForm == null || OPChildForm.IsDisposed)
{
OPChildForm = new OutPutChildForm();
OPChildForm.MdiParent = this;
OPChildForm.Show();
}
}
//Show MIDCode Window
private void ShowMIDCodeChildForm()
{
if (midCodeForm == null || midCodeForm.IsDisposed)
{
midCodeForm = new MIDCode();
midCodeForm.MdiParent = this;
midCodeForm.Show();
}
}
private void ShowSCTextAndLineNum(ref StreamReader sr)
{
StringBuilder strbTempText = new StringBuilder(500);
//read line by line and record lineNo
string strLineTemp;
lineNum = 0;
while ((strLineTemp = sr.ReadLine()) != null)
{
strbTempText.AppendLine(strLineTemp);
lineNum++;
}
sr.Close();
scChildForm.Controls["rtxtSourceCode"].Text = strbTempText.ToString();
BuildLineNum(1,lineNum);
}
private void ScanToolStripMenuItem_Click(object sender, EventArgs e)
{
if (scChildForm.Controls["rtxtSourceCode"].Text.Trim().Length != 0)
{
plxAnalyzer = new PLXAnalyzer();
plxAnalyzer.StrSourceCode = scChildForm.Controls["rtxtSourceCode"].Text;
PLXAnalyzer.lineNum = 1;
plxAnalyzer.LL = plxAnalyzer.StrSourceCode.Length;
plxAnalyzer.GetSym();
plxAnalyzer.Prog(0);
if (plxAnalyzer.StrErrorOut == null)
{
OPChildForm.Controls[0].Text = "生成成功!\n";
midCodeForm.Controls[0].Text = plxAnalyzer.ListMidCode();
InterpretToolStripMenuItem.Enabled = true;
}
else
{
OPChildForm.Controls[0].Text = plxAnalyzer.StrErrorOut;
InterpretToolStripMenuItem.Enabled = false;
}
}
}
private void InterpretToolStripMenuItem_Click(object sender, EventArgs e)
{
if (scChildForm.Controls["rtxtSourceCode"].Text.Trim().Length != 0 && plxAnalyzer.code.Length != 0)
{
PLXInterpret plxinterpret = new PLXInterpret();
plxinterpret.interpret(plxAnalyzer);
OPChildForm.Controls[0].Text = plxinterpret.StrOutPut;
}
}
public void CanScan()
{
if (scChildForm.Controls["rtxtSourceCode"].Text.Trim().Length == 0)
ScanToolStripMenuItem.Enabled = false;
else
ScanToolStripMenuItem.Enabled = true;
InterpretToolStripMenuItem.Enabled = false;
}
public static void BuildLineNum(int firstLineNum,int lastLineNum)
{
if (lastLineNum > 0)
{
StringBuilder strbLine = new StringBuilder(500);
for (int i = firstLineNum; i <= lastLineNum; i++)
{
strbLine.Append(i.ToString() + "\n");
}
scChildForm.Controls["rtxtLineNum"].Text = strbLine.ToString();
}
else
scChildForm.Controls["rtxtLineNum"].Text = "1\n";
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -