xtjoeytx / GServer-v2

GNU General Public License v3.0
7 stars 10 forks source link

[GS2-Parser] Bitwise-operators not supported #76

Closed tricxta closed 1 year ago

tricxta commented 1 year ago

The following bit-wise operators are not yet supported:

xtjoeytx commented 1 year ago

Good catch, the parser seems good from a quick look - need to execute the bytecode for the operations.

xtjoeytx commented 1 year ago

Tested with following code (may try to make a test-suite in gs2)

//#CLIENTSIDE
//#GS2
const VERBOSE = true;

function _evalresult(result) {
  return result ? "SUCCESS" : "FAILURE";
}

function _assert(val1, val2, msg) {
  temp.result = (val1 == val2);
  if (msg) {
    if (VERBOSE) {
      echo(format("Assert test for %s: %s == %s: %s", msg, val1, val2, _evalresult(temp.result)));
    }

    if (!temp.result) {
      echo(format("Assert failed for %s: %s == %s", msg, val1, val2));
    }
  }

  return temp.result;
}

function onCreated() {
  echo("Executing tests for " @ this.name);

  temp.test = 13 xor 2 + 319385 xor 255;
  _assert(temp.test, 319337, "bitwise-xor");

  temp.test = 395 | 14;
  _assert(temp.test, 399, "bitwise-or");

  temp.test = 395 << 3;
  _assert(temp.test, 3160, "bitwise-shift-left");

  temp.test = 395 >> 3;
  _assert(temp.test, 49, "bitwise-shift-right");

  temp.test = ~-~~-395;
  _assert(temp.test, -396, "bitwise-invert");

  temp.test = 17;
  temp.test <<= 3;
  _assert(temp.test, 136, "bitwise-shift-left-assign");

  temp.test >>= 5;
  _assert(temp.test, 4, "bitwise-shift-right-assign");

  temp.test = 17;
  temp.test %= 2;
  _assert(temp.test, 1, "mod-assign");
}

results:

Executing tests for tests/bitwiseops
Assert test for bitwise-xor: 319337 == 319337: SUCCESS
Assert test for bitwise-or: 399 == 399: SUCCESS
Assert test for bitwise-shift-left: 3160 == 3160: SUCCESS
Assert test for bitwise-shift-right: 49 == 49: SUCCESS
Assert test for bitwise-invert: -396 == -396: SUCCESS
Assert test for bitwise-shift-left-assign: 136 == 136: SUCCESS
Assert test for bitwise-shift-right-assign: 4 == 4: SUCCESS
Assert test for mod-assign: 1 == 1: SUCCESS