armoha / euddraft

System for pluginizing eudplib codes.
Other
29 stars 4 forks source link

Parser error in `func()[subscript] = value;`, needs extra parentheses #114

Open armoha opened 1 year ago

armoha commented 1 year ago
// epScript example
function onPluginStart() {
    const X = EUDArray(10);
    foreach (i : py_range(10)) {
        X[i] = EUDArray(10);
    }

    // ▽ generates mistyped function since 0.8.2.4 to 0.9.9.7. (fixed in euddraft 0.9.9.8)
    const x2 = function (index) : EUDArray { return X[index]; };

    x2(2)[3] = 4;  // General syntax error
    (x2(2))[3] = 4;  // Okay
}

Why hard to fix?

epScript has 'return value selection' syntax:

const foo = function () { return 0, 1, 2, 3; };

const zero = foo()[[0]];  // choose 0th return value
const one, two = foo()[[1, 2]];  // choose 1st and 2nd return values
const three = foo()[[3]];

and epScript also has array-init expression:

const a = [1, 2, 3];  // declare 3-sized array and initialize it with 1, 2 and 3

const bar = foo (arr: EUDArray) { return arr[0]; };
bar([0, 1, 2, 3]);  // okay, returns 0

If func(arg)[subscript] = value; were allowed, there should be syntax ambiguity:

foo()[[0]];
// can be interpreted in 2 ways:
// (1) choose 0th value
// (2) subscript with 1-sized array initialized with 0