--- dist/index.mjs 2021-07-22 21:19:31.000000000 +0200 +++ index-patched.mjs 2021-07-22 21:15:30.000000000 +0200 @@ -11,6 +11,7 @@ var IMEMBER = 'IMEMBER'; var IENDSTATEMENT = 'IENDSTATEMENT'; var IARRAY = 'IARRAY'; +var IMEMBEREXPR = 'IMEMBEREXPR'; function Instruction(type, value) { this.type = type; @@ -174,7 +175,15 @@ nstack.push(n1 ? true : !!evaluate(n2, expr, values)); } else if (item.value === '=') { f = expr.binaryOps[item.value]; - nstack.push(f(n1, evaluate(n2, expr, values), values)); + // Name is created as in obj.property.arrayIdx.etc + let property = ''; + while (typeof n1 === 'string' && n1.startsWith('.')) { + property = n1 + property; + n1 = nstack.pop(); + } + nstack.push( + f(n1, evaluate(n2, expr, values), values, property !== '' ? property.slice(1) : undefined) + ); } else { f = expr.binaryOps[item.value]; nstack.push(f(resolveExpression(n1, values), resolveExpression(n2, values))); @@ -247,6 +256,14 @@ nstack.push(createExpressionEvaluator(item, expr)); } else if (type === IEXPREVAL) { nstack.push(item); + } else if (type === IMEMBEREXPR) { + if (item.value === '.') { + n1 = nstack.pop(); + // Create name as in obj.property.arrayIdx.etc + nstack.push('.' + n1); + } else { + nstack.push('.' + item.value); + } } else if (type === IMEMBER) { n1 = nstack.pop(); nstack.push(n1[item.value]); @@ -1137,13 +1154,28 @@ instr.push(new Instruction(IFUNDEF, varName.value)); continue; } - if (varName.type !== IVAR && varName.type !== IMEMBER) { - throw new Error('expected variable for assignment'); + if (varName.type === IOP2 && varName.value === '[') { + this.parseVariableAssignmentExpression(varValue); + instr.push(new Instruction(IMEMBEREXPR, '.')); + instr.push(new Instruction(IEXPR, varValue)); + instr.push(binaryInstruction('=')); + continue; } - this.parseVariableAssignmentExpression(varValue); - instr.push(new Instruction(IVARNAME, varName.value)); - instr.push(new Instruction(IEXPR, varValue)); - instr.push(binaryInstruction('=')); + if (varName.type === IMEMBER) { + this.parseVariableAssignmentExpression(varValue); + instr.push(new Instruction(IMEMBEREXPR, varName.value)); + instr.push(new Instruction(IEXPR, varValue)); + instr.push(binaryInstruction('=')); + continue; + } + if (varName.type === IVAR) { + this.parseVariableAssignmentExpression(varValue); + instr.push(new Instruction(IVARNAME, varName.value)); + instr.push(new Instruction(IEXPR, varValue)); + instr.push(binaryInstruction('=')); + continue; + } + throw new Error('expected variable for assignment'); } }; @@ -1566,8 +1598,22 @@ return +(value[0] + 'e' + (value[1] ? (+value[1] + exp) : exp)); } -function setVar(name, value, variables) { - if (variables) variables[name] = value; +function setVar(nameOrObj, value, variables, property) { + if (typeof nameOrObj === 'string') { + if (variables) { + if (property) { + variables[nameOrObj][property] = value; + } else { + variables[nameOrObj] = value; + } + } + } else { + if (property) { + nameOrObj[property] = value; + } else { + throw new Error('Cannot change value of reference to array or object'); + } + } return value; }