silphius/app/astride/evaluators/unary.js
2024-06-22 08:02:23 -05:00

25 lines
677 B
JavaScript

export default function(node, {evaluate, scope}) {
const unary = (arg) => {
switch (node.operator) {
case '+' : return +arg;
case '-' : return -arg;
case '!' : return !arg;
case '~' : return ~arg;
case 'typeof': return typeof arg;
case 'void' : return undefined;
// case 'delete': ...
/* v8 ignore next 2 */
default:
throw new Error(`operator not implemented: ${node.operator}`);
}
};
const arg = evaluate(node.argument, {scope});
if (arg.async) {
return {
async: true,
value: Promise.resolve(arg.value).then(unary),
};
}
return {value: unary(arg.value)};
}