form1.cs

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

CS
614
字号
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;


        const short DIGIT = 1;
        const short POINT = 2;
        const short OTHER = 3;
        const short POWER = 4;
        const short PLUS = 5;
        const short MINUS = 6;
        const short EndState = -1;

        int w, n, p, exp, d;
        int ICON;
        double FCON;
        bool IsDouble=false;
        static int CurrentState;

        private int ChangeSymbol(char c)
        {
            if (isdigit(c)) { d = c - '0'; return DIGIT; }
            if (c == '.') return POINT;
            if (c == 'E' | c == 'e') return POWER;
            if (c == '+') return PLUS;
            if (c == '-') return MINUS;
            return OTHER;
        }

        int StateTransfer(int state, int symbol)
        {
            switch (state)
            {
                case 1: switch (symbol)
                        {
                            case DIGIT: w = w * 10 + d; break;
                            case POINT: CurrentState = 2; IsDouble = true; break;
                            case POWER: CurrentState = 4; break;
                            default: ICON = w; CurrentState = EndState; break;
                            //OUT...

                        }
                        break;
                case 2: switch (symbol)
                        {
                            case DIGIT: n++; w = w * 10 + d; break;
                            
                            case POWER: CurrentState = 4; break;
                            default: FCON = w * Math.Pow(10, (exp * p - n)); IsDouble = true; CurrentState = EndState; break;
                            //OUT...

                        }
                        break;

                case 3: switch (symbol)
                        {
                            case DIGIT: n++; w = w * 10 + d; CurrentState = 2; break;
                            default:   //ERROR
                                CurrentState = EndState; break;
                           

                        }
                        break;
                case 4: switch (symbol)
                        {
                            case DIGIT: p = p * 10 + d; CurrentState = 6; break;
                            case MINUS: exp = -1; CurrentState = 5; break;
                            case PLUS: CurrentState = 5; break;
                            default:
                                //ERROR
                                CurrentState = EndState; break;
                           

                        }
                        break;
                case 5: switch (symbol)
                        {
                            case DIGIT: p = p * 10 + d; CurrentState = 6; break;
                            default:   //ERROR
                                CurrentState = EndState; break;


                        }
                        break;
                case 6: switch (symbol)
                        {
                            case DIGIT: p = p * 10 + d; break;
                            default:   //ERROR
                                FCON = w * Math.Pow(10, (exp * p - n)); IsDouble = true; CurrentState = EndState; break;


                        }
                        break;

                default: break;
            }
            return CurrentState;
        }


        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;
            textBox1.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)|ch=='.')
                    {
                        if (ch == '.') { CurrentState = 3; w = 0; n = 0; p = 0; exp = 1; }
                        else { CurrentState = 1; w = (int)(ch-'0'); n = 0; p = 0; exp = 1; }
                        int sc;

                        while (CurrentState != EndState)
                        {
                            stemp += ch;
                            MyStreamReader.Read();
                            ch = (char)MyStreamReader.Peek();
                            sc = ChangeSymbol(ch);
                            CurrentState = StateTransfer(CurrentState, sc);
                        
                        }
                        
                                            

                        ch = (char)MyStreamReader.Peek();
                        if (MyStreamReader.Peek() == -1 | IsListSeparatorOrBlank(ch) | isrelationsign(ch) | isoperator(ch))
                        {
                            if (IsDouble) Out(INT, FCON.ToString());
                            else Out(INT, ICON.ToString());

                           // Out(INT, Convert.ToDouble(stemp).ToString());
                            CountInt++;
                            FCON = 0.0;
                            ICON = 0;
                        }
                        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;

⌨️ 快捷键说明

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