Vita
factory.cc
Go to the documentation of this file.
1
18
19namespace vita
20{
21
22namespace
23{
30domain_t find_domain(const std::string &s)
31{
32 if (is_number(s))
33 return s.find('.') == std::string::npos ? domain_t::d_int
34 : domain_t::d_double;
35 return domain_t::d_string;
36}
37} // unnamed namespace
38
43{
44 register_symbol<real::abs> ("FABS", 1);
45 register_symbol<real::add> ("FADD", 1);
46 register_symbol<real::aq> ("FAQ", 1);
47 register_symbol<real::cos> ("FCOS", 1);
48 register_symbol<real::div> ("FDIV", 1);
49 register_symbol<real::idiv> ("FIDIV", 1);
50 register_symbol<real::ife> ("FIFE", 2);
51 register_symbol<real::ifl> ("FIFL", 2);
52 register_symbol<real::ifz> ("FIFZ", 1);
53 register_symbol<real::length> ("FLENGTH", 2);
54 register_symbol<real::ln> ("FLN", 1);
55 register_symbol<real::max> ("FMAX", 1);
56 register_symbol<real::mod> ("FMOD", 1);
57 register_symbol<real::mul> ("FMUL", 1);
58 register_symbol<real::integer>("REAL", 1);
59 register_symbol<real::sigmoid>("FSIGMOID", 1);
60 register_symbol<real::sin> ("FSIN", 1);
61 register_symbol<real::sqrt> ("FSQRT", 1);
62 register_symbol<real::sub> ("FSUB", 1);
63
64 register_symbol<integer::add> ("ADD", 1);
65 register_symbol<integer::div> ("DIV", 1);
66 register_symbol<integer::ife> ("IFE", 2);
67 register_symbol<integer::ifl> ("IFL", 2);
68 register_symbol<integer::ifz> ("IFZ", 1);
69 register_symbol<integer::mod> ("MOD", 1);
70 register_symbol<integer::mul> ("MUL", 1);
71 register_symbol<integer::number>("INT", 1);
72 register_symbol<integer::shl> ("SHL", 1);
73 register_symbol<integer::sub> ("SUB", 1);
74
75 register_symbol<str::ife>("SIFE", 2);
76}
77
113std::unique_ptr<symbol> symbol_factory::make(const std::string &name, cvect c)
114{
115 Expects(!name.empty());
116 Expects(!c.empty());
117
118 if (const auto it = factory_.find(name); it != factory_.end())
119 {
120 while (c.size() < it->second.args)
121 c.push_back(category_t(0));
122
123 return it->second.make(c);
124 }
125
126 switch (find_domain(name))
127 {
128 case domain_t::d_double:
129 return std::make_unique<constant<double>>(name, c[0]);
130 case domain_t::d_int:
131 return std::make_unique<constant<int>>(name, c[0]);
132 case domain_t::d_string:
133 return std::make_unique<constant<std::string>>(name, c[0]);
134 default:
135 return nullptr;
136 }
137}
138
154std::unique_ptr<symbol> symbol_factory::make(domain_t d, int min, int max,
155 category_t c)
156{
157 Expects(d == domain_t::d_double || d == domain_t::d_int);
158
159 switch (d)
160 {
161 case domain_t::d_double:
162 return std::make_unique<real::integer>(cvect{c}, min, max);
163 case domain_t::d_int:
164 return std::make_unique<integer::number>(cvect{c}, min, max);
165 default:
166 return nullptr;
167 }
168}
169
174std::size_t symbol_factory::args(const std::string &name) const
175{
176 const auto it(factory_.find(name));
177
178 return it == factory_.end() ? 1 : it->second.args;
179}
180
191bool symbol_factory::unregister_symbol(const std::string &name)
192{
193 return factory_.erase(name) == 1;
194}
195
196} // namespace vita
symbol_factory()
The factory is preloaded with a number of common symbols.
Definition: factory.cc:42
std::size_t args(const std::string &) const
Definition: factory.cc:174
bool unregister_symbol(const std::string &)
Unregister the symbol from the factory.
Definition: factory.cc:191
std::unique_ptr< symbol > make(const std::string &, cvect={0})
Creates a specific instance of a symbol.
Definition: factory.cc:113
The main namespace for the project.
domain_t
In an environment where a symbol such as '+' may have many different meanings, it's useful to specify...
Definition: value.h:34
std::size_t category_t
A category provide operations which supplement or supersede those of the domain but which are restric...
Definition: common.h:44