syntaxanalizer.cpp 170 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;
44
    QString sourceDirName;
victor's avatar
 
victor committed
45 46 47

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

    bool tryInputOperatorAlgorithm(
            const QString & lexem,
            AST::Type & type,
            QVariant & constantValue
            ) const;
Victor Yacovlev's avatar
Victor Yacovlev committed
64 65 66 67 68 69 70 71
    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
72 73
    AST::Expression * makeCustomUnaryOperation(const QString & operatorName
                                               , AST::Expression * argument);
victor's avatar
 
victor committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87
    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);
88
    bool findUserType(const QString & name, AST::Type &type, AST::Module * module);
victor's avatar
 
victor committed
89
    QList<AST::Variable*> parseVariables(int statementIndex, VariablesGroup & group,
victor's avatar
 
victor committed
90
                                         AST::Module * mod,
victor's avatar
 
victor committed
91
                                         AST::Algorhitm * alg, bool algHeader);
victor's avatar
 
victor committed
92
    QVariant parseConstant(const std::list<Lexem*> &constant
victor's avatar
 
victor committed
93 94 95
                           , const AST::VariableBaseType pt
                           , int& maxDim
                           ) const;
victor's avatar
 
victor committed
96 97
    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
98 99
    AST::Expression * parseFunctionCall(const QList<Lexem*> &lexems, const AST::Module * mod, const AST::Algorhitm * alg);

victor's avatar
 
victor committed
100
    AST::Expression * parseSimpleName(const std::list<Lexem*> &lexems, const AST::Module * mod, const AST::Algorhitm * alg);
Victor Yacovlev's avatar
Victor Yacovlev committed
101
    void updateSliceDSCall(AST::Expression * expr, AST::Variable * var);
victor's avatar
 
victor committed
102 103
    AST::Expression * parseElementAccess(const QList<Lexem*> &lexems, const AST::Module * mod, const AST::Algorhitm * alg);
    AST::Expression * makeExpressionTree(const QList<SubexpressionElement> & s);
104 105 106 107 108 109 110 111 112 113 114 115 116
    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());
117 118
        typename List1::const_iterator it;
        for (it=s.begin(); it!=s.end(); ++it) {
119 120 121 122 123 124 125 126 127 128 129 130 131
            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
132
    static Lexem * findLexemByType(const QList<Lexem*> lxs, LexemType type);
victor's avatar
 
victor committed
133
    int currentPosition;
victor's avatar
 
victor committed
134

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

};

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

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
159 160
}

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

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

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


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

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 236
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;
}

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

victor's avatar
 
victor committed
242 243
    for (int i=0; i<d->statements.size(); i++) {
        const Statement & st = d->statements[i];
victor's avatar
 
victor committed
244
        bool wasError = st.hasError();
victor's avatar
 
victor committed
245
        if (st.type==LxPriModule) {
victor's avatar
 
victor committed
246 247
            d->parseModuleHeader(i);
        }
victor's avatar
 
victor committed
248
        if (st.type==LxPriImport) {
victor's avatar
 
victor committed
249 250
            d->parseImport(i);
        }
victor's avatar
 
victor committed
251 252 253 254 255 256
        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
257 258
    }

259
    if (!isInternalBuild)
Victor Yacovlev's avatar
Victor Yacovlev committed
260 261 262 263 264 265 266 267 268 269 270
    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
271
    for (int i=0; i<d->ast->modules.size(); i++) {
victor's avatar
 
victor committed
272
        if (d->ast->modules[i]->header.type==AST::ModTypeUser || d->ast->modules[i]->header.type==AST::ModTypeHidden)
victor's avatar
 
victor committed
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
            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;
    }

296 297 298 299 300 301 302 303 304 305 306 307 308
    // 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());
309 310 311
            char programName[1024];
            strcpy(programName, kodFilePath.toLocal8Bit().constData());
            std::ifstream programFile(programName,  std::ios::in|std::ios::binary);
312 313 314 315 316 317 318 319 320 321 322 323
            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");
                }
            }
324
            programFile.close();
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346
            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++) {
347 348
                                    if (argSignatures[argNo].length()==0)
                                        break;
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369
                                    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
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389

    // 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
390 391 392 393 394 395 396 397
                    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
398 399 400 401 402
                }
            }
        }
    }

victor's avatar
 
victor committed
403 404 405 406
    for (int i=0; i<d->statements.size(); i++) {
        const Statement & st = d->statements[i];
        bool wasError = st.hasError();
        if (st.type==LxPriAlgHeader) {
407
            d->parseAlgHeader(i, true, isInternalBuild);
victor's avatar
 
victor committed
408 409 410 411 412 413 414 415 416
        }
        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
417 418
    for (int i=0; i<d->statements.size(); i++) {
        const Statement & st = d->statements[i];
victor's avatar
 
victor committed
419
        bool wasError = st.hasError();
victor's avatar
 
victor committed
420 421 422
        if (st.type==LxNameClass && st.alg==0) {
            d->parseVarDecl(i);
        }
victor's avatar
 
victor committed
423
        if (st.type==LxPriAlgHeader) {
424
            d->parseAlgHeader(i, false, isInternalBuild);
victor's avatar
 
victor committed
425
        }
victor's avatar
 
victor committed
426 427 428 429 430 431
        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
432
    }
victor's avatar
 
victor committed
433
    d->ast->modules[0]->header.enabled = true;
victor's avatar
 
victor committed
434 435 436 437
}

void SyntaxAnalizer::processAnalisys()
{
victor's avatar
 
victor committed
438 439 440 441 442 443 444 445 446 447
    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
448
    for (int i=0; i<d->statements.size(); i++) {
victor's avatar
 
victor committed
449
        d->currentPosition = i;
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468
        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
469
        bool wasError = st.hasError();
victor's avatar
 
victor committed
470 471 472 473 474 475
        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
476
        if (st.type==LxPriAssign) {
victor's avatar
 
victor committed
477 478
            d->parseAssignment(i);
        }
victor's avatar
 
victor committed
479
        else if (st.type==LxNameClass && st.alg) {
victor's avatar
 
victor committed
480 481
            d->parseVarDecl(i);
        }
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
482
        else if (
Victor Yacovlev's avatar
Victor Yacovlev committed
483
                 st.type==LxPriAssert
victor's avatar
 
victor committed
484 485
                 || st.type==LxPriPre
                 || st.type==LxPriPost
victor's avatar
 
victor committed
486 487
                 )
        {
Victor Yacovlev's avatar
Victor Yacovlev committed
488
            d->parseAssertPrePost(i);
victor's avatar
 
victor committed
489
        }
Victor Yacovlev's avatar
Victor Yacovlev committed
490 491
        else if (st.type==LxPriInput) {
            d->parseInput(i);
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
492
        }
Victor Yacovlev's avatar
Victor Yacovlev committed
493 494
        else if (
                 st.type==LxPriOutput
victor's avatar
 
victor committed
495 496 497
                 )
        {
            d->parseOutput(i);
victor's avatar
 
victor committed
498
        }
Victor Yacovlev's avatar
Victor Yacovlev committed
499

victor's avatar
 
victor committed
500 501 502 503 504 505 506 507
        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
508 509
                 ||st.type==LxPriPause
                 ||st.type==LxPriHalt
victor's avatar
 
victor committed
510 511 512 513
                 )
        {
            d->parseOneLexemInstruction(i);
        }
victor's avatar
 
victor committed
514
        else if (st.type==LxPriEndLoop) {
victor's avatar
 
victor committed
515 516
            d->parseEndLoop(i);
        }
victor's avatar
 
victor committed
517
        else if (st.type==LxPriIf||st.type==LxPriCase) {
victor's avatar
 
victor committed
518 519
            d->parseIfCase(i);
        }
victor's avatar
 
victor committed
520
        else if (st.type==LxPriLoop) {
victor's avatar
 
victor committed
521 522
            d->parseLoopBegin(i);
        }
victor's avatar
 
victor committed
523 524 525
        for (int j=0; j<st.data.size(); j++) {
            if (!st.data[j]->error.isEmpty()) {
                if (st.statement) {
victor's avatar
 
victor committed
526 527 528 529 530
                    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
531
                    }
victor's avatar
 
victor committed
532
                    else if (st.type==LxPriCase || st.type==LxPriElse) {
victor's avatar
 
victor committed
533 534 535
                        if (st.conditionalIndex < st.statement->conditionals.size()) {
                            st.statement->conditionals[st.conditionalIndex].conditionError = st.data[j]->error;
                        }
victor's avatar
 
victor committed
536
                    }
victor's avatar
 
victor committed
537 538 539
                    else {
                        st.statement->type = AST::StError;
                        st.statement->error = st.data[j]->error;
victor's avatar
 
victor committed
540
                    }
victor's avatar
 
victor committed
541 542 543 544
                    break;
                }
            }
        }
victor's avatar
 
victor committed
545 546 547 548 549 550 551 552
        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
553 554 555
    }
}

556 557 558 559 560
void SyntaxAnalizer::setSourceDirName(const QString &dirName)
{
    d->sourceDirName = dirName;
}

victor's avatar
 
victor committed
561 562
void SyntaxAnalizerPrivate::parseImport(int str)
{
victor's avatar
 
victor committed
563
    if (statements[str].hasError())
victor's avatar
 
victor committed
564
        return;
victor's avatar
 
victor committed
565 566 567 568
    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
569 570
        return;
    }
victor's avatar
 
victor committed
571 572 573
    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
574 575 576
        }
        return;
    }
victor's avatar
 
victor committed
577 578
    if (st.data[1]->data.isEmpty()) {
        st.data[1]->error = _("No module name");
victor's avatar
 
victor committed
579 580
        return;
    }
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
    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";
596 597
            binFile = QFileInfo (QDir(sourceDirName).absoluteFilePath(binName));
            kumFile = QFileInfo (QDir(sourceDirName).absoluteFilePath(kumName));
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
            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
641
    }
victor's avatar
 
victor committed
642
    AST::Module * mod = st.mod;
victor's avatar
 
victor committed
643
    Q_CHECK_PTR(mod);
644
    mod->header.uses.insert(name);
victor's avatar
 
victor committed
645 646 647 648
}

void SyntaxAnalizerPrivate::parseModuleHeader(int str)
{
victor's avatar
 
victor committed
649
    if (statements[str].hasError())
victor's avatar
 
victor committed
650
        return;
victor's avatar
 
victor committed
651 652 653 654
    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
655 656
        return;
    }
victor's avatar
 
victor committed
657 658 659
    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
660 661 662
        }
        return;
    }
victor's avatar
 
victor committed
663 664
    if (st.data[1]->data.isEmpty()) {
        st.data[1]->error = _("No module name");
victor's avatar
 
victor committed
665 666
        return;
    }
victor's avatar
 
victor committed
667
    QString localError = lexer->testName(st.data[1]->data);
victor's avatar
 
victor committed
668
    if (localError.size()>0) {
victor's avatar
 
victor committed
669
        st.data[1]->error = localError;
victor's avatar
 
victor committed
670 671
        return;
    }
victor's avatar
 
victor committed
672
    AST::Module * mod = st.mod;
victor's avatar
 
victor committed
673
    Q_CHECK_PTR(mod);
victor's avatar
 
victor committed
674 675
    st.data[1]->type = LxNameModule;
    mod->header.name = st.data[1]->data.simplified();
victor's avatar
 
victor committed
676 677 678 679 680
}


void SyntaxAnalizerPrivate::parseVarDecl(int str)
{
victor's avatar
 
victor committed
681 682
    const Statement & st = statements[str];
    if (st.hasError())
victor's avatar
 
victor committed
683
        return;
victor's avatar
 
victor committed
684 685
    AST::Algorhitm * alg = st.alg;
    AST::Module * mod = st.mod;
victor's avatar
 
victor committed
686 687
    VariablesGroup group;
    group.access = AST::AccessRegular;
victor's avatar
 
victor committed
688 689 690 691
    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
692
    }
victor's avatar
 
victor committed
693
    QList<AST::Variable*> vars = parseVariables(str, group, mod, alg, false);
victor's avatar
 
victor committed
694 695 696 697 698 699 700 701
    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
702 703
        st.statement->type = AST::StError;
        st.statement->error = error;
victor's avatar
 
victor committed
704 705
    }
    else {
victor's avatar
 
victor committed
706 707
        st.statement->type = AST::StVarInitialize;
        st.statement->variables = vars;
victor's avatar
 
victor committed
708 709 710
    }
}

Victor Yacovlev's avatar
Victor Yacovlev committed
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
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
763
        if (!fileHandle && expr->baseType.kind==AST::TypeUser && expr->baseType.name==QString::fromUtf8("файл")) {
764 765 766 767 768 769 770 771 772 773 774
            if (i>0) {
                for (int e=0; e<st.statement->expressions.size(); e++) {
                    delete st.statement->expressions[e];
                }
                st.statement->expressions.clear();
                if (fileHandle)
                    delete fileHandle;
                foreach (Lexem * lx, groups[i])
                    lx->error = _("File must be specified at first");
                return;
            }
Victor Yacovlev's avatar
Victor Yacovlev committed
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
            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 {
831 832
                err = _("Can't output value of type %1", expr->baseType.name);
                foreach (Lexem * lx, subgroups[0])
Victor Yacovlev's avatar
Victor Yacovlev committed
833 834 835 836 837 838 839 840 841 842
                    lx->error = err;
                delete expr;
                delete expr2;
                delete expr3;
                return;
            }
        }
        st.statement->expressions << expr << expr2 << expr3;
    }
    if (fileHandle) {
843 844 845 846 847 848
        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
849 850 851 852 853 854 855 856
        st.statement->expressions << fileHandle;
    }
}




void SyntaxAnalizerPrivate::parseInput(int str)
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
857 858
{
    const Statement & st = statements[str];
Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
859
    if (st.hasError()) {
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
860
        return;
Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
861
    }
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
862
    if (st.data.size()==1) {
Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
863
        QString err = _("What to input?");
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
864 865 866
        st.data[0]->error = err;
        return;
    }
Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
867 868 869 870
    if (st.data.last()->type==LxOperComa) {
        st.data.last()->error = _("Statement ends with coma");
        return;
    }
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
871
    QList< QList<Lexem*> > groups;
Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
872 873
    QList<Lexem*> comas;
    splitLexemsByOperator(st.data.mid(1), LxOperComa, groups, comas);
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
874

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

Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
877 878 879 880 881
    for (int i=0 ; i<groups.size(); i++) {
        QString err;

        AST::Expression * expr = parseExpression(groups[i], st.mod, st.alg);
        if (!expr) {
882 883 884 885 886 887
            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
888 889 890 891
            return;
        }

        if (!fileHandle && expr->baseType.kind==AST::TypeUser && expr->baseType.name==QString::fromUtf8("файл")) {
892 893 894 895 896 897 898 899 900 901 902
            if (i>0) {
                for (int e=0; e<st.statement->expressions.size(); e++) {
                    delete st.statement->expressions[e];
                }
                st.statement->expressions.clear();
                if (fileHandle)
                    delete fileHandle;
                foreach (Lexem * lx, groups[i])
                    lx->error = _("File must be specified at first");
                return;
            }
Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
903 904 905 906 907
            fileHandle = expr;
            continue;
        }

        if (expr->baseType.kind==AST::TypeUser) {
908
            err = _("Can't input value of type %1",expr->baseType.name);
Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
909 910 911 912 913 914 915 916 917 918 919 920 921
        }

        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");
922 923
        if (expr->isStringPart)
            err = _("Can't input part of string");
Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
924 925 926 927 928

        if (err.length()>0) {
            foreach (Lexem * lx, groups[i])
                lx->error = err;
            delete expr;
929 930 931 932 933 934 935
            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
936 937 938 939 940
        }

        st.statement->expressions << expr;
    }
    if (fileHandle) {
941 942 943 944 945 946
        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
947 948
        st.statement->expressions << fileHandle;
    }
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
949 950
}

Victor Yacovlev's avatar
Victor Yacovlev committed
951
void SyntaxAnalizerPrivate::parseAssertPrePost(int str)
victor's avatar
 
victor committed
952
{
victor's avatar
 
victor committed
953 954
    const Statement & st = statements[str];
    if (st.hasError()) {
victor's avatar
 
victor committed
955 956
        return;
    }
victor's avatar
 
victor committed
957
    if (st.data.size()==1) {
victor's avatar
 
victor committed
958
        QString err = _("No expressions after '%1'", st.data[0]->data);
victor's avatar
 
victor committed
959
        if (st.data[0]->type!=LxPriPre && st.data[0]->type!=LxPriPost)
victor's avatar
 
victor committed
960
            st.data[0]->error = err;
victor's avatar
 
victor committed
961 962
        return;
    }
victor's avatar
 
victor committed
963 964
    if (st.data.last()->type==LxOperComa) {
        st.data.last()->error = _("Statement ends with coma");
victor's avatar
 
victor committed
965 966 967 968
        return;
    }
    QList< QList<Lexem*> > groups;
    QList<Lexem*> comas;
victor's avatar
 
victor committed
969
    splitLexemsByOperator(st.data.mid(1), LxOperComa, groups, comas);
victor's avatar
 
victor committed
970 971 972

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

victor's avatar
 
victor committed
973
        AST::Expression * expr = parseExpression(groups[i], st.mod, st.alg);
victor's avatar
 
victor committed
974 975 976 977
        if (!expr) {
            return;
        }
        QString err;
Victor Yacovlev's avatar
Victor Yacovlev committed
978 979
        if (expr->baseType.kind!=AST::TypeBoolean) {
            err = _("Condition is not boolean");
victor's avatar
 
victor committed
980 981 982 983 984 985 986 987 988
        }

        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
989
        st.statement->expressions << expr;
victor's avatar
 
victor committed
990 991 992
    }
}

victor's avatar
 
victor committed
993 994


victor's avatar
 
victor committed
995 996
void SyntaxAnalizerPrivate::parseOneLexemInstruction(int str)
{
victor's avatar
 
victor committed
997 998
    const Statement & st = statements[str];
    if (st.hasError()) {
victor's avatar
 
victor committed
999 1000
        return;
    }
victor's avatar
 
victor committed
1001 1002
    for (int i=1; i<st.data.size(); i++) {
        st.data[i]->error = _("Garbage at end of line");
victor's avatar
 
victor committed
1003 1004 1005 1006 1007
    }
}

void SyntaxAnalizerPrivate::parseEndLoop(int str)
{
victor's avatar
 
victor committed
1008 1009
    const Statement & st = statements[str];
    if (st.hasError()) {
victor's avatar
 
victor committed
1010 1011
        return;
    }
victor's avatar
 
victor committed
1012 1013 1014
    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
1015 1016 1017
        }
        return;
    }
victor's avatar
 
victor committed
1018
    if (st.data.size()==2 && st.data[1]->type==LxSecIf) {
victor's avatar
 
victor committed
1019

victor's avatar
 
victor committed
1020
        st.data[1]->error = _("No condition after 'end if'");
victor's avatar
 
victor committed
1021 1022 1023 1024
        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
1025 1026
        return;
    }
victor's avatar
 
victor committed
1027 1028 1029
    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
1030
        if (expr) {
1031
            if (expr->baseType.kind!=AST::TypeBoolean) {
victor's avatar
 
victor committed
1032 1033
                for (int i=2; i<st.data.size(); i++) {
                    st.data[i]->error = _("Condition is not boolean");
victor's avatar
 
victor committed
1034 1035 1036 1037
                }
                delete expr;
            }
            else {
victor's avatar
 
victor committed
1038
                st.statement->loop.endCondition = expr;
victor's avatar
 
victor committed
1039 1040 1041
            }
        }
    }
victor's avatar
 
victor committed
1042

victor's avatar
 
victor committed
1043 1044 1045 1046
}

void SyntaxAnalizerPrivate::parseIfCase(int str)
{
victor's avatar
 
victor committed
1047 1048
    const Statement & st = statements[str];
    if (st.hasError()) {
victor's avatar
 
victor committed
1049 1050
        return;
    }
victor's avatar
 
victor committed
1051 1052
    if (st.type==LxPriIf && st.data.size()==1) {
        st.data[0]->error = _("No condition after 'if'");
victor's avatar
 
victor committed
1053 1054
        return;
    }
victor's avatar
 
victor committed
1055 1056
    if (st.type==LxPriCase && st.data.size()==1) {
        st.data[0]->error = _("No condition after 'case'");
victor's avatar
 
victor committed
1057 1058
        return;
    }
victor's avatar
 
victor committed
1059 1060 1061
    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
1062 1063
        return;
    }
victor's avatar
 
victor committed
1064 1065 1066
    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
1067 1068 1069 1070
        }
        return;
    }
    QList<Lexem*> cond;
victor's avatar
 
victor committed
1071 1072
    if (st.type==LxPriCase)
        cond = st.data.mid(1, st.data.length()-2);
victor's avatar
 
victor committed
1073
    else
victor's avatar
 
victor committed
1074 1075
        cond = st.data.mid(1);
    AST::Expression * expr = parseExpression(cond, st.mod, st.alg);
victor's avatar
 
victor committed
1076
    if (expr) {
1077
        if (expr->baseType.kind!=AST::TypeBoolean) {
victor's avatar
 
victor committed
1078
            for (int i=0; i<cond.size(); i++) {
victor's avatar
 
victor committed
1079
                cond[i]->error = _("Condition after '%1' not boolean", st.data[0]->data);
victor's avatar
 
victor committed
1080 1081 1082 1083
            }
            delete expr;
        }
        else {
1084
            if (st.statement && st.conditionalIndex < st.statement->conditionals.size()) {
victor's avatar
 
victor committed
1085 1086 1087 1088 1089
                st.statement->conditionals[st.conditionalIndex].condition = expr;
            }
            else {
                delete expr;
            }
victor's avatar
 
victor committed
1090 1091 1092 1093 1094 1095
        }
    }
}

void SyntaxAnalizerPrivate::parseLoopBegin(int str)
{
victor's avatar
 
victor committed
1096
    const Statement & st = statements[str];
victor's avatar
 
victor committed
1097
    if (st.hasError() || !st.statement) {
victor's avatar
 
victor committed
1098 1099 1100
        return;
    }
    AST::LoopType type = AST::LoopWhile;
victor's avatar
 
victor committed
1101
    if (st.data.size()==1) {
victor's avatar
 
victor committed
1102 1103
        type = AST::LoopWhile;
    }
victor's avatar
 
victor committed
1104
    else if (st.data.size()>1 && st.data[1]->type==LxSecFor) {
victor's avatar
 
victor committed
1105 1106
        type = AST::LoopFor;
    }
victor's avatar