|
| 1 | +#include "expr.h" |
| 2 | +#include "interpreter.h" |
| 3 | +#include <geogram/basic/string.h> |
| 4 | + |
| 5 | +extern "C" { |
| 6 | +#include <geogram/third_party/lua/lauxlib.h> |
| 7 | +#include <geogram/third_party/lua/lualib.h> |
| 8 | +} |
| 9 | + |
| 10 | + |
| 11 | + |
| 12 | +namespace GEO { |
| 13 | + |
| 14 | + namespace String { |
| 15 | + std::string to_string(Expr* e) { |
| 16 | + return e->atomic() ? e->to_string() : "(" + e->to_string() + ")"; |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + /**************************************************************************/ |
| 21 | + |
| 22 | + Expr::~Expr() { } |
| 23 | + |
| 24 | + std::string Expr::name() const { |
| 25 | + std::string result; |
| 26 | + |
| 27 | + // Try to find this variable in Lua's global scope, |
| 28 | + // if found, use its name. |
| 29 | + lua_State* L = Interpreter::instance()->lua_state(); |
| 30 | + lua_pushglobaltable(L); |
| 31 | + int index = lua_gettop(L); |
| 32 | + lua_pushnil(L); |
| 33 | + while(lua_next(L,index) != 0) { |
| 34 | + if(lua_isexpr(L,-1) && lua_toexpr(L,-1) == this) { |
| 35 | + result = std::string(lua_tostring(L,-2)); |
| 36 | + |
| 37 | + } |
| 38 | + lua_pop(L,1); |
| 39 | + } |
| 40 | + |
| 41 | + // If not found in Lua's global scope, generate a unique ID |
| 42 | + // from this Expr's address. |
| 43 | + if(result.length() == 0) { |
| 44 | + result = "expr@" + String::to_string(this); |
| 45 | + } |
| 46 | + return result; |
| 47 | + } |
| 48 | + |
| 49 | + /**************************************************************************/ |
| 50 | + |
| 51 | + Expr::Type Constant::type() const { |
| 52 | + return SCALAR; |
| 53 | + } |
| 54 | + |
| 55 | + index_t Constant::dim() const { |
| 56 | + return 1; |
| 57 | + } |
| 58 | + |
| 59 | + std::string Constant::to_string() const { |
| 60 | + return String::to_string(value()); |
| 61 | + } |
| 62 | + |
| 63 | + bool Constant::atomic() const { |
| 64 | + return true; |
| 65 | + } |
| 66 | + |
| 67 | + /**************************************************************************/ |
| 68 | + |
| 69 | + Expr::Type Variable::type() const { |
| 70 | + return type_; |
| 71 | + } |
| 72 | + |
| 73 | + bool Variable::atomic() const { |
| 74 | + return true; |
| 75 | + } |
| 76 | + |
| 77 | + /**************************************************************************/ |
| 78 | + |
| 79 | + index_t ScalarVar::dim() const { |
| 80 | + return 1; |
| 81 | + } |
| 82 | + |
| 83 | + std::string ScalarVar::to_string() const { |
| 84 | + // return name() + ":scalar"; |
| 85 | + return name(); |
| 86 | + } |
| 87 | + |
| 88 | + /**************************************************************************/ |
| 89 | + |
| 90 | + index_t VectorVar::default_dim_ = 3; |
| 91 | + |
| 92 | + index_t VectorVar::dim() const { |
| 93 | + return dim_ == index_t(-1) ? default_dim_ : dim_; |
| 94 | + } |
| 95 | + |
| 96 | + std::string VectorVar::to_string() const { |
| 97 | + std::string result = name(); |
| 98 | + /* |
| 99 | + result += ":vec"; |
| 100 | + if(dim_ != index_t(-1)) { |
| 101 | + result += ("[" + String::to_string(dim_) + "]"); |
| 102 | + } |
| 103 | + */ |
| 104 | + return result; |
| 105 | + } |
| 106 | + |
| 107 | + /**************************************************************************/ |
| 108 | + |
| 109 | + std::string VectorComponent::to_string() const { |
| 110 | + return String::to_string(vector()) + "[" + String::to_string(component_) + "]"; |
| 111 | + } |
| 112 | + |
| 113 | + bool VectorComponent::atomic() const { |
| 114 | + return true; |
| 115 | + } |
| 116 | + |
| 117 | + Expr::Type VectorComponent::type() const { |
| 118 | + return SCALAR; |
| 119 | + } |
| 120 | + |
| 121 | + index_t VectorComponent::dim() const { |
| 122 | + return 1; |
| 123 | + } |
| 124 | + |
| 125 | + /**************************************************************************/ |
| 126 | + |
| 127 | + index_t SignVar::dim() const { |
| 128 | + return 0; |
| 129 | + } |
| 130 | + |
| 131 | + std::string SignVar::to_string() const { |
| 132 | + return name() + ":sign"; |
| 133 | + } |
| 134 | + |
| 135 | + /**************************************************************************/ |
| 136 | + |
| 137 | + Expr::Type Sum::type() const { |
| 138 | + return type_; |
| 139 | + } |
| 140 | + |
| 141 | + index_t Sum::dim() const { |
| 142 | + return dim_; |
| 143 | + } |
| 144 | + |
| 145 | + std::string Sum::to_string() const { |
| 146 | + std::string result; |
| 147 | + for(index_t i=0; i<nb_terms(); ++i) { |
| 148 | + result += String::to_string(ith_term(i)); |
| 149 | + if(i != nb_terms()-1) { |
| 150 | + result += " + "; |
| 151 | + } |
| 152 | + } |
| 153 | + return result; |
| 154 | + } |
| 155 | + |
| 156 | + void Sum::add_term(Expr* term) { |
| 157 | + { |
| 158 | + Sum* sum = dynamic_cast<Sum*>(term); |
| 159 | + if(sum != nullptr) { |
| 160 | + for(index_t i=0; i<sum->nb_terms(); ++i) { |
| 161 | + add_term(sum->ith_term(i)); |
| 162 | + } |
| 163 | + return; |
| 164 | + } |
| 165 | + } |
| 166 | + { |
| 167 | + Constant* co1 = dynamic_cast<Constant*>(term); |
| 168 | + if(co1 != nullptr) { |
| 169 | + if(co1->value() == 0.0) { |
| 170 | + return; |
| 171 | + } |
| 172 | + for(index_t i=0; i<nb_terms(); ++i) { |
| 173 | + Constant* co2 = dynamic_cast<Constant*>(ith_term(i)); |
| 174 | + if(co2 != nullptr) { |
| 175 | + terms_[i] = new Constant(co1->value() + co2->value()); |
| 176 | + return; |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | + } |
| 181 | + terms_.push_back(term); |
| 182 | + } |
| 183 | + |
| 184 | + bool Sum::atomic() const { |
| 185 | + return false; |
| 186 | + } |
| 187 | + |
| 188 | + /**************************************************************************/ |
| 189 | + |
| 190 | + Expr::Type Product::type() const { |
| 191 | + return type_; |
| 192 | + } |
| 193 | + |
| 194 | + index_t Product::dim() const { |
| 195 | + return dim_; |
| 196 | + } |
| 197 | + |
| 198 | + std::string Product::to_string() const { |
| 199 | + std::string result; |
| 200 | + for(index_t i=0; i<nb_factors(); ++i) { |
| 201 | + result += String::to_string(ith_factor(i)); |
| 202 | + if(i != nb_factors()-1) { |
| 203 | + result += " * "; |
| 204 | + } |
| 205 | + } |
| 206 | + return result; |
| 207 | + } |
| 208 | + |
| 209 | + void Product::mult_factor(Expr* factor) { |
| 210 | + { |
| 211 | + for(index_t i=0; i<nb_factors(); ++i) { |
| 212 | + if(factor == ith_factor(i)) { |
| 213 | + factors_[i] = new Pow(factor,2); |
| 214 | + return; |
| 215 | + } |
| 216 | + Pow* pow = dynamic_cast<Pow*>(ith_factor(i)); |
| 217 | + if(pow != nullptr && pow->arg() == factor) { |
| 218 | + factors_[i] = new Pow(factor,pow->exponent()+1); |
| 219 | + return; |
| 220 | + } |
| 221 | + } |
| 222 | + } |
| 223 | + { |
| 224 | + Product* prod = dynamic_cast<Product*>(factor); |
| 225 | + if(prod != nullptr) { |
| 226 | + for(index_t i=0; i<prod->nb_factors(); ++i) { |
| 227 | + mult_factor(prod->ith_factor(i)); |
| 228 | + } |
| 229 | + return; |
| 230 | + } |
| 231 | + } |
| 232 | + { |
| 233 | + Constant* co1 = dynamic_cast<Constant*>(factor); |
| 234 | + if(co1 != nullptr) { |
| 235 | + if(co1->value() == 1.0) { |
| 236 | + return; |
| 237 | + } |
| 238 | + for(index_t i=0; i<nb_factors(); ++i) { |
| 239 | + Constant* co2 = dynamic_cast<Constant*>(ith_factor(i)); |
| 240 | + if(co2 != nullptr) { |
| 241 | + factors_[i] = new Constant(co1->value() * co2->value()); |
| 242 | + return; |
| 243 | + } |
| 244 | + } |
| 245 | + } |
| 246 | + } |
| 247 | + factors_.push_back(factor); |
| 248 | + } |
| 249 | + |
| 250 | + bool Product::atomic() const { |
| 251 | + return false; |
| 252 | + } |
| 253 | + |
| 254 | + /**************************************************************************/ |
| 255 | + |
| 256 | + Expr::Type Pow::type() const { |
| 257 | + return type_; |
| 258 | + } |
| 259 | + |
| 260 | + index_t Pow::dim() const { |
| 261 | + return dim_; |
| 262 | + } |
| 263 | + |
| 264 | + std::string Pow::to_string() const { |
| 265 | + return String::to_string(arg()) + "^" + String::to_string(exponent()); |
| 266 | + } |
| 267 | + |
| 268 | + bool Pow::atomic() const { |
| 269 | + return false; |
| 270 | + } |
| 271 | + |
| 272 | + /**************************************************************************/ |
| 273 | +} |
0 commit comments