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

📄 parser.h

📁 图形软件,用QT编写的,可以用来学习软件的
💻 H
字号:
/**************************************************************************************************************************************************************                                                                           ****    equal III the graphic builder                                          ****                                                                           ****    Copyright (C) 2003 Oleksiy Pylypenko                                   ****                                                                           **** This file may be distributed and/or modified under the terms of the       **** GNU General Public License version 2 as published by the Free Software    **** Foundation and appearing in the file license included in the              **** packaging of this file.                                                   ****                                                                           **** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE   **** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. ****                                                                           **** Contact earthman@inbox.ru if any conditions of this licensing are         **** not clear to you.                                                         ****                                                                           ****                                                                           ********************************************************************************* *****************************************************************************/#ifndef _PARSER_H_#define _PARSER_H_// Some declaration classesclass FunctionDeclaration{public:	string name;	int nleft_Params,nright_Params;	double priority;		enum TypeOrder{LR,RL} order;	bool skipPreCalcOptimization;	//usefull for '=','<','>'	//because if we optimize them 	// then we couldn't detect sign afterwards	inline FunctionDeclaration(string n="",int nl=0,int nr=0,double p=0.,TypeOrder o=LR)		:name(n),nleft_Params(nl),nright_Params(nr),priority(p),order(o)	{		skipPreCalcOptimization = false;	}	inline FunctionDeclaration(const FunctionDeclaration &d){		name = d.name;		nleft_Params = d.nleft_Params;		nright_Params = d.nright_Params;		priority = d.priority;			order = d.order;		skipPreCalcOptimization = d.skipPreCalcOptimization;	}};class VariableDeclaration{public:	string name;	inline VariableDeclaration(string n = "")		:name(n)	{	}};class BracketDeclaration{public:	char open,close;};// Base parsing tree itemclass BaseParsingTreeItem{public:	enum {Function,Variable,RealConstant,Brackets,Unknown}myType;	BaseParsingTreeItem *parent;	vector<BaseParsingTreeItem *> childs;	virtual BaseParsingTreeItem *createCopy(void) const;	virtual void print(int level = 0);	virtual ~BaseParsingTreeItem();};typedef BaseParsingTreeItem bPTI;typedef vector<BaseParsingTreeItem *> vbPTI;// some simplifiers...typedef vbPTI pti_vec;// Some parseing tree items(PTI)class PTI_Function : public BaseParsingTreeItem{public:	PTI_Function();	FunctionDeclaration declaration;	virtual BaseParsingTreeItem *createCopy(void) const;	virtual void print(int level = 0);};class PTI_Variable : public BaseParsingTreeItem{public:	PTI_Variable();	VariableDeclaration declaration;	virtual BaseParsingTreeItem *createCopy(void) const;	virtual void print(int level = 0);};class PTI_RealConstant : public BaseParsingTreeItem{public:	PTI_RealConstant();	double value;	//just for symetry	virtual BaseParsingTreeItem *createCopy(void) const;	virtual void print(int level = 0);};class PTI_Brackets : public BaseParsingTreeItem{public:	PTI_Brackets();	BracketDeclaration declaration;	virtual BaseParsingTreeItem *createCopy(void) const;	virtual void print(int level = 0);};class PTI_Unknown : public BaseParsingTreeItem{public:	PTI_Unknown();	string str;	virtual BaseParsingTreeItem *createCopy(void) const;	virtual void print(int level = 0);};typedef vector<BracketDeclaration> tbrck_decl_vec;typedef vector<FunctionDeclaration> tfunc_decl_vec;typedef vector<VariableDeclaration> tvar_decl_vec;// Gramar context of parsingclass ParsingContext{public:	//brck_decl - simple priority bracket or parameter list operator brackets	//fbrck_decl - functional brackets like [x]	tbrck_decl_vec brck_decl,fbrck_decl;	tfunc_decl_vec func_decl;	tvar_decl_vec var_decl;	bool bForceUnknownTermExecption;};// CT errors (CT - CompileTime)class errBrackets{};class errParseWrongExpression{public:	inline errParseWrongExpression(int value){		num = value;	}	int num;};class errBadConstant{};// Bracket 'insideness' level counters and xref tableclass BracketCounter{public:	int len;	const char *expr;	int *counters;	const char **poss;	BracketCounter(const char *expression,const tbrck_decl_vec& brckdecl);	int operator [](const char *i);	const char *getPos(const char *i);	~BracketCounter();};// Local bracket level changerclass BracketCounterLevel{public:	BracketCounter *acounter;	int level;	BracketCounterLevel(const char *start,BracketCounter *cnt);	int operator [](const char *i);};// trash is needed to prevent from memory leak when bruteforce exits by exceptiontypedef map<BaseParsingTreeItem *,int> tpt_trash;// type Parsing Tree _ trashpti_vec RParse(char *start,char *end,BracketCounter &brkCnt,const ParsingContext &context,tpt_trash &trash);void scan_trash(const pti_vec &pti_root,tpt_trash &trash);void free_trash(tpt_trash &trash);void free_ptree(pti_vec &pti_root);void free_ptree(bPTI *pti);pti_vec copy_ptree(const pti_vec &r);bPTI *copy_ptree(const bPTI *p);void undeclare_argument(ParsingContext &context,string name);pti_vec Parse(const char *equation,ParsingContext context);typedef unsigned char byte;typedef unsigned short word;typedef unsigned long dword;/// yeah, it's realy mashine code constructor class + excute functionclass mashineCode : public vector<byte>{	byte *codes,*mem;public:	mashineCode();	virtual ~mashineCode();	mashineCode &push_byte(byte x);	mashineCode &code(byte x);	mashineCode &push_bytes(byte *x,int len);	mashineCode &push_word(word x);	mashineCode &push_dword(dword x);	mashineCode &push_ptr(void *x);		typedef void(*executeFunction)(void);		void Init(void);	void Free(void);	virtual void preExecute(byte *codes) = 0;	virtual int IsException(void) = 0;	inline void execute(void){		preExecute(codes);		(*((executeFunction*)&codes))();	}};// Context of execution// Variables,temporaries and code goes hereclass ExecutionContext{	stack<double *>      tempTrash;public:	map<string,double *> VariablesDefenition;	mashineCode *codes;	ExecutionContext();	~ExecutionContext();	void Free(void);	double *AllocTemporary(double value = 0.);};// When optimizer needs to do constant precalculation(like 1+2)// then it calls +(1,1) throught this interfaceclass FunctionExecutor{public:	FunctionExecutor();	virtual ~FunctionExecutor();	virtual double Execute(const FunctionDeclaration &decl,stack<double> params) = 0;	virtual double Execute(const BracketDeclaration &decl,stack<double> params) = 0;};int PreCalcOptimize(pti_vec &vec,FunctionExecutor &exec);void SubstituteVar(pti_vec &where,const string &varname,const bPTI *subst);void SubstituteVar(bPTI *where,const string &varname,const bPTI *subst);class UserFunction{public:	FunctionDeclaration declaration;	pti_vec definition;	vector<string> vars;};void SubstituteFunction(pti_vec &where,const UserFunction &function);namespace ArithOptimizeHelper{	int IsFunction(bPTI &bpti,char *name,int left,int right);	int IsConstant(bPTI &bpti,double val);}int ArithmeticOptimize(pti_vec &vec);/*+ Parser+ Constant optimizer+ Variables substitution+ Function substitution+ arithmetic optimizer*/#endif //_PARSER_H_

⌨️ 快捷键说明

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