silphius/app/astride/evaluators/update.js

24 lines
696 B
JavaScript
Raw Normal View History

2024-06-16 08:01:01 -05:00
export default function(node, {evaluate, scope}) {
const {argument, operator, prefix} = node;
const {value} = evaluate(argument, {scope});
const update = (value) => {
if (prefix) {
switch (operator) {
2024-06-22 10:47:17 -05:00
case '++': return scope.set(argument.name, value + 1);
case '--': return scope.set(argument.name, value - 1);
2024-06-16 08:01:01 -05:00
}
}
switch (operator) {
case '++':
2024-06-22 10:47:17 -05:00
scope.set(argument.name, value + 1);
2024-06-16 08:01:01 -05:00
return value;
case '--':
2024-06-22 10:47:17 -05:00
scope.set(argument.name, value - 1);
2024-06-16 08:01:01 -05:00
return value;
}
/* v8 ignore next */
throw new Error(`operator not implemented: ${operator}`);
};
2024-06-30 10:03:50 -05:00
return {async: false, value: update(value)};
2024-06-16 08:01:01 -05:00
}