value.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. #include "__run.h"
  2. Value *makeObject(Inter *inter, VarList *object, VarList *out_var, bool set_out_var, Inherit *inherit) {
  3. register Value **list_tmp = &inter->base;
  4. Value *last;
  5. Value *tmp;
  6. MACRO_CALLOC(tmp, 1, sizeof(Value));
  7. setGC(&tmp->gc_status);
  8. gc_addTmpLink(&tmp->gc_status);
  9. tmp->type = V_obj;
  10. tmp->gc_next = NULL;
  11. if (inter->data.base_obj[B_OBJECT] != NULL && inherit == NULL)
  12. inherit = makeInherit(inter->data.base_obj[B_OBJECT]);
  13. if (set_out_var && out_var == NULL && inherit != NULL)
  14. out_var = copyVarList(inherit->value->value->object.out_var, false, inter);
  15. tmp->object.var = makeObjectVarList(inherit, inter, object);
  16. tmp->object.out_var = out_var;
  17. tmp->object.inherit = inherit;
  18. for (last = NULL; (*list_tmp) != NULL; list_tmp = &(*list_tmp)->gc_next)
  19. last = *list_tmp;
  20. *list_tmp = tmp;
  21. tmp->gc_last = last;
  22. return tmp;
  23. }
  24. Value *useNoneValue(Inter *inter, Result *result) {
  25. LinkValue *tmp = inter->data.base_obj[B_NONE];
  26. if (result != NULL) {
  27. setResultCore(result);
  28. result->type = R_opt;
  29. result->value = tmp;
  30. gc_addTmpLink(&result->value->gc_status);
  31. }
  32. return tmp->value;
  33. }
  34. Value *makeBoolValue(bool bool_num, fline line, char *file, FUNC_NT) {
  35. Value *tmp = NULL;
  36. setResultCore(result);
  37. if (inter->data.free_mode) {
  38. callBackCore(inter->data.base_obj[B_BOOL], NULL, line, file, 0, CNEXT_NT);
  39. if (!CHECK_RESULT(result))
  40. return NULL;
  41. } else
  42. setResultOperation(result, boolCore(belong, inter->data.base_obj[B_BOOL], inter));
  43. tmp = result->value->value;
  44. tmp->data.bool_.bool_ = bool_num;
  45. return tmp;
  46. }
  47. Value *makePassValue(fline line, char *file, FUNC_NT){ // TODO-szh 让切片支持该语法 检查语法解析器支持 a[::]的语法
  48. Value *tmp = NULL;
  49. setResultCore(result);
  50. if (inter->data.free_mode) {
  51. callBackCore(inter->data.base_obj[B_PASS], NULL, line, file, 0, CNEXT_NT);
  52. if (!CHECK_RESULT(result))
  53. return NULL;
  54. } else
  55. setResultOperation(result, passCore(belong, inter->data.base_obj[B_PASS], inter));
  56. tmp = result->value->value;
  57. return tmp;
  58. }
  59. Value *makeIntValue(vint num, fline line, char *file, FUNC_NT) {
  60. Value *tmp = NULL;
  61. setResultCore(result);
  62. if (inter->data.free_mode) {
  63. callBackCore(inter->data.base_obj[B_INT_], NULL, line, file, 0, CNEXT_NT);
  64. if (!CHECK_RESULT(result))
  65. return NULL;
  66. } else
  67. setResultOperation(result, intCore(belong, inter->data.base_obj[B_INT_], inter));
  68. result->value->belong = belong;
  69. tmp = result->value->value;
  70. tmp->data.int_.num = num;
  71. return tmp;
  72. }
  73. Value *makeDouValue(vdou num, fline line, char *file, FUNC_NT) {
  74. Value *tmp = NULL;
  75. setResultCore(result);
  76. if (isnan(num) || isinf(num)) {
  77. setResultError(E_TypeException, L"decimal exception / [inf/nan]", LINEFILE, true, CNEXT_NT);
  78. return NULL;
  79. }
  80. if (inter->data.free_mode) {
  81. callBackCore(inter->data.base_obj[B_DOU], NULL, line, file, 0, CNEXT_NT);
  82. if (!CHECK_RESULT(result))
  83. return NULL;
  84. } else
  85. setResultOperation(result, douCore(belong, inter->data.base_obj[B_DOU], inter));
  86. tmp = result->value->value;
  87. tmp->data.dou.num = num;
  88. return tmp;
  89. }
  90. Value *makePointerValue(void *p, fline line, char *file, FUNC_NT) {
  91. Value *tmp = NULL;
  92. setResultCore(result);
  93. callBackCore(inter->data.base_obj[B_POINTER], NULL, line, file, 0, CNEXT_NT);
  94. if (!CHECK_RESULT(result))
  95. return NULL;
  96. tmp = result->value->value;
  97. tmp->data.pointer.pointer = p;
  98. return tmp;
  99. }
  100. Value *makeStringValue(wchar_t *str, fline line, char *file, FUNC_NT) {
  101. Value *tmp = NULL;
  102. setResultCore(result);
  103. if (inter->data.free_mode) {
  104. callBackCore(inter->data.base_obj[B_STR], NULL, line, file, 0, CNEXT_NT);
  105. if (!CHECK_RESULT(result))
  106. return NULL;
  107. } else
  108. setResultOperation(result, strCore(belong, inter->data.base_obj[B_STR], inter));
  109. tmp = result->value->value;
  110. tmp->data.str.str = memWidecpy(str);
  111. return tmp;
  112. }
  113. Value *makeFileValue(FILE *file_, char *mode, bool is_std, char *path, fline line, char *file, FUNC_NT) {
  114. Value *tmp = NULL;
  115. setResultCore(result);
  116. callBackCore(inter->data.base_obj[B_FILE], NULL, line, file, 0, CNEXT_NT);
  117. if (!CHECK_RESULT(result))
  118. return NULL;
  119. tmp = result->value->value;
  120. tmp->data.file.file = file_;
  121. tmp->data.file.mode = memStrcpy(mode);
  122. tmp->data.file.path = memStrcpy(path);
  123. tmp->data.file.is_std = is_std;
  124. return tmp;
  125. }
  126. Value *makeVMFunctionValue(Statement *st, Parameter *pt, FUNC_NT) {
  127. Value *tmp = NULL;
  128. callBackCore(inter->data.base_obj[B_FUNCTION], NULL, st->line, st->code_file, 0, CNEXT_NT);
  129. if (!CHECK_RESULT(result))
  130. return NULL;
  131. tmp = result->value->value;
  132. tmp->data.function.function = copyStatement(st);
  133. tmp->data.function.pt = copyParameter(pt);
  134. tmp->data.function.function_data.cls = belong;
  135. tmp->object.out_var = copyVarList(var_list, false, inter);
  136. result->value->belong = belong;
  137. return tmp;
  138. }
  139. Value *makeCFunctionValue(OfficialFunction of, fline line, char *file, bool set_var, bool push, FUNC_NT) {
  140. Value *tmp = NULL;
  141. callBackCore(inter->data.base_obj[B_FUNCTION], NULL, line, file, 0, CNEXT_NT);
  142. if (!CHECK_RESULT(result))
  143. return NULL;
  144. tmp = result->value->value;
  145. tmp->data.function.type = c_func;
  146. tmp->data.function.of = of;
  147. tmp->data.function.function_data.pt_type = inter->data.default_pt_type;
  148. tmp->data.function.function_data.cls = belong;
  149. tmp->data.function.function_data.push = push;
  150. if (set_var) {
  151. tmp->object.out_var = copyVarList(var_list, false, inter);
  152. if (!push)
  153. tmp->object.out_var = pushVarList(tmp->object.out_var, inter);
  154. } else
  155. tmp->object.out_var = NULL;
  156. result->value->belong = belong;
  157. return tmp;
  158. }
  159. Value *makeFFunctionValue(void (*ffunc)(), fline line, char *file, FUNC_NT) {
  160. Value *tmp = NULL;
  161. callBackCore(inter->data.base_obj[B_FUNCTION], NULL, line, file, 0, CNEXT_NT);
  162. if (!CHECK_RESULT(result))
  163. return NULL;
  164. tmp = result->value->value;
  165. tmp->data.function.type = f_func;
  166. tmp->data.function.ffunc = ffunc;
  167. tmp->data.function.function_data.cls = belong;
  168. result->value->belong = belong;
  169. return tmp;
  170. }
  171. LinkValue *makeCFunctionFromOf(OfficialFunction of, LinkValue *func, OfficialFunction function_new, LinkValue *belong, VarList *var_list, Inter *inter) {
  172. Argument *arg = makeValueArgument(func);
  173. LinkValue *return_ = NULL;
  174. Result result;
  175. setResultCore(&result);
  176. function_new(CO_FUNC(arg, func->value->object.var, &result, func));
  177. return_ = result.value;
  178. result.value = NULL;
  179. freeResult(&result);
  180. freeArgument(arg, true);
  181. return_->value->data.function.type = c_func;
  182. return_->value->data.function.of = of;
  183. return_->value->data.function.function_data.pt_type = inter->data.default_pt_type;
  184. return_->value->data.function.function_data.cls = belong;
  185. return_->value->object.out_var = copyVarList(var_list, false, inter);
  186. return_->belong = belong;
  187. gc_freeTmpLink(&return_->gc_status);
  188. return return_;
  189. }
  190. Value *makeClassValue(VarList *var_list, Inter *inter, Inherit *father) {
  191. Value *tmp;
  192. VarList *new_var = copyVarList(var_list, false, inter);
  193. tmp = makeObject(inter, NULL, new_var, true, father);
  194. tmp->type = V_class;
  195. return tmp;
  196. }
  197. Value *makeListValue(Argument *arg, fline line, char *file, enum ListType type, FUNC_NT) {
  198. Value *tmp = NULL;
  199. setResultCore(result);
  200. if (type == L_list)
  201. callBackCore(inter->data.base_obj[B_LIST], arg, line, file, 0, CNEXT_NT);
  202. else
  203. callBackCore(inter->data.base_obj[B_TUPLE], arg, line, file, 0, CNEXT_NT);
  204. if (!CHECK_RESULT(result))
  205. return NULL;
  206. tmp = result->value->value;
  207. return tmp;
  208. }
  209. Value *makeDictValue(Argument *arg, bool new_hash, fline line, char *file, FUNC_NT) {
  210. Value *tmp = NULL;
  211. setResultCore(result);
  212. callBackCore(inter->data.base_obj[B_DICT], arg, line, file, 0, CNEXT_NT);
  213. if (!CHECK_RESULT(result))
  214. return NULL;
  215. tmp = result->value->value;
  216. if (!new_hash) {
  217. tmp->data.dict.dict = NULL;
  218. tmp->data.dict.size = 0;
  219. }
  220. return tmp;
  221. }
  222. void freeValue(Value **value) {
  223. Value *free_value = *value;
  224. FREE_BASE(free_value, return_);
  225. for (VarList *tmp = free_value->object.var; tmp != NULL; tmp = freeVarList(tmp))
  226. PASS;
  227. for (VarList *tmp = free_value->object.out_var; tmp != NULL; tmp = freeVarList(tmp))
  228. PASS;
  229. for (struct Inherit *tmp = free_value->object.inherit; tmp != NULL; tmp = freeInherit(tmp))
  230. PASS;
  231. switch (free_value->type) {
  232. case V_str:
  233. memFree(free_value->data.str.str);
  234. break;
  235. case V_file:
  236. memFree(free_value->data.file.mode);
  237. memFree(free_value->data.file.path);
  238. // file在__del__中释放
  239. break;
  240. case V_func: {
  241. freeParameter(free_value->data.function.pt, true);
  242. freeStatement(free_value->data.function.function);
  243. break;
  244. }
  245. case V_list:
  246. memFree(free_value->data.list.list);
  247. break;
  248. default:
  249. break;
  250. }
  251. if ((*value)->gc_next != NULL)
  252. (*value)->gc_next->gc_last = (*value)->gc_last;
  253. *value = (*value)->gc_next;
  254. memFree(free_value);
  255. return_: return;
  256. }
  257. LinkValue *makeLinkValue(Value *value, LinkValue *belong, enum ValueAuthority aut, Inter *inter) { // TODO-szh 为LinkValue添加gc_tmpLink
  258. LinkValue **list_tmp = &inter->link_base;
  259. LinkValue *last;
  260. LinkValue *tmp;
  261. MACRO_CALLOC(tmp, 1, sizeof(LinkValue));
  262. tmp->belong = belong;
  263. tmp->value = value;
  264. tmp->gc_next = NULL;
  265. setGC(&tmp->gc_status);
  266. inter->data.run_gc ++;
  267. for (last = NULL; *list_tmp != NULL; list_tmp = &(*list_tmp)->gc_next)
  268. last = *list_tmp;
  269. *list_tmp = tmp;
  270. tmp->gc_last = last;
  271. tmp->aut = aut;
  272. return tmp;
  273. }
  274. void freeLinkValue(LinkValue **value) {
  275. LinkValue *free_value = *value;
  276. FREE_BASE(free_value, return_);
  277. if ((*value)->gc_next != NULL)
  278. (*value)->gc_next->gc_last = (*value)->gc_last;
  279. *value = (*value)->gc_next;
  280. memFree(free_value);
  281. return_: return;
  282. }
  283. void setResultCore(Result *ru) {
  284. ru->type = R_not;
  285. ru->times = 0;
  286. ru->is_yield = false;
  287. ru->error = NULL;
  288. ru->value = NULL;
  289. ru->label = NULL;
  290. ru->node = NULL;
  291. }
  292. void setResult(Result *ru, Inter *inter) {
  293. freeResult(ru);
  294. setResultBase(ru, inter);
  295. }
  296. void setResultBase(Result *ru, Inter *inter) {
  297. setResultCore(ru);
  298. useNoneValue(inter, ru);
  299. }
  300. void setResultErrorSt(BaseErrorType type, wchar_t *error_message, bool new, Statement *st, FUNC_NT) {
  301. setResultError(type, error_message, st->line, st->code_file, new, CNEXT_NT);
  302. }
  303. void setResultFromERR(enum BaseErrorType exc, FUNC_NT) {
  304. wchar_t *err = memStrToWcs(strerror(errno), false);
  305. setResultError(exc, err, LINEFILE, true, CNEXT_NT);
  306. memFree(err);
  307. }
  308. static wchar_t *getErrorInfo(LinkValue *exc, int type, FUNC_NT){
  309. wchar_t *str_name = type == 1 ? inter->data.mag_func[M_NAME] : inter->data.mag_func[M_MESSAGE];
  310. LinkValue *_info_;
  311. setResultCore(result);
  312. gc_addTmpLink(&exc->gc_status);
  313. _info_ = findAttributes(str_name, false, LINEFILE, true, CFUNC_NT(var_list, result, exc));
  314. gc_freeTmpLink(&exc->gc_status);
  315. if (!CHECK_RESULT(result))
  316. return NULL;
  317. if (_info_ != NULL && _info_->value->type == V_str)
  318. return memWidecpy(_info_->value->data.str.str);
  319. else
  320. return type == 1 ? memWidecpy(L"Error Type: Unknown") : memWidecpy(L"Error Message: Unknown");
  321. }
  322. void callException(LinkValue *exc, wchar_t *message, fline line, char *file, FUNC_NT) {
  323. LinkValue *_new_;
  324. wchar_t *type = NULL;
  325. wchar_t *error_message = NULL;
  326. setResultCore(result);
  327. gc_addTmpLink(&exc->gc_status);
  328. _new_ = findAttributes(inter->data.mag_func[M_NEW], false, LINEFILE, true, CFUNC_NT(var_list, result, exc));
  329. if (!CHECK_RESULT(result))
  330. goto return_;
  331. freeResult(result);
  332. if (_new_ != NULL){
  333. Argument *arg = NULL;
  334. LinkValue *error;
  335. makeStringValue(message, line, file, CNEXT_NT);
  336. if (!CHECK_RESULT(result))
  337. goto return_;
  338. arg = makeValueArgument(result->value);
  339. freeResult(result);
  340. gc_addTmpLink(&_new_->gc_status);
  341. callBackCore(_new_, arg, line, file, 0, CNEXT_NT);
  342. GET_RESULT(error, result); // 没有释放error的tmp link, 等于error的tmp link添加了两次
  343. gc_freeTmpLink(&_new_->gc_status);
  344. freeArgument(arg, true);
  345. type = getErrorInfo(error, 1, CNEXT_NT);
  346. if (!CHECK_RESULT(result))
  347. goto return_;
  348. freeResult(result);
  349. error_message = getErrorInfo(error, 2, CNEXT_NT);
  350. if (!CHECK_RESULT(result))
  351. goto return_;
  352. freeResult(result);
  353. setResultOperation(result, error); // 自动再次添加error的tmp link, error目前tmp link被添加了两次
  354. gc_freeTmpLink(&error->gc_status); // 释放error的tmp link
  355. }
  356. else {
  357. result->value = exc;
  358. gc_addTmpLink(&result->value->gc_status);
  359. }
  360. result->type = R_error;
  361. result->error = connectError(makeError(type, error_message, line, file), result->error);
  362. memFree(type);
  363. memFree(error_message);
  364. return_: gc_freeTmpLink(&exc->gc_status);
  365. }
  366. void setResultError(BaseErrorType type, wchar_t *error_message, fline line, char *file, bool new, FUNC_NT) {
  367. if (!new && result->type != R_error)
  368. return;
  369. if (new) {
  370. LinkValue *exc = inter->data.base_exc[type];
  371. if (exc == NULL)
  372. exc = inter->data.base_exc[E_BaseException];
  373. freeResult(result);
  374. callException(exc, error_message, line, file, CNEXT_NT);
  375. }
  376. else
  377. result->error = connectError(makeError(NULL, NULL, line, file), result->error);
  378. }
  379. void setResultOperationNone(Result *ru, Inter *inter, LinkValue *belong) {
  380. setResult(ru, inter);
  381. ru->type = R_opt;
  382. }
  383. void setResultOperation(Result *ru, LinkValue *value) {
  384. freeResult(ru);
  385. setResultOperationBase(ru, value);
  386. }
  387. void setResultOperationBase(Result *ru, LinkValue *value) {
  388. setResultCore(ru);
  389. ru->value = value;
  390. if (value != NULL)
  391. gc_addTmpLink(&ru->value->gc_status);
  392. ru->type = R_opt;
  393. }
  394. void freeResult(Result *ru){
  395. memFree(ru->label);
  396. ru->label = NULL;
  397. if (ru->error != NULL)
  398. freeError(ru);
  399. ru->error = NULL;
  400. if (ru->value != NULL) {
  401. gc_freeTmpLink(&ru->value->gc_status);
  402. ru->value = NULL;
  403. }
  404. setResultCore(ru);
  405. }
  406. Error *makeError(wchar_t *type, wchar_t *message, fline line, char *file) {
  407. Error *tmp = memCalloc(1, sizeof(Error));
  408. tmp->line = line;
  409. tmp->type = memWidecpy(type);
  410. tmp->messgae = memWidecpy(message);
  411. tmp->file = memStrcpy(file);
  412. tmp->next = NULL;
  413. return tmp;
  414. }
  415. Error *connectError(Error *new, Error *base){
  416. new->next = base;
  417. return new;
  418. }
  419. void freeError(Result *base){
  420. Error *error = base->error;
  421. for (Error *next = NULL; error != NULL; error = next){
  422. next = error->next;
  423. memFree(error->messgae);
  424. memFree(error->type);
  425. memFree(error->file);
  426. memFree(error);
  427. }
  428. base->error = NULL;
  429. }
  430. void printError(Result *result, Inter *inter, bool free) {
  431. for (Error *base = result->error; base != NULL; base = base->next){
  432. if (base->next != NULL)
  433. fprintf(inter->data.inter_stderr, "Error Backtracking: On Line: %lld In file: %s Error ID: %p\n", base->line, base->file, base);
  434. else
  435. fprintf(inter->data.inter_stderr, "%ls\n%ls\nOn Line: %lld\nIn File: %s\nError ID: %p\n", base->type, base->messgae, base->line, base->file, base);
  436. }
  437. if (free)
  438. freeError(result);
  439. fflush(inter->data.inter_stderr);
  440. }
  441. inline bool isType(Value *value, enum ValueType type){
  442. return value->type == type;
  443. }
  444. Inherit *makeInherit(LinkValue *value){
  445. Inherit *tmp;
  446. tmp = memCalloc(1, sizeof(Inherit));
  447. tmp->value = value;
  448. tmp->next = NULL;
  449. return tmp;
  450. }
  451. Inherit *copyInheritCore(Inherit *value){
  452. Inherit *tmp;
  453. if (value == NULL)
  454. return NULL;
  455. tmp = makeInherit(value->value);
  456. return tmp;
  457. }
  458. Inherit *copyInherit(Inherit *value){
  459. Inherit *base = NULL;
  460. Inherit **tmp = &base;
  461. for (PASS; value != NULL; value = value->next, tmp = &(*tmp)->next)
  462. *tmp = copyInheritCore(value);
  463. return base;
  464. }
  465. Inherit *freeInherit(Inherit *value){
  466. FREE_BASE(value, error_);
  467. Inherit *next = value->next;
  468. memFree(value);
  469. return next;
  470. error_: return NULL;
  471. }
  472. Inherit *connectInherit(Inherit *base, Inherit *back){
  473. Inherit **tmp = &base;
  474. for (PASS; *tmp != NULL; tmp = &(*tmp)->next)
  475. PASS;
  476. *tmp = back;
  477. return base;
  478. }
  479. Inherit *connectSafeInherit(Inherit *base, Inherit *back){
  480. Inherit **last_node = &base;
  481. if (back == NULL)
  482. goto return_;
  483. for (PASS; *last_node != NULL;)
  484. if ((*last_node)->value->value == back->value->value)
  485. *last_node = freeInherit(*last_node);
  486. else
  487. last_node = &(*last_node)->next;
  488. *last_node = back;
  489. return_: return base;
  490. }
  491. Inherit *getInheritFromValueCore(LinkValue *num_father) {
  492. Inherit *object_father;
  493. Argument *father_arg = makeValueArgument(num_father);
  494. gc_addTmpLink(&num_father->gc_status);
  495. object_father = setFather(father_arg);
  496. freeArgument(father_arg, true);
  497. gc_freeTmpLink(&num_father->gc_status);
  498. return object_father;
  499. }
  500. Package *makePackage(Value *value, char *md5, char *name, Package *base) {
  501. Package *tmp = memCalloc(1, sizeof(Package));
  502. Package *tmp_base = base;
  503. tmp->name = memStrcpy(name);
  504. tmp->md5 = memStrcpy(md5);
  505. tmp->package = value;
  506. gc_addStatementLink(&value->gc_status);
  507. tmp->next = NULL;
  508. if (base == NULL)
  509. return tmp;
  510. for (PASS; tmp_base->next != NULL; tmp_base = tmp_base->next)
  511. PASS;
  512. tmp_base->next = tmp;
  513. return base;
  514. }
  515. void freePackage(Package *base) {
  516. for (Package *next; base != NULL; base = next) {
  517. next = base->next;
  518. gc_freeStatementLink(&base->package->gc_status);
  519. memFree(base->name);
  520. memFree(base->md5);
  521. memFree(base);
  522. }
  523. }
  524. Value *checkPackage(Package *base, char *md5, char *name) {
  525. for (PASS; base != NULL; base = base->next) {
  526. if (eqString(name, base->name) && eqString(md5, base->md5))
  527. return base->package;
  528. }
  529. return NULL;
  530. }
  531. bool needDel(Value *object_value, Inter *inter) {
  532. LinkValue *_del_ = checkStrVar(inter->data.mag_func[M_DEL], false, CFUNC_CORE(object_value->object.var));
  533. enum FunctionPtType type;
  534. if (_del_ == NULL)
  535. return false;
  536. type = _del_->value->data.function.function_data.pt_type;
  537. if ((type == object_free_ || type == object_static_) && object_value->type == V_class)
  538. return false;
  539. if (_del_->belong == NULL || _del_->belong->value == object_value || checkAttribution(object_value, _del_->belong->value))
  540. return true;
  541. return false;
  542. }
  543. bool callDel(Value *object_value, Result *result, Inter *inter, VarList *var_list) {
  544. LinkValue *_del_ = findStrVarOnly(inter->data.mag_func[M_DEL], false, CFUNC_CORE(object_value->object.var));
  545. setResultCore(result);
  546. if (_del_ != NULL){
  547. if (_del_->belong != NULL && _del_->belong->value != object_value && checkAttribution(object_value, _del_->belong->value)) { // 与point运算道理相同
  548. _del_ = COPY_LINKVALUE(_del_, inter);
  549. _del_->belong = makeLinkValue(object_value, inter->base_belong, auto_aut, inter);
  550. }
  551. gc_addTmpLink(&_del_->gc_status);
  552. callBackCore(_del_, NULL, LINEFILE, 0, CFUNC_NT(var_list, result, inter->base_belong));
  553. gc_freeTmpLink(&_del_->gc_status);
  554. return true;
  555. } else
  556. return false;
  557. }
  558. /**
  559. * 检查 father 是否为 self 的父亲
  560. * @param self
  561. * @param father
  562. * @return
  563. */
  564. bool checkAttribution(Value *self, Value *father){
  565. for (Inherit *self_father = self->object.inherit; self_father != NULL; self_father = self_father->next)
  566. if (self_father->value->value == father)
  567. return true;
  568. return false;
  569. }
  570. void printValue(Value *value, FILE *debug, bool print_father, bool print_in) {
  571. switch (value->type){
  572. case V_int:
  573. fprintf(debug, "(%lld)", value->data.int_.num);
  574. break;
  575. case V_dou:
  576. fprintf(debug, "(%Lf : %Lg)", value->data.dou.num, value->data.dou.num);
  577. break;
  578. case V_str:
  579. fprintf(debug, "'%ls'", value->data.str.str);
  580. break;
  581. case V_file:
  582. if (print_father)
  583. fprintf(debug, "(file %s)", value->data.file.path);
  584. else
  585. fprintf(debug, "(file %s on %p)", value->data.file.path, value);
  586. break;
  587. case V_func:
  588. if (print_father)
  589. fprintf(debug, "func");
  590. else
  591. fprintf(debug, "(func on %p)", value);
  592. break;
  593. case V_list:
  594. if (print_in){
  595. fprintf(debug, "[");
  596. for (int i = 0; i < value->data.list.size; i++) {
  597. if (i > 0)
  598. fprintf(debug, ", ", NULL);
  599. printValue(value->data.list.list[i]->value, debug, false, false);
  600. }
  601. fprintf(debug, " ]", NULL);
  602. } else
  603. fprintf(debug, "[list]", NULL);
  604. break;
  605. case V_dict:
  606. if (print_in){
  607. Var *tmp = NULL;
  608. bool print_comma = false;
  609. fprintf(debug, "{");
  610. for (int i = 0; i < MAX_SIZE; i++) {
  611. for (tmp = value->data.dict.dict->hashtable[i]; tmp != NULL; tmp = tmp->next) {
  612. if (print_comma)
  613. fprintf(debug, ", ", NULL);
  614. else
  615. print_comma = true;
  616. printValue(tmp->name_->value, debug, false, false);
  617. fprintf(debug, " ['%ls'] : ", tmp->name);
  618. printValue(tmp->value->value, debug, false, false);
  619. }
  620. }
  621. fprintf(debug, " }", NULL);
  622. } else
  623. fprintf(debug, "[dict]", NULL);
  624. break;
  625. case V_none:
  626. fprintf(debug, "(null)", NULL);
  627. break;
  628. case V_class:
  629. if (print_father)
  630. fprintf(debug, "class");
  631. else
  632. fprintf(debug, "(class on %p)", value);
  633. break;
  634. case V_obj:
  635. if (print_father)
  636. fprintf(debug, "object");
  637. else
  638. fprintf(debug, "(object on %p)", value);
  639. break;
  640. case V_bool:
  641. if (value->data.bool_.bool_)
  642. fprintf(debug, "true");
  643. else
  644. fprintf(debug, "false");
  645. break;
  646. case V_ell:
  647. fprintf(debug, "...");
  648. break;
  649. case V_lib:
  650. if (print_father)
  651. fprintf(debug, "lib");
  652. else
  653. fprintf(debug, "(lib on %p)", value);
  654. break;
  655. default:
  656. fprintf(debug, "unknown");
  657. break;
  658. }
  659. if (print_father){
  660. fprintf(debug, "(");
  661. printf("<%p>", value);
  662. for (Inherit *fv = value->object.inherit; fv != NULL; fv = fv->next) {
  663. printf(" -> ");
  664. printValue(fv->value->value, debug, false, false);
  665. }
  666. fprintf(debug, ")");
  667. }
  668. }
  669. void printLinkValue(LinkValue *value, char *first, char *last, FILE *debug){
  670. if (value == NULL)
  671. return;
  672. fprintf(debug, "%s", first);
  673. if (value->belong != NULL) {
  674. printLinkValue(value->belong, "", "", debug);
  675. fprintf(debug, " . ", NULL);
  676. }
  677. if (value->value != NULL)
  678. printValue(value->value, debug, true, true);
  679. fprintf(debug, "%s", last);
  680. }