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 76d596f
Show file tree
Hide file tree
Showing 10 changed files with 160 additions and 94 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;
}
}
73 changes: 34 additions & 39 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 All @@ -98,7 +98,7 @@ private boolean tryParseOperator() {
}

private boolean tryParseIdentifier() {
var startingPoint = stream.getRestorePoint();
var startingPoint = stream.index;
for (char c = stream.peek(); stream.hasMore(); c = stream.next()) {
if (c >= '0' && c <= '9') continue;
if (c >= 'A' && c <= 'Z') continue;
Expand All @@ -113,7 +113,7 @@ private boolean tryParseIdentifier() {
break;
}

if (startingPoint.equals(stream.getRestorePoint())) {
if (startingPoint == stream.index) {
return false;
}

Expand All @@ -135,12 +135,13 @@ private boolean tryParseIdentifier() {
}

private boolean tryParseNumber(ErrorReporter error) throws LeekCompilerException {
var startingPoint = stream.getRestorePoint();

if (stream.peek() < '0' || stream.peek() > '9') {
return false;
}

var startingPoint = stream.index;

stream.next();

for (char c = stream.peek(); stream.hasMore(); c = stream.next()) {
Expand Down Expand Up @@ -177,13 +178,14 @@ private boolean tryParseNumber(ErrorReporter error) throws LeekCompilerException
}

private boolean tryParseString() {
var startingPoint = stream.getRestorePoint();

var openQuote = stream.peek();
if (openQuote != '"' && openQuote != '\'') {
return false;
}

var startingPoint = stream.index;

stream.next();

var escaped = false;
Expand All @@ -204,7 +206,8 @@ private boolean tryParseString() {
}

private boolean tryParseWhiteSpaces() {
if ("\r\n\t ".contains("" + stream.peek()) || stream.peek() == 160) {
var c = stream.peek();
if (c == ' ' || c == '\r' || c == '\n' || c == '\t' || c == 160) {
stream.next();
return true;
}
Expand Down Expand Up @@ -237,30 +240,32 @@ private boolean tryParseComments() {
}

private boolean tryParseExact(String expected, TokenType type) {
var startingPoint = stream.getRestorePoint();

for (int i = 0; i < expected.length(); i++) stream.next();

var streamWord = stream.getSubStringSince(startingPoint);
if (wordEquals(streamWord, expected)) {
addToken(expected, type);
return true;
if (stream.index + expected.length() <= stream.content.length()) {
var streamWord = stream.content.substring(stream.index, stream.index + expected.length());
if (wordEquals(streamWord, expected)) {
stream.index += expected.length();
if (stream.index < stream.content.length()) {
stream.c = stream.content.charAt(stream.index);
}
addToken(expected, type);
return true;
}
}

stream.restore(startingPoint);
return false;
}

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,17 +276,16 @@ private boolean wordEquals(String word, String expected) {
return word.equals(expected);
}

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;
private char c;

public CharStream(String content) {
this.content = content;
this.c = content.charAt(0);
}

public int getLineCounter() {
Expand All @@ -299,40 +303,31 @@ public boolean hasMore() {
public char next() {
if (index >= content.length()) return 0;

if (peek() == '\n') {
if (c == '\n') {
lineCounter++;
charCounter = 1;
charCounter = 0;
} else {
charCounter++;
}

index++;

return peek();
if (index < content.length()) {
c = content.charAt(index);
}
return c;
}

public char peek(int offset) {
if (index + offset >= content.length() || index + offset < 0) return 0;
if (index + offset >= content.length()) return 0;
return content.charAt(index + offset);
}

public char peek() {
return peek(0);
return c;
}

public CharStreamRestorePoint getRestorePoint() {
return new CharStreamRestorePoint(index, lineCounter, charCounter);
}

public void restore(CharStreamRestorePoint restorePoint) {
index = restorePoint.index;
lineCounter = restorePoint.lineCounter;
charCounter = restorePoint.charCounter;
}

public String getSubStringSince(CharStreamRestorePoint restorePoint) {
return content.substring(restorePoint.index, index);
public String getSubStringSince(int start) {
return content.substring(start, index);
}
}

}
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 76d596f

Please sign in to comment.