parameter.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824
  1. #include "__run.h"
  2. #define returnResult(result) do{ \
  3. if (!CHECK_RESULT(result)) { \
  4. goto return_; \
  5. } \
  6. }while(0)
  7. Argument *makeArgument(void){
  8. Argument *tmp = memCalloc(1, sizeof(Argument));
  9. tmp->type = value_arg;
  10. tmp->data.value = NULL;
  11. tmp->data.name = NULL;
  12. tmp->data.name_ = NULL;
  13. tmp->name_type = name_st;
  14. tmp->next = NULL;
  15. return tmp;
  16. }
  17. Argument *makeValueArgument(LinkValue *value){
  18. Argument *tmp = makeArgument();
  19. tmp->data.value = value;
  20. gc_addTmpLink(&value->gc_status);
  21. return tmp;
  22. }
  23. Argument *makeStatementNameArgument(LinkValue *value, Statement *name){
  24. Argument *tmp = makeArgument();
  25. tmp->type = name_arg;
  26. tmp->data.value = value;
  27. tmp->data.name = name;
  28. gc_addTmpLink(&value->gc_status);
  29. return tmp;
  30. }
  31. Argument *makeCharNameArgument(LinkValue *value, LinkValue *name_value, char *name) {
  32. Argument *tmp = makeArgument();
  33. tmp->type = name_arg;
  34. tmp->name_type = name_char;
  35. tmp->data.value = value;
  36. tmp->data.name_ = memStrcpy(name);
  37. tmp->data.name_value = name_value;
  38. gc_addTmpLink(&value->gc_status);
  39. gc_addTmpLink(&name_value->gc_status);
  40. return tmp;
  41. }
  42. Argument *connectArgument(Argument *new, Argument *base){
  43. Argument *tmp = base;
  44. if (base == NULL)
  45. return new;
  46. for (PASS; base->next != NULL; base = base->next)
  47. PASS;
  48. base->next = new;
  49. return tmp;
  50. }
  51. Argument *connectValueArgument(LinkValue *value, Argument *base){
  52. Argument *new = makeValueArgument(value);
  53. return connectArgument(new, base);
  54. }
  55. Argument *connectStatementNameArgument(LinkValue *value, Statement *name, Argument *base){
  56. Argument *new = makeStatementNameArgument(value, name);
  57. return connectArgument(new, base);
  58. }
  59. Argument *connectCharNameArgument(LinkValue *value, LinkValue *name_value, char *name, Argument *base) {
  60. Argument *new = makeCharNameArgument(value, name_value, name);
  61. return connectArgument(new, base);
  62. }
  63. void freeArgument(Argument *at, bool free_st) {
  64. for (Argument *tmp=NULL; at != NULL;at = tmp){
  65. tmp = at->next;
  66. if (free_st)
  67. freeStatement(at->data.name);
  68. memFree(at->data.name_);
  69. if (at->data.name_value != NULL)
  70. gc_freeTmpLink(&at->data.name_value->gc_status);
  71. if (at->data.value != NULL)
  72. gc_freeTmpLink(&at->data.value->gc_status);
  73. memFree(at);
  74. }
  75. }
  76. Parameter *makeParameter(void){
  77. Parameter *tmp = memCalloc(1, sizeof(Parameter));
  78. tmp->type = value_par;
  79. tmp->data.value = NULL;
  80. tmp->data.name = NULL;
  81. tmp->next = NULL;
  82. return tmp;
  83. }
  84. Parameter *copyenOneParameter(Parameter *base){
  85. Parameter *tmp = makeParameter();
  86. tmp->data.value = copyStatement(base->data.value);
  87. tmp->data.name = copyStatement(base->data.name);
  88. tmp->type = base->type;
  89. return tmp;
  90. }
  91. Parameter *copyParameter(Parameter *base){
  92. Parameter *base_tmp = NULL;
  93. Parameter **tmp = &base_tmp;
  94. for (PASS; base != NULL; tmp = &(*tmp)->next,base = base->next)
  95. *tmp = copyenOneParameter(base);
  96. return base_tmp;
  97. }
  98. Parameter *makeValueParameter(Statement *st){
  99. Parameter *tmp = makeParameter();
  100. tmp->data.value = st;
  101. return tmp;
  102. }
  103. Parameter *makeNameParameter(Statement *value, Statement *name){
  104. Parameter *tmp = makeParameter();
  105. tmp->type = name_par;
  106. tmp->data.value = value;
  107. tmp->data.name = name;
  108. return tmp;
  109. }
  110. Parameter *makeArgsParameter(Statement *st){
  111. Parameter *tmp = makeParameter();
  112. tmp->type = args_par;
  113. tmp->data.value = st;
  114. return tmp;
  115. }
  116. Parameter *makeKwrgsParameter(Statement *st){
  117. Parameter *tmp = makeParameter();
  118. tmp->type = kwargs_par;
  119. tmp->data.value = st;
  120. return tmp;
  121. }
  122. Parameter *connectParameter(Parameter *new, Parameter *base){
  123. if (base == NULL)
  124. return new;
  125. Parameter *tmp = base;
  126. while (base->next != NULL)
  127. base = base->next;
  128. base->next = new;
  129. return tmp;
  130. }
  131. Parameter *connectValueParameter(Statement *st, Parameter *base){
  132. Parameter *new = makeValueParameter(st);
  133. return connectParameter(new, base);
  134. }
  135. Parameter *connectNameParameter(Statement *value, Statement *name, Parameter *base){
  136. Parameter *new = makeNameParameter(value, name);
  137. return connectParameter(new, base);
  138. }
  139. Parameter *connectArgsParameter(Statement *st, Parameter *base){
  140. Parameter *new = makeArgsParameter(st);
  141. return connectParameter(new, base);
  142. }
  143. Parameter *connectKwargsParameter(Statement *st, Parameter *base){
  144. Parameter *new = makeKwrgsParameter(st);
  145. return connectParameter(new, base);
  146. }
  147. void freeParameter(Parameter *pt, bool free_st) {
  148. for (Parameter *tmp=NULL;pt != NULL;pt = tmp){
  149. tmp = pt->next;
  150. if (free_st) {
  151. freeStatement(pt->data.value);
  152. freeStatement(pt->data.name);
  153. }
  154. memFree(pt);
  155. }
  156. }
  157. Argument *listToArgument(LinkValue *list_value, long line, char *file, INTER_FUNCTIONSIG_NOT_ST){
  158. Argument *at = NULL;
  159. LinkValue *iter = NULL;
  160. setResultCore(result);
  161. getIter(list_value, 1, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  162. if (!CHECK_RESULT(result))
  163. return NULL;
  164. iter = result->value;
  165. result->value = NULL;
  166. while (true) {
  167. freeResult(result);
  168. getIter(iter, 0, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  169. if (is_iterStop(result->value, inter)){
  170. freeResult(result);
  171. break;
  172. }
  173. else if (!CHECK_RESULT(result)) {
  174. freeArgument(at, true);
  175. at = NULL;
  176. goto return_;
  177. }
  178. at = connectValueArgument(result->value, at);
  179. }
  180. setResult(result, inter, belong);
  181. return_:
  182. gc_freeTmpLink(&iter->gc_status);
  183. return at;
  184. }
  185. Argument *dictToArgument(LinkValue *dict_value, long line, char *file, INTER_FUNCTIONSIG_NOT_ST) {
  186. Argument *at = NULL;
  187. LinkValue *iter = NULL;
  188. setResultCore(result);
  189. getIter(dict_value, 1, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  190. if (!CHECK_RESULT(result))
  191. return NULL;
  192. iter = result->value;
  193. result->value = NULL;
  194. while (true) {
  195. LinkValue *name_ = NULL;
  196. char *name = NULL;
  197. freeResult(result);
  198. getIter(iter, 0, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  199. if (is_iterStop(result->value, inter)){
  200. freeResult(result);
  201. break;
  202. }
  203. else if (!CHECK_RESULT(result)) {
  204. freeArgument(at, true);
  205. at = NULL;
  206. goto return_;
  207. }
  208. name_ = result->value;
  209. result->value = NULL;
  210. freeResult(result);
  211. elementDownOne(iter, name_, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  212. if (!CHECK_RESULT(result)) {
  213. gc_freeTmpLink(&name_->gc_status);
  214. freeArgument(at, true);
  215. at = NULL;
  216. goto return_;
  217. }
  218. name = getNameFromValue(name_->value, inter);
  219. at = connectCharNameArgument(result->value, name_, name, at);
  220. gc_freeTmpLink(&name_->gc_status);
  221. memFree(name);
  222. }
  223. setResult(result, inter, belong);
  224. return_:
  225. gc_freeTmpLink(&iter->gc_status);
  226. return at;
  227. }
  228. /**
  229. * 设置形式参数的默认值
  230. * 仅支持name_value
  231. * @param function_ad
  232. * @param inter
  233. * @param var_list
  234. * @param num
  235. * @return
  236. */
  237. ResultType defaultParameter(Parameter **function_ad, vnum *num, INTER_FUNCTIONSIG_NOT_ST) {
  238. Parameter *function = *function_ad;
  239. setResultCore(result);
  240. for (*num = 0; function != NULL && function->type == name_par; (*num)++, function = function->next){
  241. LinkValue *value = NULL;
  242. if(operationSafeInterStatement(CALL_INTER_FUNCTIONSIG(function->data.value, var_list, result, belong)))
  243. goto return_;
  244. value = result->value;
  245. freeResult(result);
  246. assCore(function->data.name, value, false, false, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  247. if (!CHECK_RESULT(result))
  248. goto return_;
  249. }
  250. setResult(result, inter, belong);
  251. return_:
  252. *function_ad = function;
  253. return result->type;
  254. }
  255. /**
  256. * 设置实际参数的默认值, 仅支持name_base_value
  257. * @param call_ad
  258. * @param inter
  259. * @param var_list
  260. * @param num
  261. * @return
  262. */
  263. ResultType argumentToVar(Argument **call_ad, vnum *num, INTER_FUNCTIONSIG_NOT_ST) {
  264. Argument *call = *call_ad;
  265. setResultCore(result);
  266. for (*num = 0; call != NULL && call->type == name_arg; (*num)++, call = call->next){
  267. if (call->name_type == name_char){
  268. addFromVarList(call->data.name_, call->data.name_value, 0, call->data.value, CALL_INTER_FUNCTIONSIG_CORE(var_list));
  269. continue;
  270. }
  271. freeResult(result);
  272. assCore(call->data.name, call->data.value, false, false, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  273. if (!CHECK_RESULT(result))
  274. goto return_;
  275. }
  276. setResult(result, inter, belong);
  277. return_:
  278. *call_ad = call;
  279. return result->type;
  280. }
  281. /**
  282. * 形式参数从变量空间中获取值
  283. * @param function_ad
  284. * @param function_var
  285. * @param inter
  286. * @param var_list
  287. * @param num
  288. * @return
  289. */
  290. ResultType parameterFromVar(Parameter **function_ad, VarList *function_var, vnum *num, vnum max, bool *status,
  291. INTER_FUNCTIONSIG_NOT_ST) {
  292. Parameter *function = *function_ad;
  293. bool get = true;
  294. setResultCore(result);
  295. for (*num = 0, *status = false; function != NULL; function = function->next){
  296. int int_times;
  297. char *str_name = NULL;
  298. Statement *name = function->type == name_par ? function->data.name : function->data.value;
  299. LinkValue *value = NULL;
  300. get = true;
  301. if (function->type == kwargs_par){
  302. value = makeLinkValue(makeDictValue(NULL, false, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, NULL, belong)), belong, inter);
  303. value->value->data.dict.dict = var_list->hashtable;
  304. value->value->data.dict.size = max - *num;
  305. *status = true;
  306. goto not_return;
  307. }
  308. freeResult(result);
  309. getVarInfo(&str_name, &int_times, CALL_INTER_FUNCTIONSIG(name, var_list, result, belong));
  310. if (!CHECK_RESULT(result)) {
  311. memFree(str_name);
  312. *function_ad = function;
  313. return result->type;
  314. }
  315. freeResult(result);
  316. value = findFromVarList(str_name, int_times, del_var, CALL_INTER_FUNCTIONSIG_CORE(var_list));
  317. memFree(str_name);
  318. if(value == NULL) {
  319. get = false;
  320. if (function->type == name_par && !operationSafeInterStatement(CALL_INTER_FUNCTIONSIG(function->data.value, var_list, result, belong))) {
  321. value = result->value;
  322. goto not_return;
  323. }
  324. setResultErrorSt(E_ArgumentException, FEW_ARG, true, name, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  325. goto return_;
  326. }
  327. else if (!checkAut(name->aut, value->aut, name->line, name->code_file, NULL, false, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong)))
  328. goto return_;
  329. not_return:
  330. freeResult(result);
  331. assCore(name, value, false, false, CALL_INTER_FUNCTIONSIG_NOT_ST(function_var, result, belong));
  332. if (!CHECK_RESULT(result)) {
  333. *function_ad = function;
  334. return result->type;
  335. }
  336. else
  337. freeResult(result);
  338. if (get)
  339. (*num)++;
  340. }
  341. setResult(result, inter, belong);
  342. return_:
  343. *function_ad = function;
  344. return result->type;
  345. }
  346. /**
  347. * 对位赋值
  348. * @param call_ad
  349. * @param function_ad
  350. * @param function_var
  351. * @param inter
  352. * @param var_list
  353. * @return
  354. */
  355. ResultType argumentToParameter(Argument **call_ad, Parameter **function_ad, VarList *function_var, INTER_FUNCTIONSIG_NOT_ST){
  356. Argument *call = *call_ad;
  357. Parameter *function = *function_ad;
  358. setResultCore(result);
  359. for (PASS; call != NULL && function != NULL && (call->type == value_arg) && function->type != args_par; call = call->next, function = function->next){
  360. Statement *name = function->type == value_par ? function->data.value : function->data.name;
  361. assCore(name, call->data.value, false, false, CALL_INTER_FUNCTIONSIG_NOT_ST(function_var, result, belong));
  362. if (!CHECK_RESULT(result))
  363. goto return_;
  364. freeResult(result);
  365. }
  366. setResult(result, inter, belong);
  367. return_:
  368. *call_ad = call;
  369. *function_ad = function;
  370. return result->type;
  371. }
  372. /**
  373. * 把所有实际参数的值计算出来
  374. * @param call_ad
  375. * @param inter
  376. * @param var_list
  377. * @return
  378. */
  379. ResultType iterParameter(Parameter *call, Argument **base_ad, bool is_dict, INTER_FUNCTIONSIG_NOT_ST){
  380. Argument *base = *base_ad;
  381. setResultCore(result);
  382. for (PASS; call != NULL; call = call->next){
  383. if(operationSafeInterStatement(CALL_INTER_FUNCTIONSIG(call->data.value, var_list, result, belong)))
  384. goto return_;
  385. if (call->type == value_par)
  386. base = connectValueArgument(result->value, base);
  387. else if (call->type == name_par){
  388. if (is_dict){
  389. LinkValue *value = result->value;
  390. setResultCore(result);
  391. if(operationSafeInterStatement(CALL_INTER_FUNCTIONSIG(call->data.name, var_list, result, belong))) {
  392. gc_freeTmpLink(&value->gc_status);
  393. goto return_;
  394. }
  395. char *name_str = getNameFromValue(result->value->value, inter);
  396. base = connectCharNameArgument(value, result->value, name_str, base);
  397. memFree(name_str);
  398. gc_freeTmpLink(&value->gc_status);
  399. }
  400. else
  401. base = connectStatementNameArgument(result->value, call->data.name, base);
  402. }
  403. else if (call->type == args_par){
  404. LinkValue *start = NULL;
  405. Argument *tmp_at = NULL;
  406. start = result->value;
  407. result->value = NULL;
  408. freeResult(result);
  409. tmp_at = listToArgument(start, 0, "sys", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  410. gc_freeTmpLink(&start->gc_status);
  411. if (!CHECK_RESULT(result))
  412. goto return_;
  413. base = connectArgument(tmp_at, base);
  414. }
  415. else if (call->type == kwargs_par){
  416. LinkValue *start = NULL;
  417. Argument *tmp_at = NULL;
  418. start = result->value;
  419. result->value = NULL;
  420. freeResult(result);
  421. tmp_at = dictToArgument(start, 0, "sys", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  422. gc_freeTmpLink(&start->gc_status);
  423. if (!CHECK_RESULT(result))
  424. goto return_;
  425. base = connectArgument(tmp_at, base);
  426. }
  427. freeResult(result);
  428. }
  429. setResult(result, inter, belong);
  430. return_:
  431. *base_ad = base;
  432. return result->type;
  433. }
  434. Argument * getArgument(Parameter *call, bool is_dict, INTER_FUNCTIONSIG_NOT_ST) {
  435. Argument *new_arg = NULL;
  436. setResultCore(result);
  437. iterParameter(call, &new_arg, is_dict, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  438. return new_arg;
  439. }
  440. /**
  441. * 参数表:
  442. |实参 \ 形参| name | value | arg | kwarg | null |
  443. ----------------------------------------
  444. |name | p_3 | p_3 | p_4 | p_3! | error |
  445. |value | p_1 | p_1 | p_4 | error | error |
  446. |null | p_2 | error | p_4 | p_5 | okay |
  447. ----------------------------------------
  448. * 注解: @p_1 match_status; @p_2 default_status; @p_3 self_ass; @p_4 mul_par; @p_5 pow_par; @p_3! 通过p_3处理**kwargs
  449. * @param call
  450. * @param function
  451. * @param function_var
  452. * @param inter
  453. * @param var_list
  454. * @return
  455. */
  456. ResultType setParameter(fline line, char *file, Parameter *call_base, Parameter *function_base, VarList *function_var, LinkValue *function_father, INTER_FUNCTIONSIG_NOT_ST) {
  457. Argument *self_tmp = makeValueArgument(function_father);
  458. Argument *father_tmp = makeValueArgument(function_father->belong);
  459. Argument *call = NULL;
  460. setResultCore(result);
  461. call = getArgument(call_base, false, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  462. self_tmp->next = father_tmp;
  463. father_tmp->next = call;
  464. if (!CHECK_RESULT(result)) {
  465. freeArgument(call, false);
  466. return result->type;
  467. }
  468. freeResult(result);
  469. setParameterCore(line, file, self_tmp, function_base, function_var, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, function_father));
  470. freeArgument(self_tmp, false);
  471. return result->type;
  472. }
  473. ResultType setParameterCore(fline line, char *file, Argument *call, Parameter *function_base, VarList *function_var,
  474. INTER_FUNCTIONSIG_NOT_ST) {
  475. Parameter *function = NULL;
  476. Parameter *tmp_function = NULL; // 释放使用
  477. enum {
  478. match_status = 1,
  479. default_status = 2,
  480. self_ass = 3,
  481. mul_par = 4,
  482. space_kwargs = 5,
  483. error_to_less = -1,
  484. error_to_more = -2,
  485. error_kw = -3,
  486. finished = 0,
  487. } status = match_status;
  488. function = copyParameter(function_base);
  489. tmp_function = function;
  490. setResultCore(result);
  491. gc_freeze(inter, function_var, NULL, true);
  492. gc_freeze(inter, var_list, NULL, true);
  493. while (true){
  494. if (call == NULL && function == NULL)
  495. status = finished;
  496. else if (call != NULL && function == NULL)
  497. status = error_to_more;
  498. else if ((call == NULL && function->type == value_par))
  499. status = error_to_less;
  500. else if (call != NULL && call->type == value_par && function->type == kwargs_par)
  501. status = error_kw;
  502. else if (call == NULL && function->type == name_par) // 根据前面的条件, 已经决定function不会为NULL
  503. status = default_status;
  504. else if (call == NULL && function->type == kwargs_par)
  505. status = space_kwargs;
  506. else if (function->type == args_par) // 根据前面的条件, 已经决定call不会为NULL
  507. status = mul_par;
  508. else if (call->type == value_arg)
  509. status = match_status;
  510. else if (call->type == name_arg)
  511. status = self_ass;
  512. freeResult(result);
  513. switch (status) {
  514. case match_status: {
  515. argumentToParameter(&call, &function, function_var, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  516. returnResult(result);
  517. break;
  518. }
  519. case default_status: {
  520. vnum num = 0;
  521. defaultParameter(&function, &num, CALL_INTER_FUNCTIONSIG_NOT_ST(function_var, result, belong));
  522. returnResult(result);
  523. break;
  524. }
  525. case self_ass: {
  526. vnum set_num = 0;
  527. vnum get_num = 0;
  528. bool dict_status = false;
  529. VarList *tmp = makeVarList(inter, true);
  530. argumentToVar(&call, &set_num, CALL_INTER_FUNCTIONSIG_NOT_ST(tmp, result, belong));
  531. if (!CHECK_RESULT(result)) {
  532. freeVarList(tmp);
  533. goto return_;
  534. }
  535. freeResult(result);
  536. parameterFromVar(&function, function_var, &get_num, set_num, &dict_status, CALL_INTER_FUNCTIONSIG_NOT_ST(tmp, result, belong));
  537. freeVarList(tmp);
  538. if (!CHECK_RESULT(result))
  539. goto return_;
  540. if (!dict_status && set_num > get_num)
  541. goto to_more;
  542. break;
  543. }
  544. case mul_par: {
  545. LinkValue *tmp = makeLinkValue(makeListValue(&call, inter, value_tuple), belong, inter);
  546. assCore(function->data.value, tmp, false, false, CALL_INTER_FUNCTIONSIG_NOT_ST(function_var, result, belong));
  547. returnResult(result);
  548. function = function->next;
  549. break;
  550. }
  551. case space_kwargs:{
  552. LinkValue *tmp = makeLinkValue(makeDictValue(NULL, true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong)), belong, inter);
  553. returnResult(result);
  554. freeResult(result);
  555. assCore(function->data.value, tmp, false, false, CALL_INTER_FUNCTIONSIG_NOT_ST(function_var, result, belong));
  556. returnResult(result);
  557. function = function->next;
  558. break;
  559. }
  560. case error_to_less:
  561. setResultError(E_ArgumentException, FEW_ARG, line, file, true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  562. goto return_;
  563. case error_to_more:
  564. to_more:
  565. setResultError(E_ArgumentException, MANY_ARG, line, file, true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  566. goto return_;
  567. case error_kw:
  568. setResultError(E_ArgumentException, OBJ_NOTSUPPORT(**), line, file, true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  569. goto return_;
  570. default:
  571. goto break_;
  572. }
  573. }
  574. break_:
  575. setResult(result, inter, belong);
  576. return_:
  577. gc_freeze(inter, function_var, NULL, false);
  578. gc_freeze(inter, var_list, NULL, false);
  579. freeParameter(tmp_function, true);
  580. return result->type;
  581. }
  582. Inherit *setFather(Argument *call) {
  583. Inherit *father_tmp = NULL;
  584. for (Argument *tmp = call; tmp != NULL && tmp->type == value_arg; tmp = tmp->next)
  585. if (tmp->data.value->value->type == class) {
  586. father_tmp = connectInherit(father_tmp, makeInherit(tmp->data.value));
  587. father_tmp = connectInherit(father_tmp, copyInherit(tmp->data.value->value->object.inherit));
  588. }
  589. return setFatherCore(father_tmp);
  590. }
  591. Inherit *setFatherCore(Inherit *father_tmp) {
  592. Inherit *base_father = NULL;
  593. while (father_tmp != NULL){
  594. Inherit *next = father_tmp->next;
  595. father_tmp->next = NULL;
  596. base_father = connectSafeInherit(base_father, father_tmp);
  597. father_tmp = next;
  598. }
  599. return base_father;
  600. }
  601. bool checkFormal(Parameter *pt) {
  602. enum {
  603. Formal_1,
  604. Formal_2,
  605. } status = Formal_1;
  606. for (PASS; pt != NULL; pt = pt->next){
  607. if (status == Formal_1 && (pt->type == name_par || pt->type == args_par))
  608. status = Formal_2;
  609. else if (status == Formal_2 && (pt->type == value_par || pt->type == args_par) || pt->type == kwargs_par && pt->next != NULL)
  610. return false;
  611. }
  612. return true;
  613. }
  614. int parserArgumentUnion(ArgumentParser ap[], Argument *arg, INTER_FUNCTIONSIG_NOT_ST){
  615. setResultCore(result);
  616. if (ap->type != only_name){
  617. ArgumentParser *bak = NULL;
  618. int status = 1;
  619. arg = parserValueArgument(ap, arg, &status, &bak);
  620. if (status != 1){
  621. setResultError(E_ArgumentException, FEW_ARG, 0, "sys", true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  622. return 0;
  623. }
  624. ap = bak;
  625. }
  626. if (ap->must != -1){
  627. ArgumentParser *bak = NULL;
  628. int status;
  629. if (arg != NULL && arg->type != name_arg) {
  630. setResultError(E_ArgumentException, MANY_ARG, 0, "sys", true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  631. return -6;
  632. }
  633. status = parserNameArgument(ap, arg, &bak, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  634. if (!CHECK_RESULT(result))
  635. return -1;
  636. if (status == -3){
  637. if (parserArgumentNameDefault(ap)->must != -1){
  638. setResultError(E_ArgumentException, FEW_ARG, 0, "sys", true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  639. return -7;
  640. }
  641. }
  642. else if (status == 0){
  643. setResultError(E_ArgumentException, MANY_ARG, 0, "sys", true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  644. return -2;
  645. } else if (status == -4){
  646. setResultError(E_ArgumentException, FEW_ARG, 0, "sys", true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  647. return -3;
  648. }
  649. } else{
  650. if (arg != NULL) {
  651. setResultError(E_ArgumentException, MANY_ARG, 0, "sys", true, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  652. return -4;
  653. }
  654. }
  655. return 1;
  656. }
  657. Argument *parserValueArgument(ArgumentParser *ap, Argument *arg, int *status, ArgumentParser **bak){
  658. *status = 1;
  659. for (PASS; ap->must != -1 && (ap->type == only_value || ap->type == name_value); ap++){
  660. if (arg == NULL || arg->name_type != value_arg) { // 形参进入key=value模式
  661. if ((ap = parserArgumentValueDefault(ap))->must != -1 && ap->type == only_value) // 检查剩余的是否.must=1
  662. *status = 0; // 存在.must=1则返回 0
  663. break; // 正常情况返回 1
  664. }
  665. arg = parserArgumentValueCore(arg, ap);
  666. }
  667. if (bak != NULL)
  668. *bak = ap;
  669. return arg;
  670. }
  671. int parserNameArgument(ArgumentParser ap[], Argument *arg, ArgumentParser **bak, INTER_FUNCTIONSIG_NOT_ST){
  672. VarList *tmp = NULL;
  673. vnum set_num = 0;
  674. vnum get_num = 0;
  675. int return_;
  676. setResultCore(result);
  677. gc_freeze(inter, var_list, NULL, true);
  678. for (PASS; arg != NULL && arg->type != name_arg; arg = arg->next)
  679. PASS;
  680. if (arg == NULL) {
  681. return_ = -3; // 参数缺失
  682. goto return_;
  683. }
  684. tmp = makeVarList(inter, true);
  685. argumentToVar(&arg, &set_num, CALL_INTER_FUNCTIONSIG_NOT_ST(tmp, result, belong));
  686. if (!CHECK_RESULT(result)) {
  687. return_ = -1;
  688. goto return_;
  689. }
  690. setResultBase(result, inter, belong);
  691. for (PASS; ap->must != -1 && (ap->type == only_name || ap->type == name_value); ap++) {
  692. int status = parserArgumentVar(ap, inter, tmp);
  693. if (status == 1)
  694. get_num ++;
  695. else{
  696. return_ = -2; // 参数缺失
  697. goto return_;
  698. }
  699. }
  700. return_ = (get_num < set_num) ? 0 : ((get_num > set_num) ? -4 : 1);
  701. return_:
  702. freeVarList(tmp);
  703. gc_freeze(inter, var_list, NULL, false);
  704. if (bak != NULL)
  705. *bak = ap;
  706. return return_;
  707. }
  708. Argument *parserArgumentValueCore(Argument *arg, ArgumentParser *ap){
  709. int count = 1;
  710. ap->arg = arg;
  711. ap->value = arg->data.value;
  712. arg = arg->next;
  713. if (ap->long_arg)
  714. for (PASS; arg != NULL && arg->type == value_arg; arg = arg->next, count++)
  715. PASS;
  716. ap->c_count = count;
  717. return arg;
  718. }
  719. int parserArgumentVar(ArgumentParser *ap, Inter *inter, VarList *var_list){
  720. LinkValue *value = findStrVar(ap->name, false, CALL_INTER_FUNCTIONSIG_CORE(var_list));
  721. ap->value = value;
  722. if (value != NULL)
  723. return 1;
  724. else if (ap->must)
  725. return -1;
  726. return 0;
  727. }
  728. ArgumentParser *parserArgumentValueDefault(ArgumentParser *ap){
  729. for (PASS; ap->type == only_value && ap->must == 0; ap++) {
  730. ap->arg = NULL;
  731. ap->value = NULL;
  732. ap->c_count = 0;
  733. }
  734. return ap;
  735. }
  736. ArgumentParser *parserArgumentNameDefault(ArgumentParser *ap){
  737. for (PASS; ap->must == 0; ap++) {
  738. ap->arg = NULL;
  739. ap->value = NULL;
  740. ap->c_count = 0;
  741. }
  742. return ap;
  743. }