value.c 24 KB

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