syntaxanalizer.cpp 162 KB
Newer Older
victor's avatar
 
victor committed
1 2
#include "syntaxanalizer.h"
#include "lexer.h"
victor's avatar
 
victor committed
3
#include <limits>
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
4
#include <deque>
5 6
#include <iostream>
#include <fstream>
victor's avatar
 
victor committed
7

victor's avatar
 
victor committed
8
#include "dataformats/ast_variable.h"
9
#include "dataformats/ast_type.h"
victor's avatar
 
victor committed
10
#include "errormessages/errormessages.h"
Victor Yacovlev's avatar
Victor Yacovlev committed
11 12
#include "extensionsystem/pluginmanager.h"
#include "interfaces/actorinterface.h"
13
#include "vm/vm_bytecode.hpp"
victor's avatar
 
victor committed
14 15 16 17 18 19 20 21 22

#define BADNAME_KEYWORD TN_BAD_NAME_3
#define BADNAME_OPERATOR TN_BAD_NAME_1

using namespace Shared;

namespace KumirAnalizer {

struct VariablesGroup {
victor's avatar
 
victor committed
23
    Lexem * groupLexem;
victor's avatar
 
victor committed
24 25
    QList<Lexem*> lexems;
    AST::VariableAccessType access;
victor's avatar
 
victor committed
26
    bool accessDefined;
victor's avatar
 
victor committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40
};

struct SubexpressionElement {
    inline SubexpressionElement(AST::Expression * ee) { e = ee; o = 0; }
    inline SubexpressionElement(Lexem * oo) { e = 0; o = oo; }
    AST::Expression * e;
    Lexem * o;
};

struct SyntaxAnalizerPrivate
{
    Lexer * lexer;
    AST::Data * ast;
    AST::Algorhitm * algorhitm;
victor's avatar
 
victor committed
41
    QList<Statement> statements;
victor's avatar
 
victor committed
42
    QSet<QString> unresolvedImports;
Victor Yacovlev's avatar
Victor Yacovlev committed
43
    QStringList alwaysEnabledModules;
victor's avatar
 
victor committed
44 45 46

    void parseImport(int str);
    void parseModuleHeader(int str);
Victor Yacovlev's avatar
Victor Yacovlev committed
47
    void parseAlgHeader(int str, bool onlyName, bool allowOperatorsDeclaration);
victor's avatar
 
victor committed
48 49
    void parseVarDecl(int str);
    void parseAssignment(int str);
Victor Yacovlev's avatar
Victor Yacovlev committed
50 51
    void parseInput(int str);
    void parseAssertPrePost(int str);
victor's avatar
 
victor committed
52
    void parseOutput(int str);
victor's avatar
 
victor committed
53 54 55 56
    void parseOneLexemInstruction(int str);
    void parseEndLoop(int str);
    void parseIfCase(int str);
    void parseLoopBegin(int str);
Victor Yacovlev's avatar
Victor Yacovlev committed
57 58 59 60 61 62

    bool tryInputOperatorAlgorithm(
            const QString & lexem,
            AST::Type & type,
            QVariant & constantValue
            ) const;
Victor Yacovlev's avatar
Victor Yacovlev committed
63 64 65 66 67 68 69 70
    bool findConversionAlgorithm(const AST::Type & from
                                 , const AST::Type & to
                                 , AST::Module * mod
                                 , AST::Algorhitm * alg) const;
    AST::Expression * makeCustomBinaryOperation(const QString & operatorName
                            , AST::Expression * leftExpression
                            , AST::Expression * rightExpression
                            );
Victor Yacovlev's avatar
Victor Yacovlev committed
71 72
    AST::Expression * makeCustomUnaryOperation(const QString & operatorName
                                               , AST::Expression * argument);
victor's avatar
 
victor committed
73 74 75 76 77 78 79 80 81 82 83 84 85 86
    bool findAlgorhitm(const QString &name
                       , const AST::Module*  module
                       , AST::Algorhitm* & algorhitm);
    bool findGlobalVariable(const QString &name, const AST::Module *module, AST::Variable* & var);
    bool findLocalVariable(const QString &name
                           , const AST::Algorhitm * alg
                           , AST::Variable* & var);
    AST::Expression * parseExpression(QList<Lexem*> lexems
                                      , const AST::Module * mod
                                      , const AST::Algorhitm * alg);
    bool findVariable(const QString &name
                      , const AST::Module * module
                      , const AST::Algorhitm * algorhitm
                      , AST::Variable* & var);
87
    bool findUserType(const QString & name, AST::Type &type, AST::Module * module);
victor's avatar
 
victor committed
88
    QList<AST::Variable*> parseVariables(int statementIndex, VariablesGroup & group,
victor's avatar
 
victor committed
89
                                         AST::Module * mod,
victor's avatar
 
victor committed
90
                                         AST::Algorhitm * alg, bool algHeader);
victor's avatar
 
victor committed
91
    QVariant parseConstant(const std::list<Lexem*> &constant
victor's avatar
 
victor committed
92 93 94
                           , const AST::VariableBaseType pt
                           , int& maxDim
                           ) const;
victor's avatar
 
victor committed
95 96
    AST::VariableBaseType testConst(const std::list<Lexem *> & lxs, int &err) const;
    QVariant createConstValue(const QString &str, const AST::VariableBaseType type) const;
victor's avatar
 
victor committed
97 98
    AST::Expression * parseFunctionCall(const QList<Lexem*> &lexems, const AST::Module * mod, const AST::Algorhitm * alg);

victor's avatar
 
victor committed
99
    AST::Expression * parseSimpleName(const std::list<Lexem*> &lexems, const AST::Module * mod, const AST::Algorhitm * alg);
Victor Yacovlev's avatar
Victor Yacovlev committed
100
    void updateSliceDSCall(AST::Expression * expr, AST::Variable * var);
victor's avatar
 
victor committed
101 102
    AST::Expression * parseElementAccess(const QList<Lexem*> &lexems, const AST::Module * mod, const AST::Algorhitm * alg);
    AST::Expression * makeExpressionTree(const QList<SubexpressionElement> & s);
103 104 105 106 107 108 109 110 111 112 113 114 115
    template <typename List1, typename List2>
    inline static void splitLexemsByOperator(
            const List1 &s
            , const LexemType & op
            , List2 & result
            , List1 & comas
            )
    {
        result.clear();
        comas.clear();
        int deep = 0;
        if (s.size()>0)
            result.push_back(List1());
116 117
        typename List1::const_iterator it;
        for (it=s.begin(); it!=s.end(); ++it) {
118 119 120 121 122 123 124 125 126 127 128 129 130
            if ( (*it)->type==op && deep==0 ) {
                result.push_back(List1());
                comas.push_back((*it));
            }
            else {
                if ( (*it)->type==LxOperLeftBr || (*it)->type==LxOperLeftSqBr || (*it)->type==LxOperLeftBrace )
                    deep++;
                else if ( (*it)->type==LxOperRightBr || (*it)->type==LxOperRightSqBr || (*it)->type==LxOperRightBrace )
                    deep--;
                result.back().push_back(*it);
            }
        }
    }
victor's avatar
 
victor committed
131
    static Lexem * findLexemByType(const QList<Lexem*> lxs, LexemType type);
victor's avatar
 
victor committed
132
    int currentPosition;
victor's avatar
 
victor committed
133

victor's avatar
 
victor committed
134
    QList<Statement*> originalStatements;
victor's avatar
 
victor committed
135 136 137

};

Victor Yacovlev's avatar
Victor Yacovlev committed
138 139 140
SyntaxAnalizer::SyntaxAnalizer(Lexer * lexer,
                               const QStringList & alwaysEnabledModules,
                               QObject *parent) :
victor's avatar
 
victor committed
141 142 143 144 145 146
    QObject(parent)
{
    d = new SyntaxAnalizerPrivate;
    d->ast = 0;
    d->algorhitm = 0;
    d->lexer = lexer;
victor's avatar
 
victor committed
147
    d->currentPosition = -1;
Victor Yacovlev's avatar
Victor Yacovlev committed
148
    d->alwaysEnabledModules = alwaysEnabledModules;
victor's avatar
 
victor committed
149 150 151 152 153 154 155 156 157
}

void SyntaxAnalizer::syncStatements()
{
    Q_ASSERT(d->originalStatements.size()==d->statements.size());
    for (int i=0; i<d->originalStatements.size(); i++) {
        Q_CHECK_PTR(d->originalStatements[i]);
        d->originalStatements[i]->variables = d->statements[i].variables;
    }
victor's avatar
 
victor committed
158 159
}

victor's avatar
 
victor committed
160
void SyntaxAnalizer::init(
victor's avatar
 
victor committed
161
    QList<Statement*> &statements,
victor's avatar
 
victor committed
162 163
    AST_Data *ast,
    AST_Algorhitm *algorhitm)
victor's avatar
 
victor committed
164
{
victor's avatar
 
victor committed
165 166
    d->originalStatements = statements;
    d->currentPosition = statements.size()-1;
victor's avatar
 
victor committed
167 168
    d->ast = ast;
    d->algorhitm = algorhitm;
victor's avatar
 
victor committed
169
    d->statements.clear();
victor's avatar
 
victor committed
170 171 172
    for (int i=0; i<statements.size(); i++) {
        Statement * st = statements[i];
        Q_CHECK_PTR(st);
victor's avatar
 
victor committed
173
//        if (st->type!=LxTypeComment) {
victor's avatar
 
victor committed
174 175 176 177 178 179
            Statement sst;
            sst.type = st->type;
            sst.statement = st->statement;
            sst.alg = st->alg;
            sst.mod = st->mod;
            sst.conditionalIndex = st->conditionalIndex;
victor's avatar
 
victor committed
180 181 182
            for (int j=0; j<st->data.size(); j++) {
                Lexem * lx = st->data[j];
                Q_CHECK_PTR(lx);
victor's avatar
 
victor committed
183 184 185 186 187
                if (lx->type!=LxTypeComment)
                    sst.data << lx;
            }

            d->statements << sst;
victor's avatar
 
victor committed
188
//        }
victor's avatar
 
victor committed
189 190
    }

victor's avatar
 
victor committed
191 192 193 194
    d->unresolvedImports.clear();
}


victor's avatar
 
victor committed
195 196 197 198 199 200 201 202 203
Lexem * SyntaxAnalizerPrivate::findLexemByType(const QList<Lexem*> lxs, LexemType type)
{
    foreach (Lexem * lx , lxs) {
        if (lx->type==type)
            return lx;
    }
    return 0;
}

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
AST::Type typeFromSignature(QString s) {
    AST::Type result;
    if (s.startsWith("void"))
        result.kind = AST::TypeNone;
    else if (s.startsWith("int"))
        result.kind = AST::TypeInteger;
    else if (s.startsWith("real"))
        result.kind = AST::TypeReal;
    else if (s.startsWith("bool"))
        result.kind = AST::TypeBoolean;
    else if (s.startsWith("char"))
        result.kind = AST::TypeCharect;
    else if (s.startsWith("string"))
        result.kind = AST::TypeString;
    else if (s.startsWith("record ")) {
        result.kind = AST::TypeUser;
        s.remove(0, 7);
        int br = s.indexOf("{");
        result.name = s.left(br);
        s.remove(0, br+1);
        int lbr = s.lastIndexOf("}");
        s = s.left(lbr);
        QStringList fields = s.split(";");
        for (int i=0; i<fields.size(); i++) {
            AST::Type fieldType = typeFromSignature(fields[i]);
            AST::Field field(fields[i], fieldType);
            result.userTypeFields.append(field);
        }
    }
    return result;
}

236
void SyntaxAnalizer::buildTables(bool isInternalBuild)
victor's avatar
 
victor committed
237
{
victor's avatar
 
victor committed
238 239
//    if (d->algorhitm)
//        return; // Nothing to build if we analize just one algorhitm
victor's avatar
 
victor committed
240

victor's avatar
 
victor committed
241 242
    for (int i=0; i<d->statements.size(); i++) {
        const Statement & st = d->statements[i];
victor's avatar
 
victor committed
243
        bool wasError = st.hasError();
victor's avatar
 
victor committed
244
        if (st.type==LxPriModule) {
victor's avatar
 
victor committed
245 246
            d->parseModuleHeader(i);
        }
victor's avatar
 
victor committed
247
        if (st.type==LxPriImport) {
victor's avatar
 
victor committed
248 249
            d->parseImport(i);
        }
victor's avatar
 
victor committed
250 251 252 253 254 255
        if (!wasError && d->statements[i].hasError()) {
            foreach (Lexem * lx, d->statements[i].data) {
                if (!lx->error.isEmpty())
                    lx->errorStage = Lexem::Tables;
            }
        }
victor's avatar
 
victor committed
256 257
    }

258
    if (!isInternalBuild)
Victor Yacovlev's avatar
Victor Yacovlev committed
259 260 261 262 263 264 265 266 267 268 269
    for (int i=0; i<d->ast->modules.size(); i++) {
        AST::Module * mod = d->ast->modules[i];
        if (mod->header.type==AST::ModTypeExternal) {
            if (mod->builtInID!=0xF0 &&
                    !d->alwaysEnabledModules.contains(mod->header.name))
            {
                mod->header.enabled = false;
            }
        }
    }

victor's avatar
 
victor committed
270
    for (int i=0; i<d->ast->modules.size(); i++) {
victor's avatar
 
victor committed
271
        if (d->ast->modules[i]->header.type==AST::ModTypeUser || d->ast->modules[i]->header.type==AST::ModTypeHidden)
victor's avatar
 
victor committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294
            d->ast->modules[i]->header.enabled = true;
        if (d->ast->modules[i]->header.enabled) {
            d->unresolvedImports.unite(d->ast->modules[i]->header.uses);
        }
    }

    forever {
        bool nextBreak = true;
        for (int i=0; i<d->ast->modules.size(); i++) {
            AST::Module * mod = d->ast->modules[i];
            if (d->unresolvedImports.contains(mod->header.name)) {
                d->unresolvedImports.remove(mod->header.name);
                if (!mod->header.enabled) {
                    nextBreak = false;
                    d->unresolvedImports.unite(mod->header.uses);
                }
                mod->header.enabled = true;
            }
        }
        if (nextBreak)
            break;
    }

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
    // Find and load unresolved imports
    for (int i=0; i<d->unresolvedImports.size(); i++) {
        const QString name = d->unresolvedImports.toList()[i];
        QString error;
        if (name.endsWith(".kod")) {
            QString canonicalName = name;
            if (canonicalName.startsWith(QDir::currentPath())) {
                canonicalName.remove(0, QDir::currentPath().length());
                if (canonicalName.startsWith("/"))
                    canonicalName.remove(0,1);
            }
            QFileInfo kodFile(name);
            QString kodFilePath = QDir::toNativeSeparators(kodFile.absoluteFilePath());
            const char * programName = kodFilePath.toLocal8Bit().constData();
            std::ifstream programFile(programName);
            Bytecode::Data programData;
            if (!programFile.is_open()) {
                error = _("Can't open module file");
            }
            else {
                try {
                    Bytecode::bytecodeFromDataStream(programFile, programData);
                }
                catch (...) {
                    error = _("Module file is damaged");
                }
            }
322
            programFile.close();
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 358 359 360 361 362 363 364 365
            if (error.length()==0) {
                AST::Module * module = new AST::Module;
                module->header.type = AST::ModTypeCached;
                module->header.name = canonicalName;
                module->header.enabled = true;
                d->ast->modules.push_back(module);
                for (size_t e=0; e<programData.d.size(); e++) {
                    const Bytecode::TableElem & elem = programData.d.at(e);
                    if (elem.type==Bytecode::EL_FUNCTION || elem.type==Bytecode::EL_MAIN) {
                        const QString algName = QString::fromStdWString(elem.name);
                        if (algName.length()>0 && !algName.startsWith("_")) {
                            AST::Algorhitm * alg = new AST::Algorhitm;
                            alg->header.name = algName;
                            alg->header.implType = AST::AlgorhitmExternal;
                            alg->header.external.moduleName = canonicalName;
                            alg->header.external.id = elem.id;
                            const QString signature = QString::fromStdWString(elem.signature);
                            QStringList algSig = signature.split(":");
                            alg->header.returnType = typeFromSignature(algSig[0]);
                            if (algSig.size()>1) {
                                QStringList argSignatures = algSig[1].split(",");
                                for (int argNo=0; argNo<argSignatures.size(); argNo++) {
                                    AST::Variable * var = new AST::Variable;
                                    QStringList sigPair = argSignatures[argNo].split(" ");
                                    if (sigPair[0]=="in")
                                        var->accessType = AST::AccessArgumentIn;
                                    else if (sigPair[0]=="out")
                                        var->accessType = AST::AccessArgumentOut;
                                    else if (sigPair[0]=="inout")
                                        var->accessType = AST::AccessArgumentInOut;
                                    var->baseType = typeFromSignature(sigPair[1]);
                                    var->dimension = sigPair[1].count("[]");
                                    alg->header.arguments.push_back(var);
                                }
                            }
                            module->header.algorhitms.push_back(alg);
                        }
                    }
                }
            }

        }
    }
victor's avatar
 
victor committed
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385

    // Set errors for imports that could not be resolved
    for (int i=0; i<d->unresolvedImports.size(); i++) {
        const QString name = d->unresolvedImports.toList()[i];

        for (int j=0; j<d->statements.size(); j++) {
            if (d->statements[j].type==Shared::LxPriImport && !d->statements[j].hasError()) {
                QString nn;
                for (int k=1; k<d->statements[j].data.size(); k++) {
                    Lexem * lx = d->statements[j].data[k];
                    if (!nn.isEmpty())
                        nn += " ";
                    nn += lx->data;
                }
                if (nn==name) {
                    const QString error = _("No such module");
                    for (int k=1; k<d->statements[j].data.size(); k++) {
                        Lexem * lx = d->statements[j].data[k];
                        lx->error = error;
                    }
victor's avatar
 
victor committed
386 387 388 389 390 391 392 393
                    if (d->statements[j].mod) {
                        AST::Module * mod = d->statements[j].mod;
                        AST::Statement * errStatement = new AST::Statement;
                        errStatement->type = AST::StError;
                        errStatement->error = error;
                        errStatement->lexems = d->statements[j].data;
                        mod->impl.initializerBody.prepend(errStatement);
                    }
victor's avatar
 
victor committed
394 395 396 397 398
                }
            }
        }
    }

victor's avatar
 
victor committed
399 400 401 402
    for (int i=0; i<d->statements.size(); i++) {
        const Statement & st = d->statements[i];
        bool wasError = st.hasError();
        if (st.type==LxPriAlgHeader) {
403
            d->parseAlgHeader(i, true, isInternalBuild);
victor's avatar
 
victor committed
404 405 406 407 408 409 410 411 412
        }
        if (!wasError && d->statements[i].hasError()) {
            foreach (Lexem * lx, d->statements[i].data) {
                if (!lx->error.isEmpty())
                    lx->errorStage = Lexem::Tables;
            }
        }
    }

victor's avatar
 
victor committed
413 414
    for (int i=0; i<d->statements.size(); i++) {
        const Statement & st = d->statements[i];
victor's avatar
 
victor committed
415
        bool wasError = st.hasError();
victor's avatar
 
victor committed
416 417 418
        if (st.type==LxNameClass && st.alg==0) {
            d->parseVarDecl(i);
        }
victor's avatar
 
victor committed
419
        if (st.type==LxPriAlgHeader) {
420
            d->parseAlgHeader(i, false, isInternalBuild);
victor's avatar
 
victor committed
421
        }
victor's avatar
 
victor committed
422 423 424 425 426 427
        if (!wasError && d->statements[i].hasError()) {
            foreach (Lexem * lx, d->statements[i].data) {
                if (!lx->error.isEmpty())
                    lx->errorStage = Lexem::Tables;
            }
        }
victor's avatar
 
victor committed
428
    }
victor's avatar
 
victor committed
429
    d->ast->modules[0]->header.enabled = true;
victor's avatar
 
victor committed
430 431 432 433
}

void SyntaxAnalizer::processAnalisys()
{
victor's avatar
 
victor committed
434 435 436 437 438 439 440 441 442 443
    for (int i=0; i<d->statements.size(); i++) {
        if (d->statements[i].hasError()) {
            foreach (Lexem * lx, d->statements[i].data) {
                if (lx->errorStage == AST::Lexem::Semantics) {
                    lx->error.clear();
                    lx->errorStage = AST::Lexem::NoError;
                }
            }
        }
    }
victor's avatar
 
victor committed
444
    for (int i=0; i<d->statements.size(); i++) {
victor's avatar
 
victor committed
445
        d->currentPosition = i;
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
        Statement & st = d->statements[i];
        // Fix unmatched modules first
        if (!st.mod) {
            for (int j=0; j<d->ast->modules.size(); j++) {
                if (d->ast->modules[j]->header.type==AST::ModTypeUser &&
                        d->ast->modules[j]->header.name.isEmpty()) {
                    st.mod = d->ast->modules[j];
                    break;
                }
            }
        }
        if (!st.mod) {
            // Add dummy module for this statement
            // (it is possible to reuse dummy module later)
            AST::Module * dummyModule = new AST::Module;
            dummyModule->header.type = AST::ModTypeUser;
            d->ast->modules.push_back(dummyModule);
            st.mod  = dummyModule;
        }
victor's avatar
 
victor committed
465
        bool wasError = st.hasError();
victor's avatar
 
victor committed
466 467 468 469 470 471
        if (st.statement) {
            for (int j=0; j<st.statement->expressions.size(); j++) {
                delete d->statements[i].statement->expressions[j];
            }
            d->statements[i].statement->expressions.clear();
        }
victor's avatar
 
victor committed
472
        if (st.type==LxPriAssign) {
victor's avatar
 
victor committed
473 474
            d->parseAssignment(i);
        }
victor's avatar
 
victor committed
475
        else if (st.type==LxNameClass && st.alg) {
victor's avatar
 
victor committed
476 477
            d->parseVarDecl(i);
        }
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
478
        else if (
Victor Yacovlev's avatar
Victor Yacovlev committed
479
                 st.type==LxPriAssert
victor's avatar
 
victor committed
480 481
                 || st.type==LxPriPre
                 || st.type==LxPriPost
victor's avatar
 
victor committed
482 483
                 )
        {
Victor Yacovlev's avatar
Victor Yacovlev committed
484
            d->parseAssertPrePost(i);
victor's avatar
 
victor committed
485
        }
Victor Yacovlev's avatar
Victor Yacovlev committed
486 487
        else if (st.type==LxPriInput) {
            d->parseInput(i);
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
488
        }
Victor Yacovlev's avatar
Victor Yacovlev committed
489 490
        else if (
                 st.type==LxPriOutput
victor's avatar
 
victor committed
491 492 493
                 )
        {
            d->parseOutput(i);
victor's avatar
 
victor committed
494
        }
Victor Yacovlev's avatar
Victor Yacovlev committed
495

victor's avatar
 
victor committed
496 497 498 499 500 501 502 503
        else if (st.type==LxPriEndModule
                 ||st.type==LxPriAlgBegin
                 ||st.type==LxPriAlgEnd
                 ||st.type==LxPriThen
                 ||st.type==LxPriElse
                 ||st.type==LxPriFi
                 ||st.type==LxPriSwitch
                 ||st.type==LxPriExit
victor's avatar
 
victor committed
504 505
                 ||st.type==LxPriPause
                 ||st.type==LxPriHalt
victor's avatar
 
victor committed
506 507 508 509
                 )
        {
            d->parseOneLexemInstruction(i);
        }
victor's avatar
 
victor committed
510
        else if (st.type==LxPriEndLoop) {
victor's avatar
 
victor committed
511 512
            d->parseEndLoop(i);
        }
victor's avatar
 
victor committed
513
        else if (st.type==LxPriIf||st.type==LxPriCase) {
victor's avatar
 
victor committed
514 515
            d->parseIfCase(i);
        }
victor's avatar
 
victor committed
516
        else if (st.type==LxPriLoop) {
victor's avatar
 
victor committed
517 518
            d->parseLoopBegin(i);
        }
victor's avatar
 
victor committed
519 520 521
        for (int j=0; j<st.data.size(); j++) {
            if (!st.data[j]->error.isEmpty()) {
                if (st.statement) {
victor's avatar
 
victor committed
522 523 524 525 526
                    if (st.type==LxPriLoop || st.type==LxPriSwitch) {
                        st.statement->beginBlockError = st.data[j]->error;
                    }
                    else if (st.type==LxPriEndLoop || st.type==LxPriFi) {
                        st.statement->endBlockError = st.data[j]->error;
victor's avatar
 
victor committed
527
                    }
victor's avatar
 
victor committed
528
                    else if (st.type==LxPriCase || st.type==LxPriElse) {
victor's avatar
 
victor committed
529 530 531
                        if (st.conditionalIndex < st.statement->conditionals.size()) {
                            st.statement->conditionals[st.conditionalIndex].conditionError = st.data[j]->error;
                        }
victor's avatar
 
victor committed
532
                    }
victor's avatar
 
victor committed
533 534 535
                    else {
                        st.statement->type = AST::StError;
                        st.statement->error = st.data[j]->error;
victor's avatar
 
victor committed
536
                    }
victor's avatar
 
victor committed
537 538 539 540
                    break;
                }
            }
        }
victor's avatar
 
victor committed
541 542 543 544 545 546 547 548
        if (!wasError && d->statements[i].hasError()) {
            foreach (Lexem * lx, d->statements[i].data) {
                if (lx->errorStage==AST::Lexem::NoError && !lx->error.isEmpty())  {
                    lx->errorStage = AST::Lexem::Semantics;
                }

            }
        }
victor's avatar
 
victor committed
549 550 551 552 553
    }
}

void SyntaxAnalizerPrivate::parseImport(int str)
{
victor's avatar
 
victor committed
554
    if (statements[str].hasError())
victor's avatar
 
victor committed
555
        return;
victor's avatar
 
victor committed
556 557 558 559
    Statement & st = statements[str];
    if (st.data.size()<2) {
        Q_ASSERT(st.data.size()==1);
        st.data[0]->error = _("No module name");
victor's avatar
 
victor committed
560 561
        return;
    }
victor's avatar
 
victor committed
562 563 564
    if (st.data.size()>2) {
        for (int i=2; i<st.data.size(); i++) {
            st.data[i]->error = _("Garbage afrer module name");
victor's avatar
 
victor committed
565 566 567
        }
        return;
    }
victor's avatar
 
victor committed
568 569
    if (st.data[1]->data.isEmpty()) {
        st.data[1]->error = _("No module name");
victor's avatar
 
victor committed
570 571
        return;
    }
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
    QString name;
    if (st.data[1]->type==LxConstLiteral) {
        name = st.data[1]->data.trimmed();
        if (name.isEmpty()) {
            st.data[1]->error = _("Must be Kumir program file name");
            return;
        }
        if (!name.endsWith(".kum") && !name.endsWith(".kod")) {
            name += ".kum";
        }
        QString kumName, binName;
        QFileInfo binFile, kumFile;
        if (name.endsWith(".kum")) {
            kumName = name;
            name = binName = name.left(name.length()-4)+".kod";
            binFile = QFileInfo (QDir::current().absoluteFilePath(binName));
            kumFile = QFileInfo (QDir::current().absoluteFilePath(kumName));
            QString kumir2bc = QDir(QCoreApplication::applicationDirPath()).absoluteFilePath("kumir2-bc");
#ifdef Q_OS_WIN32
            kumir2bc += ".exe";
#endif
            if (kumFile.exists()) {
                if (!binFile.exists() || binFile.lastModified()<kumFile.lastModified()) {
                    // Run kumir2-bc to create (or update) binary module
                    int status = QProcess::execute(kumir2bc, QStringList()
                                                   << "--debuglevel=2" << kumFile.absoluteFilePath());
                    if (status==-2) {
                        st.data[1]->error = _("Can't run kumir2-bc to compile this module");
                        return;
                    }
                    else if (status==-1) {
                        st.data[1]->error = _("kumir2-bc crashed while compiling this module");
                        return;
                    }
                    binFile = QFileInfo (QDir::current().absoluteFilePath(binName));
                    if (!binFile.exists()) {
                        st.data[1]->error = _("Error compiling this module");
                        return;
                    }
                }
            }
        }
        else {
            binName = name;
            binFile = QFileInfo (QDir::current().absoluteFilePath(binName));
        }
        if (!kumFile.exists() && !binFile.exists()) {
            st.data[1]->error = _("No such file");
            return;
        }
        name = binFile.absoluteFilePath();
    }
    else {
        QString localError = lexer->testName(st.data[1]->data);
        if (localError.size()>0) {
            st.data[1]->error = localError;
            return;
        }
        name = st.data[1]->data.simplified();
        st.data[1]->type = LxNameModule;
victor's avatar
 
victor committed
632
    }
victor's avatar
 
victor committed
633
    AST::Module * mod = st.mod;
victor's avatar
 
victor committed
634
    Q_CHECK_PTR(mod);
635
    mod->header.uses.insert(name);
victor's avatar
 
victor committed
636 637 638 639
}

void SyntaxAnalizerPrivate::parseModuleHeader(int str)
{
victor's avatar
 
victor committed
640
    if (statements[str].hasError())
victor's avatar
 
victor committed
641
        return;
victor's avatar
 
victor committed
642 643 644 645
    const Statement & st = statements[str];
    if (st.data.size()<2) {
        Q_ASSERT(st.data.size()==1);
        st.data[0]->error = _("No module name");
victor's avatar
 
victor committed
646 647
        return;
    }
victor's avatar
 
victor committed
648 649 650
    if (st.data.size()>2) {
        for (int i=2; i<st.data.size(); i++) {
            st.data[i]->error = _("Garbage afrer module name");
victor's avatar
 
victor committed
651 652 653
        }
        return;
    }
victor's avatar
 
victor committed
654 655
    if (st.data[1]->data.isEmpty()) {
        st.data[1]->error = _("No module name");
victor's avatar
 
victor committed
656 657
        return;
    }
victor's avatar
 
victor committed
658
    QString localError = lexer->testName(st.data[1]->data);
victor's avatar
 
victor committed
659
    if (localError.size()>0) {
victor's avatar
 
victor committed
660
        st.data[1]->error = localError;
victor's avatar
 
victor committed
661 662
        return;
    }
victor's avatar
 
victor committed
663
    AST::Module * mod = st.mod;
victor's avatar
 
victor committed
664
    Q_CHECK_PTR(mod);
victor's avatar
 
victor committed
665 666
    st.data[1]->type = LxNameModule;
    mod->header.name = st.data[1]->data.simplified();
victor's avatar
 
victor committed
667 668 669 670 671
}


void SyntaxAnalizerPrivate::parseVarDecl(int str)
{
victor's avatar
 
victor committed
672 673
    const Statement & st = statements[str];
    if (st.hasError())
victor's avatar
 
victor committed
674
        return;
victor's avatar
 
victor committed
675 676
    AST::Algorhitm * alg = st.alg;
    AST::Module * mod = st.mod;
victor's avatar
 
victor committed
677 678
    VariablesGroup group;
    group.access = AST::AccessRegular;
victor's avatar
 
victor committed
679 680 681 682
    for (int i=0; i<st.data.size(); i++) {
        if (st.data[i]->type & LxTypeComment)
            break;
        group.lexems << st.data[i];
victor's avatar
 
victor committed
683
    }
victor's avatar
 
victor committed
684
    QList<AST::Variable*> vars = parseVariables(str, group, mod, alg, false);
victor's avatar
 
victor committed
685 686 687 688 689 690 691 692
    QString error;
    for (int i=0; i<group.lexems.size()-1; i++) {
        if (group.lexems[i]->error.size()>0) {
            error = group.lexems[i]->error;
            break;
        }
    }
    if (!error.isEmpty()) {
victor's avatar
 
victor committed
693 694
        st.statement->type = AST::StError;
        st.statement->error = error;
victor's avatar
 
victor committed
695 696
    }
    else {
victor's avatar
 
victor committed
697 698
        st.statement->type = AST::StVarInitialize;
        st.statement->variables = vars;
victor's avatar
 
victor committed
699 700 701
    }
}

Victor Yacovlev's avatar
Victor Yacovlev committed
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
void SyntaxAnalizerPrivate::parseOutput(int str)
{
    const Statement & st = statements[str];
    if (st.hasError()) {
        return;
    }
    if (st.data.size()==1) {
        QString err = _("What to output?");
        st.data[0]->error = err;
        return;
    }
    if (st.data.last()->type==LxOperComa) {
        st.data.last()->error = _("Statement ends with coma");
        return;
    }
    QList< QList<Lexem*> > groups;
    QList<Lexem*> comas;
    splitLexemsByOperator(st.data.mid(1), LxOperComa, groups, comas);

    AST::Expression * fileHandle = 0;

    for (int i=0 ; i<groups.size(); i++) {
        QString err;

        QList< QList<Lexem*> > subgroups;
        QList< Lexem* > colons;
        splitLexemsByOperator(groups[i], LxOperColon, subgroups, colons);
        AST::Expression * expr = parseExpression(subgroups[0], st.mod, st.alg);
        if (!expr) {
            return;
        }
        int maxSubgroups;
        if (expr->baseType.kind==AST::TypeReal)
            maxSubgroups = 3;
        else if (expr->baseType.kind==AST::TypeInteger)
            maxSubgroups = 2;
        else
            maxSubgroups = 1;
        Q_ASSERT(colons.size() == subgroups.size()-1);
        if (subgroups.size()>maxSubgroups) {
            delete expr;
            err = _("Extra format parameter");
            for (int j=maxSubgroups-1 ; j<colons.size(); j++) {
                colons[j]->error = err;
            }
            for (int j=maxSubgroups; j<subgroups.size(); j++) {
                foreach (Lexem * lx, subgroups[j]) {
                    lx->error = err;
                }
            }
            return;
        }
Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
754
        if (!fileHandle && expr->baseType.kind==AST::TypeUser && expr->baseType.name==QString::fromUtf8("файл")) {
Victor Yacovlev's avatar
Victor Yacovlev committed
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
            fileHandle = expr;
            continue;
        }
        AST::Expression * expr2 = 0, *expr3 = 0;
        if (subgroups.size()>1) {
            expr2 = parseExpression(subgroups[1], st.mod, st.alg);
            if (!expr2) {
                delete expr;
                return;
            }
            if (expr2->baseType.kind!=AST::TypeInteger) {
                err = _("Format parameter not integer");
                foreach (Lexem * lx, subgroups[1])
                    lx->error = err;
                delete expr;
                delete expr2;
                return;
            }
        }
        else {
            expr2 = new AST::Expression;
            expr2->baseType.kind = AST::TypeInteger;
            expr2->dimension = 0;
            expr2->kind = AST::ExprConst;
            expr2->constant = QVariant(0);
        }
        if (subgroups.size()>2) {
            expr3 = parseExpression(subgroups[2], st.mod, st.alg);
            if (!expr3) {
                delete expr;
                delete expr2;
                return;
            }
            if (expr3->baseType.kind!=AST::TypeInteger) {
                err = _("Format parameter not integer");
                foreach (Lexem * lx, subgroups[2])
                    lx->error = err;
                delete expr;
                delete expr2;
                delete expr3;
                return;
            }
        }
        else {
            expr3 = new AST::Expression;
            expr3->baseType.kind = AST::TypeInteger;
            expr3->dimension = 0;
            expr3->kind = AST::ExprConst;
            expr3->constant = QVariant(6);
        }
        if (expr->baseType.kind==AST::TypeUser) {
            AST::Expression * helperCall = makeCustomUnaryOperation(lexer->outputLexemName(), expr);
            if (helperCall) {
                expr = helperCall;
            }
            else {
811 812
                err = _("Can't output value of type %1", expr->baseType.name);
                foreach (Lexem * lx, subgroups[0])
Victor Yacovlev's avatar
Victor Yacovlev committed
813 814 815 816 817 818 819 820 821 822
                    lx->error = err;
                delete expr;
                delete expr2;
                delete expr3;
                return;
            }
        }
        st.statement->expressions << expr << expr2 << expr3;
    }
    if (fileHandle) {
823 824 825 826 827 828
        if (st.statement->expressions.size()==0) {
            delete fileHandle;
            QString err = _("What to output to file?");
            st.data.at(0)->error = err;
            return;
        }
Victor Yacovlev's avatar
Victor Yacovlev committed
829 830 831 832 833 834 835 836
        st.statement->expressions << fileHandle;
    }
}




void SyntaxAnalizerPrivate::parseInput(int str)
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
837 838
{
    const Statement & st = statements[str];
Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
839
    if (st.hasError()) {
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
840
        return;
Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
841
    }
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
842
    if (st.data.size()==1) {
Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
843
        QString err = _("What to input?");
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
844 845 846
        st.data[0]->error = err;
        return;
    }
Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
847 848 849 850
    if (st.data.last()->type==LxOperComa) {
        st.data.last()->error = _("Statement ends with coma");
        return;
    }
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
851
    QList< QList<Lexem*> > groups;
Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
852 853
    QList<Lexem*> comas;
    splitLexemsByOperator(st.data.mid(1), LxOperComa, groups, comas);
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
854

Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
855
    AST::Expression * fileHandle = 0;
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
856

Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
857 858 859 860 861
    for (int i=0 ; i<groups.size(); i++) {
        QString err;

        AST::Expression * expr = parseExpression(groups[i], st.mod, st.alg);
        if (!expr) {
862 863 864 865 866 867
            for (int e=0; e<st.statement->expressions.size(); e++) {
                delete st.statement->expressions[e];
            }
            st.statement->expressions.clear();
            if (fileHandle)
                delete fileHandle;
Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
868 869 870 871 872 873 874 875 876
            return;
        }

        if (!fileHandle && expr->baseType.kind==AST::TypeUser && expr->baseType.name==QString::fromUtf8("файл")) {
            fileHandle = expr;
            continue;
        }

        if (expr->baseType.kind==AST::TypeUser) {
877
            err = _("Can't input value of type %1",expr->baseType.name);
Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
878 879 880 881 882 883 884 885 886 887 888 889 890
        }

        if (expr->kind==AST::ExprConst &&
                (expr->baseType!=AST::TypeCharect || expr->constant.toChar()!='\n'))
        {
            err = _("Can't input constant");
        }
        if (expr->kind==AST::ExprFunctionCall)
            err = _("Can't input function call");
        if (expr->kind==AST::ExprSubexpression)
            err = _("Can't input subexpression");
        if (expr->dimension>0)
            err = _("Can't input an array");
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
891

Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
892 893 894 895 896

        if (err.length()>0) {
            foreach (Lexem * lx, groups[i])
                lx->error = err;
            delete expr;
897 898 899 900 901 902 903
            for (int e=0; e<st.statement->expressions.size(); e++) {
                delete st.statement->expressions[e];
            }
            st.statement->expressions.clear();
            if (fileHandle)
                delete fileHandle;
            return;
Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
904 905 906 907 908
        }

        st.statement->expressions << expr;
    }
    if (fileHandle) {
909 910 911 912 913 914
        if (st.statement->expressions.size()==0) {
            delete fileHandle;
            QString err = _("What to input from file?");
            st.data.at(0)->error = err;
            return;
        }
Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
915 916
        st.statement->expressions << fileHandle;
    }
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
917 918
}

Victor Yacovlev's avatar
Victor Yacovlev committed
919
void SyntaxAnalizerPrivate::parseAssertPrePost(int str)
victor's avatar
 
victor committed
920
{
victor's avatar
 
victor committed
921 922
    const Statement & st = statements[str];
    if (st.hasError()) {
victor's avatar
 
victor committed
923 924
        return;
    }
victor's avatar
 
victor committed
925
    if (st.data.size()==1) {
victor's avatar
 
victor committed
926
        QString err = _("No expressions after '%1'", st.data[0]->data);
victor's avatar
 
victor committed
927
        if (st.data[0]->type!=LxPriPre && st.data[0]->type!=LxPriPost)
victor's avatar
 
victor committed
928
            st.data[0]->error = err;
victor's avatar
 
victor committed
929 930
        return;
    }
victor's avatar
 
victor committed
931 932
    if (st.data.last()->type==LxOperComa) {
        st.data.last()->error = _("Statement ends with coma");
victor's avatar
 
victor committed
933 934 935 936
        return;
    }
    QList< QList<Lexem*> > groups;
    QList<Lexem*> comas;
victor's avatar
 
victor committed
937
    splitLexemsByOperator(st.data.mid(1), LxOperComa, groups, comas);
victor's avatar
 
victor committed
938 939 940

    for (int i=0 ; i<groups.size(); i++) {

victor's avatar
 
victor committed
941
        AST::Expression * expr = parseExpression(groups[i], st.mod, st.alg);
victor's avatar
 
victor committed
942 943 944 945
        if (!expr) {
            return;
        }
        QString err;
Victor Yacovlev's avatar
Victor Yacovlev committed
946 947
        if (expr->baseType.kind!=AST::TypeBoolean) {
            err = _("Condition is not boolean");
victor's avatar
 
victor committed
948 949 950 951 952 953 954 955 956
        }

        if (!err.isEmpty()) {
            for (int a=0; a<groups[i].size(); a++) {
                groups[i][a]->error = err;
            }
            delete expr;
            return;
        }
victor's avatar
 
victor committed
957
        st.statement->expressions << expr;
victor's avatar
 
victor committed
958 959 960
    }
}

victor's avatar
 
victor committed
961 962


victor's avatar
 
victor committed
963 964
void SyntaxAnalizerPrivate::parseOneLexemInstruction(int str)
{
victor's avatar
 
victor committed
965 966
    const Statement & st = statements[str];
    if (st.hasError()) {
victor's avatar
 
victor committed
967 968
        return;
    }
victor's avatar
 
victor committed
969 970
    for (int i=1; i<st.data.size(); i++) {
        st.data[i]->error = _("Garbage at end of line");
victor's avatar
 
victor committed
971 972 973 974 975
    }
}

void SyntaxAnalizerPrivate::parseEndLoop(int str)
{
victor's avatar
 
victor committed
976 977
    const Statement & st = statements[str];
    if (st.hasError()) {
victor's avatar
 
victor committed
978 979
        return;
    }
victor's avatar
 
victor committed
980 981 982
    if (st.data.size()>1 && st.data[1]->type!=LxSecIf) {
        for (int i=1; i<st.data.size(); i++) {
            st.data[i]->error = _("Garbage at end of line");
victor's avatar
 
victor committed
983 984 985
        }
        return;
    }
victor's avatar
 
victor committed
986
    if (st.data.size()==2 && st.data[1]->type==LxSecIf) {
victor's avatar
 
victor committed
987

victor's avatar
 
victor committed
988
        st.data[1]->error = _("No condition after 'end if'");
victor's avatar
 
victor committed
989 990 991 992
        if (st.data[1]->data.startsWith("_")) {
            // Mark "endloop_if" first lexem if it's written close to second
            st.data[0]->error = st.data[1]->error;
        }
victor's avatar
 
victor committed
993 994
        return;
    }
victor's avatar
 
victor committed
995 996 997
    if (st.data.size()>2) {
        QList<Lexem*> condLexems = st.data.mid(2);
        AST::Expression * expr = parseExpression(condLexems, st.mod, st.alg);
victor's avatar
 
victor committed
998
        if (expr) {
999
            if (expr->baseType.kind!=AST::TypeBoolean) {
victor's avatar
 
victor committed
1000 1001
                for (int i=2; i<st.data.size(); i++) {
                    st.data[i]->error = _("Condition is not boolean");
victor's avatar
 
victor committed
1002 1003 1004 1005
                }
                delete expr;
            }
            else {
victor's avatar
 
victor committed
1006
                st.statement->loop.endCondition = expr;
victor's avatar
 
victor committed
1007 1008 1009
            }
        }
    }
victor's avatar
 
victor committed
1010

victor's avatar
 
victor committed
1011 1012 1013 1014
}

void SyntaxAnalizerPrivate::parseIfCase(int str)
{
victor's avatar
 
victor committed
1015 1016
    const Statement & st = statements[str];
    if (st.hasError()) {
victor's avatar
 
victor committed
1017 1018
        return;
    }
victor's avatar
 
victor committed
1019 1020
    if (st.type==LxPriIf && st.data.size()==1) {
        st.data[0]->error = _("No condition after 'if'");
victor's avatar
 
victor committed
1021 1022
        return;
    }
victor's avatar
 
victor committed
1023 1024
    if (st.type==LxPriCase && st.data.size()==1) {
        st.data[0]->error = _("No condition after 'case'");
victor's avatar
 
victor committed
1025 1026
        return;
    }
victor's avatar
 
victor committed
1027 1028 1029
    if (st.type==LxPriCase && st.data.size()==2 && st.data[1]->type==LxOperColon) {
        st.data[0]->error = _("No condition between 'case' and colon");
        st.data[1]->error = _("No condition between 'case' and colon");
victor's avatar
 
victor committed
1030 1031
        return;
    }
victor's avatar
 
victor committed
1032 1033 1034
    if (st.type==LxPriCase && st.data.last()->type!=LxOperColon) {
        for (int i=0; i<st.data.size(); i++) {
            st.data[i]->error = _("No colon after condition");
victor's avatar
 
victor committed
1035 1036 1037 1038
        }
        return;
    }
    QList<Lexem*> cond;
victor's avatar
 
victor committed
1039 1040
    if (st.type==LxPriCase)
        cond = st.data.mid(1, st.data.length()-2);
victor's avatar
 
victor committed
1041
    else
victor's avatar
 
victor committed
1042 1043
        cond = st.data.mid(1);
    AST::Expression * expr = parseExpression(cond, st.mod, st.alg);
victor's avatar
 
victor committed
1044
    if (expr) {
1045
        if (expr->baseType.kind!=AST::TypeBoolean) {
victor's avatar
 
victor committed
1046
            for (int i=0; i<cond.size(); i++) {
victor's avatar
 
victor committed
1047
                cond[i]->error = _("Condition after '%1' not boolean", st.data[0]->data);
victor's avatar
 
victor committed
1048 1049 1050 1051
            }
            delete expr;
        }
        else {
victor's avatar
 
victor committed
1052 1053 1054 1055 1056 1057
            if (st.conditionalIndex < st.statement->conditionals.size()) {
                st.statement->conditionals[st.conditionalIndex].condition = expr;
            }
            else {
                delete expr;
            }
victor's avatar
 
victor committed
1058 1059 1060 1061 1062 1063
        }
    }
}

void SyntaxAnalizerPrivate::parseLoopBegin(int str)
{
victor's avatar
 
victor committed
1064
    const Statement & st = statements[str];
victor's avatar
 
victor committed
1065
    if (st.hasError() || !st.statement) {
victor's avatar
 
victor committed
1066 1067 1068
        return;
    }
    AST::LoopType type = AST::LoopWhile;
victor's avatar
 
victor committed
1069
    if (st.data.size()==1) {
victor's avatar
 
victor committed
1070 1071
        type = AST::LoopWhile;
    }
victor's avatar
 
victor committed
1072
    else if (st.data.size()>1 && st.data[1]->type==LxSecFor) {
victor's avatar
 
victor committed
1073 1074
        type = AST::LoopFor;
    }
victor's avatar
 
victor committed
1075
    else if (st.data.size()>1 && st.data[1]->type==LxSecWhile) {
victor's avatar
 
victor committed
1076 1077
        type = AST::LoopWhile;
    }
victor's avatar
 
victor committed
1078
    else if (findLexemByType(st.data, LxSecTimes)) {
victor's avatar
 
victor committed
1079 1080 1081
        type = AST::LoopTimes;
    }
    else {
victor's avatar
 
victor committed
1082
        st.data[0]->error = _("Loop type not specified");
victor's avatar
 
victor committed
1083 1084
        return;
    }
victor's avatar
 
victor committed
1085
    st.statement->loop.type = type;
victor's avatar
 
victor committed
1086 1087 1088 1089 1090
    if (type==AST::LoopFor) {
        QList<Lexem*> forr;
        QList<Lexem*> from;
        QList<Lexem*> to;
        QList<Lexem*> step;
victor's avatar
 
victor committed
1091 1092 1093 1094
        Lexem * forLexem = st.data[1];
        Lexem * fromLexem = findLexemByType(st.data, LxSecFrom);
        Lexem * toLexem = findLexemByType(st.data, LxSecTo);
        Lexem * stepLexem = findLexemByType(st.data, LxSecStep);
victor's avatar
 
victor committed
1095

victor's avatar
 
victor committed
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111

        int forIndex = st.data.indexOf(forLexem);
        int fromIndex = st.data.indexOf(fromLexem);
        int toIndex = st.data.indexOf(toLexem);
        int stepIndex = st.data.indexOf(stepLexem);

        if (fromIndex!=-1 && forIndex!=-1 && fromIndex-forIndex==1) {
            forLexem->error =  _("No loop variable");
            return;
        }

        if (toIndex!=-1 && fromIndex>toIndex) {
            toLexem->error = _("'to' earler then 'from'");
            return;
        }

victor's avatar
 
victor committed
1112 1113 1114 1115