silphius/app/astride/evaluators/update.js
2024-06-30 11:18:26 -05:00

24 lines
696 B
JavaScript

export default function(node, {evaluate, scope}) {
const {argument, operator, prefix} = node;
const {value} = evaluate(argument, {scope});
const update = (value) => {
if (prefix) {
switch (operator) {
case '++': return scope.set(argument.name, value + 1);
case '--': return scope.set(argument.name, value - 1);
}
}
switch (operator) {
case '++':
scope.set(argument.name, value + 1);
return value;
case '--':
scope.set(argument.name, value - 1);
return value;
}
/* v8 ignore next */
throw new Error(`operator not implemented: ${operator}`);
};
return {async: false, value: update(value)};
}