📄 form1.cs
字号:
using System;
using System.Threading;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using compiler;
namespace compiler
{
public partial class Form1 : Form
{
Tree tree = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void scanner_Click(object sender, EventArgs e)
{
try
{
console.Text = "";
string[] read = new string[sources.Lines.Length];
read = sources.Lines;
// start first pass
FirstPass fp = new FirstPass(read);
if (fp.fill())
{
foreach (Token tk in fp.getTokens())
{
string temp = tk.getValue();
string temp2 = temp == ";" ? " " + temp + "\n" : " " + temp;
console.Text += temp2;
}
}
else
{
string temp = "There is a mistake at row " + fp.getRow() +" "+ "column " + (fp.getColumn()-1);
console.Text = temp + "\n";
}
}catch(Exception ef){
Console.WriteLine(ef.ToString());
}
}
private void parser_Click(object sender, EventArgs e)
{
console.Text = "";
string[] read = new string[sources.Lines.Length];
read = sources.Lines;
// start first pass
FirstPass fp = new FirstPass(read);
//词法正确 再做语法
if (fp.fill())
{
//语法正确再输出树
if(fp.parser() && fp.isAnyError() )
{
Tree theTree = fp.getTree();
console.Text = theTree.printTree();
}
else
{
foreach (string error in fp.getError())
{
console.Text += error+"\n";
}
}
}
else
{
string temp = "There is a mistake at row " + fp.getRow() +" "+ "column " + (fp.getColumn()-1);
console.Text = temp + "\n";
}
}
private void open_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
System.IO.StreamReader sr = new
System.IO.StreamReader(openFileDialog1.FileName);
string text = sr.ReadToEnd();
sources.Text = "";
sources.Text = text;
sr.Close();
}
}
private void menu_save_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK && saveFileDialog1.FileName!="")
{
saveFileDialog1.Filter = "Text files (*.txt)|*.txt";
saveFileDialog1.CreatePrompt = true;
saveFileDialog1.OverwritePrompt = true;
saveFileDialog1.DefaultExt = "txt";
saveFileDialog1.InitialDirectory = "c:\\";
sources.SaveFile(saveFileDialog1.FileName,RichTextBoxStreamType.PlainText);
}
}
private void console_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
string theText = console.Text;
tree.setRead(theText);
}
}
private void go_Click(object sender, EventArgs e)
{
this.console.Text = "";
//Thread theThread = new Thread(new ThreadStart(this.doThread));
//theThread.Start();
string[] theRead = new string[sources.Lines.Length];
theRead = sources.Lines;
// start first pass
FirstPass fp = new FirstPass(theRead);
//词法正确 再做语法
if (fp.fill())
{
//语法正确再输出树
if (fp.parser() && fp.isAnyError())
{
this.tree = fp.getTree();
//如果语义正确 在输出执行结果
//确定输出
tree.setOut(this.console);
//确定提示
tree.remind(this.label1);
Thread theThread = new Thread(new ThreadStart(tree.secondPass));
theThread.Start();
}
else
{
foreach (string error in fp.getError())
{
console.Text += error + "\n";
}
}
}
else
{
string temp = "There is a mistake at row " + fp.getRow() + " " + "column " + (fp.getColumn() - 1);
console.Text = temp + "\n";
}
}
private void menu_copy_Click(object sender, EventArgs e)
{
// Ensure that text is selected in the text box.
if (this.sources.SelectionLength > 0)
{ // Copy the selected text to the Clipboard.
sources.Copy();
}
else if (this.console.SelectionLength > 0)
{
console.Copy();
}
}
private void menu_paste_Click(object sender, EventArgs e)
{
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text) == true)
{
// Determine if any text is selected in the text box.
if (sources.Focused == true)
{
if (sources.SelectionLength > 0)
{
// Ask user if they want to paste over currently selected text.
// Move selection to the point after the current selection and paste.
sources.SelectionStart = sources.SelectionStart + sources.SelectionLength;
}
// Paste current text in Clipboard into text box.
sources.Paste();
}
}
}
private void menu_cut_Click(object sender, EventArgs e)
{
if (sources.SelectedText != "")
{// Cut the selected text in the control and paste it into the Clipboard.
sources.Cut();
}
}
private void menu_undo_Click(object sender, EventArgs e)
{
if (sources.CanUndo == true)
{
// Undo the last operation.
sources.Undo();
// Clear the undo buffer to prevent last action from being redone.
sources.ClearUndo();
}
}
private void 新建ToolStripMenuItem_Click(object sender, EventArgs e)
{
sources.Clear();
console.Clear();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -