📄 symbol_table.cc
字号:
#include "Symbol_table.h"#include "Param_symbol.h"#include "Default_symbol.h"#include "Var_symbol.h"#include "Number_expr.h"#include "Item.h"#include "Globals.h"#include "Cmd_options.h"#include "Min_max_eps.h"#include <stdio.h>#include <string>string license_note(bool comment);struct comp_order_compare { bool operator() (const Var_symbol * before, const Var_symbol * later) const { assert(before->get_computable_order() != -1 && later->get_computable_order() != -1); //fine return before->get_computable_order() < later->get_computable_order(); }};struct count_compare { bool operator() (const Param_symbol * before, const Param_symbol * later) const { return before->get_constr_order() < later->get_constr_order(); }};Symbol_table::Symbol_table(const Globals * glob) { globals = glob; symbols = map < string, Symbol * > (); for (int i = 0; i < 8; i++) var_count[i] = 0; param_count = 0; additional_count = 0; all_computable = false; add_default_symbols();}Symbol_table::~Symbol_table() { for (symbols_iter iter = symbols.begin(); iter != symbols.end(); iter++) { delete(* iter).second; } symbols.clear();}void Symbol_table::add_default_symbols() { Default_symbol * s; string name; name = string("pi"); s = new Default_symbol(REAL_TYPE, name, globals); symbols[name] = s; symb_set_value(s, new Number_expr(3.141592653589793238462643383279, globals)); name = string("MLD_epsilon"); s = new Default_symbol(REAL_TYPE, name, globals); symbols[name] = s; symb_set_value(s, new Number_expr(1e-6, globals));}const Symbol * Symbol_table::find_symbol(string name) const { if (symbols.find(name) != symbols.end()) return (* symbols.find(name)).second; else return NULL;}const Symbol * Symbol_table::create_symbol(Symb_kind kind, Symb_type type, string name) { Symbol * ret; const Symbol * found; found = find_symbol(name); assert(!found); //fine (name mustn't exist) if (kind == PARAM_KIND) { ret = new Param_symbol(type, param_count, name, globals); param_count++; } else { ret = new Var_symbol(kind, type, var_count[2 * kind + type], name, globals); var_count[2 * kind + type] ++; } symbols[name] = ret; return ret;}const Symbol * Symbol_table::create_additional(Symb_kind kind, Symb_type type) { string name; char buf[100]; additional_count++; sprintf(buf, "__additional_%d", additional_count); name = string(buf); return create_symbol(kind, type, name);}int Symbol_table::count_symbols(Symb_kind kind, Symb_type type) const { assert(kind != PARAM_KIND); //fine assert(0 <= 2 * kind + type && 2 * kind + type < 8); //fine return var_count[2 * kind + type];}void Symbol_table::remove(string name) { symbols_iter found; found = symbols.find(name); assert(found != symbols.end()); //fine delete((* found).second); symbols.erase(found);}void Symbol_table::symb_set_declared(const Symbol * s, int line_no) { symbols_iter found; Symbol * nonconst; found = symbols.find(s->get_name()); assert(found != symbols.end()); //fine nonconst = (* found).second; nonconst->set_declared_nonconst(line_no);}void Symbol_table::symb_set_used(const Symbol * s, int line_no) { symbols_iter found; Symbol * nonconst; found = symbols.find(s->get_name()); assert(found != symbols.end()); //fine nonconst = (* found).second; nonconst->set_used_nonconst(line_no);}void Symbol_table::symb_set_defined(const Var_symbol * s, const Definition_item * i) { symbols_iter found; Var_symbol * nonconst; found = symbols.find(s->get_name()); assert(found != symbols.end()); //fine nonconst = (Var_symbol *) (* found).second; nonconst->set_defined_nonconst(i);}void Symbol_table::symb_set_minmaxeps(const Symbol * s, Min_max_eps * m) { symbols_iter found; Symbol * nonconst; found = symbols.find(s->get_name()); assert(found != symbols.end()); //fine nonconst = (* found).second; assert(nonconst->is_var_symbol()); //fine ((Var_symbol *) nonconst)->set_minmaxeps_nonconst(m);}void Symbol_table::symb_set_value(const Param_symbol * s, Expr * v) { symbols_iter found; Symbol * nonconst; found = symbols.find(s->get_name()); assert(found != symbols.end()); //fine nonconst = (* found).second; assert(nonconst->is_param_symbol()); //fine ((Param_symbol *) nonconst)->set_value_nonconst(v);}void Symbol_table::symb_compute_minmax(const Var_symbol * s, const Item * originator, list < const Symbol * > l) { symbols_iter found; Symbol * nonconst; found = symbols.find(s->get_name()); assert(found != symbols.end()); //fine nonconst = (* found).second; ((Var_symbol *) nonconst)->compute_minmax_nonconst(originator, l);}int Symbol_table::symb_find_computable_order(const Var_symbol * s, list < const Var_symbol * > l) { symbols_iter found; Var_symbol * nonconst; found = symbols.find(s->get_name()); assert(found != symbols.end()); //fine nonconst = (Var_symbol *) ((* found).second); return nonconst->find_computable_order_nonconst(l);}void Symbol_table::semantic_checks() { for (symbols_iter iter = symbols.begin(); iter != symbols.end(); iter++) (* iter).second->semantic_checks();}void Symbol_table::find_computable_order() { list < const Var_symbol * > empty; Var_symbol* vsym; all_computable = true; for (symbols_iter iter = symbols.begin(); iter != symbols.end(); iter++) { if ((* iter).second->is_var_symbol()) { vsym = (Var_symbol *)((* iter).second); if (vsym->find_computable_order(empty) == 0) { if ( vsym->get_kind()!=INPUT_KIND ) { all_computable = false; // cout << (*iter).second->get_name() << " not computable" << endl; } } } }}string Symbol_table::to_matlab(string prefix) const { int cnt; string res; string bounds_vector; string prefix2; char buf[100]; //cout << "printing Symbol_table" << endl; cout.flush(); cnt = 1; for (const_symbols_iter iter = symbols.begin(); iter != symbols.end(); iter++, cnt++) { prefix2 = prefix; sprintf(buf, "symtable{%d}.", cnt); prefix2 += string(buf); res += (* iter).second->status_matlab(prefix2); if ((* iter).second->is_var_symbol()) { bounds_vector+=((Var_symbol*)((* iter).second))->bounds_vector_entry(prefix); } } //cout << "printing Symbol_table done" << endl; cout.flush(); res += summary(prefix); res+=init_bounds_vector(prefix); res+=bounds_vector; return res;}string Symbol_table::init_bounds_vector(string prefix) const { string res; res+=prefix + string("ul = -inf * ones(") + prefix + string("nu, 1);\n"); res+=prefix + string("uu = +inf * ones(") + prefix + string("nu, 1);\n"); res+=prefix + string("xl = -inf * ones(") + prefix + string("nx, 1);\n"); res+=prefix + string("xu = +inf * ones(") + prefix + string("nx, 1);\n"); res+=prefix + string("yl = -inf * ones(") + prefix + string("ny, 1);\n"); res+=prefix + string("yu = +inf * ones(") + prefix + string("ny, 1);\n"); res+=prefix + string("dl = -inf * ones(") + prefix + string("nd, 1);\n"); res+=prefix + string("du = +inf * ones(") + prefix + string("nd, 1);\n"); res+=prefix + string("zl = -inf * ones(") + prefix + string("nz, 1);\n"); res+=prefix + string("zu = +inf * ones(") + prefix + string("nz, 1);\n"); return res;}string Symbol_table::initialize_nonsymb_params_matlab() const { string res;; const Symbol * sym; const Param_symbol * psym; list < const Param_symbol * > todo; list < const Param_symbol * >::const_iterator todo_iter; struct count_compare comparator; for (const_symbols_iter iter = symbols.begin(); iter != symbols.end();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -