scripts/rpn: refactor and fix incorrect return behavior