Êóðñîâàÿ ðàáîòà: Èíòåðàêòèâíûé èíòåðïðåòàòîð
m_hp.GetNext(out tbeg, out tlen, out type);
m_tb.SelectionStart = beg + tbeg;
m_tb.SelectionLength = tlen;
switch (type) {
case HighlightParser.TokenType.Comment:
{
m_tb.SelectionColor = Color.DarkGreen;
break;
}
case HighlightParser.TokenType.Identifier:
{
m_tb.SelectionColor = Color.Purple;
break;
}
case HighlightParser.TokenType.Keyword:
{
m_tb.SelectionColor = Color.Blue;
break;
}
case HighlightParser.TokenType.Number:
{
m_tb.SelectionColor = Color.Red;
break;
}
case HighlightParser.TokenType.String:
{
m_tb.SelectionColor = Color.Brown;
break;
}
case HighlightParser.TokenType.Other:
{
m_tb.SelectionColor = Color.Black;
break;
}
}
}
m_tb.SelectionStart = curbeg;
m_tb.SelectionLength = curlen;
}
public enum LineState {
ErrorLine,
CurrentLine,
NormalLine
}
private void ColorLine(int index, LineState state) {
int curbeg = m_tb.SelectionStart;
int curlen = m_tb.SelectionLength;
SelectLine(index);
switch (state) {
case LineState.ErrorLine:
{
m_tb.SelectionColor = Color.Red;
break;
}
case LineState.CurrentLine:
{
m_tb.SelectionFont = s_cfont;
break;
}
case LineState.NormalLine:
{
m_tb.SelectionFont = s_nfont;
HighlightLine(index);
break;
}
}
m_tb.SelectionStart = curbeg;
m_tb.SelectionLength = curlen;
}
private void HighlightText(bool anyway) {
int l = GetCurrentLine();
int lc = GetLinesCount();
if ((l != m_curline) || (lc != m_lincount) || anyway) {
m_tb_2.Focus();
m_curline = l;
m_lincount = lc;
int bi = m_tb.GetCharIndexFromPosition(new Point(0, 0));
int ei = m_tb.GetCharIndexFromPosition(new Point(m_tb.Size));
int bl = m_tb.GetLineFromCharIndex(bi);
int el = m_tb.GetLineFromCharIndex(ei);
if (bl > 0) bl--;
if (el < lc) el++;
for (int i = bl; i < el; i++)
HighlightLine(i);
m_tb.Focus();
}
}
private void m_tb_KeyUp(object sender, KeyEventArgs e) {
HighlightText(false);
}
private void m_tb_MouseUp(object sender, MouseEventArgs e) {
if (e.Button == MouseButtons.Left)
HighlightText(true);
}
public string[] Lines {
get { return (string[]) m_tb.Lines.Clone(); }
}
public bool LoadFile(string filename) {
try {
m_tb.LoadFile(filename, RichTextBoxStreamType.PlainText);
HighlightText(true);
return true;
}
catch {
return false;
}
}
public bool SaveFile(string filename) {
try {
m_tb.SaveFile(filename, RichTextBoxStreamType.PlainText);
return true;
}
catch {
return false;
}
}
public int CurrentLine {
get { return m_tb.GetLineFromCharIndex(m_tb.SelectionStart); }
}
private class HighlightParser {
private char[] m_a;
private int m_len;
private int m_cur;
public enum TokenType {
String,
Number,
Keyword,
Comment,
Identifier,
Other
}
public void Reset(string str) {
m_a = str.ToCharArray();
m_len = str.Length;
m_cur = 0;
while ((m_cur < m_len) && Char.IsWhiteSpace(m_a[m_cur]))
m_cur++;
}
public bool HasMore() {
return m_cur < m_len;
}
private bool IsKeyword(string str)
public void GetNext(out int beg, out int len, out TokenType type) {
if (m_cur >= m_len)
throw new IndexOutOfRangeException();
beg = m_cur;
if (m_a[m_cur] == '\"') {
m_cur++;
while ((m_cur < m_len) && (m_a[m_cur] != '\"'))
m_cur++;
if (m_cur < m_len)
m_cur++;
len = m_cur - beg;
type = TokenType.String;
}
else if (isL(m_a[m_cur])) {
m_cur++;
while ((m_cur < m_len) && isLD(m_a[m_cur]))
m_cur++;
len = m_cur - beg;
if (IsKeyword(new string(m_a, beg, len)))
type = TokenType.Keyword;
else
type = TokenType.Identifier;
}
else if (m_a[m_cur] == '#') {
len = m_len - m_cur;
m_cur = m_len;
type = TokenType.Comment;
}
else if (m_a[m_cur] == '.') {
if (GetNumber()) {
len = m_cur - beg;
type = TokenType.Number;
}
else {
m_cur = beg + 1;
len = 1;
type = TokenType.Other;
}
}
else if (char.IsDigit(m_a[m_cur])) {
GetNumber();
len = m_cur - beg;
type = TokenType.Number;
}
else {
m_cur++;
len = 1;
type = TokenType.Other;
}
while ((m_cur < m_len) && Char.IsWhiteSpace(m_a[m_cur]))
m_cur++;
}
private bool GetNumber() {
if (!((m_a[m_cur] == '.') || char.IsDigit(m_a[m_cur])))
return false;
while ((m_cur < m_len) && char.IsDigit(m_a[m_cur]))
m_cur++;
if (m_cur == m_len)
return true;
else if (m_a[m_cur] == '.') {
m_cur++;
while ((m_cur < m_len) && char.IsDigit(m_a[m_cur]))
m_cur++;
if (m_cur == m_len)
return true;
else if ((m_a[m_cur] == 'e') || (m_a[m_cur] == 'E')) {
int p1 = m_cur;
m_cur++;
if (m_cur == m_len) {
m_cur = p1;
return true;
}
else if ((m_a[m_cur] == '-') || (m_a[m_cur] == '+')) {
m_cur++;
if ((m_cur == m_len) || !char.IsDigit(m_a[m_cur])) {
m_cur = p1;
return true;
}
while ((m_cur < m_len) && char.IsDigit(m_a[m_cur]))
m_cur++;
return true;
}
else if (char.IsDigit(m_a[m_cur])) {
while ((m_cur < m_len) && char.IsDigit(m_a[m_cur]))
m_cur++;
return true;
}
else {
m_cur = p1;
return true;
}
}
else
return true;
}
else if ((m_a[m_cur] == 'e') || (m_a[m_cur] == 'E')) {
int p1 = m_cur;
m_cur++;
if (m_cur == m_len) {
m_cur = p1;
return true;
}
else if ((m_a[m_cur] == '-') || (m_a[m_cur] == '+')) {
m_cur++;
if ((m_cur == m_len) || !char.IsDigit(m_a[m_cur])) {
m_cur = p1;
return true;
}
while ((m_cur < m_len) && char.IsDigit(m_a[m_cur]))
m_cur++;
return true;
}
else if (char.IsDigit(m_a[m_cur])) {
while ((m_cur < m_len) && char.IsDigit(m_a[m_cur]))
m_cur++;
return true;
}
else {
m_cur = p1;
return true;
}
}
else
return true;
}
private static bool isLD(char c) (c == '_');
private static bool isL(char c)
}
}
}
19. Êëàññ Form1.
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace interpr {
public class Form1 : Form {
private Panel panel1;
private Button button1;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private ConsoleBox consoleBox1;
private Facade m_fasade;
private void Form1_Load(object sender, EventArgs e) {
Facade.Create(consoleBox1);
m_fasade = Facade.Instance;
if (m_fasade.NotRestored) {
MessageBox.Show("Îøèáêà! Ïåðåìåííûå íå áûëè óñïåøíî âîññòàíîâëåíû.");
}
m_fasade.Done += new Facade.CommandDoneHandler(EndExec);
m_fasade.LoadSubs();
consoleBox1.Prompt();
}
private void EndExec() {
consoleBox1.Prompt();
}
private void button1_Click(object sender, EventArgs e) {
if (m_fasade.Busy) {
MessageBox.Show("Íå ìîãó îòêðûòü îêíî ôóíêöèé âî âðåìÿ âûïîëíåíèÿ êîììàíäû!");
return;
}
FunctionsForm ff = new FunctionsForm(m_fasade);
ff.ShowDialog();
EditorForm ef = ff.LastOpenedEditorForm;
if (ef != null) {
ef.Activate();
ff.SetLastEditorFormNull();
}
else
consoleBox1.Focus();
}
private void button2_Click(object sender, EventArgs e) {
if (m_fasade.Busy) {
MessageBox.Show("Íå ìîãó îòêðûòü îêíî ïåðåìåííûõ âî âðåìÿ âûïîëíåíèÿ êîììàíäû!");
return;
}
VariablesForm vf = new VariablesForm(m_fasade);
vf.ShowDialog();
consoleBox1.Focus();
}
private void consoleBox1_GetCommand(object sender, ConsoleBoxGetCommandEventArgs e) {
if (e.Command.Length > 0)
m_fasade.ExecuteCommand(e.Command);
else
consoleBox1.Prompt();
}
private void button3_Click(object sender, EventArgs e) {
m_fasade.Restart();
if (m_fasade.NotRestored) {
MessageBox.Show("Îøèáêà! Ïåðåìåííûå íå áûëè óñïåøíî âîññòàíîâëåíû.");
}
consoleBox1.Focus();
}
private void button5_Click(object sender, EventArgs e) {
if (m_fasade.Busy) {
MessageBox.Show("Íå ìîãó ñîõðàíèòü ïåðåìåííûå âî âðåìÿ âûïîëíåíèÿ ïðîãðàììû");
return;
}
m_fasade.SaveVariables();
consoleBox1.Focus();
}
private void Form1_Closing(object sender, CancelEventArgs e) {
if (EditorForm.ThereAreOpened()) {
MessageBox.Show("Ñíà÷àëà çàêðîéòå âñå îêíà ðåäàêòîðà êîäà.");
e.Cancel = true;
return;
}
m_fasade.SaveVariables();
}
private void button4_Click(object sender, EventArgs e) {
this.Close();
}
}
}
Èñïîëüçîâàííàÿ ëèòåðàòóðà è äîêóìåíòàöèÿ.
1. À. Àõî, Äæ. Õîïêðîôò, Ä. Óëüìàí. Ñòðóêòóðû äàííûõ è àëãîðèòìû – Ì. «Âèëüÿìñ», 2003.
2. Ý. Ãàììà, Ð. Õåëì, Ð. Äæîíñîí, Äæ. Âëèññèäåñ. Ïðèåìû îáúåêòíî-îðèåíòèðîâàííîãî ïðîåêòèðîâàíèÿ: ïàòòåðíû ïðîåêòèðîâàíèÿ – ÑÏá., «Ïèòåð», 2001.
3. Ä. Ãðèñ. Êîíñòðóèðîâàíèå êîìïèëÿòîðîâ äëÿ öèôðîâûõ âû÷èñëèòåëüíûõ ìàøèí – Ì. «Ìèð», 1975.
4. Ã. Êîðíåëë, Äæ. Ìîððèñîí. Ïðîãðàììèðîâàíèå íà VB.NET. Ó÷åáíûé êóðñ – ÑÏá., «Ïèòåð», 2002.
5. Ý. Òðîåëñåí. C# è ïëàòôîðìà .NET – ÑÏá., «Ïèòåð», 2004.
6. MSDN Library – April 2003.