robin/laufey

git clone https://git.robinwobin.dev/robin/laufey

zeertzjqvim-patch:9.2.0155: filetype: ObjectScript are not recognized (#38288)f58d24040a

master
5.5 KiB243 linesraw
1#include <inttypes.h>
2#include <stdlib.h>
3
4#include "nvim/errors.h"
5#include "nvim/eval.h"
6#include "nvim/eval/executor.h"
7#include "nvim/eval/typval.h"
8#include "nvim/eval/typval_defs.h"
9#include "nvim/garray.h"
10#include "nvim/gettext_defs.h"
11#include "nvim/message.h"
12#include "nvim/strings.h"
13#include "nvim/types_defs.h"
14#include "nvim/vim_defs.h"
15
16#include "eval/executor.c.generated.h"
17
18/// Handle "blob1 += blob2".
19/// Returns OK or FAIL.
20static int tv_op_blob(typval_T *tv1, const typval_T *tv2, const char *op)
21  FUNC_ATTR_NONNULL_ALL
22{
23  if (*op != '+' || tv2->v_type != VAR_BLOB) {
24    return FAIL;
25  }
26
27  // Blob += Blob
28  if (tv2->vval.v_blob == NULL) {
29    return OK;
30  }
31
32  if (tv1->vval.v_blob == NULL) {
33    tv1->vval.v_blob = tv2->vval.v_blob;
34    tv1->vval.v_blob->bv_refcount++;
35    return OK;
36  }
37
38  blob_T *const b1 = tv1->vval.v_blob;
39  blob_T *const b2 = tv2->vval.v_blob;
40  const int len = tv_blob_len(b2);
41
42  if (len > 0) {
43    ga_grow(&b1->bv_ga, len);
44    memmove((uint8_t *)b1->bv_ga.ga_data + b1->bv_ga.ga_len,
45            (uint8_t *)b2->bv_ga.ga_data, (size_t)len);
46    b1->bv_ga.ga_len += len;
47  }
48
49  return OK;
50}
51
52/// Handle "list1 += list2".
53/// Returns OK or FAIL.
54static int tv_op_list(typval_T *tv1, const typval_T *tv2, const char *op)
55  FUNC_ATTR_NONNULL_ALL
56{
57  if (*op != '+' || tv2->v_type != VAR_LIST) {
58    return FAIL;
59  }
60
61  // List += List
62  if (tv2->vval.v_list == NULL) {
63    return OK;
64  }
65
66  if (tv1->vval.v_list == NULL) {
67    tv1->vval.v_list = tv2->vval.v_list;
68    tv_list_ref(tv1->vval.v_list);
69  } else {
70    tv_list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
71  }
72
73  return OK;
74}
75
76/// Handle number operations:
77///      nr += nr , nr -= nr , nr *=nr , nr /= nr , nr %= nr
78///
79/// Returns OK or FAIL.
80static int tv_op_number(typval_T *tv1, const typval_T *tv2, const char *op)
81  FUNC_ATTR_NONNULL_ALL
82{
83  varnumber_T n = tv_get_number(tv1);
84  if (tv2->v_type == VAR_FLOAT) {
85    float_T f = (float_T)n;
86    if (*op == '%') {
87      return FAIL;
88    }
89    switch (*op) {
90    case '+':
91      f += tv2->vval.v_float; break;
92    case '-':
93      f -= tv2->vval.v_float; break;
94    case '*':
95      f *= tv2->vval.v_float; break;
96    case '/':
97      f /= tv2->vval.v_float; break;
98    }
99    tv_clear(tv1);
100    tv1->v_type = VAR_FLOAT;
101    tv1->vval.v_float = f;
102  } else {
103    switch (*op) {
104    case '+':
105      n += tv_get_number(tv2); break;
106    case '-':
107      n -= tv_get_number(tv2); break;
108    case '*':
109      n *= tv_get_number(tv2); break;
110    case '/':
111      n = num_divide(n, tv_get_number(tv2)); break;
112    case '%':
113      n = num_modulus(n, tv_get_number(tv2)); break;
114    }
115    tv_clear(tv1);
116    tv1->v_type = VAR_NUMBER;
117    tv1->vval.v_number = n;
118  }
119
120  return OK;
121}
122
123/// Handle "str1 .= str2"
124/// Returns OK or FAIL.
125static int tv_op_string(typval_T *tv1, const typval_T *tv2, const char *op)
126  FUNC_ATTR_NONNULL_ALL
127{
128  if (tv2->v_type == VAR_FLOAT) {
129    return FAIL;
130  }
131
132  char numbuf[NUMBUFLEN];
133  // str .= str
134  const char *s2 = tv_get_string_buf(tv2, numbuf);
135  if (grow_string_tv(tv1, s2) == OK) {
136    return OK;
137  }
138
139  const char *tvs = tv_get_string(tv1);
140  char *const s = concat_str(tvs, s2);
141  tv_clear(tv1);
142  tv1->v_type = VAR_STRING;
143  tv1->vval.v_string = s;
144
145  return OK;
146}
147
148/// Handle "tv1 += tv2", "tv1 -= tv2", "tv1 *= tv2", "tv1 /= tv2", "tv1 %= tv2"
149/// and "tv1 .= tv2"
150/// Returns OK or FAIL.
151static int tv_op_nr_or_string(typval_T *tv1, const typval_T *tv2, const char *op)
152  FUNC_ATTR_NONNULL_ALL
153{
154  if (tv2->v_type == VAR_LIST) {
155    return FAIL;
156  }
157
158  if (vim_strchr("+-*/%", (uint8_t)(*op)) != NULL) {
159    return tv_op_number(tv1, tv2, op);
160  }
161
162  return tv_op_string(tv1, tv2, op);
163}
164
165/// Handle "f1 += f2", "f1 -= f2", "f1 *= f2", "f1 /= f2".
166/// Returns OK or FAIL.
167static int tv_op_float(typval_T *tv1, const typval_T *tv2, const char *op)
168  FUNC_ATTR_NONNULL_ALL
169{
170  if (*op == '%' || *op == '.'
171      || (tv2->v_type != VAR_FLOAT
172          && tv2->v_type != VAR_NUMBER
173          && tv2->v_type != VAR_STRING)) {
174    return FAIL;
175  }
176
177  const float_T f = (tv2->v_type == VAR_FLOAT
178                     ? tv2->vval.v_float
179                     : (float_T)tv_get_number(tv2));
180  switch (*op) {
181  case '+':
182    tv1->vval.v_float += f; break;
183  case '-':
184    tv1->vval.v_float -= f; break;
185  case '*':
186    tv1->vval.v_float *= f; break;
187  case '/':
188    tv1->vval.v_float /= f; break;
189  }
190
191  return OK;
192}
193
194/// Handle tv1 += tv2, -=, *=, /=,  %=, .=
195///
196/// @param[in,out]  tv1  First operand, modified typval.
197/// @param[in]  tv2  Second operand.
198/// @param[in]  op  Used operator.
199///
200/// @return OK or FAIL.
201int eexe_mod_op(typval_T *const tv1, const typval_T *const tv2, const char *const op)
202  FUNC_ATTR_NONNULL_ALL
203{
204  // Can't do anything with a Funcref or Dict on the right.
205  // v:true and friends only work with "..=".
206  if (tv2->v_type == VAR_FUNC || tv2->v_type == VAR_DICT
207      || ((tv2->v_type == VAR_BOOL || tv2->v_type == VAR_SPECIAL) && *op == '.')) {
208    semsg(_(e_letwrong), op);
209    return FAIL;
210  }
211
212  int retval = FAIL;
213
214  switch (tv1->v_type) {
215  case VAR_DICT:
216  case VAR_FUNC:
217  case VAR_PARTIAL:
218  case VAR_BOOL:
219  case VAR_SPECIAL:
220    break;
221  case VAR_BLOB:
222    retval = tv_op_blob(tv1, tv2, op);
223    break;
224  case VAR_LIST:
225    retval = tv_op_list(tv1, tv2, op);
226    break;
227  case VAR_NUMBER:
228  case VAR_STRING:
229    retval = tv_op_nr_or_string(tv1, tv2, op);
230    break;
231  case VAR_FLOAT:
232    retval = tv_op_float(tv1, tv2, op);
233    break;
234  case VAR_UNKNOWN:
235    abort();
236  }
237
238  if (retval != OK) {
239    semsg(_(e_letwrong), op);
240  }
241
242  return retval;
243}