mslang.cpp 14 KB
Newer Older
kraus's avatar
kraus committed
1 2 3
#include "Algorithms/CoordinateSystemTrafo.h"
#include "Physics/Physics.h"

4 5 6 7
#include <boost/regex.hpp>

#include <iostream>
#include <string>
8 9
#include <fstream>
#include <streambuf>
10 11 12 13 14 15 16
#include <cstdlib>

std::string UDouble("([0-9]+\\.?[0-9]*([Ee][+-]?[0-9]+)?)");
std::string Double("(-?[0-9]+\\.?[0-9]*([Ee][+-]?[0-9]+)?)");
std::string UInt("([0-9]+)");
std::string FCall("([a-z]*)\\((.*)");

kraus's avatar
kraus committed
17 18
struct base;

19
struct function {
kraus's avatar
kraus committed
20 21
    virtual ~function() {};

22
    virtual void print(int indent) = 0;
kraus's avatar
kraus committed
23
    virtual void apply(std::vector<base*> &bfuncs) = 0;
24 25 26 27 28 29
};

typedef std::string::iterator iterator;

bool parse(iterator &it, const iterator &end, function* &fun);

kraus's avatar
kraus committed
30 31 32 33
struct base: public function {
    CoordinateSystemTrafo trafo;
    base():
        trafo(Vector_t(0.0),
34
              Quaternion(1.0, 0.0, 0.0, 0.0))
kraus's avatar
kraus committed
35 36 37 38 39 40
    { }

    virtual base* clone() = 0;
};

struct rectangle: public base {
41 42 43
    double width;
    double height;

kraus's avatar
kraus committed
44 45 46 47 48 49 50 51
    rectangle():
        base(),
        width(0.0),
        height(0.0)
    { }

    virtual ~rectangle() { }

52 53 54
    virtual void print(int indentwidth) {
        std::string indent(indentwidth, ' ');
        std::string indent2(indentwidth + 8, ' ');
kraus's avatar
kraus committed
55
        Vector_t origin = trafo.getOrigin();
56
        double angle = acos(trafo.getRotation()[0]) * 2 * Physics::rad2deg;
57 58
        std::cout << indent << "rectangle, \n"
                  << indent2 << "w: " << width << ", \n"
kraus's avatar
kraus committed
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
                  << indent2 << "h: " << height << ", \n"
                  << indent2 << "origin: " << origin[0] << ", " << origin[1] << ",\n"
                  << indent2 << "angle: " << angle;
    }

    virtual void apply(std::vector<base*> &bfuncs) {
        bfuncs.push_back(this->clone());
    }

    virtual base* clone() {
        rectangle *rect = new rectangle;
        rect->width = width;
        rect->height = height;
        rect->trafo = trafo;

        return rect;
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
    }

    static
    bool parse_detail(iterator &it, const iterator &end, function* fun) {
        std::string str(it, end);
        boost::regex argumentList(UDouble + "," + UDouble + "(\\).*)");
        boost::smatch what;

        if (!boost::regex_match(str, what, argumentList)) return false;

        rectangle *rect = static_cast<rectangle*>(fun);
        rect->width = atof(std::string(what[1]).c_str());
        rect->height = atof(std::string(what[3]).c_str());

        std::string fullMatch = what[0];
        std::string rest = what[5];
        it += (fullMatch.size() - rest.size() + 1);

        return true;

    }
};

kraus's avatar
kraus committed
98
struct ellipse: public base {
99 100 101
    double width;
    double height;

kraus's avatar
kraus committed
102 103 104 105 106 107 108
    ellipse():
        width(0.0),
        height(0.0)
    { }

    virtual ~ellipse() { }

109 110 111
    virtual void print(int indentwidth) {
        std::string indent(indentwidth, ' ');
        std::string indent2(indentwidth + 8, ' ');
kraus's avatar
kraus committed
112
        Vector_t origin = trafo.getOrigin();
113
        double angle = acos(trafo.getRotation()[0]) * 2 * Physics::rad2deg;
114 115
        std::cout << indent << "ellipse, \n"
                  << indent2 << "w: " << width << ", \n"
kraus's avatar
kraus committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
                  << indent2 << "h: " << height << ", \n"
                  << indent2 << "origin: " << origin[0] << ", " << origin[1] << ",\n"
                  << indent2 << "angle: " << angle;
    }

    virtual void apply(std::vector<base*> &bfuncs) {
        bfuncs.push_back(this->clone());
    }

    virtual base* clone() {
        ellipse *elps = new ellipse;
        elps->width = width;
        elps->height = height;
        elps->trafo = trafo;

        return elps;
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
    }

    static
    bool parse_detail(iterator &it, const iterator &end, function* fun) {
        std::string str(it, end);
        boost::regex argumentList(UDouble + "," + UDouble + "(\\).*)");
        boost::smatch what;

        if (!boost::regex_match(str, what, argumentList)) return false;

        ellipse *elps = static_cast<ellipse*>(fun);
        elps->width = atof(std::string(what[1]).c_str());
        elps->height = atof(std::string(what[3]).c_str());

        std::string fullMatch = what[0];
        std::string rest = what[5];
        it += (fullMatch.size() - rest.size() + 1);

        return true;

    }
};

struct repeat: public function {
    function* func;
    unsigned int N;
    double shiftx;
    double shifty;

kraus's avatar
kraus committed
161 162 163 164
    virtual ~repeat() {
        delete func;
    }

165 166 167 168 169 170 171 172 173
    virtual void print(int indentwidth) {
        std::string indent(indentwidth, ' ');
        std::string indent2(indentwidth + 8, ' ');
        std::cout << indent << "repeat, " << std::endl;
        func->print(indentwidth + 8);
        std::cout << ",\n"
                  << indent2 << "N: " << N << ", \n"
                  << indent2 << "dx: " << shiftx << ", \n"
                  << indent2 << "dy: " << shifty;
kraus's avatar
kraus committed
174 175 176 177
    }

    virtual void apply(std::vector<base*> &bfuncs) {
        CoordinateSystemTrafo shift(Vector_t(shiftx, shifty, 0.0),
178
                                    Quaternion(1.0, 0.0, 0.0, 0.0));
kraus's avatar
kraus committed
179 180 181 182 183 184 185 186 187 188 189 190 191
        func->apply(bfuncs);
        const unsigned int size = bfuncs.size();

        CoordinateSystemTrafo current_shift = shift;
        for (unsigned int i = 0; i < N; ++ i) {
            for (unsigned int j = 0; j < size; ++ j) {
                base *obj = bfuncs[j]->clone();
                obj->trafo = current_shift * obj->trafo;
                bfuncs.push_back(obj);
            }

            current_shift *= shift;
        }
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
    }

    static
    bool parse_detail(iterator &it, const iterator &end, function* &fun) {
        repeat *rep = static_cast<repeat*>(fun);
        if (!parse(it, end, rep->func)) return false;

        boost::regex argumentList("," + UInt + "," + Double + "," + Double + "\\)(.*)");
        boost::smatch what;

        std::string str(it, end);
        if (!boost::regex_match(str, what, argumentList)) return false;

        rep->N = atof(std::string(what[1]).c_str());
        rep->shiftx = atof(std::string(what[2]).c_str());
        rep->shifty = atof(std::string(what[4]).c_str());

        std::string fullMatch = what[0];
        std::string rest = what[6];

        it += (fullMatch.size() - rest.size());

        return true;
    }
};

struct translate: public function {
    function* func;
    double shiftx;
    double shifty;

kraus's avatar
kraus committed
223 224 225 226
    virtual ~translate() {
        delete func;
    }

227 228 229 230 231 232 233 234
    virtual void print(int indentwidth) {
        std::string indent(indentwidth, ' ');
        std::string indent2(indentwidth + 8, ' ');
        std::cout << indent << "translate, " << std::endl;
        func->print(indentwidth + 8);
        std::cout << ",\n"
                  << indent2 << "dx: " << shiftx << ", \n"
                  << indent2 << "dy: " << shifty;
kraus's avatar
kraus committed
235 236 237 238
    }

    virtual void apply(std::vector<base*> &bfuncs) {
        CoordinateSystemTrafo shift(Vector_t(shiftx, shifty, 0.0),
239
                                    Quaternion(1.0, 0.0, 0.0, 0.0));
kraus's avatar
kraus committed
240 241 242 243 244
        func->apply(bfuncs);
        const unsigned int size = bfuncs.size();

        for (unsigned int j = 0; j < size; ++ j) {
            base *obj = bfuncs[j];
245
            CoordinateSystemTrafo trafo = obj->trafo;
kraus's avatar
kraus committed
246 247
            obj->trafo = shift * obj->trafo;
        }
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
    }

    static
    bool parse_detail(iterator &it, const iterator &end, function* &fun) {
        translate *trans = static_cast<translate*>(fun);
        if (!parse(it, end, trans->func)) return false;

        boost::regex argumentList("," + Double + "," + Double + "\\)(.*)");
        boost::smatch what;

        std::string str(it, end);
        if (!boost::regex_match(str, what, argumentList)) return false;

        trans->shiftx = atof(std::string(what[1]).c_str());
        trans->shifty = atof(std::string(what[3]).c_str());

        std::string fullMatch = what[0];
        std::string rest = what[5];

        it += (fullMatch.size() - rest.size());

        return true;
    }
};


struct rotate: public function {
    function* func;
    double angle;

kraus's avatar
kraus committed
278 279 280 281
    virtual ~rotate() {
        delete func;
    }

282 283 284 285 286 287 288
    virtual void print(int indentwidth) {
        std::string indent(indentwidth, ' ');
        std::string indent2(indentwidth + 8, ' ');
        std::cout << indent << "rotate, " << std::endl;
        func->print(indentwidth + 8);
        std::cout << ",\n"
                  << indent2 << "angle: " << angle;
kraus's avatar
kraus committed
289 290 291 292
    }

    virtual void apply(std::vector<base*> &bfuncs) {
        CoordinateSystemTrafo rotation(Vector_t(0.0, 0.0, 0.0),
293
                                       Quaternion(cos(0.5 * angle), 0.0, 0.0, sin(0.5 * angle)));
kraus's avatar
kraus committed
294 295 296 297 298 299 300
        func->apply(bfuncs);
        const unsigned int size = bfuncs.size();

        for (unsigned int j = 0; j < size; ++ j) {
            base *obj = bfuncs[j];
            obj->trafo = rotation * obj->trafo;
        }
301 302 303 304 305 306
    }

    static
    bool parse_detail(iterator &it, const iterator &end, function* &fun) {
        rotate *rot = static_cast<rotate*>(fun);
        if (!parse(it, end, rot->func)) return false;
kraus's avatar
kraus committed
307 308
        std::cout << std::string(it, end) << std::endl;
        boost::regex argumentList("," + Double + "\\)(.*)");
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
        boost::smatch what;

        std::string str(it, end);
        if (!boost::regex_match(str, what, argumentList)) return false;

        rot->angle = atof(std::string(what[1]).c_str());

        std::string fullMatch = what[0];
        std::string rest = what[3];

        it += (fullMatch.size() - rest.size());

        return true;
    }
};

struct unionf: public function {
    std::vector<function*> funcs;

kraus's avatar
kraus committed
328 329 330 331 332 333
    virtual ~unionf () {
        for (function* func: funcs) {
            delete func;
        }
    }

334 335 336 337 338 339 340 341
    virtual void print(int indentwidth) {
        std::string indent(indentwidth, ' ');
        std::string indent2(indentwidth + 8, ' ');
        std::string indent3(indentwidth + 16, ' ');
        std::cout << indent << "union, " << std::endl;
        std::cout << indent2 << "funcs: {\n";
        funcs.front()->print(indentwidth + 16);
        for (unsigned int i = 1; i < funcs.size(); ++ i) {
kraus's avatar
kraus committed
342 343
            std::cout << "\n"
                      << indent3 << "," << std::endl;
344 345 346
            funcs[i]->print(indentwidth + 16);
        }
        std::cout << "\n"
kraus's avatar
kraus committed
347 348 349 350 351
                  << indent2 << "} ";
    }

    virtual void apply(std::vector<base*> &bfuncs) {
        for (unsigned int i = 0; i < funcs.size(); ++ i) {
kraus's avatar
kraus committed
352
            std::vector<base*> children;
kraus's avatar
kraus committed
353
            function *func = funcs[i];
354
            func->apply(children);
kraus's avatar
kraus committed
355
            bfuncs.insert(bfuncs.end(), children.begin(), children.end());
kraus's avatar
kraus committed
356
        }
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438
    }

    static
    bool parse_detail(iterator &it, const iterator &end, function* &fun) {
        unionf *unin = static_cast<unionf*>(fun);
        unin->funcs.push_back(NULL);
        if (!parse(it, end, unin->funcs.back())) return false;

        boost::regex argumentList("(,[a-z]+\\(.*)");
        boost::regex endParenthesis("\\)(.*)");
        boost::smatch what;

        std::string str(it, end);
        while (boost::regex_match(str, what, argumentList)) {
            iterator it2 = it + 1;
            unin->funcs.push_back(NULL);

            if (!parse(it2, end, unin->funcs.back())) return false;

            it = it2;
            str = std::string(it, end);
        }

        str = std::string(it, end);
        if (!boost::regex_match(str, what, endParenthesis)) return false;

        std::string fullMatch = what[0];
        std::string rest = what[1];

        it += (fullMatch.size() - rest.size());

        return true;
    }
};

bool parse(std::string str, function* &fun) {
    str = boost::regex_replace(str, boost::regex("\\s"), std::string(""), boost::match_default | boost::format_all);
    iterator it = str.begin();
    iterator end = str.end();
    if (!parse(it, end, fun)) {
        std::cout << "parsing failed here:" << std::string(it, end) << std::endl;
        return false;
    }

    return true;
}

bool parse(iterator &it, const iterator &end, function* &fun) {
    boost::regex functionCall(FCall);
    boost::smatch what;

    std::string str(it, end);
    if( !boost::regex_match(str , what, functionCall ) ) return false;

    std::string identifier = what[1];
    std::string arguments = what[2];
    unsigned int shift = identifier.size() + 1;

    if (identifier == "rectangle") {
        fun = new rectangle;
        iterator it2 = it + shift;
        if (!rectangle::parse_detail(it2, end, fun)) return false;

        it = it2;
        return true;
    } else if (identifier == "ellipse") {
        fun = new ellipse;
        iterator it2 = it + shift;
        if (!ellipse::parse_detail(it2, end, fun)) return false;

        it = it2;
        return true;
    } else if (identifier == "repeat") {
        fun = new repeat;
        iterator it2 = it + shift;
        if (!repeat::parse_detail(it2, end, fun)) return false;

        it = it2;

        return true;
    } else if (identifier == "rotate") {
        fun = new rotate;
kraus's avatar
kraus committed
439 440 441
        // iterator it2 =
            it += shift;
        if (!rotate::parse_detail(it, end, fun)) return false;
442

kraus's avatar
kraus committed
443
        // it = it2;
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468

        return true;
    } else if (identifier == "translate") {
        fun = new translate;
        iterator it2 = it + shift;
        if (!translate::parse_detail(it2, end, fun)) return false;

        it = it2;

        return true;
    } else if (identifier == "union") {
        fun = new unionf;
        iterator it2 = it + shift;
        if (!unionf::parse_detail(it2, end, fun)) return false;

        it = it2;

        return true;
    }


    return (it == end);

}

469
int main(int argc, char *argv[])
470
{
471 472 473 474
    if (argc < 2) {
        std::cout << "please provide the name of the file that contains your code" << std::endl;
        return 1;
    }
475
    function *fun;
476 477 478 479 480 481

    std::ifstream t(argv[1]);
    std::string str((std::istreambuf_iterator<char>(t)),
                    std::istreambuf_iterator<char>());

    // std::string str("repeat( translate(union(rectangle(0.1, 0.1), ellipse(0.1, 0.1)), -0.01, -0.02), 2, 0.1, 0.2)");
482 483 484

    if (parse(str, fun)) {
        fun->print(0);
kraus's avatar
kraus committed
485 486 487 488 489 490 491 492 493 494 495 496 497
        std::cout << "\n" << std::endl;

        std::vector<base*> baseBlocks;
        fun->apply(baseBlocks);

        for (base* bfun: baseBlocks) {
            bfun->print(0);
            std::cout << std::endl;
        }

        for (base* func: baseBlocks) {
            delete func;
        }
498
    }
kraus's avatar
kraus committed
499 500 501

    delete fun;

502 503
    return 0;
}