value.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678
  1. #include "__run.h"
  2. Value *makeObject(Inter *inter, VarList *object, VarList *out_var, Inherit *inherit) {
  3. Value *tmp, *list_tmp = inter->base;
  4. tmp = memCalloc(1, sizeof(Value));
  5. setGC(&tmp->gc_status);
  6. tmp->type = object_;
  7. tmp->gc_next = NULL;
  8. if (inter->data.object != NULL && inherit == NULL)
  9. inherit = makeInherit(makeLinkValue(inter->data.object, NULL, inter));
  10. if (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. if (list_tmp == NULL){
  16. inter->base = tmp;
  17. tmp->gc_last = NULL;
  18. goto return_;
  19. }
  20. for (PASS; list_tmp->gc_next != NULL; list_tmp = list_tmp->gc_next)
  21. PASS;
  22. list_tmp->gc_next = tmp;
  23. tmp->gc_last = list_tmp;
  24. return_:
  25. return tmp;
  26. }
  27. Value *makeNoneValue(fline line, char *file, INTER_FUNCTIONSIG_NOT_ST) {
  28. Value *tmp;
  29. setResultCore(result);
  30. callBackCore(makeLinkValue(inter->data.object, inter->base_father, inter), NULL, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result ,belong));
  31. if (!CHECK_RESULT(result))
  32. return NULL;
  33. tmp = result->value->value;
  34. tmp->type = none;
  35. return tmp;
  36. }
  37. Value *useNoneValue(Inter *inter, Result *result) {
  38. Value *tmp = inter->data.none;
  39. if (result != NULL) {
  40. setResultCore(result);
  41. result->type = operation_return;
  42. result->value = makeLinkValue(tmp, inter->base_father, inter);
  43. gc_addTmpLink(&result->value->gc_status);
  44. }
  45. return tmp;
  46. }
  47. Value *makeBoolValue(bool bool_num, fline line, char *file, INTER_FUNCTIONSIG_NOT_ST) {
  48. Value *tmp = NULL;
  49. callBackCore(makeLinkValue(inter->data.bool_, inter->base_father, inter), NULL, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result ,belong));
  50. if (!CHECK_RESULT(result))
  51. return NULL;
  52. tmp = result->value->value;
  53. tmp->data.bool_.bool_ = bool_num;
  54. return tmp;
  55. }
  56. Value *makePassValue(fline line, char *file, INTER_FUNCTIONSIG_NOT_ST){ // TODO-szh 让切片支持该语法
  57. Value *tmp = NULL;
  58. callBackCore(makeLinkValue(inter->data.pass_, inter->base_father, inter), NULL, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result ,belong));
  59. if (!CHECK_RESULT(result))
  60. return NULL;
  61. tmp = result->value->value;
  62. return tmp;
  63. }
  64. Value *makeNumberValue(vnum num, fline line, char *file, INTER_FUNCTIONSIG_NOT_ST) {
  65. Value *tmp = NULL;
  66. callBackCore(makeLinkValue(inter->data.num, inter->base_father, inter), NULL, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result ,belong));
  67. if (!CHECK_RESULT(result))
  68. return NULL;
  69. tmp = result->value->value;
  70. tmp->data.num.num = num;
  71. return tmp;
  72. }
  73. Value *makeStringValue(char *str, fline line, char *file, INTER_FUNCTIONSIG_NOT_ST) {
  74. Value *tmp = NULL;
  75. callBackCore(makeLinkValue(inter->data.str, inter->base_father, inter), NULL, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result ,belong));
  76. if (!CHECK_RESULT(result))
  77. return NULL;
  78. tmp = result->value->value;
  79. memFree(tmp->data.str.str);
  80. tmp->data.str.str = memStrcpy(str);
  81. return tmp;
  82. }
  83. Value *makeVMFunctionValue(Statement *st, Parameter *pt, INTER_FUNCTIONSIG_NOT_ST) { // TODO-szh 设置无var_list的函数 (允许使用装饰器装饰,该功能未测试)
  84. Value *tmp = NULL;
  85. callBackCore(makeLinkValue(inter->data.function, inter->base_father, inter), NULL, st->line, st->code_file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result ,belong));
  86. if (!CHECK_RESULT(result))
  87. return NULL;
  88. tmp = result->value->value;
  89. tmp->data.function.function = copyStatement(st);
  90. tmp->data.function.pt = copyParameter(pt);
  91. for (VarList *vl = tmp->object.out_var, *vl_next; vl != NULL; vl = vl_next) {
  92. vl_next = vl->next;
  93. freeVarList(vl);
  94. }
  95. tmp->object.out_var = copyVarList(var_list, false, inter);
  96. result->value->belong = belong;
  97. return tmp;
  98. }
  99. Value *makeCFunctionValue(OfficialFunction of, fline line, char *file, INTER_FUNCTIONSIG_NOT_ST) {
  100. Value *tmp = NULL;
  101. callBackCore(makeLinkValue(inter->data.function, inter->base_father, inter), NULL, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result ,belong));
  102. if (!CHECK_RESULT(result))
  103. return NULL;
  104. tmp = result->value->value;
  105. tmp->data.function.type = c_function;
  106. tmp->data.function.of = of;
  107. tmp->data.function.function_data.pt_type = inter->data.default_pt_type;
  108. for (VarList *vl = tmp->object.out_var, *vl_next; vl != NULL; vl = vl_next) {
  109. vl_next = vl->next;
  110. freeVarList(vl);
  111. }
  112. tmp->object.out_var = copyVarList(var_list, false, inter);
  113. result->value->belong = belong;
  114. return tmp;
  115. }
  116. LinkValue *makeCFunctionFromOf(OfficialFunction of, LinkValue *func, OfficialFunction function_new, OfficialFunction function_init, LinkValue *belong, VarList *var_list, Inter *inter) {
  117. Argument *arg = makeValueArgument(func);
  118. Argument *init_arg = NULL;
  119. LinkValue *return_ = NULL;
  120. Result result;
  121. setResultCore(&result);
  122. function_new(CALL_OFFICAL_FUNCTION(arg, func->value->object.var, &result, func));
  123. return_ = result.value;
  124. result.value = NULL;
  125. freeResult(&result);
  126. init_arg = makeValueArgument(return_);
  127. function_init(CALL_OFFICAL_FUNCTION(init_arg, func->value->object.var, &result, func));
  128. freeResult(&result);
  129. freeArgument(init_arg, true);
  130. freeArgument(arg, true);
  131. return_->value->data.function.type = c_function;
  132. return_->value->data.function.of = of;
  133. return_->value->data.function.function_data.pt_type = inter->data.default_pt_type;
  134. for (VarList *vl = return_->value->object.out_var, *vl_next; vl != NULL; vl = vl_next) {
  135. vl_next = vl->next;
  136. freeVarList(vl);
  137. }
  138. return_->value->object.out_var = copyVarList(var_list, false, inter);
  139. return_->belong = belong;
  140. gc_freeTmpLink(&return_->gc_status);
  141. return return_;
  142. }
  143. Value *makeClassValue(VarList *var_list, Inter *inter, Inherit *father) {
  144. Value *tmp;
  145. tmp = makeObject(inter, NULL, var_list, father);
  146. tmp->type = class;
  147. return tmp;
  148. }
  149. Value *makeListValue(Argument *arg, fline line, char *file, enum ListType type, INTER_FUNCTIONSIG_NOT_ST) {
  150. Value *tmp = NULL;
  151. if (type == value_list)
  152. callBackCore(makeLinkValue(inter->data.list, inter->base_father, inter), arg, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result ,belong));
  153. else
  154. callBackCore(makeLinkValue(inter->data.tuple, inter->base_father, inter), arg, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result ,belong));
  155. if (!CHECK_RESULT(result))
  156. return NULL;
  157. tmp = result->value->value;
  158. return tmp;
  159. }
  160. Value *makeDictValue(Argument *arg, bool new_hash, fline line, char *file, INTER_FUNCTIONSIG_NOT_ST) {
  161. Value *tmp = NULL;
  162. LinkValue *dict = makeLinkValue(inter->data.dict, inter->base_father, inter);
  163. callBackCore(dict, arg, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result ,belong));
  164. if (!CHECK_RESULT(result))
  165. return NULL;
  166. tmp = result->value->value;
  167. if (!new_hash) {
  168. tmp->data.dict.dict = NULL;
  169. tmp->data.dict.size = 0;
  170. }
  171. return tmp;
  172. }
  173. void freeValue(Value **value) {
  174. Value *free_value = *value;
  175. FREE_BASE(free_value, return_);
  176. for (VarList *tmp = free_value->object.var; tmp != NULL; tmp = freeVarList(tmp))
  177. PASS;
  178. for (VarList *tmp = free_value->object.out_var; tmp != NULL; tmp = freeVarList(tmp))
  179. PASS;
  180. for (struct Inherit *tmp = free_value->object.inherit; tmp != NULL; tmp = freeInherit(tmp))
  181. PASS;
  182. switch (free_value->type) {
  183. case string:
  184. memFree(free_value->data.str.str);
  185. break;
  186. case function: {
  187. freeParameter(free_value->data.function.pt, true);
  188. freeStatement(free_value->data.function.function);
  189. break;
  190. }
  191. case list:
  192. memFree(free_value->data.list.list);
  193. break;
  194. default:
  195. break;
  196. }
  197. if ((*value)->gc_next != NULL)
  198. (*value)->gc_next->gc_last = (*value)->gc_last;
  199. *value = (*value)->gc_next;
  200. memFree(free_value);
  201. return_: return;
  202. }
  203. LinkValue *makeLinkValue(Value *value, LinkValue *belong, Inter *inter){
  204. LinkValue *tmp;
  205. LinkValue *list_tmp = inter->link_base;
  206. tmp = memCalloc(1, sizeof(Value));
  207. tmp->belong = belong;
  208. tmp->value = value;
  209. setGC(&tmp->gc_status);
  210. if (list_tmp == NULL){
  211. inter->link_base = tmp;
  212. tmp->gc_last = NULL;
  213. goto return_;
  214. }
  215. for (PASS; list_tmp->gc_next != NULL; list_tmp = list_tmp->gc_next)
  216. PASS;
  217. list_tmp->gc_next = tmp;
  218. tmp->gc_last = list_tmp;
  219. tmp->aut = auto_aut;
  220. return_:
  221. return tmp;
  222. }
  223. void freeLinkValue(LinkValue **value) {
  224. LinkValue *free_value = *value;
  225. FREE_BASE(free_value, return_);
  226. if ((*value)->gc_next != NULL)
  227. (*value)->gc_next->gc_last = (*value)->gc_last;
  228. *value = (*value)->gc_next;
  229. memFree(free_value);
  230. return_: return;
  231. }
  232. LinkValue *copyLinkValue(LinkValue *value, Inter *inter) {
  233. LinkValue *tmp = NULL;
  234. if (value == NULL)
  235. return NULL;
  236. tmp = makeLinkValue(value->value, value->belong, inter);
  237. tmp->aut = value->aut;
  238. return tmp;
  239. }
  240. void setResultCore(Result *ru) {
  241. ru->type = not_return;
  242. ru->times = 0;
  243. ru->error = NULL;
  244. ru->value = NULL;
  245. ru->label = NULL;
  246. ru->node = NULL;
  247. }
  248. void setResult(Result *ru, Inter *inter, LinkValue *belong) {
  249. freeResult(ru);
  250. setResultBase(ru, inter);
  251. }
  252. void setResultBase(Result *ru, Inter *inter) {
  253. setResultCore(ru);
  254. useNoneValue(inter, ru);
  255. }
  256. void setResultErrorSt(BaseErrorType type, char *error_message, bool new, Statement *st, INTER_FUNCTIONSIG_NOT_ST) {
  257. setResultError(type, error_message, st->line, st->code_file, new, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  258. }
  259. Value *findBaseError(BaseErrorType type, Inter *inter){
  260. switch (type) {
  261. case E_BaseException:
  262. return inter->data.base_exc;
  263. case E_Exception:
  264. return inter->data.exc;
  265. case E_TypeException:
  266. return inter->data.type_exc;
  267. case E_ArgumentException:
  268. return inter->data.arg_exc;
  269. case E_PermissionsException:
  270. return inter->data.per_exc;
  271. case E_GotoException:
  272. return inter->data.goto_exc;
  273. case E_ResultException:
  274. return inter->data.result_exc;
  275. case E_NameExceptiom:
  276. return inter->data.name_exc;
  277. case E_AssertException:
  278. return inter->data.assert_exc;
  279. case E_IndexException:
  280. return inter->data.index_exc;
  281. case E_KeyException:
  282. return inter->data.key_exc;
  283. case E_StrideException:
  284. return inter->data.stride_exc;
  285. case E_StopIterException:
  286. return inter->data.iterstop_exc;
  287. case E_SuperException:
  288. return inter->data.super_exc;
  289. case E_ImportException:
  290. return inter->data.import_exc;
  291. case E_IncludeException:
  292. return inter->data.include_exp;
  293. case E_SystemException:
  294. return inter->data.sys_exc;
  295. case E_KeyInterrupt:
  296. return inter->data.keyInterrupt_exc;
  297. case E_QuitException:
  298. return inter->data.quit_exc;
  299. default:
  300. return NULL;
  301. }
  302. }
  303. char *getErrorInfo(LinkValue *exc, int type, Inter *inter){
  304. char *str_name = type == 1 ? inter->data.object_name : inter->data.object_message;
  305. LinkValue *_info_ = findAttributes(str_name, false, exc, inter);
  306. if (_info_ != NULL && _info_->value->type == string)
  307. return memStrcpy(_info_->value->data.str.str);
  308. else
  309. return type == 1 ? memStrcpy("Error Type: Unknown") : memStrcpy("Error Message: Unknown");
  310. }
  311. void callException(LinkValue *exc, char *message, fline line, char *file, INTER_FUNCTIONSIG_NOT_ST) {
  312. LinkValue *_new_ = findAttributes(inter->data.object_new, false, exc, inter);
  313. char *type = NULL;
  314. char *error_message = NULL;
  315. setResultCore(result);
  316. gc_addTmpLink(&exc->gc_status);
  317. if (_new_ != NULL){
  318. Argument *arg = NULL;
  319. makeStringValue(message, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  320. if (!CHECK_RESULT(result))
  321. goto return_;
  322. arg = makeValueArgument(result->value);
  323. freeResult(result);
  324. gc_addTmpLink(&_new_->gc_status);
  325. callBackCore(_new_, arg, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  326. gc_freeTmpLink(&_new_->gc_status);
  327. freeArgument(arg, true);
  328. type = getErrorInfo(result->value, 1, inter);
  329. error_message = getErrorInfo(result->value, 2, inter);
  330. }
  331. else {
  332. result->value = exc;
  333. gc_addTmpLink(&result->value->gc_status);
  334. }
  335. result->type = error_return;
  336. result->error = connectError(makeError(type, error_message, line, file), result->error);
  337. memFree(type);
  338. memFree(error_message);
  339. return_: gc_freeTmpLink(&exc->gc_status);
  340. }
  341. void setResultError(BaseErrorType type, char *error_message, fline line, char *file, bool new, INTER_FUNCTIONSIG_NOT_ST) {
  342. if (!new && result->type != error_return)
  343. return;
  344. if (new) {
  345. Value *exc = findBaseError(type, inter);
  346. if (exc == NULL)
  347. exc = inter->data.base_exc;
  348. freeResult(result);
  349. callException(makeLinkValue(exc, belong, inter), error_message, line, file, CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, belong));
  350. }
  351. else
  352. result->error = connectError(makeError(NULL, NULL, line, file), result->error);
  353. }
  354. void setResultOperationNone(Result *ru, Inter *inter, LinkValue *belong) {
  355. setResult(ru, inter, belong);
  356. ru->type = operation_return;
  357. }
  358. void setResultOperation(Result *ru, LinkValue *value) {
  359. freeResult(ru);
  360. setResultOperationBase(ru, value);
  361. }
  362. void setResultOperationBase(Result *ru, LinkValue *value) {
  363. setResultCore(ru);
  364. ru->value = value;
  365. if (value != NULL)
  366. gc_addTmpLink(&ru->value->gc_status);
  367. ru->type = operation_return;
  368. }
  369. void freeResult(Result *ru){
  370. memFree(ru->label);
  371. ru->label = NULL;
  372. freeResultSafe(ru);
  373. if (ru->value != NULL) {
  374. gc_freeTmpLink(&ru->value->gc_status);
  375. ru->value = NULL;
  376. }
  377. }
  378. void freeResultSafe(Result *ru){
  379. if (ru->error != NULL)
  380. freeError(ru);
  381. ru->error = NULL;
  382. }
  383. Error *makeError(char *type, char *message, fline line, char *file) {
  384. Error *tmp = memCalloc(1, sizeof(Error));
  385. tmp->line = line;
  386. tmp->type = memStrcpy(type);
  387. tmp->messgae = memStrcpy(message);
  388. tmp->file = memStrcpy(file);
  389. tmp->next = NULL;
  390. return tmp;
  391. }
  392. Error *connectError(Error *new, Error *base){
  393. new->next = base;
  394. return new;
  395. }
  396. void freeError(Result *base){
  397. Error *error = base->error;
  398. for (Error *next = NULL; error != NULL; error = next){
  399. next = error->next;
  400. memFree(error->messgae);
  401. memFree(error->type);
  402. memFree(error->file);
  403. memFree(error);
  404. }
  405. base->error = NULL;
  406. }
  407. void printError(Result *result, Inter *inter, bool free) {
  408. for (Error *base = result->error; base != NULL; base = base->next){
  409. if (base->next != NULL)
  410. fprintf(inter->data.inter_stderr, "Error Backtracking: On Line: %lld In file: %s Error ID: %p\n", base->line, base->file, base);
  411. else
  412. fprintf(inter->data.inter_stderr, "%s\n%s\nOn Line: %lld\nIn File: %s\nError ID: %p\n", base->type, base->messgae, base->line, base->file, base);
  413. }
  414. if (free)
  415. freeError(result);
  416. }
  417. inline bool isType(Value *value, enum ValueType type){
  418. return value->type == type;
  419. }
  420. Inherit *makeInherit(LinkValue *value){
  421. Inherit *tmp;
  422. tmp = memCalloc(1, sizeof(Inherit));
  423. tmp->value = value;
  424. tmp->next = NULL;
  425. return tmp;
  426. }
  427. Inherit *copyInheritCore(Inherit *value){
  428. Inherit *tmp;
  429. if (value == NULL)
  430. return NULL;
  431. tmp = makeInherit(value->value);
  432. return tmp;
  433. }
  434. Inherit *copyInherit(Inherit *value){
  435. Inherit *base = NULL;
  436. Inherit **tmp = &base;
  437. for (PASS; value != NULL; value = value->next, tmp = &(*tmp)->next)
  438. *tmp = copyInheritCore(value);
  439. return base;
  440. }
  441. Inherit *freeInherit(Inherit *value){
  442. FREE_BASE(value, error_);
  443. Inherit *next = value->next;
  444. memFree(value);
  445. return next;
  446. error_: return NULL;
  447. }
  448. Inherit *connectInherit(Inherit *base, Inherit *back){
  449. Inherit **tmp = &base;
  450. for (PASS; *tmp != NULL; tmp = &(*tmp)->next)
  451. PASS;
  452. *tmp = back;
  453. return base;
  454. }
  455. Inherit *connectSafeInherit(Inherit *base, Inherit *back){
  456. Inherit **last_node = &base;
  457. if (back == NULL)
  458. goto return_;
  459. for (PASS; *last_node != NULL;)
  460. if ((*last_node)->value->value == back->value->value)
  461. *last_node = freeInherit(*last_node);
  462. else
  463. last_node = &(*last_node)->next;
  464. *last_node = back;
  465. return_: return base;
  466. }
  467. Inherit *getInheritFromValue(Value *value, Inter *inter){
  468. LinkValue *num_father = makeLinkValue(value, inter->base_father, inter);
  469. return getInheritFromValueCore(num_father);
  470. }
  471. Inherit *getInheritFromValueCore(LinkValue *num_father) {
  472. Inherit *object_father;
  473. Argument *father_arg = makeValueArgument(num_father);
  474. gc_addTmpLink(&num_father->gc_status);
  475. object_father = setFather(father_arg);
  476. freeArgument(father_arg, true);
  477. gc_freeTmpLink(&num_father->gc_status);
  478. return object_father;
  479. }
  480. bool needDel(Value *object_value, Inter *inter) {
  481. LinkValue *_del_ = checkStrVar(inter->data.object_del, false, CALL_INTER_FUNCTIONSIG_CORE(object_value->object.var));
  482. enum FunctionPtType type;
  483. if (_del_ == NULL)
  484. return false;
  485. type = _del_->value->data.function.function_data.pt_type;
  486. if ((type == object_free_ || type == object_static_) && object_value->type == class)
  487. return false;
  488. if (_del_->belong == NULL || _del_->belong->value == object_value || checkAttribution(object_value, _del_->belong->value))
  489. return true;
  490. return false;
  491. }
  492. bool callDel(Value *object_value, Result *result, Inter *inter, VarList *var_list) {
  493. LinkValue *_del_ = findStrVar(inter->data.object_del, false, CALL_INTER_FUNCTIONSIG_CORE(object_value->object.var));
  494. setResultCore(result);
  495. if (_del_ != NULL){
  496. gc_addTmpLink(&_del_->gc_status);
  497. if (_del_->belong == NULL || _del_->belong->value != object_value && checkAttribution(object_value, _del_->belong->value))
  498. _del_->belong = makeLinkValue(object_value, inter->base_father, inter);
  499. callBackCore(_del_, NULL, 0, "sys", CALL_INTER_FUNCTIONSIG_NOT_ST(var_list, result, inter->base_father));
  500. gc_freeTmpLink(&_del_->gc_status);
  501. return true;
  502. } else
  503. return false;
  504. }
  505. /**
  506. * 检查 father 是否为 self 的父亲
  507. * @param self
  508. * @param father
  509. * @return
  510. */
  511. bool checkAttribution(Value *self, Value *father){
  512. for (Inherit *self_father = self->object.inherit; self_father != NULL; self_father = self_father->next)
  513. if (self_father->value->value == father)
  514. return true;
  515. return false;
  516. }
  517. void printValue(Value *value, FILE *debug, bool print_father, bool print_in) {
  518. switch (value->type){
  519. case number:
  520. fprintf(debug, "%lld", value->data.num.num);
  521. break;
  522. case string:
  523. fprintf(debug, "%s", value->data.str.str);
  524. break;
  525. case function:
  526. if (print_father)
  527. fprintf(debug, "function");
  528. else
  529. fprintf(debug, "(function on %p)", value);
  530. break;
  531. case list:
  532. if (print_in){
  533. fprintf(debug, "[");
  534. for (int i = 0; i < value->data.list.size; i++) {
  535. if (i > 0)
  536. fprintf(debug, ", ", NULL);
  537. printValue(value->data.list.list[i]->value, debug, false, false);
  538. }
  539. fprintf(debug, " ]", NULL);
  540. } else
  541. fprintf(debug, "[list]", NULL);
  542. break;
  543. case dict:
  544. if (print_in){
  545. Var *tmp = NULL;
  546. bool print_comma = false;
  547. fprintf(debug, "{");
  548. for (int i = 0; i < MAX_SIZE; i++) {
  549. for (tmp = value->data.dict.dict->hashtable[i]; tmp != NULL; tmp = tmp->next) {
  550. if (print_comma)
  551. fprintf(debug, ", ", NULL);
  552. else
  553. print_comma = true;
  554. printValue(tmp->name_->value, debug, false, false);
  555. fprintf(debug, " ['%s'] : ", tmp->name);
  556. printValue(tmp->value->value, debug, false, false);
  557. }
  558. }
  559. fprintf(debug, " }", NULL);
  560. } else
  561. fprintf(debug, "[dict]", NULL);
  562. break;
  563. case none:
  564. fprintf(debug, "(null)", NULL);
  565. break;
  566. case class:
  567. if (print_father)
  568. fprintf(debug, "class");
  569. else
  570. fprintf(debug, "(class on %p)", value);
  571. break;
  572. case object_:
  573. if (print_father)
  574. fprintf(debug, "object");
  575. else
  576. fprintf(debug, "(object on %p)", value);
  577. break;
  578. case bool_:
  579. if (value->data.bool_.bool_)
  580. fprintf(debug, "true");
  581. else
  582. fprintf(debug, "false");
  583. break;
  584. case pass_:
  585. fprintf(debug, "...");
  586. break;
  587. default:
  588. fprintf(debug, "unknown");
  589. break;
  590. }
  591. if (print_father){
  592. fprintf(debug, "(");
  593. printf("<%p>", value);
  594. for (Inherit *fv = value->object.inherit; fv != NULL; fv = fv->next) {
  595. printf(" -> ");
  596. printValue(fv->value->value, debug, false, false);
  597. }
  598. fprintf(debug, ")");
  599. }
  600. }
  601. void printLinkValue(LinkValue *value, char *first, char *last, FILE *debug){
  602. if (value == NULL)
  603. return;
  604. fprintf(debug, "%s", first);
  605. if (value->belong != NULL) {
  606. printLinkValue(value->belong, "", "", debug);
  607. fprintf(debug, " . ", NULL);
  608. }
  609. if (value->value != NULL)
  610. printValue(value->value, debug, true, true);
  611. fprintf(debug, "%s", last);
  612. }