⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 一个用C#编写的解释器
💻 CS
📖 第 1 页 / 共 3 页
字号:
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;
using Microsoft.VisualBasic.FileIO;

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 DOUBLE = 21;

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

        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 = CountDouble = 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) | 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(DOUBLE, FCON.ToString()); CountDouble++; }
                            else { Out(INT, ICON.ToString()); CountInt++; }
                            //if (IsDouble) { Out(INT, stemp); CountDouble++; }
                            //else { Out(INT, ICON.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++;
                        }
                        IsDouble = false;
                    }
                    else switch (ch)
                        {
                            case '<':

                                stemp += ch;

⌨️ 快捷键说明

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