variant.hpp 32 KB
Newer Older
Victor Yacovlev's avatar
Victor Yacovlev committed
1 2 3
#ifndef VARIANT_HPP
#define VARIANT_HPP

4

Victor Yacovlev's avatar
Victor Yacovlev committed
5 6 7 8 9 10 11 12
#ifdef DO_NOT_DECLARE_STATIC
#define DO_NOT_DECLARE_STATIC_VARIANT
#endif

#define DO_NOT_DECLARE_STATIC
#include "stdlib/kumirstdlib.hpp"
#include "vm_enums.h"

13 14 15 16




Victor Yacovlev's avatar
Victor Yacovlev committed
17 18 19 20 21 22 23
namespace VM {

using Kumir::Char;
using Kumir::String;
using Kumir::real;
using namespace Bytecode;

Victor Yacovlev's avatar
Victor Yacovlev committed
24
typedef std::vector< class AnyValue > Record;
25

Victor Yacovlev's avatar
Victor Yacovlev committed
26 27 28 29 30
class AnyValue
{
    friend class Variable;
public:
    inline explicit AnyValue() { __init__(); }
Victor Yacovlev's avatar
Victor Yacovlev committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
    inline AnyValue(const AnyValue & other) {
        __init__();
        tp = other.tp;
        if (other.svalue) {
            svalue = new String(*(other.svalue));
        }
        if (other.uvalue) {
            uvalue = new Record(*(other.uvalue));
        }
        if (other.avalue) {
            avalue = new std::vector<class AnyValue>(*(other.avalue));
        }
        if (tp==VT_int)
            ivalue = other.ivalue;
        if (tp==VT_real)
            rvalue = other.rvalue;
        if (tp==VT_bool)
            bvalue = other.bvalue;
        if (tp==VT_char)
            cvalue = other.cvalue;
    }

Victor Yacovlev's avatar
Victor Yacovlev committed
53
    inline explicit AnyValue(ValueType t) { __init__(); tp = t;  svalue = t==VT_string? new String() : 0; ivalue = 0; }
Victor Yacovlev's avatar
Victor Yacovlev committed
54 55 56 57 58
    inline explicit AnyValue(int v) {
        __init__();
        tp = VT_int;
        ivalue = v;
    }
Victor Yacovlev's avatar
Victor Yacovlev committed
59 60 61
    inline explicit AnyValue(real v) { __init__(); tp = VT_real;  rvalue = v; }
    inline explicit AnyValue(bool v) { __init__(); tp = VT_bool; bvalue = v; }
    inline explicit AnyValue(Char v) { __init__(); tp = VT_char; cvalue = v; }
Victor Yacovlev's avatar
Victor Yacovlev committed
62 63 64 65 66 67
    inline explicit AnyValue(const String & v) { __init__(); tp = VT_string; svalue = new String(v); }
    inline explicit AnyValue(const Record & value) {
        __init__();
        tp = VT_record;
        uvalue = new Record(value);
    }
Victor Yacovlev's avatar
Victor Yacovlev committed
68 69 70 71 72 73 74

    inline void operator=(ValueType t) { __init__(); tp = t;  svalue = t==VT_string? new String() : 0; }
    inline void operator=(int v) { __init__(); tp = VT_int;  ivalue = v; }
    inline void operator=(real v) { __init__(); tp = VT_real; rvalue = v; }
    inline void operator=(bool v) { __init__(); tp = VT_bool; bvalue = v; }
    inline void operator=(Char v) { __init__(); tp = VT_char; cvalue = v; }
    inline void operator=(const String & v) { __init__(); tp = VT_string; svalue = new String(v); }
Victor Yacovlev's avatar
Victor Yacovlev committed
75
    inline void operator=(const Record & value) {
76
        __init__();
Victor Yacovlev's avatar
Victor Yacovlev committed
77
        tp = VT_record;
Victor Yacovlev's avatar
Victor Yacovlev committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
        uvalue = new Record(value);
    }
    inline void operator=(const AnyValue &other) {
        __init__();
        tp = other.tp;
        if (other.svalue) {
            svalue = new String(*(other.svalue));
        }
        if (other.uvalue) {
            uvalue = new Record(*(other.uvalue));
        }
        if (other.avalue) {
            avalue = new std::vector<class AnyValue>(*(other.avalue));
        }
        if (tp==VT_int)
            ivalue = other.ivalue;
        if (tp==VT_real)
            rvalue = other.rvalue;
        if (tp==VT_bool)
            bvalue = other.bvalue;
        if (tp==VT_char)
            cvalue = other.cvalue;
100
    }
Victor Yacovlev's avatar
Victor Yacovlev committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130

    inline int toInt() const {
        if (tp==VT_bool) return bvalue? 1 : 0;
        else if (tp==VT_char) return static_cast<int>(cvalue);
        else return ivalue;
    }
    inline real toReal() const {
        if (tp==VT_bool || tp==VT_int) return static_cast<real>(toInt());
        else return rvalue;
    }
    inline bool toBool() const {
        if (tp==VT_int) return ivalue!=0;
        else if (tp==VT_real) return rvalue!=0.0;
        else return bvalue;
    }
    inline Char toChar() const {
        if (tp==VT_int) return static_cast<Char>(ivalue);
        else if (tp==VT_string && svalue && svalue->length()==1) return svalue->at(0);
        else return cvalue;
    }
    inline String toString() const {
        if (tp==VT_int) return Kumir::Converter::sprintfInt(ivalue, 10, 0, 0);
        else if (tp==VT_real) return Kumir::Converter::sprintfReal(rvalue, '.', false, 0, 0, 0);
        else if (tp==VT_bool) return bvalue? Kumir::Core::fromUtf8("да") : Kumir::Core::fromUtf8("нет");
        else if (tp==VT_char) {
            String sval;
            sval.push_back(cvalue);
            return sval;
        }
        else if (tp==VT_void) return String();
Victor Yacovlev's avatar
Victor Yacovlev committed
131
        else return String(*svalue);
Victor Yacovlev's avatar
Victor Yacovlev committed
132
    }
Victor Yacovlev's avatar
Victor Yacovlev committed
133
    inline const Record & toRecord() const {
Victor Yacovlev's avatar
Victor Yacovlev committed
134
        return *uvalue;
Victor Yacovlev's avatar
Victor Yacovlev committed
135 136
    }
    inline Record & toRecord() {
Victor Yacovlev's avatar
Victor Yacovlev committed
137
        return *uvalue;
138
    }
Victor Yacovlev's avatar
Victor Yacovlev committed
139

Victor Yacovlev's avatar
Victor Yacovlev committed
140 141 142 143 144 145 146 147 148 149 150
    template <typename T>
    inline T toUserValue() const {
        size_t fields = strlen(T::_());
        T value;
        void * ptr = &value;
        for (size_t i=0; i<fields; i++) {
            void * fieldptr = 0;
            size_t copySize = 0;
            char tp = T::_()[i];
            switch (tp) {
            case 'i': {
Victor Yacovlev's avatar
Victor Yacovlev committed
151
                int val = uvalue->at(i).toInt();
Victor Yacovlev's avatar
Victor Yacovlev committed
152 153
                fieldptr = &val;
                copySize = sizeof(int);
Denis Khachko's avatar
Denis Khachko committed
154 155
                ptr = (void*)((size_t)(ptr)+copySize);
             //   ptr +=(uint)copySize;
Victor Yacovlev's avatar
Victor Yacovlev committed
156 157 158 159
                memcpy(ptr, fieldptr, copySize);
                break;
            }
            case 'd': {
Victor Yacovlev's avatar
Victor Yacovlev committed
160
                double val = uvalue->at(i).toReal();
Victor Yacovlev's avatar
Victor Yacovlev committed
161
                fieldptr = &val;
Denis Khachko's avatar
Denis Khachko committed
162 163
                copySize = sizeof(double);
                ptr = (void*)((size_t)(ptr)+copySize);
Victor Yacovlev's avatar
Victor Yacovlev committed
164 165 166 167
                memcpy(ptr, fieldptr, copySize);
                break;
            }
            case 'b': {
Victor Yacovlev's avatar
Victor Yacovlev committed
168
                bool val = uvalue->at(i).toBool();
Victor Yacovlev's avatar
Victor Yacovlev committed
169
                fieldptr = &val;
Denis Khachko's avatar
Denis Khachko committed
170 171
                copySize = sizeof(bool);
                ptr = (void*)((size_t)(ptr)+copySize);
Victor Yacovlev's avatar
Victor Yacovlev committed
172 173 174 175
                memcpy(ptr, fieldptr, copySize);
                break;
            }
            case 'c': {
Victor Yacovlev's avatar
Victor Yacovlev committed
176
                Char val = uvalue->at(i).toChar();
Victor Yacovlev's avatar
Victor Yacovlev committed
177 178
                fieldptr = &val;
                copySize = sizeof(Char);
Denis Khachko's avatar
Denis Khachko committed
179 180

                ptr = (void*)((size_t)(ptr)+copySize);
Victor Yacovlev's avatar
Victor Yacovlev committed
181 182 183
                break;
            }
            case 's': {
Victor Yacovlev's avatar
Victor Yacovlev committed
184
                String val = uvalue->at(i).toString();
Victor Yacovlev's avatar
Victor Yacovlev committed
185
                fieldptr = &val;
Denis Khachko's avatar
Denis Khachko committed
186 187
                copySize = sizeof(String);
                ptr = (void*)((size_t)(ptr)+copySize);
Victor Yacovlev's avatar
Victor Yacovlev committed
188 189 190 191 192 193 194 195 196 197 198
                String * stringRef = reinterpret_cast<String*>(fieldptr);
                stringRef->assign(val); // copy container data
                break;
            }
            default:
                break;
            }
        }
        return value;
    }

Victor Yacovlev's avatar
Victor Yacovlev committed
199
    inline bool isValid() const { return tp!=VT_void || ( avalue && avalue->size()>0 ); }
Victor Yacovlev's avatar
Victor Yacovlev committed
200 201

    inline ValueType type() const { return tp; }
Victor Yacovlev's avatar
Victor Yacovlev committed
202
    inline const AnyValue & at(size_t index) const { return avalue->at(index); }
Victor Yacovlev's avatar
Victor Yacovlev committed
203
    inline const AnyValue & operator[](size_t index) const { return at(index); }
Victor Yacovlev's avatar
Victor Yacovlev committed
204
    inline AnyValue & at(size_t index) { return avalue->at(index); }
Victor Yacovlev's avatar
Victor Yacovlev committed
205 206
    inline AnyValue & operator[](size_t index) { return at(index); }

Victor Yacovlev's avatar
Victor Yacovlev committed
207 208 209 210 211 212 213 214 215 216 217 218 219
    inline size_t rawSize() const { return avalue? avalue->size() : 0; }
    inline ~AnyValue() {
        if (svalue)
            delete svalue;
        if (avalue) {
            avalue->clear();
            delete avalue;
        }
        if (uvalue) {
            uvalue->clear();
            delete uvalue;
        }
    }
Victor Yacovlev's avatar
Victor Yacovlev committed
220 221 222 223 224


protected:

    inline void resize(size_t size) {
Victor Yacovlev's avatar
Victor Yacovlev committed
225 226
        if (!avalue)
            avalue = new std::vector<class AnyValue>(size);
Victor Yacovlev's avatar
Victor Yacovlev committed
227
        if (size==0) {
Victor Yacovlev's avatar
Victor Yacovlev committed
228 229
            if (avalue->size())
                avalue->clear();
Victor Yacovlev's avatar
Victor Yacovlev committed
230 231
        }
        else {
Victor Yacovlev's avatar
Victor Yacovlev committed
232 233 234
            if (size != avalue->size()) {
                size_t asize = avalue->size();
                avalue->resize(size);
Victor Yacovlev's avatar
Victor Yacovlev committed
235 236 237 238 239 240 241 242 243
            }
        }
    }

private:
    inline void __init__() {
        tp = VT_void;
        svalue = 0;
        ivalue = 0;
Victor Yacovlev's avatar
Victor Yacovlev committed
244 245
        uvalue = 0;
        avalue = 0;
Victor Yacovlev's avatar
Victor Yacovlev committed
246 247 248 249 250 251 252 253 254
    }

    ValueType tp;
    union {
        int ivalue;
        real rvalue;
        Char cvalue;
        bool bvalue;
    };
Victor Yacovlev's avatar
Victor Yacovlev committed
255
    Record * uvalue;
Victor Yacovlev's avatar
Victor Yacovlev committed
256
    String * svalue;
Victor Yacovlev's avatar
Victor Yacovlev committed
257
    std::vector<class AnyValue> * avalue;
Victor Yacovlev's avatar
Victor Yacovlev committed
258 259 260
};


261

Victor Yacovlev's avatar
Victor Yacovlev committed
262 263 264 265 266 267 268 269
class Variable
{
public:

    inline explicit Variable() { create(); }

    inline void create()
    {
Victor Yacovlev's avatar
Victor Yacovlev committed
270 271 272
        l_referenceIndeces[0] = l_referenceIndeces[1] = l_referenceIndeces[2] =         l_referenceIndeces[3] = 0;
        l_bounds[0] = l_bounds[1] = l_bounds[2] = l_bounds[3] = l_bounds[4] = l_bounds[5] = l_bounds[6] = 0;
        l_restrictedBounds[0] = l_restrictedBounds[1] = l_restrictedBounds[2] = l_restrictedBounds[3] = l_restrictedBounds[4] = l_restrictedBounds[5] = l_restrictedBounds[6] = 0;
Victor Yacovlev's avatar
Victor Yacovlev committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286
        m_value = VT_void;
        i_dimension = 0;
        e_baseType = VT_void;
        m_reference = 0;
        b_constant = false;
    }

    static bool ignoreUndefinedError;

    inline explicit Variable(int v) { create() ; e_baseType = VT_int; m_value = v; }
    inline explicit Variable(double v) { create(); e_baseType = VT_real; m_value = v; }
    inline explicit Variable(Char & v) { create(); e_baseType = VT_char; m_value = v; }
    inline explicit Variable(const String & v) { create(); e_baseType = VT_string; m_value = v; }
    inline explicit Variable(bool v) { create(); e_baseType = VT_bool; m_value = v; }
Victor Yacovlev's avatar
Victor Yacovlev committed
287
    inline explicit Variable(const Record & value) {
288
        create();
Victor Yacovlev's avatar
Victor Yacovlev committed
289
        e_baseType = VT_record;
290 291
        m_value = value;
    }
Victor Yacovlev's avatar
Victor Yacovlev committed
292 293
    inline explicit Variable(Variable * ref) { create(); m_reference = ref; }
    inline explicit Variable(const AnyValue & v) { create(); e_baseType = v.type(); m_value = v; }
Victor Yacovlev's avatar
Victor Yacovlev committed
294 295 296 297 298 299 300 301 302 303 304 305 306
//    inline Variable(const Variable & other) {
//        create();
//        e_baseType = other.e_baseType;
//        i_dimension = other.i_dimension;
//        memcpy(l_bounds, other.l_bounds, sizeof(l_bounds));
//        memcpy(l_restrictedBounds, other.l_restrictedBounds, sizeof(l_restrictedBounds));
//        memcpy(l_referenceIndeces, other.l_referenceIndeces, sizeof(l_referenceIndeces));
//        m_reference = other.m_reference;
//        b_constant = other.b_constant;
//        s_name = other.s_name;
//        s_algorhitmName = other.s_algorhitmName;
//        m_value = other.m_value;
//    }
Victor Yacovlev's avatar
Victor Yacovlev committed
307 308 309 310 311 312 313

    inline bool isValid() const { return e_baseType!=VT_void; }

    inline bool isConstant() const { return m_reference? m_reference->isConstant() : b_constant; }
    inline void setConstantFlag(bool value) { b_constant = value; }

    inline void init();
Victor Yacovlev's avatar
Victor Yacovlev committed
314
    inline uint8_t dimension() const { return m_reference? m_reference->dimension() : i_dimension; }
Victor Yacovlev's avatar
Victor Yacovlev committed
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
    inline void setDimension(uint8_t v) { i_dimension = v; }
    inline void setName(const String & n) {
        s_name = n;
    }
    inline String fullReferenceName() const;
    inline const String & name() const {
        if(m_reference)
            return m_reference->name();
        else
            return s_name;
    }
    inline const String & myName() const {
        return s_name;
    }
    inline void setAlgorhitmName(const String & n) {
        s_algorhitmName = n;
    }
    inline const String & algorhitmName() const {
        if(m_reference)
            return m_reference->algorhitmName();
        else
            return s_algorhitmName;
    }

    inline void setBounds(int bounds[7]);
    inline void updateBounds(int bounds[7]);
    inline void getBounds(/*out*/ int bounds[7]) const { memcpy(bounds, l_bounds, 7*sizeof(int)); }
    inline void getEffectiveBounds(/*out*/ int bounds[7]) const { memcpy(bounds, l_restrictedBounds, 7*sizeof(int)); }

    inline bool hasValue() const;
    inline bool hasValue(int indeces[4]) const;
    inline bool hasValue(int index0) const;
    inline bool hasValue(int index0, int index1) const;
    inline bool hasValue(int index0, int index1, int index2) const;

    inline AnyValue value() const;
    inline AnyValue value(int index0) const;
    inline AnyValue value(int index0, int index1) const;
    inline AnyValue value(int index0, int index1, int index2) const;

    inline AnyValue value(int indeces[4]) const;

    inline size_t rawSize() const { return m_value.rawSize(); }
Victor Yacovlev's avatar
Victor Yacovlev committed
358
    inline const AnyValue & at(size_t index) const { return m_value.avalue->at(index); }
Victor Yacovlev's avatar
Victor Yacovlev committed
359
    inline const AnyValue & operator[](size_t index) const { return at(index); }
Victor Yacovlev's avatar
Victor Yacovlev committed
360
    inline AnyValue & at(size_t index) { return m_value.avalue->at(index); }
Victor Yacovlev's avatar
Victor Yacovlev committed
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
    inline AnyValue & operator[](size_t index) { return at(index); }

    inline bool isReference() const { return m_reference!=0; }
    inline void setReference(Variable * r, int effectiveBounds[7]) {
        m_reference = r;
        memcpy(l_bounds, effectiveBounds, 7*sizeof(int));
        memcpy(l_restrictedBounds, effectiveBounds, 7*sizeof(int));
    }
    inline Variable * reference() { return m_reference; }

    inline void setReferenceIndeces(int v[4]) {
        memcpy(l_referenceIndeces, v, 4*sizeof(int));
    }
    inline void getReferenceIndeces(int result[4]) const {
        memcpy(result, l_referenceIndeces, 4*sizeof(int));
    }

    inline int toInt() const { return value().toInt(); }
    inline real toReal() const { return value().toReal(); }
    inline double toDouble() const { return toReal(); }
    inline bool toBool() const { return value().toBool(); }
    inline Char toChar() const { return value().toChar(); }
    inline String toString() const;
    inline String toString(int indeces[4]) const;
Victor Yacovlev's avatar
Victor Yacovlev committed
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
    inline const Record toRecord() const {
        if (m_reference) {
            const Record result = m_reference->toRecord();
            return result;
        }
        else {
            return m_value.toRecord();
        }
    }
    inline Record & toRecord() {
        if (m_reference) {
            Record & result = m_reference->toRecord();
            return result;
        }
        else {
            Record & result = m_value.toRecord();
            return result;
        }
    }
Victor Yacovlev's avatar
Victor Yacovlev committed
404

Victor Yacovlev's avatar
Victor Yacovlev committed
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
    inline Variable toReference();
    inline static Variable toConstReference(const AnyValue & value);
    inline Variable toReference(int indeces[4]);
    inline void setValue(const AnyValue & value);
    inline void setValue(int index0, const AnyValue & value);
    inline void setValue(int index0, int index1, const AnyValue & value);
    inline void setValue(int index0, int index1, int index2, const AnyValue & value);
    inline void setValue(int indeces[4], const AnyValue & value);

    inline void setConstValue(const Variable & ctab);

    inline ValueType baseType() const { return m_reference? m_reference->baseType() : e_baseType; }
    inline void setBaseType(ValueType vt) { e_baseType = vt; }

    inline static void unsetError() { Kumir::Core::unsetError(); }
private:
    inline size_t linearIndex(int a) const;
    inline size_t linearIndex(int a, int b) const;
    inline size_t linearIndex(int a, int b, int c) const;
    AnyValue m_value;
Victor Yacovlev's avatar
Victor Yacovlev committed
425 426 427 428 429 430
    uint8_t i_dimension;
    int l_bounds[7];
    int l_restrictedBounds[7];
    ValueType e_baseType;
    Variable * m_reference;
    int l_referenceIndeces[4];
Victor Yacovlev's avatar
Victor Yacovlev committed
431 432
    String s_name;
    String s_algorhitmName;
Victor Yacovlev's avatar
Victor Yacovlev committed
433
    bool b_constant;
Victor Yacovlev's avatar
Victor Yacovlev committed
434 435 436 437 438 439 440 441 442 443 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 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
};

/* ----------------------- IMPLEMENTATION ----------------------*/

bool Variable::hasValue() const
{
    if (m_reference) {
        if (l_referenceIndeces[3]==0)
            return m_reference->hasValue();
        else if (l_referenceIndeces[3]==1)
            return m_reference->hasValue(l_referenceIndeces[0]);
        else if (l_referenceIndeces[3]==2)
            return m_reference->hasValue(l_referenceIndeces[0], l_referenceIndeces[1]);
        else
            return m_reference->hasValue(l_referenceIndeces[0], l_referenceIndeces[1], l_referenceIndeces[2]);
    }
    else
        return m_value.isValid();
}


bool Variable::hasValue(int indeces[4]) const
{
    if (indeces[3]==1)
        return hasValue(indeces[0]);
    else if (indeces[3]==2)
        return hasValue(indeces[0], indeces[1]);
    else if (indeces[3]==3)
        return hasValue(indeces[0], indeces[1], indeces[2]);
    else
        return hasValue();
}

AnyValue Variable::value() const
{
    if (m_reference) {
        if (l_referenceIndeces[3]==0) {
            return m_reference->value();
        }
        else if (l_referenceIndeces[3]==1) {
            return m_reference->value(l_referenceIndeces[0]);
        }
        else if (l_referenceIndeces[3]==2) {
            return m_reference->value(l_referenceIndeces[0], l_referenceIndeces[1]);
        }
        else if (l_referenceIndeces[3]==3) {
            return m_reference->value(l_referenceIndeces[0], l_referenceIndeces[1], l_referenceIndeces[2]);
        }
    }
    else {
        if (!m_value.isValid() && !ignoreUndefinedError)
            Kumir::Core::abort(Kumir::Core::fromUtf8("Нет значения у величины"));
        return m_value;
    }
Victor Yacovlev's avatar
Victor Yacovlev committed
488
    return m_value;
Victor Yacovlev's avatar
Victor Yacovlev committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
}

void Variable::setValue(const AnyValue &v)
{
    if (m_reference) {
        if (l_referenceIndeces[3]==0) {
            m_reference->setValue(v);
        }
        else if (l_referenceIndeces[3]==1) {
            m_reference->setValue(l_referenceIndeces[0], v);
        }
        else if (l_referenceIndeces[3]==2) {
            m_reference->setValue(l_referenceIndeces[0],l_referenceIndeces[1], v);
        }
        else if (l_referenceIndeces[3]==3) {
            m_reference->setValue(l_referenceIndeces[0],l_referenceIndeces[1],l_referenceIndeces[2], v);
        }

    }
    else {
        m_value = v;
    }

}




String Variable::toString() const
{
    String result;
    switch (e_baseType)
    {
    case VT_bool:
        if (value().toBool())
            result = Kumir::Core::fromUtf8("да");
        else
            result = Kumir::Core::fromUtf8("нет");
        break;
    case VT_real:
        result = Kumir::Converter::sprintfReal(value().toReal(), '.', false, 0,0,0);
        break;
    case VT_int:
        result = Kumir::Converter::sprintfInt(value().toInt(), 10, 0, 0);
        break;
    case VT_char:
        result.push_back(value().toChar());
        break;
    case VT_string:
        result = value().toString();
        break;
    default:
        break;
    }
    return result;
}

String Variable::toString(int indeces[4]) const
{
    String result;
    switch (e_baseType)
    {
    case VT_bool:
        if (value(indeces).toBool())
            result = Kumir::Core::fromUtf8("да");
        else
            result = Kumir::Core::fromUtf8("нет");
        break;
    case VT_real:
        result = Kumir::Converter::sprintfReal(value(indeces).toReal(), '.', false, 0,0,0);
        break;
    case VT_int:
        result = Kumir::Converter::sprintfInt(value(indeces).toInt(), 10, 0, 0);
        break;
    case VT_char:
        result.push_back(value(indeces).toChar());
        break;
    case VT_string:
        result = value(indeces).toString();
        break;
    default:
        break;
    }
    return result;
}

AnyValue Variable::value(int indeces[4]) const
{
    if (indeces[3]==1)
        return value(indeces[0]);
    else if (indeces[3]==2)
        return value(indeces[0], indeces[1]);
    else if (indeces[3]==3)
        return value(indeces[0], indeces[1], indeces[2]);
    else
        return value();
}

void Variable::setValue(int indeces[4], const AnyValue &value)
{
    if (indeces[3]==1)
        setValue(indeces[0], value);
    else if (indeces[3]==2)
        setValue(indeces[0], indeces[1], value);
    else if (indeces[3]==3)
        setValue(indeces[0], indeces[1], indeces[2], value);
    else
        setValue(value);
}


size_t Variable::linearIndex(int a) const
{
    return a-l_bounds[0];
}

size_t Variable::linearIndex(int a, int b) const
{
    int size0 = l_bounds[3]-l_bounds[2]+1;
    int offset0 = (a - l_bounds[0]) * size0;
    int result = offset0 + b-l_bounds[2];
    return result;
}

size_t Variable::linearIndex(int a, int b, int c) const
{
    int size0 = l_bounds[3]-l_bounds[2]+1;
    int size1 = l_bounds[5]-l_bounds[4]+1;
    return (a-l_bounds[0])*size0*size1 + (b-l_bounds[2])*size1 + c-l_bounds[4];
}


void Variable::setConstValue(const Variable & ctab)
{
    if (isReference()) {
        m_reference->setConstValue(ctab);
        return;
    }
    const int dim = ctab.dimension();
    int cbounds[7];
    if (dim>0) {
        ctab.getBounds(cbounds);
        for (int i=0; i<dim; i++) {
            const int mysize = l_bounds[2*i+1] - l_bounds[2*i];
            const int csize  = cbounds [2*i+1] - cbounds [2*i];
            if (mysize < csize) {
                Kumir::Core::abort(Kumir::Core::fromUtf8("Выход за границу таблицы"));
                return;
            }
        }
    }
    switch (dim)
    {
    case 0: {
        setValue(ctab.value());
        break;
    }
    case 1: {
        const int cx = cbounds [0];
        const int mx = l_bounds[0];
        const int sx = cbounds [1] - cbounds [0];
        for (int x=0; x<=sx; x++) {
            setValue(mx+x, ctab.value(cx+x));
        }
        break;
    }
    case 2: {
        const int cy = cbounds [0];
        const int my = l_bounds[0];
        const int cx = cbounds [2];
        const int mx = l_bounds[2];
        const int sy = cbounds [1] - cbounds [0];
        const int sx = cbounds [3] - cbounds [2];
        for (int y=0; y<=sy; y++) {
            for (int x=0; x<=sx; x++) {
                setValue(my+y, mx+x, ctab.value(cy+y, cx+x));
665
                unsetError();
Victor Yacovlev's avatar
Victor Yacovlev committed
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
            }
        }
        break;
    }
    case 3: {
        const int cz = cbounds [0];
        const int mz = l_bounds[0];
        const int cy = cbounds [2];
        const int my = l_bounds[2];
        const int cx = cbounds [4];
        const int mx = l_bounds[4];
        const int sz = cbounds [1] - cbounds [0];
        const int sy = cbounds [3] - cbounds [2];
        const int sx = cbounds [5] - cbounds [4];
        for (int z=0; z<sz; z++) {
            for (int y=0; y<=sy; y++) {
                for (int x=0; x<=sx; x++) {
                    setValue(mz+z, my+y, mx+x, ctab.value(cz+z, cy+y, cx+x));
                }
            }
        }
        break;
        }
        default: {
        break;
    }
    }
}

// DIM = 1

bool Variable::hasValue(int index0) const
{
    if (m_reference)
        return m_reference->hasValue(index0);
    if (m_value.rawSize()==0 || l_restrictedBounds[6]<1) {
        return false;
    }
    if (index0<l_restrictedBounds[0] || index0>l_restrictedBounds[1]) {
        return false;
    }
    int index = linearIndex(index0);
    return m_value.isValid() && m_value[index].isValid();
}

AnyValue Variable::value(int index0) const
{
    if (m_reference)
        return m_reference->value(index0);
    if (m_value.rawSize()==0 || l_restrictedBounds[6]<1) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Таблица не инициализирована"));
        return AnyValue(VT_void);
    }
    if (index0<l_restrictedBounds[0] || index0>l_restrictedBounds[1]) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Выход за границу таблицы"));
        return AnyValue(VT_void);
    }
    int index = linearIndex(index0);
    if (m_value[index].type()==VT_void) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Значение элемента таблицы не определено"));
        return AnyValue(VT_void);
    }
    return m_value[index];
}

void Variable::setValue(int index0, const AnyValue &value)
{
    if (!m_reference && (m_value.rawSize()==0 || l_restrictedBounds[6]<1)) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Таблица не инициализирована"));
        return ;
    }
    if (index0<l_restrictedBounds[0] || index0>l_restrictedBounds[1]) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Выход за границу таблицы"));
        return ;
    }
    if (m_reference) {
        m_reference->setValue(index0, value);
        return;
    }
    size_t index = linearIndex(index0);
    m_value[index] = value;
}



// DIM = 2

bool Variable::hasValue(int index0, int index1) const
{
    if (m_reference)
        return m_reference->hasValue(index0, index1);
    if (m_value.rawSize()==0 || l_restrictedBounds[6]<2) {
        return false;
    }
    if (index0<l_restrictedBounds[0] || index0>l_restrictedBounds[1] || index1<l_restrictedBounds[2]|| index1>l_restrictedBounds[3]) {
        return false;
    }
    size_t index = linearIndex(index0, index1);
    return m_value.isValid() && m_value[index].isValid();
}

AnyValue Variable::value(int index0, int index1) const
{
    if (m_reference)
        return m_reference->value(index0, index1);
    if (m_value.rawSize()==0 || l_restrictedBounds[6]<2) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Таблица не инициализирована"));
        return AnyValue(VT_void);
    }
    if (index0<l_restrictedBounds[0] || index0>l_restrictedBounds[1] || index1<l_restrictedBounds[2] || index1>l_restrictedBounds[3]) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Выход за границу таблицы"));
        return AnyValue(VT_void);
    }
    size_t index = linearIndex(index0, index1);
    if (m_value[index].type()==VT_void) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Значение элемента таблицы не определено"));
        return AnyValue(VT_void);
    }
    return m_value[index];
}

void Variable::setValue(int index0, int index1, const AnyValue &value)
{
    if (!m_reference && (m_value.rawSize()==0 || l_restrictedBounds[6]<2)) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Таблица не инициализирована"));
        return ;
    }
    if (index0<l_restrictedBounds[0] || index0>l_restrictedBounds[1] || index1<l_restrictedBounds[2] || index1>l_restrictedBounds[3]) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Выход за границу таблицы"));
        return ;
    }
    if (m_reference) {
        m_reference->setValue(index0, index1, value);
        return;
    }
    size_t index = linearIndex(index0, index1);
    m_value[index] = value;
}


// DIM = 3

bool Variable::hasValue(int index0, int index1, int index2) const
{
    if (m_reference)
        return m_reference->hasValue(index0, index1, index2);
    if (m_value.rawSize()==0 || l_restrictedBounds[6]<3) {
        return false;
    }
    if (index0<l_restrictedBounds[0] || index0>l_restrictedBounds[1] || index1<l_restrictedBounds[2]|| index1>l_restrictedBounds[3]|| index2<l_restrictedBounds[4] || index2>l_restrictedBounds[5]) {
        return false;
    }
    size_t index = linearIndex(index0, index1, index2);
    return m_value.isValid() && m_value[index].isValid();
}

AnyValue Variable::value(int index0, int index1, int index2) const
{
    if (m_reference)
        return m_reference->value(index0, index1, index2);
    if (m_value.rawSize()==0 || l_restrictedBounds[6]<3) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Таблица не инициализирована"));
        return AnyValue(VT_void);
    }
    if (index0<l_restrictedBounds[0] || index0>l_restrictedBounds[1] || index1<l_restrictedBounds[2] || index1>l_restrictedBounds[3]|| index2<l_restrictedBounds[4] || index2>l_restrictedBounds[5]) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Выход за границу таблицы"));
        return AnyValue(VT_void);
    }
    size_t index = linearIndex(index0, index1, index2);
    if (m_value[index].type()==VT_void) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Значение элемента таблицы не определено"));
        return AnyValue(VT_void);
    }
    return m_value[index];
}

void Variable::setValue(int index0, int index1, int index2, const AnyValue &value)
{
    if (!m_reference && (m_value.rawSize()==0 || l_restrictedBounds[6]<3)) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Таблица не инициализирована"));
        return ;
    }
    if (index0<l_restrictedBounds[0] || index0>l_restrictedBounds[1] || index1<l_restrictedBounds[2] || index1>l_restrictedBounds[3]|| index2<l_restrictedBounds[4] || index2>l_restrictedBounds[5]) {
        Kumir::Core::abort(Kumir::Core::fromUtf8("Выход за границу таблицы"));
        return ;
    }
    if (m_reference) {
        m_reference->setValue(index0, index1, index2, value);
        return;
    }
    size_t index = linearIndex(index0, index1, index2);
    m_value[index] = value;
}

void Variable::init()
{
    if (m_reference) {
        m_reference->init();
        return;
    }
    if (i_dimension==0) {
        m_value = VT_void;
    }
    else {
        if (i_dimension==1) {
            for (int x=l_restrictedBounds[0]; x<=l_restrictedBounds[1]; x++) {
                size_t index = linearIndex(x);
                m_value[index] = VT_void;
            }
        }
        else if (i_dimension==2) {
            for (int y=l_restrictedBounds[0]; y<=l_restrictedBounds[1]; y++) {
                for (int x=l_restrictedBounds[2]; x<=l_restrictedBounds[3]; x++) {
                    size_t index = linearIndex(y, x);
                    m_value[index] = VT_void;
                }
            }
        }
        else if (i_dimension==3) {
            for (int z=l_restrictedBounds[0]; z<=l_restrictedBounds[1]; z++) {
                for (int y=l_restrictedBounds[2]; y<=l_restrictedBounds[3]; y++) {
                    for (int x=l_restrictedBounds[4]; x<=l_restrictedBounds[5]; x++) {
                        size_t index = linearIndex(z, y, x);
                        m_value[index] = VT_void;
                    }
                }
            }
        }

    }
}

void Variable::setBounds(int bounds[7])
{
    size_t size = 0;
    i_dimension = bounds[6]/2;
    if (i_dimension>=1) {
        size = bounds[1]-bounds[0]+1;
        if (size<=0) {
            Kumir::Core::abort(Kumir::Core::fromUtf8("Неверный размер таблицы"));
            return;
        }
    }
    if (i_dimension>=2) {
        size *= bounds[3]-bounds[2]+1;
        if (size<=0) {
            Kumir::Core::abort(Kumir::Core::fromUtf8("Неверный размер таблицы"));
            return;
        }
    }
    if (i_dimension>=3) {
        size *= bounds[5]-bounds[4]+1;
        if (size<=0) {
            Kumir::Core::abort(Kumir::Core::fromUtf8("Неверный размер таблицы"));
            return;
        }
    }

    m_value.resize(size);

    memcpy(l_bounds, bounds, 7*sizeof(int));
    memcpy(l_restrictedBounds, l_bounds, 7*sizeof(int));
}

void Variable::updateBounds(int bounds[7])
{
    size_t size = 0;
    i_dimension = bounds[6]/2;
    if (i_dimension>=1) {
        size = bounds[1]-bounds[0]+1;
        if (size<=0) {
            Kumir::Core::abort(Kumir::Core::fromUtf8("Неверный размер таблицы"));
            return;
        }
    }
    if (i_dimension>=2) {
        size *= bounds[3]-bounds[2]+1;
        if (size<=0) {
            Kumir::Core::abort(Kumir::Core::fromUtf8("Неверный размер таблицы"));
            return;
        }
    }
    if (i_dimension>=3) {
        size *= bounds[5]-bounds[4]+1;
        if (size<=0) {
            Kumir::Core::abort(Kumir::Core::fromUtf8("Неверный размер таблицы"));
            return;
        }
    }

    l_restrictedBounds[6] = bounds[6];

    for (int i=0; i<bounds[6]; i+=2) {
        l_restrictedBounds[i] = l_bounds[6]
Victor Yacovlev's avatar
Victor Yacovlev committed
960
                ? Kumir::Math::imax(l_bounds[i], bounds[i])
Victor Yacovlev's avatar
Victor Yacovlev committed
961 962 963 964
                : bounds[i];
    }
    for (int i=1; i<bounds[6]; i+=2) {
        l_restrictedBounds[i] = l_bounds[6]
Victor Yacovlev's avatar
Victor Yacovlev committed
965
                ? Kumir::Math::imin(l_bounds[i], bounds[i])
Victor Yacovlev's avatar
Victor Yacovlev committed
966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027
                : bounds[i];
    }

}

Variable Variable::toReference()
{
    Variable result;
    if (m_reference) {
        result.m_reference = m_reference;
    }
    else {
        result.m_reference = this;
    }
    memcpy(result.l_bounds, l_restrictedBounds, 7*sizeof(int));
    memcpy(result.l_restrictedBounds, l_restrictedBounds, 7*sizeof(int));
    return result;
}



Variable Variable::toReference(int indeces[4])
{
    Variable result;
    if (m_reference) {
        result.m_reference = m_reference;
    }
    else {
        result.m_reference = this;
    }
    memcpy(result.l_referenceIndeces, indeces, 4*sizeof(int));
    return result;
}

String Variable::fullReferenceName() const
{
    if (m_reference) {
        String result = name();
        if (l_referenceIndeces[3]) {
            result.push_back('[');
            for (size_t i=0; i<l_referenceIndeces[3]; i++) {
                result += Kumir::Converter::sprintfInt(l_referenceIndeces[i],10,0,0);
                if (i<l_referenceIndeces[3]-1)
                    result.push_back(',');
            }
            result.push_back(']');
        }
        return result;
    }
    else {
        return name();
    }
}

#ifndef DO_NOT_DECLARE_STATIC_VARIANT
bool Variable::ignoreUndefinedError = false;
#endif

}


#endif