Minsky
variableType.cc
Go to the documentation of this file.
1 /*
2  @copyright Steve Keen 2013
3  @author Russell Standish
4  This file is part of Minsky.
5 
6  Minsky is free software: you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Minsky is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with Minsky. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "variableType.h"
20 #include "variableType.rcd"
21 #include "minsky_epilogue.h"
22 using namespace classdesc;
23 
24 #include <string>
25 #include <sstream>
26 using namespace std;
27 
28 namespace minsky
29 {
30  string VariableType::typeName(int t)
31  {return enumKey<Type>(t);}
32 
33  string Units::str() const
34  {
35  ostringstream s;
36  s<<*this;
37  return s.str();
38  }
39 
40  string Units::latexStr() const
41  {
42  string s;
43  for (auto& i: *this)
44  if (!i.first.empty() && i.second!=0)
45  {
46  s+=i.first;
47  if (i.second!=1)
48  s+="^{"+to_string(i.second)+"}";
49  else
50  s+=" ";
51  }
52  return s;
53  }
54 
55 
56  Units::Units(const string& x)
57  {
58  if (x.empty()) return;
59  // split into numerator and denominator components if / found
60  auto divPos=x.find('/');
61  vector<string> components;
62  components.push_back(x.substr(0,divPos));
63  if (divPos!=string::npos)
64  components.push_back(x.substr(divPos+1));
65 
66  if (components.back().find('/')!=string::npos)
67  throw runtime_error("too many division signs: "+x);
68 
69  int coef=1;
70  for (auto& i: components)
71  {
72  if (i.empty())
73  throw runtime_error("empty numerator or denominator: "+x);
74  const char* b=i.c_str();
75  for (const char*j=b;;)
76  {
77  if (*j=='^'||*j=='\0'||isspace(*j))
78  {
79  const string name(b,j); // stash end of unit name
80  if (name.empty())
81  throw runtime_error("empty unit name: "+x);
82  while (isspace(*j)) ++j;
83  if (*j=='^')
84  {
85  auto k=j+1;
86  const int v=strtol(k,const_cast<char**>(&j),10);
87  if (j==k)
88  throw runtime_error("invalid exponent: "+x);
89  if (name!="1")
90  (*this)[name]+=coef*v;
91  }
92  else if (name!="1")
93  (*this)[name]+=coef;
94  if ((*this)[name]==0)
95  erase(name);
96  while (isspace(*j)) ++j;
97  b=j; // update to next unit name
98  if (*j=='\0') break; //loop exits here
99  }
100  else
101  ++j;
102  }
103  if (b==i.c_str()) // we haven't found any exponents
104  emplace(i,coef);
105  coef*=-1;
106  }
107  }
108 
109 
110 }
STL namespace.
Creation and access to the minskyTCL_obj object, which has code to record whenever Minsky&#39;s state cha...
Definition: constMap.h:22
std::string str(T x)
utility function to create a string representation of a numeric type
Definition: str.h:33
CLASSDESC_ACCESS_EXPLICIT_INSTANTIATION(minsky::VariableType)
string to_string(CONST84 char *x)
Definition: minskyTCLObj.h:33