syntaxanalizer.cpp 174 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
#include <stack>
6 7
#include <iostream>
#include <fstream>
victor's avatar
 
victor committed
8

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

#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
24
    Lexem * groupLexem;
victor's avatar
 
victor committed
25 26
    QList<Lexem*> lexems;
    AST::VariableAccessType access;
victor's avatar
 
victor committed
27
    bool accessDefined;
victor's avatar
 
victor committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41
};

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
42
    QList<Statement> statements;
victor's avatar
 
victor committed
43
    QSet<QString> unresolvedImports;
Victor Yacovlev's avatar
Victor Yacovlev committed
44
    QStringList alwaysEnabledModules;
45
    QString sourceDirName;
victor's avatar
 
victor committed
46 47 48

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

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

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

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

};

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

victor's avatar
 
victor committed
562 563
void SyntaxAnalizerPrivate::parseImport(int str)
{
victor's avatar
 
victor committed
564
    if (statements[str].hasError())
victor's avatar
 
victor committed
565
        return;
victor's avatar
 
victor committed
566 567 568 569
    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
570 571
        return;
    }
victor's avatar
 
victor committed
572 573 574
    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
575 576 577
        }
        return;
    }
victor's avatar
 
victor committed
578 579
    if (st.data[1]->data.isEmpty()) {
        st.data[1]->error = _("No module name");
victor's avatar
 
victor committed
580 581
        return;
    }
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596
    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";
597 598
            binFile = QFileInfo (QDir(sourceDirName).absoluteFilePath(binName));
            kumFile = QFileInfo (QDir(sourceDirName).absoluteFilePath(kumName));
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
            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;
                    }
616
                    binFile = QFileInfo (QDir(sourceDirName).absoluteFilePath(binName));
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641
                    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
642
    }
victor's avatar
 
victor committed
643
    AST::Module * mod = st.mod;
victor's avatar
 
victor committed
644
    Q_CHECK_PTR(mod);
645
    mod->header.uses.insert(name);
victor's avatar
 
victor committed
646 647 648 649
}

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


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

Victor Yacovlev's avatar
Victor Yacovlev committed
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
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;
750 751 752 753 754 755 756
        if (expr->baseType.kind==AST::TypeNone && expr->kind==AST::ExprFunctionCall) {
            delete expr;
            err = _("This algorithm has no return value");
            foreach (Lexem * lx, groups[i])
                lx->error = err;
            return;
        }
Victor Yacovlev's avatar
Victor Yacovlev committed
757 758 759 760 761 762 763 764 765 766 767 768 769 770
        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
771
        if (!fileHandle && expr->baseType.kind==AST::TypeUser && expr->baseType.name==QString::fromUtf8("файл")) {
772 773 774 775 776 777 778 779 780 781 782
            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
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838
            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 {
839 840
                err = _("Can't output value of type %1", expr->baseType.name);
                foreach (Lexem * lx, subgroups[0])
Victor Yacovlev's avatar
Victor Yacovlev committed
841 842 843 844 845 846 847 848 849 850
                    lx->error = err;
                delete expr;
                delete expr2;
                delete expr3;
                return;
            }
        }
        st.statement->expressions << expr << expr2 << expr3;
    }
    if (fileHandle) {
851 852 853 854 855 856
        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
857 858 859 860 861 862 863 864
        st.statement->expressions << fileHandle;
    }
}




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

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

Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
885 886 887 888 889
    for (int i=0 ; i<groups.size(); i++) {
        QString err;

        AST::Expression * expr = parseExpression(groups[i], st.mod, st.alg);
        if (!expr) {
890 891 892 893 894 895
            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
896 897 898 899
            return;
        }

        if (!fileHandle && expr->baseType.kind==AST::TypeUser && expr->baseType.name==QString::fromUtf8("файл")) {
900 901 902 903 904 905 906 907 908 909 910
            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
911 912 913 914 915
            fileHandle = expr;
            continue;
        }

        if (expr->baseType.kind==AST::TypeUser) {
916
            err = _("Can't input value of type %1",expr->baseType.name);
Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
917 918 919 920 921 922 923 924 925 926 927 928 929
        }

        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");
930 931
        if (expr->isStringPart)
            err = _("Can't input part of string");
Victor Yacovlev's avatar
FS#1149  
Victor Yacovlev committed
932 933
        if (expr->kind==AST::ExprVariable && expr->variable->accessType==AST::AccessArgumentIn)
            err = _("Can't input in-argument");
Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
934 935 936 937 938

        if (err.length()>0) {
            foreach (Lexem * lx, groups[i])
                lx->error = err;
            delete expr;
939 940 941 942 943 944 945
            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
946 947 948 949 950
        }

        st.statement->expressions << expr;
    }
    if (fileHandle) {
951 952 953 954 955 956
        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
957 958
        st.statement->expressions << fileHandle;
    }
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
959 960
}

Victor Yacovlev's avatar
Victor Yacovlev committed
961
void SyntaxAnalizerPrivate::parseAssertPrePost(int str)
victor's avatar
 
victor committed
962
{
victor's avatar
 
victor committed
963 964
    const Statement & st = statements[str];
    if (st.hasError()) {
victor's avatar
 
victor committed
965 966
        return;
    }
victor's avatar
 
victor committed
967
    if (st.data.size()==1) {
victor's avatar
 
victor committed
968
        QString err = _("No expressions after '%1'", st.data[0]->data);
victor's avatar
 
victor committed
969
        if (st.data[0]->type!=LxPriPre && st.data[0]->type!=LxPriPost)
victor's avatar
 
victor committed
970
            st.data[0]->error = err;
victor's avatar
 
victor committed
971 972
        return;
    }
victor's avatar
 
victor committed
973 974
    if (st.data.last()->type==LxOperComa) {
        st.data.last()->error = _("Statement ends with coma");
victor's avatar
 
victor committed
975 976 977 978
        return;
    }
    QList< QList<Lexem*> > groups;
    QList<Lexem*> comas;
victor's avatar
 
victor committed
979
    splitLexemsByOperator(st.data.mid(1), LxOperComa, groups, comas);
victor's avatar
 
victor committed
980 981 982

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

victor's avatar
 
victor committed
983
        AST::Expression * expr = parseExpression(groups[i], st.mod, st.alg);
victor's avatar
 
victor committed
984 985 986 987
        if (!expr) {
            return;
        }
        QString err;
Victor Yacovlev's avatar
Victor Yacovlev committed
988 989
        if (expr->baseType.kind!=AST::TypeBoolean) {
            err = _("Condition is not boolean");
victor's avatar
 
victor committed
990 991 992 993 994 995 996 997 998
        }

        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
999
        st.statement->expressions << expr;
victor's avatar
 
victor committed
1000 1001 1002
    }
}

victor's avatar
 
victor committed
1003 1004


victor's avatar
 
victor committed
1005 1006
void SyntaxAnalizerPrivate::parseOneLexemInstruction(int str)
{
victor's avatar
 
victor committed
1007 1008
    const Statement & st = statements[str];
    if (st.hasError()) {
victor's avatar
 
victor committed
1009 1010
        return;
    }
victor's avatar
 
victor committed
1011 1012
    for (int i=1; i<st.data.size(); i++) {
        st.data[i]->error = _("Garbage at end of line");
victor's avatar
 
victor committed
1013 1014 1015 1016 1017
    }
}

void SyntaxAnalizerPrivate::parseEndLoop(int str)
{
victor's avatar
 
victor committed
1018 1019
    const Statement & st = statements[str];
    if (st.hasError()) {
victor's avatar
 
victor committed
1020 1021
        return;
    }
victor's avatar
 
victor committed
1022 1023 1024
    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
1025 1026 1027
        }
        return;
    }
victor's avatar
 
victor committed
1028
    if (st.data.size()==2 && st.data[1]->type==LxSecIf) {
victor's avatar
 
victor committed
1029

victor's avatar
 
victor committed
1030
        st.data[1]->error = _("No condition after 'end if'");
victor's avatar
 
victor committed
1031 1032 1033 1034
        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
1035 1036
        return;
    }
victor's avatar
 
victor committed
1037 1038 1039
    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
1040
        if (expr) {
1041
            if (expr->baseType.kind!=AST::TypeBoolean) {
victor's avatar
 
victor committed
1042 1043
                for (int i=2; i<st.data.size(); i++) {
                    st.data[i]->error = _("Condition is not boolean");
victor's avatar
 
victor committed
1044 1045 1046 1047
                }
                delete expr;
            }
            else {
victor's avatar
 
victor committed
1048
                st.statement->loop.endCondition = expr;
victor's avatar
 
victor committed
1049 1050 1051
            }
        }
    }
victor's avatar
 
victor committed
1052

victor's avatar
 
victor committed
1053 1054 1055 1056
}

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

void SyntaxAnalizerPrivate::parseLoopBegin(int str)
{
victor's avatar
 
victor committed
1106
    const Statement & st = statements[str];