silphius/app/swcx/evaluators/unary.js

25 lines
677 B
JavaScript
Raw Normal View History

2024-06-16 08:01:01 -05:00
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)};
}