From d28f17fc7ecda16e3733b9829827879004ada29c Mon Sep 17 00:00:00 2001 From: Victor Date: Mon, 7 Dec 2015 17:22:11 +0200 Subject: [PATCH] Parser if statement --- public_html/js/Parser.js | 37 ++++++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/public_html/js/Parser.js b/public_html/js/Parser.js index 3ddb286..edd8c0b 100644 --- a/public_html/js/Parser.js +++ b/public_html/js/Parser.js @@ -70,7 +70,7 @@ Parser.prototype.statement = function() { if (this.match(token, TokenType.HIDE)) return this.hide(); if (this.match(token, TokenType.JUMP)) return this.jump(); -// if (this.match(token, TokenType.IF)) return this.ifStatement(); + if (this.match(token, TokenType.IF)) return this.ifStatement(); /*if (this.lookMatch(1, TokenType.COLON)) { // menu: @@ -329,6 +329,41 @@ Parser.prototype.jump = function() { return false; }; +Parser.prototype.ifStatement = function() { + var condition = expression(); + this.consume(TokenType.COLON); + + if (!this.hasEndIf) return false; + if (condition.eval() == 0) { + // Если условие не верно, пропускаем блок до следующего ENDIF + var pos = 0; + var level = 1; // уровень вложенности блока + while (true) { + // Расчёт уровня блока. + if (this.lookMatch(pos, TokenType.IF)) { + level++; + pos++; + } + if (this.lookMatch(pos, TokenType.ELSE)) { + level--; + pos += 2; // пропускаем ELSE и двоеточие + // Завершаем работу по достижению ELSE первого уровня. + if (level <= 0) break; + } + if (this.lookMatch(pos, TokenType.ENDIF)) { + level--; + // Завершаем работу по достижению ENDIF первого уровня. + if (level <= 0) break; + } + + if (this.lookMatch(pos, TokenType.EOF)) return false; + pos++; + } + this.position += pos; + } + return false; +}; + Parser.prototype.expression = function() { return this.orTest(); };