form1.cs

来自「一个用C#编写的解释器」· CS 代码 · 共 522 行 · 第 1/2 页

CS
522
字号
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Microsoft.VisualBasic.Devices;


namespace WindowsApplication6
{
    public partial class Form1 : Form
    {
        const short ERROR = -1;
        
        const short LT = 1;     // <
        const short LE = 2;     // <=
        const short EQ = 3;    // =
        const short NE = 4;    // <> 
        const short GT = 5;    // >
        const short GE = 6;    // >=
        const short ADD = 7;    //+
        const short SUB = 8;    //-
        const short MUL = 9;    //*
        const short DIV = 10;   // /
        const short INC = 11;   // ++
        const short DEC = 12;   // --
        const short LP = 13;    // (
        const short RP = 14;    // )
        const short LN = 15;    // /*
        const short RN = 16;    // */
        const short LFP = 17;    // {
        const short RFP = 18;    // {
        const short ID = 19;     //标识符
        const short INT = 20;    //整数

        const short BEGIN = 21;
        const short END = 22;
        const short IF = 23;
        const short THEN = 24;
        const short ELSE = 25;
        const short WHILE = 26;
        const short DO = 27;
        const short VAR = 28;
        const short INTEGER = 29;
        const short PROCEDURE = 30;
        const short PARBEGIN = 31;
        const short PAREND = 32;
        




        public Form1()
        {
            InitializeComponent();
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {

            CountInt = CountID = CountKeyword = CountSign = CountError = 0;
            listBox2.Items.Clear();
            textBox4.Clear();
            try
            {
                string FILENAME = string.Empty;
                FILENAME = textBox2.Text;
                Computer MyComputer = new Computer();
                System.IO.StreamReader MyStreamReader;
                MyStreamReader = MyComputer.FileSystem.OpenTextFileReader(FILENAME, Encoding.Default);
                textBox4.Text = MyComputer.FileSystem.ReadAllText(FILENAME, Encoding.Default);
/////////////////////////////////////////////////////////////////////////////
                


                int c;
                char ch;
                string stemp = string.Empty;
                

                while (MyStreamReader.Peek()!=-1)
                {
                    stemp = string.Empty;
                    while(IsListSeparatorOrBlank((char)MyStreamReader.Peek()))
                    {
                        MyStreamReader.Read();
                    }
                    if (MyStreamReader.Peek() == -1)
                        continue;
                    ch = (char)MyStreamReader.Peek();

                    if (isalpha(ch))
                    {
                        stemp += ch;
                        MyStreamReader.Read();
                        ch = (char)MyStreamReader.Peek();
                        while (isalnum(ch))
                        {
                            stemp += ch;
                            MyStreamReader.Read();
                            ch = (char)MyStreamReader.Peek();
                        }
                        ch = (char)MyStreamReader.Peek();
                        if (MyStreamReader.Peek() == -1 | IsListSeparatorOrBlank(ch) | isrelationsign(ch)|isoperator(ch))
                        {
                            c = lookup(stemp);
                            if (c == 0)
                            {
                                Out(ID, stemp);
                                CountID++;
                            }
                            else
                            {
                                Out(c, "");
                                CountKeyword++;
                            }
                        }
                        else
                        {
                            stemp += ch;
                            MyStreamReader.Read();
                            ch = (char)MyStreamReader.Peek();
                            while (MyStreamReader.Peek() != -1 && !IsListSeparatorOrBlank((char)MyStreamReader.Peek()))
                            {
                                stemp += ch;
                                MyStreamReader.Read();
                                ch = (char)MyStreamReader.Peek();
                            }
                            Out(ERROR, stemp);
                            CountError++;
                        }
                    }
                    else if (isdigit(ch))
                    {
                        stemp += ch;
                        MyStreamReader.Read();
                        ch = (char)MyStreamReader.Peek();
                        while (isdigit(ch))
                        {
                            stemp += ch;
                            MyStreamReader.Read();
                            ch = (char)MyStreamReader.Peek();
                        }
                        ch = (char)MyStreamReader.Peek();
                        if (MyStreamReader.Peek() == -1 | IsListSeparatorOrBlank(ch) | isrelationsign(ch) | isoperator(ch))
                        {
                            Out(INT, stemp);
                            CountInt++;
                        }
                        else
                        {
                            stemp += ch;
                            MyStreamReader.Read();
                            ch = (char)MyStreamReader.Peek();
                            while (MyStreamReader.Peek() != -1 && !IsListSeparatorOrBlank((char)MyStreamReader.Peek()))
                            {
                                stemp += ch;
                                MyStreamReader.Read();
                                ch = (char)MyStreamReader.Peek();
                            }
                            Out(ERROR, stemp);
                            CountError++;
                        }
                    }
                    else switch (ch)
                        {
                            case '<':

                                stemp += ch;
                                MyStreamReader.Read();
                                ch = (char)MyStreamReader.Peek();
                                if (ch == '=')
                                { stemp += ch; Out(LE, ""); CountSign++; MyStreamReader.Read(); }
                                else if (ch == '>')
                                { stemp += ch; Out(NE, ""); CountSign++; MyStreamReader.Read(); }
                                else Out(LT, "");
                                CountSign++;
                                break;


                            case '=':
                                stemp += ch; Out(EQ, "");
                                MyStreamReader.Read();
                                CountSign++;
                                break;


                            case '(':
                                stemp += ch; Out(LP, "");
                                MyStreamReader.Read();
                                CountSign++;
                                break;

                            case '{':
                                stemp += ch; Out(LFP, "");
                                MyStreamReader.Read();
                                CountSign++;
                                break;

                            case ')':
                                stemp += ch; Out(RP, "");
                                MyStreamReader.Read();
                                CountSign++;
                                break;

                            case '}':
                                stemp += ch; Out(RFP, "");
                                MyStreamReader.Read();
                                CountSign++;
                                break;



                            case '>':
                                stemp += ch;
                                MyStreamReader.Read();
                                ch = (char)MyStreamReader.Peek();
                                if (ch == '=')
                                { stemp += ch; Out(LE, ""); CountSign++; MyStreamReader.Read(); }
                                else Out(GT, "");
                                CountSign++;
                                break;

                            case '*':
                                stemp += ch;
                                MyStreamReader.Read();
                                ch = (char)MyStreamReader.Peek();
                                if (ch == '/')
                                { stemp += ch; Out(RN, ""); CountSign++; MyStreamReader.Read(); }
                                else Out(MUL, "");
                                CountSign++;
                                break;
                            case '/':
                                stemp += ch;
                                MyStreamReader.Read();
                                ch = (char)MyStreamReader.Peek();
                                if (ch == '*')
                                { stemp += ch; Out(LN, ""); CountSign++; MyStreamReader.Read(); }
                                else Out(DIV, "");
                                CountSign++;
                                break;
                            case '-':
                                stemp += ch;
                                MyStreamReader.Read();
                                ch = (char)MyStreamReader.Peek();
                                if (ch == '-')
                                { stemp += ch; Out(DEC, ""); CountSign++; MyStreamReader.Read(); }
                                else Out(SUB, "");
                                CountSign++;
                                break;
                            case '+':
                                stemp += ch;
                                MyStreamReader.Read();
                                ch = (char)MyStreamReader.Peek();

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?