From 43f4fd68d57ede8546c432924577379675c1480a Mon Sep 17 00:00:00 2001 From: Pilow Date: Sun, 1 Oct 2023 11:08:38 +0200 Subject: [PATCH] Fix typed global operators --- .../compiler/expression/LeekVariable.java | 35 +++++++++++++++++++ src/test/java/test/TestGlobals.java | 13 +++++++ 2 files changed, 48 insertions(+) diff --git a/src/main/java/leekscript/compiler/expression/LeekVariable.java b/src/main/java/leekscript/compiler/expression/LeekVariable.java index f7195feb..54bd8b2b 100644 --- a/src/main/java/leekscript/compiler/expression/LeekVariable.java +++ b/src/main/java/leekscript/compiler/expression/LeekVariable.java @@ -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)"); } @@ -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)"); } @@ -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)"); } @@ -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)"); } @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); @@ -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); diff --git a/src/test/java/test/TestGlobals.java b/src/test/java/test/TestGlobals.java index f547e92f..d736d821 100644 --- a/src/test/java/test/TestGlobals.java +++ b/src/test/java/test/TestGlobals.java @@ -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");