Skip to content

Commit

Permalink
java 21, console
Browse files Browse the repository at this point in the history
  • Loading branch information
5pilow committed May 3, 2024
1 parent ce431b1 commit 8a88aeb
Show file tree
Hide file tree
Showing 31 changed files with 308 additions and 52 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ jobs:

steps:
- uses: actions/checkout@v2
- name: Set up JDK 17
- name: Set up JDK 21
uses: actions/setup-java@v2
with:
java-version: '17'
java-version: '21'
distribution: 'adopt'
- name: Setup locale
run: |
Expand Down
4 changes: 2 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
apply plugin: 'java'

sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21

sourceSets {
test {
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/leekscript/AILog.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package leekscript;

import com.alibaba.fastjson.JSONArray;

import leekscript.runner.AI;
import leekscript.runner.LeekRunException;

public abstract class AILog {

public interface Stream {
public void write(JSONArray a);
}

public final static int STANDARD = 1;
public final static int WARNING = 2;
public final static int ERROR = 3;
Expand Down Expand Up @@ -35,4 +41,6 @@ public boolean addSize(int size) {
public boolean isFull() {
return mSize >= MAX_LENGTH;
}

public abstract void setStream(Stream stream);
}
1 change: 0 additions & 1 deletion src/main/java/leekscript/TopLevel.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import leekscript.compiler.LeekScript;
import leekscript.compiler.Options;
import leekscript.runner.AI;
import leekscript.runner.LeekRunException;
import leekscript.runner.Session;

public class TopLevel {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/leekscript/common/Type.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class Type {
public static final Type ARRAY = array(Type.ANY);
public static final Type EMPTY_ARRAY = array(Type.VOID);
public static final Type SET = set(Type.ANY);
public static final Type SET_REAL = set(Type.REAL);
public static final Type SET_INT = set(Type.INT);
public static final Type EMPTY_SET = set(Type.VOID);
public static final Type INTERVAL = new IntervalType(Type.ANY);
public static final Type EMPTY_INTERVAL = new IntervalType(Type.VOID);
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/leekscript/compiler/AIFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public String toJson() {

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

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

LeekScript.getFileSystem().loadDependencies(this);

Expand Down Expand Up @@ -180,4 +180,8 @@ public String toString() {
public boolean isStrict() {
return strict;
}

public Options getOptions() {
return new Options(version, strict, true, true, null, true);
}
}
8 changes: 4 additions & 4 deletions src/main/java/leekscript/compiler/IACompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public AnalyzeResult analyze(AIFile ai) throws LeekCompilerException {
ai.clearErrors();
// On lance la compilation du code de l'IA
// Si on est là c'est qu'on a une liste de words correcte, on peut commencer à lire
WordCompiler compiler = new WordCompiler(ai, ai.getVersion());
WordCompiler compiler = new WordCompiler(ai, ai.getVersion(), ai.getOptions());
MainLeekBlock main = new MainLeekBlock(this, compiler, ai);
main.setWordCompiler(compiler);

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

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

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

compiler.readCode();
compiler.analyze();
// System.out.println("errors " + compiler.getErrors().size());
// System.out.println("errors " + ai.getErrors().size());

if (ai.getErrors().size() > 0) {
for (var error : ai.getErrors()) {
Expand All @@ -139,7 +139,7 @@ public AICode compile(AIFile ai, String AIClass, Options options) throws LeekCom
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());
WordCompiler compiler = new WordCompiler(ai, ai.getVersion(), ai.getOptions());
MainLeekBlock main = new MainLeekBlock(this, compiler, ai);
main.setWordCompiler(compiler);
compiler.readCode();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/leekscript/compiler/JavaWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -463,4 +463,8 @@ public boolean isInConstructor() {
public boolean isOperationsEnabled() {
return operationsEnabled;
}

public Options getOptions() {
return options;
}
}
8 changes: 4 additions & 4 deletions src/main/java/leekscript/compiler/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import leekscript.runner.Session;

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

public Options() {
this(LeekScript.LATEST_VERSION, false, false, false, null);
this(LeekScript.LATEST_VERSION, false, false, false, null, true);
}
public Options(boolean operations) {
this(LeekScript.LATEST_VERSION, false, false, operations, null);
this(LeekScript.LATEST_VERSION, false, false, operations, null, true);
}
public Options(Session session) {
this(LeekScript.LATEST_VERSION, false, true, false, session);
this(LeekScript.LATEST_VERSION, false, true, true, session, true);
}
}
17 changes: 14 additions & 3 deletions src/main/java/leekscript/compiler/WordCompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ public class WordCompiler {
private int mLine;
private AIFile mAI = null;
private final int version;
private final Options options;

public WordCompiler(AIFile ai, int version) {
public WordCompiler(AIFile ai, int version, Options options) {
mAI = ai;
this.version = version;
this.options = options;
}

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

var token = mTokens.eat();
var optional = false;
if (mTokens.get().getWord().equals("?")) {
optional = true;
mTokens.eat();
}
Expression exp = null;
if (mTokens.get().getType() != TokenType.END_INSTRUCTION && mTokens.get().getType() != TokenType.ACCOLADE_RIGHT) {
exp = readExpression();
}
if (mTokens.hasMoreTokens() && mTokens.get().getType() == TokenType.END_INSTRUCTION) {
mTokens.skip();
}
mCurentBlock.addInstruction(this, new LeekReturnInstruction(token, exp));
mCurentBlock.addInstruction(this, new LeekReturnInstruction(token, exp, optional));
return;

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

// Expression seule
var body = readExpression();
block.addInstruction(this, new LeekReturnInstruction(lambdaToken, body));
block.addInstruction(this, new LeekReturnInstruction(lambdaToken, body, false));
}

if (surroudingParenthesis) {
Expand Down Expand Up @@ -2181,4 +2188,8 @@ public String getCurrentClassVariable() {
}
return null;
}

public Options getOptions() {
return options;
}
}
9 changes: 7 additions & 2 deletions src/main/java/leekscript/compiler/bloc/AbstractLeekBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -153,13 +153,15 @@ public String getCode() {
@Override
public void writeJavaCode(MainLeekBlock mainblock, JavaWriter writer) {
int i = 0;
writer.lastInstruction = false;
for (LeekInstruction instruction : mInstructions) {
i++;
writer.lastInstruction = i == mInstructions.size();
if (writer.lastInstruction && this instanceof MainLeekBlock) {
var last = i == mInstructions.size();
if (last && this instanceof MainLeekBlock) {
if (instruction instanceof LeekExpressionInstruction) {
mainblock.writeBeforeReturn(writer);
writer.addCode("return ");
writer.lastInstruction = true;
instruction.writeJavaCode(mainblock, writer);
} else {
instruction.writeJavaCode(mainblock, writer);
Expand All @@ -172,6 +174,9 @@ public void writeJavaCode(MainLeekBlock mainblock, JavaWriter writer) {
instruction.writeJavaCode(mainblock, writer);
}
}
if (this instanceof MainLeekBlock && mInstructions.size() == 0) {
writer.addLine("return null;");
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ public void writeJavaCode(MainLeekBlock mainblock, JavaWriter writer) {
mainblock.getWordCompiler().setCurrentBlock(initialBlock);

writer.addLine(sb.toString(), getLocation());
writer.addCounter(1);
// Instructions
super.writeJavaCode(mainblock, writer);

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/leekscript/compiler/bloc/MainLeekBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public boolean includeAIFirstPass(WordCompiler compiler, String path) throws Lee
mIncludedFirstPass.add(ai);
var previousAI = mCompiler.getCurrentAI();
mCompiler.setCurrentAI(ai);
WordCompiler newCompiler = new WordCompiler(ai, compiler.getVersion());
WordCompiler newCompiler = new WordCompiler(ai, compiler.getVersion(), compiler.getOptions());
newCompiler.setMainBlock(this);
newCompiler.firstPass();
compiler.getAI().getErrors().addAll(ai.getErrors());
Expand All @@ -176,7 +176,7 @@ public boolean includeAI(WordCompiler compiler, String path) throws LeekCompiler
mIncluded.add(ai);
var previousAI = mCompiler.getCurrentAI();
mCompiler.setCurrentAI(ai);
WordCompiler newCompiler = new WordCompiler(ai, compiler.getVersion());
WordCompiler newCompiler = new WordCompiler(ai, compiler.getVersion(), compiler.getOptions());
newCompiler.setMainBlock(this);
newCompiler.secondPass();
compiler.getAI().getErrors().addAll(ai.getErrors());
Expand Down Expand Up @@ -355,7 +355,7 @@ public void writeJavaCode(JavaWriter writer, String className, String AIClass, O
if (user_function != null) {
user_function.compileAnonymousFunction(this, writer);
} else {
var system_function = LeekFunctions.getValue(redefined);
var system_function = LeekFunctions.getValue(redefined, writer.getOptions().useExtra());
writer.generateAnonymousSystemFunction(system_function);
writer.addCode(system_function.getStandardClass() + "_" + redefined);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ else if (!mExpression.getType().canBeCallable()) {
}
if (!ok) {
// Est-ce que c'est une fonction système ?
system_function = LeekFunctions.getValue(v.getName());
system_function = LeekFunctions.getValue(v.getName(), compiler.getOptions().useExtra());
if (system_function != null) {
verifySystemFunction(compiler, v, system_function);

Expand Down Expand Up @@ -441,7 +441,7 @@ else if (!mExpression.getType().canBeCallable()) {
}
if (!ok) {
// Est-ce que c'est une fonction système ?
system_function = LeekFunctions.getValue(v.getName());
system_function = LeekFunctions.getValue(v.getName(), compiler.getOptions().useExtra());
if (system_function != null) {
verifySystemFunction(compiler, v, system_function);
if (mParameters.size() >= system_function.getArgumentsMin() && mParameters.size() <= system_function.getArguments()) {
Expand Down Expand Up @@ -469,7 +469,7 @@ else if (!mExpression.getType().canBeCallable()) {
if (compiler.getMainBlock().isRedefinedFunction(v.getName())) {
// Nothing
} else {
system_function = LeekFunctions.getValue(v.getName());
system_function = LeekFunctions.getValue(v.getName(), compiler.getOptions().useExtra());
verifySystemFunction(compiler, v, system_function);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ public void preAnalyze(WordCompiler compiler) throws LeekCompilerException {
return;
}
// LS functions
var lf = LeekFunctions.getValue(token.getWord());
var lf = LeekFunctions.getValue(token.getWord(), compiler.getOptions().useExtra());
if (lf != null) {
this.type = VariableType.SYSTEM_FUNCTION;
this.variableType = lf.getVersions()[0].getType();
Expand Down Expand Up @@ -279,7 +279,7 @@ else if (constant.getType() == Type.REAL) {
if (user_function != null) {
user_function.compileAnonymousFunction(mainblock, writer);
} else {
var system_function = LeekFunctions.getValue(token.getWord());
var system_function = LeekFunctions.getValue(token.getWord(), writer.getOptions().useExtra());
writer.generateAnonymousSystemFunction(system_function);
// String namespace = LeekFunctions.getNamespace(token.getWord());
writer.addCode(system_function.getStandardClass() + "_" + token.getWord());
Expand Down Expand Up @@ -334,7 +334,7 @@ public void compileL(MainLeekBlock mainblock, JavaWriter writer) {
if (user_function != null) {
user_function.compileAnonymousFunction(mainblock, writer);
} else {
var system_function = LeekFunctions.getValue(token.getWord());
var system_function = LeekFunctions.getValue(token.getWord(), writer.getOptions().useExtra());
writer.generateAnonymousSystemFunction(system_function);
// String namespace = LeekFunctions.getNamespace(token.getWord());
writer.addCode(system_function.getStandardClass() + "_" + token.getWord());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,11 @@ public void writeJavaCode(MainLeekBlock mainblock, JavaWriter writer) {
if (trimmed instanceof LeekTernaire || trimmed instanceof LeekFunctionCall || (trimmed instanceof LeekExpression && ((LeekExpression) trimmed).needsWrapper())) {
if (writer.isOperationsEnabled() && trimmed.getOperations() > 0) writer.addCode("ops(");
else if (!writer.lastInstruction) writer.addCode("nothing(");
var last = writer.lastInstruction;
writer.lastInstruction = false;
trimmed.writeJavaCode(mainblock, writer);
if (writer.isOperationsEnabled() && trimmed.getOperations() > 0) writer.addCode(", " + trimmed.getOperations() + ")");
else if (!writer.lastInstruction) writer.addCode(")");
else if (!last) writer.addCode(")");
} else {
if (writer.isOperationsEnabled() && trimmed.getOperations() > 0) writer.addCode("ops(");
trimmed.writeJavaCode(mainblock, writer);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ public class LeekReturnInstruction extends LeekInstruction {

private final Token token;
private final Expression expression;
private final boolean optional;
private Type returnType;

public LeekReturnInstruction(Token token, Expression exp) {
public LeekReturnInstruction(Token token, Expression exp, boolean optional) {
this.token = token;
this.expression = exp;
this.optional = optional;
}

@Override
Expand Down Expand Up @@ -70,7 +72,9 @@ public void analyze(WordCompiler compiler) throws LeekCompilerException {

@Override
public void writeJavaCode(MainLeekBlock mainblock, JavaWriter writer) {
mainblock.writeBeforeReturn(writer);
if (writer.currentBlock == mainblock) {
mainblock.writeBeforeReturn(writer);
}
if (expression == null) {
writer.addPosition(token);
writer.addCode("return null;");
Expand All @@ -79,19 +83,26 @@ public void writeJavaCode(MainLeekBlock mainblock, JavaWriter writer) {
if (finalExpression.getOperations() > 0) {
writer.addCode("ops(" + finalExpression.getOperations() + "); ");
}
writer.addCode("return ");
if (mainblock.getWordCompiler().getVersion() == 1) {
finalExpression.compileL(mainblock, writer);
} else {
if (optional) {
String r = "r" + mainblock.getCount();
writer.addCode(returnType.getJavaName(mainblock.getVersion()) + " " + r + " = ");
writer.compileConvert(mainblock, 0, finalExpression, returnType);
writer.addLine("; if (bool(" + r + ")) return " + r + ";", getLocation());
} else {
writer.addCode("return ");
if (mainblock.getWordCompiler().getVersion() == 1) {
finalExpression.compileL(mainblock, writer);
} else {
writer.compileConvert(mainblock, 0, finalExpression, returnType);
}
writer.addLine(";", getLocation());
}
writer.addLine(";", getLocation());
}
}

@Override
public int getEndBlock() {
return 1;
return optional ? 0 : 1;
}

@Override
Expand Down
Loading

0 comments on commit 8a88aeb

Please sign in to comment.