Skip to content

Commit

Permalink
fixes + timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
5pilow committed Jun 24, 2023
1 parent 6f0fe20 commit ab4c3dc
Show file tree
Hide file tree
Showing 10 changed files with 132 additions and 61 deletions.
1 change: 1 addition & 0 deletions src/main/java/leekscript/compiler/AIFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public String getCode() {
}
public void setCode(String code) {
this.code = code;
this.tokens = null;
}
public Folder getFolder() {
return folder;
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/leekscript/compiler/IACompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ public static class AnalyzeResult {
public Throwable tooMuchErrors;
}

public static final long TIMEOUT_MS = 30 * 1000; // 30 seconds

private final JSONArray informations = new JSONArray();
private AIFile mCurrentAI;
private long analyzeStart;

public IACompiler() {}

Expand All @@ -43,6 +46,7 @@ public void addError(Location location, Error errorType, String[] parameters) {

public AnalyzeResult analyze(AIFile ai) throws LeekCompilerException {
AnalyzeResult result = new AnalyzeResult();
this.analyzeStart = System.currentTimeMillis(); // For timeout
try {
ai.clearErrors();
// On lance la compilation du code de l'IA
Expand Down Expand Up @@ -80,6 +84,7 @@ public AnalyzeResult analyze(AIFile ai) throws LeekCompilerException {
}

public AICode compile(AIFile ai, String AIClass, boolean enableOperations) throws LeekCompilerException {
this.analyzeStart = System.currentTimeMillis(); // For timeout
JavaWriter writer = new JavaWriter(true, ai.getJavaClass(), enableOperations);
try {
ai.clearErrors();
Expand Down Expand Up @@ -111,6 +116,7 @@ public AICode compile(AIFile ai, String AIClass, boolean enableOperations) throw

public String merge(AIFile ai) throws LeekCompilerException {
// System.out.println("Merge ai " + ai);
this.analyzeStart = System.currentTimeMillis(); // For timeout
WordCompiler compiler = new WordCompiler(ai, ai.getVersion());
MainLeekBlock main = new MainLeekBlock(this, compiler, ai);
main.setWordCompiler(compiler);
Expand All @@ -127,4 +133,8 @@ public AIFile getCurrentAI() {
public void setCurrentAI(AIFile ai) {
mCurrentAI = ai;
}

public long getAnalyzeStart() {
return analyzeStart;
}
}
12 changes: 6 additions & 6 deletions src/main/java/leekscript/compiler/LexicalParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private boolean tryParseOperator() {
"===", "==", "=",
"!=", "!",
"<<<=", "<<<", "<<=", "<<", "<=", "<",
">>>=", ">>>", ">>=", ">>", ">=", ">",
">>>=", /* ">>>", ">>=", ">>", */ ">=", ">",
"^=", "^",
"~", "@",
"?", "\\"
Expand Down Expand Up @@ -253,14 +253,15 @@ private boolean tryParseExact(String expected, TokenType type) {

private boolean tryParseExact(char expected, TokenType type) {
if (stream.peek() == expected) {
addToken("" + expected, type);
stream.next();
addToken("" + expected, type);
return true;
}
return false;
}

private void addToken(String word, TokenType type) {
// System.out.println("addToken " + word + " " + type + " " + stream.getLineCounter() + " " + stream.getCharCounter());
tokens.add(new Token(type, word, aiFile, stream.getLineCounter(), stream.getCharCounter()));
}

Expand All @@ -271,12 +272,11 @@ private boolean wordEquals(String word, String expected) {
return word.equals(expected);
}

private record CharStreamRestorePoint(int index, int lineCounter, int charCounter) {
}
private record CharStreamRestorePoint(int index, int lineCounter, int charCounter) {}

private class CharStream {
private int lineCounter = 1;
private int charCounter = 1;
private int charCounter = 0;
private int index = 0;
private String content;

Expand All @@ -301,7 +301,7 @@ public char next() {

if (peek() == '\n') {
lineCounter++;
charCounter = 1;
charCounter = 0;
} else {
charCounter++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,5 @@ public void setPosition(LexicalParserTokenStreamPosition position) {
cursor = position.cursor;
}

public record LexicalParserTokenStreamPosition(int cursor) {
}
public record LexicalParserTokenStreamPosition(int cursor) {}
}
5 changes: 5 additions & 0 deletions src/main/java/leekscript/compiler/Location.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,9 @@ public Object toJSON() {
a.add(this.endColumn);
return a;
}

@Override
public String toString() {
return file + " [" + this.startLine + ", " + this.startColumn + ", " + this.endLine + ", " + this.endColumn + "]";
}
}
Loading

0 comments on commit ab4c3dc

Please sign in to comment.