| 22194 | } |
| 22195 | |
| 22196 | expression(expr, context) { |
| 22197 | const ast = this.ast; |
| 22198 | const builtins = this.builtins; |
| 22199 | const typing = this.typing; |
| 22200 | const self = context.get('self'); |
| 22201 | switch (expr.__class__.__name__) { |
| 22202 | case 'Assign': { |
| 22203 | const [target] = expr.targets; |
| 22204 | if (target instanceof ast.Name) { |
| 22205 | const value = this.expression(expr.value, context); |
| 22206 | context.set(target.id, value); |
| 22207 | return undefined; |
| 22208 | } else if (target instanceof ast.Subscript) { |
| 22209 | if (target.value instanceof ast.Name && |
| 22210 | target.slice instanceof ast.List && |
| 22211 | target.slice.elts.length === 1) { |
| 22212 | const index = this.expression(target.slice.elts[0], context); |
| 22213 | const id = target.value.id; |
| 22214 | if (id === '__annotations__') { |
| 22215 | context.set(id, context.get(id) || {}); |
| 22216 | } |
| 22217 | const obj = context.get(id); |
| 22218 | const value = this.expression(expr.value, context); |
| 22219 | if (obj instanceof Map) { |
| 22220 | obj.set(index, value); |
| 22221 | } else { |
| 22222 | obj[index] = value; |
| 22223 | } |
| 22224 | return undefined; |
| 22225 | } |
| 22226 | } else if (target instanceof ast.Attribute) { |
| 22227 | const obj = this.expression(target.value, context); |
| 22228 | const value = this.expression(expr.value, context); |
| 22229 | obj[target.attr] = value; |
| 22230 | return undefined; |
| 22231 | } else if (target instanceof ast.Tuple) { |
| 22232 | context.target.push(target.elts); |
| 22233 | const value = this.expression(expr.value, context); |
| 22234 | context.target.pop(); |
| 22235 | if (target.elts.every((elt) => elt instanceof ast.Name)) { |
| 22236 | if (target.elts.length < value.length) { |
| 22237 | throw new python.Error(`ValueError: too many values to unpack (expected ${target.value.length}, actual ${value.length}).`); |
| 22238 | } |
| 22239 | if (target.elts.length > value.length) { |
| 22240 | throw new python.Error(`ValueError: not enough values to unpack (expected ${target.value.length}, actual ${value.length}).`); |
| 22241 | } |
| 22242 | for (let i = 0; i < value.length; i++) { |
| 22243 | context.set(target.elts[i].id, value[i]); |
| 22244 | } |
| 22245 | return undefined; |
| 22246 | } |
| 22247 | } |
| 22248 | break; |
| 22249 | } |
| 22250 | case 'List': { |
| 22251 | return expr.elts.map((expr) => this.expression(expr, context)); |
| 22252 | } |
| 22253 | case 'Constant': { |