Mercurial > vim
comparison src/vim9cmds.c @ 30065:6cf788ab844c v9.0.0370
patch 9.0.0370: cleaning up afterwards can make a function messy
Commit: https://github.com/vim/vim/commit/1d84f7608f1e41dad03b8cc7925895437775f7c0
Author: Bram Moolenaar <Bram@vim.org>
Date: Sat Sep 3 21:35:53 2022 +0100
patch 9.0.0370: cleaning up afterwards can make a function messy
Problem: Cleaning up afterwards can make a function messy.
Solution: Add the :defer command.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Sat, 03 Sep 2022 22:45:03 +0200 |
parents | d269dd3cd31d |
children | a542dfb1c1a2 |
comparison
equal
deleted
inserted
replaced
30064:a8f1fbaa43c8 | 30065:6cf788ab844c |
---|---|
1652 return NULL; | 1652 return NULL; |
1653 | 1653 |
1654 return p; | 1654 return p; |
1655 } | 1655 } |
1656 | 1656 |
1657 /* | |
1658 * Compile an expression or function call. | |
1659 */ | |
1657 char_u * | 1660 char_u * |
1658 compile_eval(char_u *arg, cctx_T *cctx) | 1661 compile_eval(char_u *arg, cctx_T *cctx) |
1659 { | 1662 { |
1660 char_u *p = arg; | 1663 char_u *p = arg; |
1661 int name_only; | 1664 int name_only; |
1677 | 1680 |
1678 // drop the result | 1681 // drop the result |
1679 generate_instr_drop(cctx, ISN_DROP, 1); | 1682 generate_instr_drop(cctx, ISN_DROP, 1); |
1680 | 1683 |
1681 return skipwhite(p); | 1684 return skipwhite(p); |
1685 } | |
1686 | |
1687 /* | |
1688 * Compile "defer func(arg)". | |
1689 */ | |
1690 char_u * | |
1691 compile_defer(char_u *arg_start, cctx_T *cctx) | |
1692 { | |
1693 char_u *p; | |
1694 char_u *arg = arg_start; | |
1695 int argcount = 0; | |
1696 dfunc_T *dfunc; | |
1697 type_T *type; | |
1698 int func_idx; | |
1699 | |
1700 // Get a funcref for the function name. | |
1701 // TODO: better way to find the "(". | |
1702 p = vim_strchr(arg, '('); | |
1703 if (p == NULL) | |
1704 { | |
1705 semsg(_(e_missing_parenthesis_str), arg); | |
1706 return NULL; | |
1707 } | |
1708 *p = NUL; | |
1709 func_idx = find_internal_func(arg); | |
1710 if (func_idx >= 0) | |
1711 // TODO: better type | |
1712 generate_PUSHFUNC(cctx, (char_u *)internal_func_name(func_idx), | |
1713 &t_func_any, FALSE); | |
1714 else if (compile_expr0(&arg, cctx) == FAIL) | |
1715 return NULL; | |
1716 *p = '('; | |
1717 | |
1718 // check for function type | |
1719 type = get_type_on_stack(cctx, 0); | |
1720 if (type->tt_type != VAR_FUNC) | |
1721 { | |
1722 emsg(_(e_function_name_required)); | |
1723 return NULL; | |
1724 } | |
1725 | |
1726 // compile the arguments | |
1727 arg = skipwhite(p + 1); | |
1728 if (compile_arguments(&arg, cctx, &argcount, CA_NOT_SPECIAL) == FAIL) | |
1729 return NULL; | |
1730 | |
1731 // TODO: check argument count with "type" | |
1732 | |
1733 dfunc = ((dfunc_T *)def_functions.ga_data) + cctx->ctx_ufunc->uf_dfunc_idx; | |
1734 if (dfunc->df_defer_var_idx == 0) | |
1735 { | |
1736 lvar_T *lvar = reserve_local(cctx, (char_u *)"@defer@", 7, | |
1737 TRUE, &t_list_any); | |
1738 if (lvar == NULL) | |
1739 return NULL; | |
1740 dfunc->df_defer_var_idx = lvar->lv_idx + 1; | |
1741 } | |
1742 if (generate_DEFER(cctx, dfunc->df_defer_var_idx - 1, argcount) == FAIL) | |
1743 return NULL; | |
1744 | |
1745 return skipwhite(arg); | |
1682 } | 1746 } |
1683 | 1747 |
1684 /* | 1748 /* |
1685 * compile "echo expr" | 1749 * compile "echo expr" |
1686 * compile "echomsg expr" | 1750 * compile "echomsg expr" |