str.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. #include "__ofunc.h"
  2. LinkValue *strCore(LinkValue *belong, LinkValue *class, Inter *inter) {
  3. LinkValue *value;
  4. value = make_new(inter, belong, class);
  5. value->value->type = V_str;
  6. value->value->data.str.str = NULL; // 设置为NULL, 因此strCore不能单独使用, strCore()后需要显式设定str的内容
  7. return value;
  8. }
  9. static ResultType str_new(O_FUNC){
  10. LinkValue *value = NULL;
  11. ArgumentParser ap[] = {{.type=only_value, .must=1, .long_arg=false},
  12. {.must=-1}};
  13. int status = 1;
  14. setResultCore(result);
  15. arg = parserValueArgument(ap, arg, &status, NULL);
  16. if (status != 1){
  17. setResultError(E_ArgumentException, FEW_ARG, LINEFILE, true, CNEXT_NT);
  18. return R_error;
  19. }
  20. freeResult(result);
  21. value = make_new(inter, belong, ap[0].value); // 保持与strCore的行为相同
  22. value->value->type = V_str;
  23. value->value->data.str.str = memWidecpy(L"");
  24. run_init(value, arg, LINEFILE, CNEXT_NT);
  25. return result->type;
  26. }
  27. static ResultType str_init(O_FUNC){
  28. ArgumentParser ap[] = {{.type=only_value, .must=1, .long_arg=false},
  29. {.type=only_value, .must=0, .long_arg=false},
  30. {.must=-1}};
  31. LinkValue *base;
  32. wchar_t *str = NULL;
  33. setResultCore(result);
  34. parserArgumentUnion(ap, arg, CNEXT_NT);
  35. if (!CHECK_RESULT(result))
  36. return result->type;
  37. freeResult(result);
  38. base = ap[0].value;
  39. if (ap[1].value != NULL){
  40. str = getRepoStr(ap[1].value, false, LINEFILE, CNEXT_NT);
  41. if (!CHECK_RESULT(result))
  42. return result->type;
  43. memFree(base->value->data.str.str);
  44. base->value->data.str.str = memWidecpy(str);
  45. }
  46. setResult(result, inter);
  47. return result->type;
  48. }
  49. static ResultType str_slice(O_FUNC){
  50. ArgumentParser ap[] = {{.type=only_value, .must=1, .long_arg=false},
  51. {.type=only_value, .must=1, .long_arg=false},
  52. {.type=only_value, .must=0, .long_arg=false},
  53. {.type=only_value, .must=0, .long_arg=false},
  54. {.must=-1}};
  55. vint size;
  56. vint first;
  57. vint second;
  58. vint stride;
  59. setResultCore(result);
  60. parserArgumentUnion(ap, arg, CNEXT_NT);
  61. if (!CHECK_RESULT(result))
  62. return result->type;
  63. freeResult(result);
  64. if (ap[0].value->value->type != V_str) {
  65. setResultError(E_TypeException, INSTANCE_ERROR(str), LINEFILE, true, CNEXT_NT);
  66. return R_error;
  67. }
  68. size = memWidelen(ap[0].value->value->data.str.str);
  69. first = 0;
  70. second = size;
  71. stride = 1;
  72. for (vint *list[]={&first, &second, &stride}, i=0; i < 3; i++) {
  73. if (ap[i + 1].value != NULL && ap[i + 1].value->value->type == V_int) // 检查是否存在或是否为数字
  74. *(list[i]) = ap[i + 1].value->value->data.int_.num;
  75. else if (ap[i + 1].value != NULL && ap[i + 1].value->value->type != V_none) { // 若不是数字则报错
  76. setResultError(E_TypeException, VALUE_ERROR(first/second/stride, num or null), LINEFILE, true, CNEXT_NT);
  77. return R_error;
  78. }
  79. }
  80. if (!checkSlice(&first, &second, &stride, size, CNEXT_NT))
  81. return result->type;
  82. {
  83. wchar_t *str = NULL;
  84. for (vint i = stride > 0 ? first : second; stride > 0 ? (i < second) : (i > first); i += stride)
  85. str = memWideCharcpy(str, 1, true, true, ap[0].value->value->data.str.str[i]);
  86. makeStringValue(str, LINEFILE, CNEXT_NT);
  87. memFree(str);
  88. }
  89. return result->type;
  90. }
  91. static ResultType str_down(O_FUNC){
  92. ArgumentParser ap[] = {{.type=only_value, .must=1, .long_arg=false},
  93. {.type=only_value, .must=1, .long_arg=false},
  94. {.must=-1}};
  95. vint size;
  96. vint index;
  97. wchar_t element[2] = { NUL };
  98. setResultCore(result);
  99. parserArgumentUnion(ap, arg, CNEXT_NT);
  100. if (!CHECK_RESULT(result))
  101. return result->type;
  102. freeResult(result);
  103. if (ap[0].value->value->type != V_str){
  104. setResultError(E_TypeException, INSTANCE_ERROR(str), LINEFILE, true, CNEXT_NT);
  105. return R_error;
  106. }
  107. if (ap[1].value->value->type != V_int){
  108. setResultError(E_TypeException, ONLY_ACC(str index, int), LINEFILE, true, CNEXT_NT);
  109. return R_error;
  110. }
  111. size = memWidelen(ap[0].value->value->data.str.str);
  112. index = ap[1].value->value->data.int_.num;
  113. if (!checkIndex(&index, &size, CNEXT_NT))
  114. return result->type;
  115. *element = ap[0].value->value->data.str.str[index];
  116. makeStringValue(element, LINEFILE, CNEXT_NT);
  117. return result->type;
  118. }
  119. static ResultType str_to_list(O_FUNC){
  120. ArgumentParser ap[] = {{.type=only_value, .must=1, .long_arg=false},
  121. {.must=-1}};
  122. vint size;
  123. setResultCore(result);
  124. parserArgumentUnion(ap, arg, CNEXT_NT);
  125. if (!CHECK_RESULT(result))
  126. return result->type;
  127. freeResult(result);
  128. if (ap[0].value->value->type != V_str){
  129. setResultError(E_TypeException, INSTANCE_ERROR(str), LINEFILE, true, CNEXT_NT);
  130. return R_error;
  131. }
  132. size = memWidelen(ap[0].value->value->data.str.str);
  133. {
  134. Argument *new_list = NULL;
  135. for (vint i = 0; i < size; i ++) {
  136. wchar_t str[2] = { NUL };
  137. str[0] = ap[0].value->value->data.str.str[i];
  138. makeStringValue(str, LINEFILE, CNEXT_NT);
  139. new_list = connectValueArgument(result->value, new_list);
  140. freeResult(result);
  141. }
  142. makeListValue(new_list, LINEFILE, L_list, CNEXT_NT);
  143. freeArgument(new_list, true);
  144. }
  145. return result->type;
  146. }
  147. static ResultType str_iter(O_FUNC){
  148. ArgumentParser ap[] = {{.type=only_value, .must=1, .long_arg=false},
  149. {.must=-1}};
  150. LinkValue *to_list = NULL;
  151. setResultCore(result);
  152. parserArgumentUnion(ap, arg, CNEXT_NT);
  153. if (!CHECK_RESULT(result))
  154. return result->type;
  155. freeResult(result);
  156. to_list = findAttributes(L"to_list", false, LINEFILE, true, CFUNC_NT(var_list, result, ap[0].value));
  157. if (!CHECK_RESULT(result))
  158. return result->type;
  159. if (to_list == NULL){
  160. setResultError(E_TypeException, L"string cannot be converted to list", LINEFILE, true, CNEXT_NT);
  161. return R_error;
  162. }
  163. gc_addTmpLink(&to_list->gc_status);
  164. callBackCore(to_list, NULL, LINEFILE, 0, CNEXT_NT);
  165. gc_freeTmpLink(&to_list->gc_status);
  166. if (CHECK_RESULT(result)) { // 若没有出现Exception
  167. LinkValue *str_list;
  168. GET_RESULT(str_list, result);
  169. getIter(str_list, 1, LINEFILE, CNEXT_NT);
  170. gc_freeTmpLink(&str_list->gc_status);
  171. }
  172. return result->type;
  173. }
  174. void registeredStr(R_FUNC){
  175. LinkValue *object = inter->data.base_obj[B_STR];
  176. NameFunc tmp[] = {{L"to_list", str_to_list, fp_obj, .var=nfv_notpush},
  177. {inter->data.mag_func[M_NEW], str_new, fp_class, .var=nfv_notpush},
  178. {inter->data.mag_func[M_INIT], str_init, fp_obj, .var=nfv_notpush},
  179. {inter->data.mag_func[M_SLICE], str_slice, fp_obj, .var=nfv_notpush},
  180. {inter->data.mag_func[M_ITER], str_iter, fp_obj, .var=nfv_notpush},
  181. {inter->data.mag_func[M_DOWN], str_down, fp_obj, .var=nfv_notpush},
  182. {inter->data.mag_func[M_SLICE], str_slice, fp_obj, .var=nfv_notpush},
  183. {NULL, NULL}};
  184. gc_addTmpLink(&object->gc_status);
  185. iterBaseClassFunc(tmp, object, CFUNC_CORE(inter->var_list));
  186. addBaseClassVar(L"str", object, belong, inter);
  187. gc_freeTmpLink(&object->gc_status);
  188. }
  189. LinkValue *callClassOf(LinkValue *obj, Inter *inter, LinkValue *new_func, LinkValue *init_func) {
  190. Argument *arg;
  191. Argument *init_arg;
  192. Result result;
  193. LinkValue *new_name;
  194. setResultCore(&result);
  195. arg = makeValueArgument(obj);
  196. new_func->value->data.function.of(arg, obj, &result, inter, new_func->value->object.out_var);
  197. new_name = result.value;
  198. freeResult(&result);
  199. freeArgument(arg, true);
  200. init_arg = makeValueArgument(new_name);
  201. init_func->value->data.function.of(init_arg, new_name, &result, inter, init_func->value->object.out_var);
  202. freeResult(&result);
  203. freeArgument(init_arg, true);
  204. return new_name;
  205. }
  206. LinkValue *makeStrFromOf(LinkValue *str, LinkValue *new, LinkValue *init, wchar_t *str_, Inter *inter) {
  207. LinkValue *return_;
  208. return_ = callClassOf(str, inter, new, init);
  209. memFree(return_->value->data.str.str);
  210. return_->value->data.str.str = memWidecpy(str_);
  211. return return_;
  212. }
  213. static LinkValue *makeFunctionFromValue(LinkValue *func, LinkValue *new, LinkValue *init, OfficialFunction of, LinkValue *belong, VarList *var_list, Inter *inter) {
  214. LinkValue *new_func;
  215. new_func = callClassOf(func, inter, new, init);
  216. new_func->value->data.function.type = c_func;
  217. new_func->value->data.function.of = of;
  218. new_func->value->data.function.function_data.pt_type = inter->data.default_pt_type;
  219. for (VarList *vl = new_func->value->object.out_var; vl != NULL; vl = freeVarList(vl))
  220. PASS;
  221. new_func->value->object.out_var = copyVarList(var_list, false, inter);
  222. new_func->belong = belong;
  223. return new_func;
  224. }
  225. void strFunctionPresetting(LinkValue *func, LinkValue *func_new, LinkValue *func_init, Inter *inter) {
  226. LinkValue *obj = inter->data.base_obj[B_STR];
  227. LinkValue *new_func = NULL;
  228. LinkValue *new_name = NULL;
  229. wchar_t *new_name_ = setStrVarName(inter->data.mag_func[M_NEW], false, inter);
  230. LinkValue *init_func = NULL;
  231. LinkValue *init_name = NULL;
  232. wchar_t *init_name_ = setStrVarName(inter->data.mag_func[M_INIT], false, inter);
  233. new_func = makeFunctionFromValue(func, func_new, func_init, str_new, obj, NULL, inter); // 声明为内联函数
  234. init_func = makeFunctionFromValue(func, func_new, func_init, str_init, obj, NULL, inter);
  235. new_func->value->data.function.function_data.pt_type = fp_class;
  236. init_func->value->data.function.function_data.pt_type = fp_obj;
  237. new_name = makeStrFromOf(obj, new_func, init_func, inter->data.mag_func[M_NEW], inter);
  238. init_name = makeStrFromOf(obj, new_func, init_func, inter->data.mag_func[M_INIT], inter);
  239. addFromVarList(new_name_, new_name, 0, new_func, CFUNC_CORE(obj->value->object.var));
  240. addFromVarList(init_name_, init_name, 0, init_func, CFUNC_CORE(obj->value->object.var));
  241. newObjectSettingPresetting(new_func, new_name, inter);
  242. newObjectSettingPresetting(init_func, init_name, inter);
  243. memFree(new_name_);
  244. memFree(init_name_);
  245. }
  246. void makeBaseStr(Inter *inter){
  247. LinkValue *str = makeBaseChildClass(inter->data.base_obj[B_VOBJECT], inter);
  248. gc_addStatementLink(&str->gc_status);
  249. inter->data.base_obj[B_STR] = str;
  250. }