str.c 10.0 KB

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