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

📄 plxcompiler.h

📁 一个PLX教学编译器IDE
💻 H
字号:
/////////////////////////////////////////////////////////////////////////////////////////////////
//                             Author : tuwei                                                  //
//                               ECNU.CS.03                                                    //
//                             2006.9 ~ 2006.11                                                //
//                           Copyright (C) reserved                                            //
//                                                                                             //
//                                                                                             //
//   Function:                                                                                 //                          
//           Compiler.                                                                         //
//                                                                                             //
//   Credit:                                                                                   // 
//           If you use this control in your application please keep the file header in        //
//           your source or put author's name in your product.                                 //
//                                                                                             //
//                                                                                             //
/////////////////////////////////////////////////////////////////////////////////////////////////
#ifndef __PLXCOMPILER__H_
#define __PLXCOMPILER__H_

#define MAX_LINECHAR      1024     /* 每行字符数目最大值 */
#define MAX_NUMBERSIZE    14       /* 每个数字的最大位数 */
#define MAX_ANSICNUMBER   128      /* ANSIC字符数目      */
#define MAX_INDENTFIERLEN 36       
#define MAX_INDENTNUMBER  1024
#define MAX_CODENUMBER    2048 
#define MAX_STACKLIMIT    2048*4
#define MAX_KEYWORD       32

#include <stdio.h>
#include <fstream.h>
#include <string.h>
#include <stdarg.h>
#include <iomanip.h>

class COutputView : public CEdit
{
public:
	COutputView(){};
	virtual ~COutputView(){};
	virtual void Clean() = 0;
	virtual void AddText(LPCTSTR strWndText,int index = -1) = 0;	
};

class CVariableList : public CListCtrl
{
public:
	CVariableList(){};
	virtual ~CVariableList(){};
	virtual int AddText(LPCTSTR strVarName,int value) = 0;
    virtual void Clean() = 0;
	virtual void ChangeValue(int index,int value) = 0;
};

class plxCompiler  
{
public:
	 plxCompiler();
	~plxCompiler();

public:	
    bool OpenFile(const char *fileName);
	void Compiler();
	bool Build(LPCTSTR outputfile);

    int  GetErrorNumber();
	void Debug();
	void showCode(int index = -1);
	bool outputCode(LPCTSTR outputfile);

	void SetDebugView(COutputView *pDebug);
	void SetOutputView(COutputView *pOutView);
	void SetCodeOutView(COutputView *pCode);
    void SetVariableList(CVariableList *pList);

protected:
    COutputView   *pOutputView;
    COutputView   *pDebugView;
    COutputView   *pCodeView;
	CVariableList *pVariableView;

	CString      sourceFilepath;
	
protected:   	
	void clearStack();
	bool textIn(int index);
	void text(int index);
	void Satement();
	void textOut(char *text);
	void error(const char *format,...);	
	
	void clear();
	void closeResource();

private:	

	/* 词法分析  */
    ifstream    InFile;                     /* 文件输入流  */
	
	int         cc;                         /* 当前字符号  */
	int         ll;                         /* 当前行数目  */
    char        line[MAX_LINECHAR];         /* 当前行字符  */          
	char        ch;                         /* 当前读取字符*/          
	int         lineIndex;                  /* 当前行号    */

	char        word[MAX_KEYWORD][16];      /* 关键字串    */
	int         wsym[MAX_KEYWORD];          /* 关键字索引  */       
	char        a[MAX_INDENTFIERLEN];       /* 标识符缓存  */       
	int         ssym[MAX_ANSICNUMBER];      /* 运算符索引  */        

    char        id[MAX_INDENTFIERLEN];      /* 三种变量缓存*/       
    int         sym;                          
	int         num;   
	
	void getsym();
	void getch();


	/*  语法分析 */
    enum _kind{varConst,varInteger,varLogical,varProc};
	typedef enum _kind  _kind;
    
	/* 变量,过程存储数据结构 */
    typedef struct _table{char  name[MAX_INDENTFIERLEN];
		                  _kind kind;
						  int   adr;
						  int   level;
						  int   size;
						  int   val;
						  int   ldex;
	}TABLE;

	TABLE table[MAX_INDENTNUMBER];           /*  存储区       */
    int   tx;                                /*  table表指针  */

	int   lev;                               /*  静态层次计数 */
	int   dx;                                /*  局部变量指针 */
	int   errornum;                          /*  语法错误数目 */

   	void  enter(_kind indent,int value = 0); /*  变量入表函数 */
	int   position(char *ident);
	void  vardeclaration(_kind varkind);
	void  expandArray(int len);
	void  constdeclaration();

	void  af();
	void  at();
	void  re();
	void  bf();
	void  bt();
	void  ae();
	void  be();
	void  d();
	void  ddInt();
	void  ddBool();
	void  ddConst();
	void  ss();
	void  offset();
	void  ds();
	void  block();
	void  Prog();

	/* 出错处理     */

	/* Follow集结构 */
    typedef struct _follow{
	        int  num;
			int  followSym[32];
	}FOLLOW;
	
    FOLLOW non_finis[24];

	/* 中间码结构   */
	enum instruction{OPR,LIT,LOD,STO,INT,JMP,JPC,SHO,CAL};
	typedef enum instruction fct;

	typedef struct _code{ fct f;
                          int l;
	                      int a;
						  int lev;
						  int line;
	}CODE;

	/* 存储代码的数组和指标 */
	CODE code[MAX_CODENUMBER];
	int  cx;
    int  codeIndex; 

	void  gen(fct _f,int _l,int _a); /*  生成代码         */


	/*  虚拟机     */  
    int  s[MAX_STACKLIMIT];          /*  运行栈           */

	int  t;                          /*  运行栈指针       */	
	int  p;                          /*  PC寄存器         */
	int  b;                          /*  基地址寄存器     */
	int  breakpoint;                 /*  断点             */

	
	int  base(int l);
	int  nDebugStyle;

/*  debug 信息 */
public:				
	enum{RUNBREAKPOINT,RUNINTO,RUNOUT};

	int  interpret(int debugStyle = RUNBREAKPOINT); 
	int  GetCodeSize();		
	bool SetBreakpointLine(int line);

protected:
    void ListVariable(CODE i,int value);
	int  getTableIndex(int lev,int dax);

};
#endif 

⌨️ 快捷键说明

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