sys.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #include "__ofunc.h"
  2. ResultType vm_super(O_FUNC){
  3. Value *arg_father = NULL;
  4. Value *arg_child = NULL;
  5. LinkValue *next_father = NULL;
  6. ArgumentParser ap[] = {{.type=name_value, .name=L"class_", .must=1, .long_arg=false},
  7. {.type=name_value, .name=L"obj_", .must=1, .long_arg=false},
  8. {.must=-1}};
  9. setResultCore(result);
  10. parserArgumentUnion(ap, arg, CNEXT_NT);
  11. if (!CHECK_RESULT(result))
  12. return result->type;
  13. freeResult(result);
  14. arg_father = ap[0].value->value;
  15. arg_child = ap[1].value->value;
  16. if (arg_child == arg_father) {
  17. if (arg_child->object.inherit != NULL){
  18. result->value = copyLinkValue(arg_child->object.inherit->value, inter);
  19. result->type = R_opt;
  20. gc_addTmpLink(&result->value->gc_status);
  21. } else
  22. setResultError(E_SuperException, L"Object has no next father", LINEFILE, true, CNEXT_NT);
  23. return result->type;
  24. }
  25. for (Inherit *self_father = arg_child->object.inherit; self_father != NULL; self_father = self_father->next) {
  26. if (self_father->value->value == arg_father) {
  27. if (self_father->next != NULL)
  28. next_father = copyLinkValue(self_father->next->value, inter);
  29. break;
  30. }
  31. }
  32. if (next_father != NULL){
  33. result->value = next_father;
  34. result->type = R_opt;
  35. gc_addTmpLink(&result->value->gc_status);
  36. }
  37. else
  38. setResultError(E_SuperException, L"Object has no next father", LINEFILE, true, CNEXT_NT);
  39. return result->type;
  40. }
  41. ResultType vm_setNowRunCore(O_FUNC, bool type){
  42. LinkValue *function_value = NULL;
  43. ArgumentParser ap[] = {{.type=name_value, .name=L"func", .must=1, .long_arg=false}, {.must=-1}};
  44. setResultCore(result);
  45. {
  46. parserArgumentUnion(ap, arg, CNEXT_NT);
  47. if (!CHECK_RESULT(result))
  48. return result->type;
  49. freeResult(result);
  50. }
  51. function_value = ap[0].value;
  52. function_value->value->data.function.function_data.run = type;
  53. result->value = function_value;
  54. gc_addTmpLink(&result->value->gc_status);
  55. result->type = R_opt;
  56. return R_opt;
  57. }
  58. ResultType vm_setMethodCore(O_FUNC, enum FunctionPtType type){
  59. LinkValue *function_value = NULL;
  60. ArgumentParser ap[] = {{.type=name_value, .name=L"func", .must=1, .long_arg=false}, {.must=-1}};
  61. setResultCore(result);
  62. {
  63. parserArgumentUnion(ap, arg, CNEXT_NT);
  64. if (!CHECK_RESULT(result))
  65. return result->type;
  66. freeResult(result);
  67. }
  68. function_value = ap[0].value;
  69. function_value->value->data.function.function_data.pt_type = type;
  70. result->value = function_value;
  71. gc_addTmpLink(&result->value->gc_status);
  72. result->type = R_opt;
  73. return R_opt;
  74. }
  75. ResultType vm_clsfreemethod(O_FUNC){
  76. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), cls_free_);
  77. }
  78. ResultType vm_clsmethod(O_FUNC){
  79. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), cls_static_);
  80. }
  81. ResultType vm_freemethod(O_FUNC){
  82. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), free_);
  83. }
  84. ResultType vm_staticmethod(O_FUNC){
  85. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), static_);
  86. }
  87. ResultType vm_classmethod(O_FUNC){
  88. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), class_static_);
  89. }
  90. ResultType vm_objectmethod(O_FUNC){
  91. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), object_static_);
  92. }
  93. ResultType vm_classfreemethod(O_FUNC){
  94. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), class_free_);
  95. }
  96. ResultType vm_objectfreemethod(O_FUNC){
  97. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), object_free_);
  98. }
  99. ResultType vm_allfreemethod(O_FUNC){
  100. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), all_free_);
  101. }
  102. ResultType vm_allstaticmethod(O_FUNC){
  103. return vm_setMethodCore(CO_FUNC(arg, var_list, result, belong), all_static_);
  104. }
  105. ResultType vm_isnowrun(O_FUNC){
  106. return vm_setNowRunCore(CO_FUNC(arg, var_list, result, belong), true);
  107. }
  108. ResultType vm_disnowrun(O_FUNC){
  109. return vm_setNowRunCore(CO_FUNC(arg, var_list, result, belong), false);
  110. }
  111. ResultType vm_quit(O_FUNC){
  112. if (arg != NULL)
  113. setResultError(E_ArgumentException, MANY_ARG, LINEFILE, true, CNEXT_NT);
  114. else
  115. setResultError(E_QuitException, L"VirtualMath Quit", LINEFILE, true, CNEXT_NT);
  116. return R_error;
  117. }
  118. ResultType vm_open(O_FUNC){
  119. return callBackCore(inter->data.base_obj[B_FILE], arg, LINEFILE, 0, CNEXT_NT);
  120. }
  121. ResultType vm_exec(O_FUNC){
  122. ArgumentParser ap[] = {{.type=name_value, .name=L"cm", .must=1, .long_arg=false},
  123. {.type=name_value, .name=L"var", .must=0, .long_arg=false},
  124. {.type=name_value, .name=L"out", .must=0, .long_arg=false},
  125. {.must=-1}};
  126. LinkValue *str;
  127. LinkValue *var;
  128. LinkValue *out;
  129. bool out_;
  130. Statement *new_st;
  131. VarList *run;
  132. setResultCore(result);
  133. parserArgumentUnion(ap, arg, CNEXT_NT);
  134. if (!CHECK_RESULT(result))
  135. return result->type;
  136. freeResult(result);
  137. str = ap[0].value;
  138. var = ap[1].value;
  139. out = ap[2].value;
  140. if (str->value->type != V_str) {
  141. setResultError(E_TypeException, ONLY_ACC(cm, str), LINEFILE, true, CNEXT_NT);
  142. return result->type;
  143. }
  144. if (var != NULL && var->value->type != V_dict) {
  145. setResultError(E_TypeException, ONLY_ACC(var, dict), LINEFILE, true, CNEXT_NT);
  146. return R_error;
  147. }
  148. if (out != NULL) {
  149. if (out->value->type != V_bool) {
  150. setResultError(E_TypeException, ONLY_ACC(out, bool), LINEFILE, true, CNEXT_NT);
  151. return R_error;
  152. } else if (var == NULL) {
  153. setResultError(E_TypeException, L"missing parameters: var", LINEFILE, true, CNEXT_NT);
  154. return R_error;
  155. }
  156. out_ = out->value->data.bool_.bool_;
  157. } else
  158. out = false;
  159. {
  160. ParserMessage *pm = makeParserMessageStr(str->value->data.str.str, false);
  161. new_st = makeStatement(0, "exec");
  162. parserCommandList(pm, inter, true, new_st);
  163. if (pm->status == int_error) {
  164. setResultError(E_KeyInterrupt, KEY_INTERRUPT, LINEFILE, true, CNEXT_NT);
  165. return R_error;
  166. }
  167. else if (pm->status != success) {
  168. wchar_t *wcs_message = memStrToWcs(pm->status_message, false);
  169. setResultError(E_TypeException, wcs_message, LINEFILE, true, CNEXT_NT);
  170. memFree(wcs_message);
  171. return R_error;
  172. }
  173. freeParserMessage(pm, true);
  174. }
  175. if (var != NULL) {
  176. run = makeVarList(inter, false);
  177. run->hashtable = var->value->data.dict.dict;
  178. if (out)
  179. run->next = var_list;
  180. else
  181. gc_freeze(inter, var_list, run, true);
  182. } else
  183. run = var_list;
  184. includeSafeInterStatement(CFUNC(new_st, run, result, belong));
  185. freeStatement(new_st);
  186. if (var != NULL) {
  187. if (!out)
  188. gc_freeze(inter, var_list, run, false);
  189. freeVarList(run);
  190. }
  191. return result->type;
  192. }
  193. void registeredSysFunction(R_FUNC){
  194. NameFunc tmp[] = {{L"super", vm_super, free_},
  195. {L"freemethod", vm_freemethod, free_},
  196. {L"staticmethod", vm_staticmethod, free_},
  197. {L"staticclassmethod", vm_classmethod, free_},
  198. {L"staticobjectmethod", vm_objectmethod, free_},
  199. {L"classmethod", vm_classfreemethod, free_},
  200. {L"objectmethod", vm_objectfreemethod, free_},
  201. {L"simplemethod", vm_allfreemethod, free_},
  202. {L"simplestaticmethod", vm_allstaticmethod, free_},
  203. {L"clsmethod", vm_clsfreemethod, free_},
  204. {L"clsstaticmethod", vm_clsmethod, free_},
  205. {L"isnowrun", vm_isnowrun, free_},
  206. {L"disnowrun", vm_disnowrun, free_},
  207. {L"quit", vm_quit, free_},
  208. {L"exec", vm_exec, free_},
  209. {L"open", vm_open, free_},
  210. {NULL, NULL}};
  211. iterBaseNameFunc(tmp, belong, CFUNC_CORE(var_list));
  212. }