Skip to content

Commit 8a88aeb

Browse files
committed
java 21, console
1 parent ce431b1 commit 8a88aeb

31 files changed

+308
-52
lines changed

.github/workflows/build.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ jobs:
1616

1717
steps:
1818
- uses: actions/checkout@v2
19-
- name: Set up JDK 17
19+
- name: Set up JDK 21
2020
uses: actions/setup-java@v2
2121
with:
22-
java-version: '17'
22+
java-version: '21'
2323
distribution: 'adopt'
2424
- name: Setup locale
2525
run: |

build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apply plugin: 'java'
22

3-
sourceCompatibility = JavaVersion.VERSION_17
4-
targetCompatibility = JavaVersion.VERSION_17
3+
sourceCompatibility = JavaVersion.VERSION_21
4+
targetCompatibility = JavaVersion.VERSION_21
55

66
sourceSets {
77
test {

src/main/java/leekscript/AILog.java

+8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
package leekscript;
22

3+
import com.alibaba.fastjson.JSONArray;
4+
35
import leekscript.runner.AI;
46
import leekscript.runner.LeekRunException;
57

68
public abstract class AILog {
79

10+
public interface Stream {
11+
public void write(JSONArray a);
12+
}
13+
814
public final static int STANDARD = 1;
915
public final static int WARNING = 2;
1016
public final static int ERROR = 3;
@@ -35,4 +41,6 @@ public boolean addSize(int size) {
3541
public boolean isFull() {
3642
return mSize >= MAX_LENGTH;
3743
}
44+
45+
public abstract void setStream(Stream stream);
3846
}

src/main/java/leekscript/TopLevel.java

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
import leekscript.compiler.LeekScript;
77
import leekscript.compiler.Options;
88
import leekscript.runner.AI;
9-
import leekscript.runner.LeekRunException;
109
import leekscript.runner.Session;
1110

1211
public class TopLevel {

src/main/java/leekscript/common/Type.java

+2
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public class Type {
3636
public static final Type ARRAY = array(Type.ANY);
3737
public static final Type EMPTY_ARRAY = array(Type.VOID);
3838
public static final Type SET = set(Type.ANY);
39+
public static final Type SET_REAL = set(Type.REAL);
40+
public static final Type SET_INT = set(Type.INT);
3941
public static final Type EMPTY_SET = set(Type.VOID);
4042
public static final Type INTERVAL = new IntervalType(Type.ANY);
4143
public static final Type EMPTY_INTERVAL = new IntervalType(Type.VOID);

src/main/java/leekscript/compiler/AIFile.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ public String toJson() {
121121

122122
public AI compile(Options options) throws LeekScriptException, LeekCompilerException {
123123

124-
// System.out.println("LeekScript compile AI " + this.getPath() + " timestamp : " + this.getTimestamp());
124+
// System.out.println("LeekScript compile AI " + this.getPath() + " timestamp : " + this.getTimestamp() + " options " + options);
125125

126126
LeekScript.getFileSystem().loadDependencies(this);
127127

@@ -180,4 +180,8 @@ public String toString() {
180180
public boolean isStrict() {
181181
return strict;
182182
}
183+
184+
public Options getOptions() {
185+
return new Options(version, strict, true, true, null, true);
186+
}
183187
}

src/main/java/leekscript/compiler/IACompiler.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public AnalyzeResult analyze(AIFile ai) throws LeekCompilerException {
5454
ai.clearErrors();
5555
// On lance la compilation du code de l'IA
5656
// Si on est là c'est qu'on a une liste de words correcte, on peut commencer à lire
57-
WordCompiler compiler = new WordCompiler(ai, ai.getVersion());
57+
WordCompiler compiler = new WordCompiler(ai, ai.getVersion(), ai.getOptions());
5858
MainLeekBlock main = new MainLeekBlock(this, compiler, ai);
5959
main.setWordCompiler(compiler);
6060

@@ -103,7 +103,7 @@ public AICode compile(AIFile ai, String AIClass, Options options) throws LeekCom
103103

104104
// On lance la compilation du code de l'IA
105105
// Si on est là c'est qu'on a une liste de words correcte, on peut commencer à lire
106-
WordCompiler compiler = new WordCompiler(ai, ai.getVersion());
106+
WordCompiler compiler = new WordCompiler(ai, ai.getVersion(), options);
107107
MainLeekBlock main = new MainLeekBlock(this, compiler, ai);
108108
main.setWordCompiler(compiler);
109109

@@ -117,7 +117,7 @@ public AICode compile(AIFile ai, String AIClass, Options options) throws LeekCom
117117

118118
compiler.readCode();
119119
compiler.analyze();
120-
// System.out.println("errors " + compiler.getErrors().size());
120+
// System.out.println("errors " + ai.getErrors().size());
121121

122122
if (ai.getErrors().size() > 0) {
123123
for (var error : ai.getErrors()) {
@@ -139,7 +139,7 @@ public AICode compile(AIFile ai, String AIClass, Options options) throws LeekCom
139139
public String merge(AIFile ai) throws LeekCompilerException {
140140
// System.out.println("Merge ai " + ai);
141141
this.analyzeStart = System.currentTimeMillis(); // For timeout
142-
WordCompiler compiler = new WordCompiler(ai, ai.getVersion());
142+
WordCompiler compiler = new WordCompiler(ai, ai.getVersion(), ai.getOptions());
143143
MainLeekBlock main = new MainLeekBlock(this, compiler, ai);
144144
main.setWordCompiler(compiler);
145145
compiler.readCode();

src/main/java/leekscript/compiler/JavaWriter.java

+4
Original file line numberDiff line numberDiff line change
@@ -463,4 +463,8 @@ public boolean isInConstructor() {
463463
public boolean isOperationsEnabled() {
464464
return operationsEnabled;
465465
}
466+
467+
public Options getOptions() {
468+
return options;
469+
}
466470
}

src/main/java/leekscript/compiler/Options.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
import leekscript.runner.Session;
44

5-
public record Options(int version, boolean strict, boolean useCache, boolean enableOperations, Session session) {
5+
public record Options(int version, boolean strict, boolean useCache, boolean enableOperations, Session session, boolean useExtra) {
66

77
public Options() {
8-
this(LeekScript.LATEST_VERSION, false, false, false, null);
8+
this(LeekScript.LATEST_VERSION, false, false, false, null, true);
99
}
1010
public Options(boolean operations) {
11-
this(LeekScript.LATEST_VERSION, false, false, operations, null);
11+
this(LeekScript.LATEST_VERSION, false, false, operations, null, true);
1212
}
1313
public Options(Session session) {
14-
this(LeekScript.LATEST_VERSION, false, true, false, session);
14+
this(LeekScript.LATEST_VERSION, false, true, true, session, true);
1515
}
1616
}

src/main/java/leekscript/compiler/WordCompiler.java

+14-3
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,12 @@ public class WordCompiler {
6161
private int mLine;
6262
private AIFile mAI = null;
6363
private final int version;
64+
private final Options options;
6465

65-
public WordCompiler(AIFile ai, int version) {
66+
public WordCompiler(AIFile ai, int version, Options options) {
6667
mAI = ai;
6768
this.version = version;
69+
this.options = options;
6870
}
6971

7072
private void parse() throws LeekCompilerException {
@@ -325,14 +327,19 @@ private void compileWord() throws LeekCompilerException {
325327
} else if (word.getType() == TokenType.RETURN) {
326328

327329
var token = mTokens.eat();
330+
var optional = false;
331+
if (mTokens.get().getWord().equals("?")) {
332+
optional = true;
333+
mTokens.eat();
334+
}
328335
Expression exp = null;
329336
if (mTokens.get().getType() != TokenType.END_INSTRUCTION && mTokens.get().getType() != TokenType.ACCOLADE_RIGHT) {
330337
exp = readExpression();
331338
}
332339
if (mTokens.hasMoreTokens() && mTokens.get().getType() == TokenType.END_INSTRUCTION) {
333340
mTokens.skip();
334341
}
335-
mCurentBlock.addInstruction(this, new LeekReturnInstruction(token, exp));
342+
mCurentBlock.addInstruction(this, new LeekReturnInstruction(token, exp, optional));
336343
return;
337344

338345
} else if (word.getType() == TokenType.FOR) {
@@ -1374,7 +1381,7 @@ public Expression readExpression(boolean inList, boolean inSet, boolean inInterv
13741381

13751382
// Expression seule
13761383
var body = readExpression();
1377-
block.addInstruction(this, new LeekReturnInstruction(lambdaToken, body));
1384+
block.addInstruction(this, new LeekReturnInstruction(lambdaToken, body, false));
13781385
}
13791386

13801387
if (surroudingParenthesis) {
@@ -2181,4 +2188,8 @@ public String getCurrentClassVariable() {
21812188
}
21822189
return null;
21832190
}
2191+
2192+
public Options getOptions() {
2193+
return options;
2194+
}
21842195
}

src/main/java/leekscript/compiler/bloc/AbstractLeekBlock.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,15 @@ public String getCode() {
153153
@Override
154154
public void writeJavaCode(MainLeekBlock mainblock, JavaWriter writer) {
155155
int i = 0;
156+
writer.lastInstruction = false;
156157
for (LeekInstruction instruction : mInstructions) {
157158
i++;
158-
writer.lastInstruction = i == mInstructions.size();
159-
if (writer.lastInstruction && this instanceof MainLeekBlock) {
159+
var last = i == mInstructions.size();
160+
if (last && this instanceof MainLeekBlock) {
160161
if (instruction instanceof LeekExpressionInstruction) {
161162
mainblock.writeBeforeReturn(writer);
162163
writer.addCode("return ");
164+
writer.lastInstruction = true;
163165
instruction.writeJavaCode(mainblock, writer);
164166
} else {
165167
instruction.writeJavaCode(mainblock, writer);
@@ -172,6 +174,9 @@ public void writeJavaCode(MainLeekBlock mainblock, JavaWriter writer) {
172174
instruction.writeJavaCode(mainblock, writer);
173175
}
174176
}
177+
if (this instanceof MainLeekBlock && mInstructions.size() == 0) {
178+
writer.addLine("return null;");
179+
}
175180
}
176181

177182
@Override

src/main/java/leekscript/compiler/bloc/ForeachKeyBlock.java

+1
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ public void writeJavaCode(MainLeekBlock mainblock, JavaWriter writer) {
271271
mainblock.getWordCompiler().setCurrentBlock(initialBlock);
272272

273273
writer.addLine(sb.toString(), getLocation());
274+
writer.addCounter(1);
274275
// Instructions
275276
super.writeJavaCode(mainblock, writer);
276277

src/main/java/leekscript/compiler/bloc/MainLeekBlock.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public boolean includeAIFirstPass(WordCompiler compiler, String path) throws Lee
151151
mIncludedFirstPass.add(ai);
152152
var previousAI = mCompiler.getCurrentAI();
153153
mCompiler.setCurrentAI(ai);
154-
WordCompiler newCompiler = new WordCompiler(ai, compiler.getVersion());
154+
WordCompiler newCompiler = new WordCompiler(ai, compiler.getVersion(), compiler.getOptions());
155155
newCompiler.setMainBlock(this);
156156
newCompiler.firstPass();
157157
compiler.getAI().getErrors().addAll(ai.getErrors());
@@ -176,7 +176,7 @@ public boolean includeAI(WordCompiler compiler, String path) throws LeekCompiler
176176
mIncluded.add(ai);
177177
var previousAI = mCompiler.getCurrentAI();
178178
mCompiler.setCurrentAI(ai);
179-
WordCompiler newCompiler = new WordCompiler(ai, compiler.getVersion());
179+
WordCompiler newCompiler = new WordCompiler(ai, compiler.getVersion(), compiler.getOptions());
180180
newCompiler.setMainBlock(this);
181181
newCompiler.secondPass();
182182
compiler.getAI().getErrors().addAll(ai.getErrors());
@@ -355,7 +355,7 @@ public void writeJavaCode(JavaWriter writer, String className, String AIClass, O
355355
if (user_function != null) {
356356
user_function.compileAnonymousFunction(this, writer);
357357
} else {
358-
var system_function = LeekFunctions.getValue(redefined);
358+
var system_function = LeekFunctions.getValue(redefined, writer.getOptions().useExtra());
359359
writer.generateAnonymousSystemFunction(system_function);
360360
writer.addCode(system_function.getStandardClass() + "_" + redefined);
361361
}

src/main/java/leekscript/compiler/expression/LeekFunctionCall.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ else if (!mExpression.getType().canBeCallable()) {
405405
}
406406
if (!ok) {
407407
// Est-ce que c'est une fonction système ?
408-
system_function = LeekFunctions.getValue(v.getName());
408+
system_function = LeekFunctions.getValue(v.getName(), compiler.getOptions().useExtra());
409409
if (system_function != null) {
410410
verifySystemFunction(compiler, v, system_function);
411411

@@ -441,7 +441,7 @@ else if (!mExpression.getType().canBeCallable()) {
441441
}
442442
if (!ok) {
443443
// Est-ce que c'est une fonction système ?
444-
system_function = LeekFunctions.getValue(v.getName());
444+
system_function = LeekFunctions.getValue(v.getName(), compiler.getOptions().useExtra());
445445
if (system_function != null) {
446446
verifySystemFunction(compiler, v, system_function);
447447
if (mParameters.size() >= system_function.getArgumentsMin() && mParameters.size() <= system_function.getArguments()) {
@@ -469,7 +469,7 @@ else if (!mExpression.getType().canBeCallable()) {
469469
if (compiler.getMainBlock().isRedefinedFunction(v.getName())) {
470470
// Nothing
471471
} else {
472-
system_function = LeekFunctions.getValue(v.getName());
472+
system_function = LeekFunctions.getValue(v.getName(), compiler.getOptions().useExtra());
473473
verifySystemFunction(compiler, v, system_function);
474474
}
475475

src/main/java/leekscript/compiler/expression/LeekVariable.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public void preAnalyze(WordCompiler compiler) throws LeekCompilerException {
176176
return;
177177
}
178178
// LS functions
179-
var lf = LeekFunctions.getValue(token.getWord());
179+
var lf = LeekFunctions.getValue(token.getWord(), compiler.getOptions().useExtra());
180180
if (lf != null) {
181181
this.type = VariableType.SYSTEM_FUNCTION;
182182
this.variableType = lf.getVersions()[0].getType();
@@ -279,7 +279,7 @@ else if (constant.getType() == Type.REAL) {
279279
if (user_function != null) {
280280
user_function.compileAnonymousFunction(mainblock, writer);
281281
} else {
282-
var system_function = LeekFunctions.getValue(token.getWord());
282+
var system_function = LeekFunctions.getValue(token.getWord(), writer.getOptions().useExtra());
283283
writer.generateAnonymousSystemFunction(system_function);
284284
// String namespace = LeekFunctions.getNamespace(token.getWord());
285285
writer.addCode(system_function.getStandardClass() + "_" + token.getWord());
@@ -334,7 +334,7 @@ public void compileL(MainLeekBlock mainblock, JavaWriter writer) {
334334
if (user_function != null) {
335335
user_function.compileAnonymousFunction(mainblock, writer);
336336
} else {
337-
var system_function = LeekFunctions.getValue(token.getWord());
337+
var system_function = LeekFunctions.getValue(token.getWord(), writer.getOptions().useExtra());
338338
writer.generateAnonymousSystemFunction(system_function);
339339
// String namespace = LeekFunctions.getNamespace(token.getWord());
340340
writer.addCode(system_function.getStandardClass() + "_" + token.getWord());

src/main/java/leekscript/compiler/instruction/LeekExpressionInstruction.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,11 @@ public void writeJavaCode(MainLeekBlock mainblock, JavaWriter writer) {
5252
if (trimmed instanceof LeekTernaire || trimmed instanceof LeekFunctionCall || (trimmed instanceof LeekExpression && ((LeekExpression) trimmed).needsWrapper())) {
5353
if (writer.isOperationsEnabled() && trimmed.getOperations() > 0) writer.addCode("ops(");
5454
else if (!writer.lastInstruction) writer.addCode("nothing(");
55+
var last = writer.lastInstruction;
56+
writer.lastInstruction = false;
5557
trimmed.writeJavaCode(mainblock, writer);
5658
if (writer.isOperationsEnabled() && trimmed.getOperations() > 0) writer.addCode(", " + trimmed.getOperations() + ")");
57-
else if (!writer.lastInstruction) writer.addCode(")");
59+
else if (!last) writer.addCode(")");
5860
} else {
5961
if (writer.isOperationsEnabled() && trimmed.getOperations() > 0) writer.addCode("ops(");
6062
trimmed.writeJavaCode(mainblock, writer);

src/main/java/leekscript/compiler/instruction/LeekReturnInstruction.java

+19-8
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ public class LeekReturnInstruction extends LeekInstruction {
1818

1919
private final Token token;
2020
private final Expression expression;
21+
private final boolean optional;
2122
private Type returnType;
2223

23-
public LeekReturnInstruction(Token token, Expression exp) {
24+
public LeekReturnInstruction(Token token, Expression exp, boolean optional) {
2425
this.token = token;
2526
this.expression = exp;
27+
this.optional = optional;
2628
}
2729

2830
@Override
@@ -70,7 +72,9 @@ public void analyze(WordCompiler compiler) throws LeekCompilerException {
7072

7173
@Override
7274
public void writeJavaCode(MainLeekBlock mainblock, JavaWriter writer) {
73-
mainblock.writeBeforeReturn(writer);
75+
if (writer.currentBlock == mainblock) {
76+
mainblock.writeBeforeReturn(writer);
77+
}
7478
if (expression == null) {
7579
writer.addPosition(token);
7680
writer.addCode("return null;");
@@ -79,19 +83,26 @@ public void writeJavaCode(MainLeekBlock mainblock, JavaWriter writer) {
7983
if (finalExpression.getOperations() > 0) {
8084
writer.addCode("ops(" + finalExpression.getOperations() + "); ");
8185
}
82-
writer.addCode("return ");
83-
if (mainblock.getWordCompiler().getVersion() == 1) {
84-
finalExpression.compileL(mainblock, writer);
85-
} else {
86+
if (optional) {
87+
String r = "r" + mainblock.getCount();
88+
writer.addCode(returnType.getJavaName(mainblock.getVersion()) + " " + r + " = ");
8689
writer.compileConvert(mainblock, 0, finalExpression, returnType);
90+
writer.addLine("; if (bool(" + r + ")) return " + r + ";", getLocation());
91+
} else {
92+
writer.addCode("return ");
93+
if (mainblock.getWordCompiler().getVersion() == 1) {
94+
finalExpression.compileL(mainblock, writer);
95+
} else {
96+
writer.compileConvert(mainblock, 0, finalExpression, returnType);
97+
}
98+
writer.addLine(";", getLocation());
8799
}
88-
writer.addLine(";", getLocation());
89100
}
90101
}
91102

92103
@Override
93104
public int getEndBlock() {
94-
return 1;
105+
return optional ? 0 : 1;
95106
}
96107

97108
@Override

0 commit comments

Comments
 (0)