Skip to content

Commit

Permalink
Fix typed global operators
Browse files Browse the repository at this point in the history
  • Loading branch information
5pilow committed Oct 1, 2023
1 parent 8a15ef7 commit 43f4fd6
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/main/java/leekscript/compiler/expression/LeekVariable.java
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,8 @@ public void compileIncrement(MainLeekBlock mainblock, JavaWriter writer) {
} else if (type == VariableType.GLOBAL) {
if (isBox()) {
writer.addCode("g_" + token.getWord() + ".increment()");
} else if (this.variableType.isPrimitiveNumber()) {
writer.addCode("g_" + token.getWord() + "++");
} else {
writer.addCode("sub(g_" + token.getWord() + " = add(g_" + token.getWord() + ", 1l), 1l)");
}
Expand All @@ -502,6 +504,8 @@ public void compileDecrement(MainLeekBlock mainblock, JavaWriter writer) {
} else if (type == VariableType.GLOBAL) {
if (isBox()) {
writer.addCode("g_" + token.getWord() + ".decrement()");
} else if (this.variableType.isPrimitiveNumber()) {
writer.addCode("g_" + token.getWord() + "--");
} else {
writer.addCode("add(g_" + token.getWord() + " = sub(g_" + token.getWord() + ", 1l), 1l)");
}
Expand All @@ -525,6 +529,8 @@ public void compilePreIncrement(MainLeekBlock mainblock, JavaWriter writer) {
} else if (type == VariableType.GLOBAL) {
if (isBox()) {
writer.addCode("g_" + token.getWord() + ".pre_increment()");
} else if (this.variableType.isPrimitiveNumber()) {
writer.addCode("++g_" + token.getWord());
} else {
writer.addCode("g_" + token.getWord() + " = add(g_" + token.getWord() + ", 1l)");
}
Expand All @@ -548,6 +554,8 @@ public void compilePreDecrement(MainLeekBlock mainblock, JavaWriter writer) {
} else if (type == VariableType.GLOBAL) {
if (isBox()) {
writer.addCode("g_" + token.getWord() + ".pre_decrement()");
} else if (this.variableType.isPrimitiveNumber()) {
writer.addCode("--g_" + token.getWord());
} else {
writer.addCode("g_" + token.getWord() + " = sub(g_" + token.getWord() + ", 1l)");
}
Expand Down Expand Up @@ -628,6 +636,9 @@ public void compileSubEq(MainLeekBlock mainblock, JavaWriter writer, Expression
writer.addCode("g_" + token.getWord() + ".sub_eq(");
expr.writeJavaCode(mainblock, writer);
writer.addCode(")");
} else if (this.variableType.isNumber() && expr.getType().isPrimitiveNumber()) {
writer.addCode("g_" + token.getWord() + " -= ");
expr.writeJavaCode(mainblock, writer);
} else {
writer.addCode("g_" + token.getWord() + " = sub(g_" + token.getWord() + ", ");
expr.writeJavaCode(mainblock, writer);
Expand Down Expand Up @@ -668,6 +679,9 @@ public void compileMulEq(MainLeekBlock mainblock, JavaWriter writer, Expression
writer.addCode("g_" + token.getWord() + ".mul_eq(");
expr.writeJavaCode(mainblock, writer);
writer.addCode(")");
} else if (this.variableType.isNumber() && expr.getType().isPrimitiveNumber()) {
writer.addCode("g_" + token.getWord() + " *= ");
expr.writeJavaCode(mainblock, writer);
} else {
writer.addCode("g_" + token.getWord() + " = mul(g_" + token.getWord() + ", ");
expr.writeJavaCode(mainblock, writer);
Expand Down Expand Up @@ -834,6 +848,9 @@ public void compileModEq(MainLeekBlock mainblock, JavaWriter writer, Expression
writer.addCode("g_" + token.getWord() + ".mod_eq(");
expr.writeJavaCode(mainblock, writer);
writer.addCode(")");
} else if (this.variableType.isPrimitiveNumber()) {
writer.addCode("g_" + token.getWord() + " %= ");
expr.writeJavaCode(mainblock, writer);
} else {
writer.addCode("g_" + token.getWord() + " = mod(g_" + token.getWord() + ", ");
expr.writeJavaCode(mainblock, writer);
Expand Down Expand Up @@ -874,6 +891,9 @@ public void compileBitOrEq(MainLeekBlock mainblock, JavaWriter writer, Expressio
writer.addCode("g_" + token.getWord() + ".bor_eq(");
expr.writeJavaCode(mainblock, writer);
writer.addCode(")");
} else if (this.variableType.isPrimitiveNumber()) {
writer.addCode("g_" + token.getWord() + " |= ");
expr.writeJavaCode(mainblock, writer);
} else {
writer.addCode("g_" + token.getWord() + " = bor(g_" + token.getWord() + ", ");
expr.writeJavaCode(mainblock, writer);
Expand Down Expand Up @@ -911,6 +931,9 @@ public void compileBitAndEq(MainLeekBlock mainblock, JavaWriter writer, Expressi
writer.addCode("g_" + token.getWord() + ".band_eq(");
expr.writeJavaCode(mainblock, writer);
writer.addCode(")");
} else if (this.variableType.isPrimitiveNumber()) {
writer.addCode("g_" + token.getWord() + " &= ");
expr.writeJavaCode(mainblock, writer);
} else {
writer.addCode("g_" + token.getWord() + " = band(g_" + token.getWord() + ", ");
expr.writeJavaCode(mainblock, writer);
Expand Down Expand Up @@ -947,6 +970,9 @@ public void compileBitXorEq(MainLeekBlock mainblock, JavaWriter writer, Expressi
writer.addCode("g_" + token.getWord() + ".bxor_eq(");
expr.writeJavaCode(mainblock, writer);
writer.addCode(")");
} else if (this.variableType.isPrimitiveNumber()) {
writer.addCode("g_" + token.getWord() + " ^= ");
expr.writeJavaCode(mainblock, writer);
} else {
writer.addCode("g_" + token.getWord() + " = bxor(g_" + token.getWord() + ", ");
expr.writeJavaCode(mainblock, writer);
Expand Down Expand Up @@ -983,6 +1009,9 @@ public void compileShiftLeftEq(MainLeekBlock mainblock, JavaWriter writer, Expre
writer.addCode("g_" + token.getWord() + ".shl_eq(");
expr.writeJavaCode(mainblock, writer);
writer.addCode(")");
} else if (this.variableType.isPrimitiveNumber()) {
writer.addCode("g_" + token.getWord() + " <<= ");
expr.writeJavaCode(mainblock, writer);
} else {
writer.addCode("g_" + token.getWord() + " = shl(g_" + token.getWord() + ", ");
expr.writeJavaCode(mainblock, writer);
Expand Down Expand Up @@ -1019,6 +1048,9 @@ public void compileShiftRightEq(MainLeekBlock mainblock, JavaWriter writer, Expr
writer.addCode("g_" + token.getWord() + ".shr_eq(");
expr.writeJavaCode(mainblock, writer);
writer.addCode(")");
} else if (this.variableType.isPrimitiveNumber()) {
writer.addCode("g_" + token.getWord() + " >>= ");
expr.writeJavaCode(mainblock, writer);
} else {
writer.addCode("g_" + token.getWord() + " = shr(g_" + token.getWord() + ", ");
expr.writeJavaCode(mainblock, writer);
Expand Down Expand Up @@ -1055,6 +1087,9 @@ public void compileShiftUnsignedRightEq(MainLeekBlock mainblock, JavaWriter writ
writer.addCode("g_" + token.getWord() + ".ushr_eq(");
expr.writeJavaCode(mainblock, writer);
writer.addCode(")");
} else if (this.variableType.isPrimitiveNumber()) {
writer.addCode("g_" + token.getWord() + " >>>= ");
expr.writeJavaCode(mainblock, writer);
} else {
writer.addCode("g_" + token.getWord() + " = ushr(g_" + token.getWord() + ", ");
expr.writeJavaCode(mainblock, writer);
Expand Down
13 changes: 13 additions & 0 deletions src/test/java/test/TestGlobals.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,32 @@ public void run() throws Exception {

section("Globals operators");
code("global x = 12; x++; return x;").equals("13");
code("global integer x = 12; x++; return x;").equals("13");
code("global x = 12; x--; return x;").equals("11");
code("global integer x = 12; x--; return x;").equals("11");
code("global x = 12; ++x; return x;").equals("13");
code("global integer x = 12; ++x; return x;").equals("13");
code("global x = 12; --x; return x;").equals("11");
code("global integer x = 12; --x; return x;").equals("11");
code("global x = 12; x += 5; return x;").equals("17");
code("global integer x = 12; x += 5; return x;").equals("17");
code("global x = 12; x -= 5; return x;").equals("7");
code("global integer x = 12; x -= 5; return x;").equals("7");
code("global x = 12; x *= 5; return x;").equals("60");
code("global integer x = 12; x *= 5; return x;").equals("60");
code_v1("global x = 12; x /= 5; return x;").equals("2,4");
code_v2_("global x = 12; x /= 5; return x;").equals("2.4");
code_v2_("global real x = 12; x /= 5; return x;").equals("2.4");
code("global x = 12; x %= 5; return x;").equals("2");
code("global integer x = 12; x %= 5; return x;").equals("2");
code("global x = 2; x **= 5; return x;").equals("32");
code("global integer x = 2; x **= 5; return x;").equals("32");
code("global x = 12; x |= 5; return x;").equals("13");
code("global integer x = 12; x |= 5; return x;").equals("13");
code("global x = 12; x &= 5; return x;").equals("4");
code("global integer x = 12; x &= 5; return x;").equals("4");
code_v1("global x = 12; x ^= 5; return x;").equals("248832");
code_v1("global integer x = 12; x ^= 5; return x;").equals("248832");
code_v2_("global x = 12; x ^= 5; return x;").equals("9");
code("global x = 12; return x == 5;").equals("false");
code("global x = 12; return x === 5;").equals("false");
Expand Down

0 comments on commit 43f4fd6

Please sign in to comment.