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
                    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;
397
                        errStatement->skipErrorEvaluation = false;
victor's avatar
 
victor committed
398 399
                        mod->impl.initializerBody.prepend(errStatement);
                    }
victor's avatar
 
victor committed
400 401 402 403 404
                }
            }
        }
    }

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

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

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

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

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

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


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

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




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

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

Victor Yacovlev's avatar
sync  
Victor Yacovlev committed
890 891 892 893 894
    for (int i=0 ; i<groups.size(); i++) {
        QString err;

        AST::Expression * expr = parseExpression(groups[i], st.mod, st.alg);
        if (!expr) {
895 896 897 898 899 900
            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
901 902 903 904
            return;
        }

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

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

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

        if (err.length()>0) {
            foreach (Lexem * lx, groups[i])
                lx->error = err;
            delete expr;
944 945 946 947 948 949 950
            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
951 952 953 954 955
        }

        st.statement->expressions << expr;
    }
    if (fileHandle) {
956 957 958 959 960 961
        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
962 963
        st.statement->expressions << fileHandle;
    }
Victor Yacovlev's avatar
New IO  
Victor Yacovlev committed
964 965
}

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

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

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

        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
1004
        st.statement->expressions << expr;
victor's avatar
 
victor committed
1005 1006 1007
    }
}

victor's avatar
 
victor committed
1008 1009


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

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

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

victor's avatar
 
victor committed
1058 1059 1060 1061
}

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