Parser if statement
This commit is contained in:
parent
debec57bbd
commit
d28f17fc7e
@ -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();
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user