Поддержка переходов и меток (jump / label)

This commit is contained in:
Victor 2015-04-13 14:42:17 +03:00
parent 22f0ac668f
commit 3801a48468
3 changed files with 36 additions and 2 deletions

View File

@ -33,7 +33,7 @@ public final class Lexer {
"window", "hide", "show",
"with",
"return", "menu", "endmenu",
"return", "menu", "endmenu", "jump", "label",
"renpy.pause", "persistent.sprite_time",
"prolog_time", "day_time", "sunset_time", "night_time"
@ -48,7 +48,7 @@ public final class Lexer {
TokenType.WINDOW, TokenType.HIDE, TokenType.SHOW,
TokenType.WITH,
TokenType.RETURN, TokenType.MENU, TokenType.ENDMENU,
TokenType.RETURN, TokenType.MENU, TokenType.ENDMENU, TokenType.JUMP, TokenType.LABEL,
TokenType.RENPY_PAUSE, TokenType.PERSISTENT_SPRITE_TIME,
TokenType.PROLOG_TIME, TokenType.DAY_TIME, TokenType.SUNSET_TIME, TokenType.NIGHT_TIME

View File

@ -1,6 +1,8 @@
package com.annimon.everlastingsummer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.text.TextUtils;
/**
@ -26,10 +28,14 @@ public final class Parser {
private final int tokensCount;
private int position;
private final Map<String, Integer> labels;
public Parser(List<Token> tokens) {
this.tokens = tokens;
tokensCount = tokens.size();
position = 0;
labels = new HashMap<String, Integer>();
scanLabels();
}
public List<Token> getTokens() {
@ -117,6 +123,8 @@ public final class Parser {
return false;
}
if (match(token, TokenType.JUMP)) return jump();
if (lookMatch(1, TokenType.COLON)) {
// menu:
if (match(token, TokenType.MENU)) return menu();
@ -326,6 +334,30 @@ public final class Parser {
return true;
}
private boolean jump() {
final String labelName = consume(TokenType.WORD).getText();
if (labels.containsKey(labelName)) {
position = labels.get(labelName);
}
return false;
}
private void scanLabels() {
// Сканируем все метки, для быстрого перехода командой jump.
for (int i = 0; i < tokensCount - 2; i++) {
if ( (tokens.get(i).getType() == TokenType.LABEL) &&
(tokens.get(i + 2).getType() == TokenType.COLON) ) {
// label word :
final Token token = tokens.get(i + 1);
if (token.getType() == TokenType.WORD) {
// Добавляем позицию команды, следующей после метки.
labels.put(token.getText(), i + 3);
}
i += 2;
}
}
}
private double consumeDouble() {
return Double.parseDouble(consume(TokenType.NUMBER).getText());

View File

@ -44,6 +44,8 @@ public enum TokenType {
MENU,
ENDMENU,
JUMP,
LABEL,
RETURN,
// команды