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

📄 var_symbol.cc

📁 由matlab开发的hybrid系统的描述语言
💻 CC
字号:
#include "Var_symbol.h"#include "Min_max_eps.h"#include "Globals.h"#include "Symbol_table.h"#include "Definition_item.h"#include <stdio.h>#include "Expr.h"#include "Problem.h"#include "Problem_handler.h"Var_symbol::Var_symbol(Symb_kind i_kind, Symb_type i_type, int i_count,	string i_name, const Globals * glob) : Symbol(i_type, i_name, glob) {		first_def = NULL;		second_def = NULL;		kind = i_kind;		count = i_count;		mme = NULL;}Var_symbol::~Var_symbol() {	if (mme) delete mme;}Min_max_eps * Var_symbol::get_minmaxeps() const {	if (mme != NULL) {		return mme->clone();	} else {		return NULL;	}}int Var_symbol::get_count() const {	return count;}Symb_kind Var_symbol::get_kind() const {	return kind;}void Var_symbol::set_minmaxeps(Min_max_eps * m) const {	globals->symbol_table->symb_set_minmaxeps(this, m);}void Var_symbol::set_minmaxeps_nonconst(Min_max_eps * m) {	mme = m;	//if (mme) cout << "set MME for " << get_name() << endl; cout.flush();}list < const Var_symbol * > Var_symbol::get_required_mme() const {	assert(first_def); //fine	return first_def->get_required_mme();}list < const Var_symbol * > Var_symbol::get_required_simu() const {	assert(first_def); //fine	return first_def->get_required_simu();}void Var_symbol::semantic_checks() {	Symbol::semantic_checks();	if (second_def) {		string msg;		char buf[70];		sprintf(buf, "line %d: ", second_def->get_source_line());		msg += string(buf);		msg += string("Variable ") + get_name() + string(" redefined");		sprintf(buf, " (first definition is at line %d)",			first_def->get_source_line());		msg += string(buf);		globals->problem_handler->process(new Problem(ERROR, msg));	}	if (kind != INPUT_KIND)		if (!first_def) {			string msg;			char buf[20];			msg += string("Variable ") + get_name() +				string(" is never defined (") + get_name() +				string(" was declared at line ");			sprintf(buf, "%d)", line_of_decl);			msg += string(buf);			globals->problem_handler->process(				new Problem(WARNING, msg, VAR_NOT_DEFINED));		}	if (mme)	  mme->semantic_checks();}void Var_symbol::set_defined(const Definition_item * i) const {	globals->symbol_table->symb_set_defined(this, i);}void Var_symbol::set_defined_nonconst(const Definition_item * i) {	if (first_def != NULL) {		if (second_def == NULL) second_def = i;	} else {		first_def = i;	}}void Var_symbol::compute_minmax_nonconst(const Item * originator,	list < const Symbol * > path) {		list < const Var_symbol * > req;		list < const Symbol * >::iterator iter;		if (mme) {// mme specified by user, or logic variable		  if (get_type()==BOOL_TYPE) {		    max_computed=true;		    min_computed=true;		  } else {		    min_computed=false;		    max_computed=false;		  }		  return;		}		min_computed=true;		max_computed=true;		if (!is_defined()) {			string msg;			char buf[20];			sprintf(buf, "line %d: ",				originator->get_source_line());			msg = buf;			msg += string("Failed to compute bounds\n");			msg += string("no definition for Variable ") + get_name();			path.reverse(); // better: use reverse iterator			for (list < const Symbol * >				::const_iterator iter2 = path.begin();				iter2 != path.end(); iter2++) {					msg += string("\nneeded to compute bounds for variable ") +						(* iter2)->get_name();			}			throw new Problem(ERROR, msg);		}		for (iter = path.begin(); iter != path.end(); iter++) {			if (this == (* iter)) {				string msg;				char buf[20];				sprintf(buf, "line %d: ",					originator->get_source_line());				msg = buf;				msg += string("Failed to compute bounds\n");				msg += string("circular definition of variable ") + get_name();				path.reverse(); // better: use reverse iterator				for (list < const Symbol * >					::const_iterator iter2 = path.begin();					iter2 != path.end(); iter2++) {						msg += string("\nneeded in definition of variable ") +							(* iter2)->get_name();						if ((* iter2) == this) break;				}				throw new Problem(ERROR, msg);			}		}		if (!first_def->minmax_known()) {			req = get_required_mme();			path.push_back(this);			for (list < const Var_symbol * >				::const_iterator iter = req.begin();				iter != req.end(); iter++) {					(* iter)->compute_minmax(originator, path);			}		}		mme = first_def->compute_minmax();}Expr * Var_symbol::get_min() const {	assert(mme); //fine	return mme->get_min();}Expr * Var_symbol::get_max() const {	assert(mme); //fine	return mme->get_max();}void Var_symbol::compute_minmax(const Item * originator,	list < const Symbol * > path) const {		globals->symbol_table->symb_compute_minmax(this,			originator, path);}string Var_symbol::status_matlab(string prefix) const {	string res;	char buf[100];	res += Symbol::status_matlab(prefix);	switch (kind) {		case INPUT_KIND:			res += prefix + string("kind = \'u\';\n");			break;		case OUTPUT_KIND:			res += prefix + string("kind = \'y\';\n");			break;		case STATE_KIND:			res += prefix + string("kind = \'x\';\n");			break;		case AUX_KIND:			if (type == REAL_TYPE)				res += prefix + string("kind = \'z\';\n"); else				res += prefix + string("kind = \'d\';\n");			break;		default:			assert(0); //fine	}	sprintf(buf, "index = %d;\n", count+1);	res += prefix + buf;	if (first_def) {		sprintf(buf, "defined = %d;\n", first_def->get_group()+1);		res += prefix + buf;	} else {		sprintf(buf, "defined = NaN;\n");		res += prefix + buf;	}	if (mme) {		Expr * min, * max;		min = get_min();		max = get_max();		res += prefix + string("min = ") +			min->to_matlab() + string(";\n");		if (min_computed)		  res+= prefix + string("min_computed = 1;\n");		else		       	       		  res+= prefix + string("min_computed = 0;\n");		res += prefix + string("max = ") +			max->to_matlab() + string(";\n");		if (min_computed)		  res+= prefix + string("max_computed = 1;\n");		else		       	       		  res+= prefix + string("max_computed = 0;\n");		delete min;		delete max;	} else {		res += prefix + string("min = NaN;\n");		res+= prefix + string("min_computed = 0;\n");		res += prefix + string("max = NaN;\n");		res+= prefix + string("max_computed = 0;\n");	}	return res;}string Var_symbol::bounds_vector_entry(string prefix) const {	string res;	char buf[100];	Expr *tmp;	if (mme) {	  tmp=get_min();	  res+=prefix;	  switch (get_kind()) {	  case STATE_KIND:	    res += string("x");	    break;	  case OUTPUT_KIND:	    res += string("y");	    break;	  case INPUT_KIND:	    res += string("u");	    break;	  case AUX_KIND:	    if (get_type() == REAL_TYPE) res += string("z"); else	      res += string("d");	    break;	  case PARAM_KIND:	    assert(0); //fine	  }	  res+=string("l");	  	  if (get_kind() != AUX_KIND) {	    if (get_type() == REAL_TYPE)	      sprintf(buf, "(%d)", get_count() + 1); else		sprintf(buf, "(%d)",			globals->symbol_table->count_symbols(get_kind(),							     REAL_TYPE) + get_count() + 1);	  } else {	    sprintf(buf, "(%d)", get_count() + 1);	  }	  res += buf;	  res+=string(" = ");	  res+=tmp->to_matlab();	  res+=string(";\n");	  delete tmp;	tmp=get_max();	res+=prefix;	switch (get_kind()) {		case STATE_KIND:			res += string("x");			break;		case OUTPUT_KIND:			res += string("y");			break;		case INPUT_KIND:			res += string("u");			break;		case AUX_KIND:			if (get_type() == REAL_TYPE) res += string("z"); else				res += string("d");			break;		case PARAM_KIND:			assert(0); //fine	}	res+=string("u");	if (get_kind() != AUX_KIND) {		if (get_type() == REAL_TYPE)			sprintf(buf, "(%d)", get_count() + 1); else			sprintf(buf, "(%d)",				globals->symbol_table->count_symbols(get_kind(),				REAL_TYPE) + get_count() + 1);	} else {		sprintf(buf, "(%d)", get_count() + 1);	}	res += buf;	res+=string(" = ");	res+=tmp->to_matlab();	res+=string(";\n");	delete tmp;}return res;}string Var_symbol::to_matlab() const {	string res;	char buf[100];	switch (get_kind()) {		case STATE_KIND:			res = string("x");			break;		case OUTPUT_KIND:			res = string("y");			break;		case INPUT_KIND:			res = string("u");			break;		case AUX_KIND:			if (get_type() == REAL_TYPE) res = string("z"); else				res = string("d");			break;		case PARAM_KIND:			assert(0); //fine	}	if (get_kind() != AUX_KIND) {		if (get_type() == REAL_TYPE)			sprintf(buf, "(%d)", get_count() + 1); else			sprintf(buf, "(%d)",				globals->symbol_table->count_symbols(get_kind(),				REAL_TYPE) + get_count() + 1);	} else {		sprintf(buf, "(%d)", get_count() + 1);	}	res += buf;	return res;}string Var_symbol::to_matlab_newstate() const {	string res;	char buf[100];	if (get_type() == REAL_TYPE) sprintf(buf, "xn(%d)", get_count() + 1);	else sprintf(buf, "xn(%d)",		globals->symbol_table->count_symbols(get_kind(),		REAL_TYPE) + get_count() + 1);	res = buf;	return res;}int Var_symbol::find_computable_order(list < const Var_symbol * > path) const {	return globals->symbol_table->symb_find_computable_order(this, path);}int Var_symbol::find_computable_order_nonconst(list <	const Var_symbol * > path) {		list < const Var_symbol * > req;		list < const Var_symbol * >::iterator iter;		int maxorder, order;		if (computable_order != -1) return computable_order;		if (get_kind() == INPUT_KIND) {			computable_order = 1;			return computable_order;		}		if (!is_defined()) {			computable_order = 0; // not defined -> not computable			return computable_order;		}		req = get_required_simu();		for (iter = path.begin(); iter != path.end(); iter++) {			if ((* iter) == this) {				computable_order = 0; // not computable				return computable_order;			}		}		maxorder = 1;		path.push_back(this);		for (list < const Var_symbol * >::const_iterator iter = req.begin();			iter != req.end(); iter++) {				if ((* iter)->get_kind() != STATE_KIND &&					(* iter)->get_kind() != INPUT_KIND) {						order = (* iter)->find_computable_order(path);						if (order == 0) {							computable_order = 0; // not computable							return computable_order;						}						if (maxorder < order + 1)							maxorder = order + 1;				}		}		computable_order = maxorder;		return computable_order;}const Item *Var_symbol::get_definition() const{	assert(first_def);  //fine	return first_def;}

⌨️ 快捷键说明

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