robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
f58d24040a
master
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 ) 21FUNC_ATTR_NONNULL_ALL 22{ 23if (* op != '+' || tv2 -> v_type != VAR_BLOB ) { 24return FAIL ; 25 } 26 27// Blob += Blob 28if (tv2 -> vval .v_blob == NULL ) { 29return OK ; 30 } 31 32if (tv1 -> vval .v_blob == NULL ) { 33tv1 -> vval .v_blob = tv2 -> vval .v_blob ; 34tv1 -> vval .v_blob -> bv_refcount ++ ; 35return OK ; 36 } 37 38blob_T * const b1 = tv1 -> vval .v_blob ; 39blob_T * const b2 = tv2 -> vval .v_blob ; 40const int len = tv_blob_len (b2 ); 41 42if (len > 0 ) { 43ga_grow (& b1 -> bv_ga ,len ); 44memmove ((uint8_t * )b1 -> bv_ga .ga_data + b1 -> bv_ga .ga_len , 45 (uint8_t * )b2 -> bv_ga .ga_data , (size_t )len ); 46b1 -> bv_ga .ga_len += len ; 47 } 48 49return 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 ) 55FUNC_ATTR_NONNULL_ALL 56{ 57if (* op != '+' || tv2 -> v_type != VAR_LIST ) { 58return FAIL ; 59 } 60 61// List += List 62if (tv2 -> vval .v_list == NULL ) { 63return OK ; 64 } 65 66if (tv1 -> vval .v_list == NULL ) { 67tv1 -> vval .v_list = tv2 -> vval .v_list ; 68tv_list_ref (tv1 -> vval .v_list ); 69 }else { 70tv_list_extend (tv1 -> vval .v_list ,tv2 -> vval .v_list ,NULL ); 71 } 72 73return 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 ) 81FUNC_ATTR_NONNULL_ALL 82{ 83varnumber_T n = tv_get_number (tv1 ); 84if (tv2 -> v_type == VAR_FLOAT ) { 85float_T f = (float_T )n ; 86if (* op == '%' ) { 87return FAIL ; 88 } 89switch (* op ) { 90case '+' : 91f += tv2 -> vval .v_float ;break ; 92case '-' : 93f -= tv2 -> vval .v_float ;break ; 94case '*' : 95f *=tv2 -> vval .v_float ;break ; 96case '/' : 97f /=tv2 -> vval .v_float ;break ; 98 } 99tv_clear (tv1 ); 100tv1 -> v_type = VAR_FLOAT ; 101tv1 -> vval .v_float = f ; 102 }else { 103switch (* op ) { 104case '+' : 105n += tv_get_number (tv2 );break ; 106case '-' : 107n -= tv_get_number (tv2 );break ; 108case '*' : 109n *=tv_get_number (tv2 );break ; 110case '/' : 111n = num_divide (n ,tv_get_number (tv2 ));break ; 112case '%' : 113n = num_modulus (n ,tv_get_number (tv2 ));break ; 114 } 115tv_clear (tv1 ); 116tv1 -> v_type = VAR_NUMBER ; 117tv1 -> vval .v_number = n ; 118 } 119 120return 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 ) 126FUNC_ATTR_NONNULL_ALL 127{ 128if (tv2 -> v_type == VAR_FLOAT ) { 129return FAIL ; 130 } 131 132char numbuf [NUMBUFLEN ]; 133// str .= str 134const char * s2 = tv_get_string_buf (tv2 ,numbuf ); 135if (grow_string_tv (tv1 ,s2 )== OK ) { 136return OK ; 137 } 138 139const char * tvs = tv_get_string (tv1 ); 140char * const s = concat_str (tvs ,s2 ); 141tv_clear (tv1 ); 142tv1 -> v_type = VAR_STRING ; 143tv1 -> vval .v_string = s ; 144 145return 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 ) 152FUNC_ATTR_NONNULL_ALL 153{ 154if (tv2 -> v_type == VAR_LIST ) { 155return FAIL ; 156 } 157 158if (vim_strchr ("+-*/%" , (uint8_t )(* op ))!= NULL ) { 159return tv_op_number (tv1 ,tv2 ,op ); 160 } 161 162return 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 ) 168FUNC_ATTR_NONNULL_ALL 169{ 170if (* op == '%' || * op == '.' 171|| (tv2 -> v_type != VAR_FLOAT 172&& tv2 -> v_type != VAR_NUMBER 173&& tv2 -> v_type != VAR_STRING )) { 174return FAIL ; 175 } 176 177const float_T f = (tv2 -> v_type == VAR_FLOAT 178 ?tv2 -> vval .v_float 179 : (float_T )tv_get_number (tv2 )); 180switch (* op ) { 181case '+' : 182tv1 -> vval .v_float += f ;break ; 183case '-' : 184tv1 -> vval .v_float -= f ;break ; 185case '*' : 186tv1 -> vval .v_float *=f ;break ; 187case '/' : 188tv1 -> vval .v_float /=f ;break ; 189 } 190 191return 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 ) 202FUNC_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 "..=". 206if (tv2 -> v_type == VAR_FUNC || tv2 -> v_type == VAR_DICT 207|| ((tv2 -> v_type == VAR_BOOL || tv2 -> v_type == VAR_SPECIAL )&& * op == '.' )) { 208semsg (_ (e_letwrong ),op ); 209return FAIL ; 210 } 211 212int retval = FAIL ; 213 214switch (tv1 -> v_type ) { 215case VAR_DICT : 216case VAR_FUNC : 217case VAR_PARTIAL : 218case VAR_BOOL : 219case VAR_SPECIAL : 220break ; 221case VAR_BLOB : 222retval = tv_op_blob (tv1 ,tv2 ,op ); 223break ; 224case VAR_LIST : 225retval = tv_op_list (tv1 ,tv2 ,op ); 226break ; 227case VAR_NUMBER : 228case VAR_STRING : 229retval = tv_op_nr_or_string (tv1 ,tv2 ,op ); 230break ; 231case VAR_FLOAT : 232retval = tv_op_float (tv1 ,tv2 ,op ); 233break ; 234case VAR_UNKNOWN : 235abort (); 236 } 237 238if (retval != OK ) { 239semsg (_ (e_letwrong ),op ); 240 } 241 242return retval ; 243}