BruceSherwood/glowscript

destructuring assignments don't work

Closed this issue · 2 comments

context

#! /usr/bin/python
from __future__ import print_function
(x, y) = [123, 'x']
print(x, y)

The LHS here is called a destructuring assignment. It is a standard feature of virtually all modern high-level languages.

Desired and Expected Behavior (standard python)

123 x

Observed behavior (firefox)

Runtime error: 'assignment to undeclared variable x'
From __$main  parent: 207  runline: 246  char: 17  cmpline: 40  pyline: -3
>>> /*    28 */     ▼[x,y,] = [123,"x",];
From __func  parent: 207  runline: 209  char: 178  cmpline: 3  pyline: undefined
>>> ⊕__g.frame;__g.frame=frame;try{▼body()}catch(e){__g.setEF(e,frame.prev)⊕
From main  parent: 207  runline: 227  char: 1  cmpline: 21  pyline: undefined

This is fixable by fiddling with the python source, but there is bigger game afoot:

Observed behavior (chrome)

Unable to load compiled code.... thrown errmsg: (Invalid left-hand side in assignment) file: (undefined) line: (undefined)
(program):215 ReferenceError: Invalid left-hand side in assignment
    at driver (eval at doit (file:///mnt/more/jsd/physics/glorpy-play.html:84:25), <anonymous>:207:25)
    at doit (file:///mnt/more/jsd/physics/glorpy-play.html:93:7)
    at onload (file:///mnt/more/jsd/physics/glorpy-play.html:114:115)

The problem is, the browser-based compiler implements destructuring in a way that does not work on chrome, IE, or opera. Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment

Maybe in a few years all browsers will support something like this. However, in the meantime, the compiler should generate code to implement destructuring in terms of features that are reliably available at runtime. This is, after all, the métier and the raison d'être of a compiler: to implement high-level constructs in terms of lower-level instructions.

Maybe the "direct" implementation could be available as an optional optimization, but for right now reliability and portability should take precedence over optimization.

This is almost certainly an issue with RapydScript, not GlowScript.

it is only (x,y) = [10,20] that fails.

x,y = [10,20] works

[x,y] = [10,20] works