cfunc.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  1. #include<stdio.h>
  2. // #include "interpreter.h"
  3. int len_only_double(double num);
  4. int len_double(double num);
  5. int len_int(int num);
  6. int len_intx(unsigned int num);
  7. GWARF_value to_object(GWARF_value, var_list *);
  8. void login_official_func(int type, int is_class, var_list *the_var, char *name, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *));
  9. void login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *));
  10. // 内置函数
  11. GWARF_result official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *);
  12. // object内置类
  13. class_object *object_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *));
  14. GWARF_result object_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var);
  15. // gobject内置类
  16. class_object *gobject_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list);
  17. GWARF_result gobject_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var);
  18. // int内置类
  19. class_object *int_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list);
  20. GWARF_result int_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *);
  21. // double内置类
  22. class_object *double_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list);
  23. GWARF_result double_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *);
  24. // str内置类
  25. class_object *str_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list);
  26. GWARF_result str_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var);
  27. // bool内置类
  28. class_object *bool_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list);
  29. GWARF_result bool_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var);
  30. int len_int(int num){
  31. int count = 1; // 默认得有1位
  32. while(1){
  33. num = num / 10;
  34. if(num <= 0){
  35. break;
  36. }
  37. count += 1;
  38. }
  39. return count;
  40. }
  41. int len_only_double(double num){
  42. int count = 1; // 默认得有1位
  43. while(1){
  44. num = num * 10;
  45. if(num - (int)num <= 0){
  46. break;
  47. }
  48. count += 1;
  49. }
  50. return count;
  51. }
  52. int len_double(double num){
  53. int count = 1, i = (int)num;
  54. count += len_int(i);
  55. count += len_only_double(num);
  56. return count;
  57. }
  58. int len_intx(unsigned int num){ // 16进制
  59. int count = 1; // 默认得有1位
  60. while(1){
  61. num = num / 16;
  62. if(num <= 0){
  63. break;
  64. }
  65. count += 1;
  66. }
  67. return count;
  68. }
  69. GWARF_value to_object(GWARF_value value, var_list *the_var){ // 把GWARF_value封装成objct
  70. GWARF_result return_value;
  71. if((value.type == CLASS_value) || (value.type == OBJECT_value) || (value.type == FUNC_value) || (value.type == NULL_value)){ // 可以直接返回
  72. return value;
  73. }
  74. GWARF_result func_result;
  75. if(value.type == NUMBER_value){
  76. func_result.value = find_var(the_var, 0, "double")->value;
  77. }
  78. else if(value.type == INT_value){
  79. func_result.value = find_var(the_var, 0, "int")->value;
  80. }
  81. else if(value.type == BOOL_value){
  82. func_result.value = find_var(the_var, 0, "bool")->value;
  83. }
  84. else if(value.type == STRING_value){
  85. func_result.value = find_var(the_var, 0, "str")->value;
  86. }
  87. else{
  88. return value;
  89. }
  90. return call_back_core(func_result, the_var, pack_value_parameter(value)).value;
  91. }
  92. void login_official_func(int type, int is_class, var_list *the_var, char *name, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *)){ // 注册单个official func
  93. GWARF_result func_value;
  94. func *func_tmp = malloc(sizeof(func));
  95. func_tmp->done = NULL;
  96. func_tmp->parameter_list = NULL;
  97. func_tmp->the_var = copy_var_list(the_var);
  98. func_tmp->type = official;
  99. func_tmp->official_func = type;
  100. func_tmp->is_class = is_class;
  101. func_tmp->paser = paser;
  102. func_value.value.type = FUNC_value;
  103. func_value.value.value.func_value = func_tmp;
  104. assigment_func(name, func_value, the_var, 0); // 注册函数到指定的位置
  105. }
  106. void login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *)){
  107. // {{official_func_type, is_class}}
  108. int a[][2] = {{1,0}};
  109. // {login_name}
  110. char *name[] = {"print"};
  111. int lenth = sizeof(a)/sizeof(a[0]);
  112. for(int i = 0;i < lenth;i+=1){
  113. login_official_func(a[i][0], a[i][1], the_var, name[i], paser);
  114. }
  115. }
  116. // global 全局内置函数解析器
  117. GWARF_result official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var){
  118. GWARF_result return_value;
  119. return_value.u = return_def;
  120. return_value.return_times = 0;
  121. switch (the_func->official_func)
  122. {
  123. case printf_func:{ // printf something
  124. if(tmp_s == NULL){ // 没有东西要打印
  125. goto return_result;
  126. }
  127. while(1){
  128. GWARF_result tmp = traverse(tmp_s->u.value, out_var, false);
  129. if((tmp.value.type == INT_value)){
  130. printf("%d", tmp.value.value.int_value);
  131. }
  132. else if(tmp.value.type == BOOL_value){
  133. if(tmp.value.value.bool_value){
  134. printf("true");
  135. }
  136. else{
  137. printf("false");
  138. }
  139. }
  140. else if(tmp.value.type == NUMBER_value){
  141. printf("%f", tmp.value.value.double_value);
  142. }
  143. else if(tmp.value.type == NULL_value){
  144. printf("<-None->");
  145. }
  146. else if(tmp.value.type == STRING_value){
  147. printf("'%s'", tmp.value.value.string);
  148. }
  149. else if(tmp.value.type == FUNC_value){
  150. printf("<-function on %u->", tmp.value.value.func_value);
  151. }
  152. else if(tmp.value.type == CLASS_value){
  153. printf("<-class on %u->", tmp.value.value.class_value);
  154. }
  155. else if(tmp.value.type == OBJECT_value){
  156. printf("<-object on %u->", tmp.value.value.object_value);
  157. }
  158. else{
  159. printf("var value = other\n");
  160. }
  161. if (tmp_s->next == NULL){ // the last
  162. break;
  163. }
  164. tmp_s = tmp_s->next;
  165. }
  166. printf("\n"); // 换行
  167. return_value.u = statement_end;
  168. break;
  169. }
  170. default:
  171. break;
  172. }
  173. return_result: return return_value;
  174. }
  175. class_object *object_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *)){ // 内置对象继承的类
  176. // 创建对象[空对象]
  177. puts("----set class----");
  178. GWARF_result class_value;
  179. class_object *class_tmp = malloc(sizeof(class_object));
  180. class_tmp->the_var = make_var_base(make_var()); // make class var list
  181. class_tmp->out_var = append_by_var_list(class_tmp->the_var, copy_var_list(the_var)); // make class var list with out var
  182. class_value.value.type = CLASS_value;
  183. class_value.value.value.class_value = class_tmp;
  184. assigment_func("object", class_value, the_var, 0); // 注册class 的 位置
  185. puts("----stop set class----");
  186. // 注册函数
  187. int a[][2] = {{3,1}};
  188. char *name[] = {"__value__"};
  189. int lenth = sizeof(a)/sizeof(a[0]);
  190. for(int i = 0;i < lenth;i+=1){
  191. login_official_func(a[i][0], a[i][1], class_tmp->the_var, name[i], paser);
  192. }
  193. return class_tmp;
  194. }
  195. GWARF_result object_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var){ // out_var是外部环境
  196. GWARF_result return_value;
  197. var_list *login_var;
  198. return_value.u = return_def;
  199. return_value.return_times = 0;
  200. if(father.father->type == CLASS_value){ // is class so that can use "."
  201. login_var = father.father->value.class_value->the_var;
  202. }
  203. else if(father.father->type == OBJECT_value){
  204. login_var = father.father->value.object_value->the_var;
  205. }
  206. switch (the_func->official_func)
  207. {
  208. case __value__func:{ // 若想实现运算必须要有这个方法
  209. return_value.value.type = STRING_value; // 取得用于计算的数值
  210. return_value.value = to_str(*(father.father), out_var);
  211. break;
  212. }
  213. }
  214. return_result: return return_value;
  215. }
  216. class_object *gobject_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list){ // 内置对象继承的类
  217. // 创建对象[空对象]
  218. puts("----set class----");
  219. GWARF_result class_value;
  220. class_object *class_tmp = malloc(sizeof(class_object));
  221. class_tmp->the_var = make_var_base(make_var()); // make class var list
  222. if(father_var_list != NULL){
  223. append_by_var_list(class_tmp->the_var, father_var_list); // 一切类都需要继承object类[包括set class如果tmp_s == NULL则需要继承object]
  224. }
  225. class_tmp->out_var = append_by_var_list(class_tmp->the_var, copy_var_list(the_var)); // make class var list with out var
  226. class_value.value.type = CLASS_value;
  227. class_value.value.value.class_value = class_tmp;
  228. assigment_func("gobject", class_value, the_var, 0); // 注册class 的 位置
  229. puts("----stop set class----");
  230. // 注册函数
  231. int a[][2] = {{2,1}, {3,1}, {4,1}, {5,1}, {6,1}, {7,1}, {8,1}, {9,1}, {10,1}, {11,1}, {12,1}, {13,1}, {14,1}, {15,1}, {16,1}, {17,1}, {3,1}};
  232. char *name[] = {"__init__", "__value__", "__add__", "__sub__", "__mul__","__div__","__eq__", "__more__", "__less__", "__eqmore__", "__eqless__","__noteq__", "__pow__", "__log__","__sqrt__","__negative__","__bool__"};
  233. int lenth = sizeof(a)/sizeof(a[0]);
  234. for(int i = 0;i < lenth;i+=1){
  235. login_official_func(a[i][0], a[i][1], class_tmp->the_var, name[i], paser);
  236. }
  237. return class_tmp;
  238. }
  239. GWARF_result gobject_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var){ // out_var是外部环境
  240. GWARF_result return_value;
  241. var_list *login_var;
  242. return_value.u = return_def;
  243. return_value.return_times = 0;
  244. if(father.father->type == CLASS_value){ // is class so that can use "."
  245. login_var = father.father->value.class_value->the_var;
  246. }
  247. else if(father.father->type == OBJECT_value){
  248. login_var = father.father->value.object_value->the_var;
  249. }
  250. else{
  251. printf("NO login, father type = %d\n", father.father->type);
  252. }
  253. switch (the_func->official_func)
  254. {
  255. case __init__func:{ // printf something
  256. GWARF_result tmp;
  257. tmp.value.type = INT_value;
  258. tmp.value.value.int_value = 0;
  259. assigment_func("value", tmp, login_var, 0); // 注册到self
  260. return_value.u = statement_end; // __init__没有return
  261. break;
  262. }
  263. case __value__func:{ // 若想实现运算必须要有这个方法
  264. var *tmp = find_var(login_var, 0, "value"); // gobject类的value存储在self.value中
  265. return_value.value = tmp->value; // 取得用于计算的数值
  266. break;
  267. }
  268. case __add__func:{
  269. GWARF_result reight_tmp, left_tmp;
  270. GWARF_value base_the_var = traverse(tmp_s->u.value, out_var, false).value; // 只有一个参数
  271. reight_tmp = get__value__(&base_the_var, the_var);
  272. left_tmp.value = find_var(login_var, 0, "value")->value;
  273. return_value = add_func(left_tmp, reight_tmp, out_var);
  274. break;
  275. }
  276. case __sub__func:{
  277. GWARF_result reight_tmp, left_tmp;
  278. GWARF_value base_the_var = traverse(tmp_s->u.value, out_var, false).value; // 只有一个参数
  279. reight_tmp = get__value__(&base_the_var, the_var);
  280. left_tmp.value = find_var(login_var, 0, "value")->value;
  281. return_value = sub_func(left_tmp, reight_tmp, out_var);
  282. break;
  283. }
  284. case __mul__func:{
  285. GWARF_result reight_tmp, left_tmp;
  286. GWARF_value base_the_var = traverse(tmp_s->u.value, out_var, false).value; // 只有一个参数
  287. reight_tmp = get__value__(&base_the_var, the_var);
  288. left_tmp.value = find_var(login_var, 0, "value")->value;
  289. return_value = mul_func(left_tmp, reight_tmp, out_var);
  290. break;
  291. }
  292. case __div__func:{
  293. GWARF_result reight_tmp, left_tmp;
  294. GWARF_value base_the_var = traverse(tmp_s->u.value, out_var, false).value; // 只有一个参数
  295. reight_tmp = get__value__(&base_the_var, the_var);
  296. left_tmp.value = find_var(login_var, 0, "value")->value;
  297. return_value = div_func(left_tmp, reight_tmp, out_var);
  298. break;
  299. }
  300. case __eq__func:{
  301. GWARF_result reight_tmp, left_tmp;
  302. GWARF_value base_the_var = traverse(tmp_s->u.value, out_var, false).value; // 只有一个参数
  303. reight_tmp = get__value__(&base_the_var, the_var);
  304. left_tmp.value = find_var(login_var, 0, "value")->value;
  305. return_value = equal_func(left_tmp, reight_tmp, out_var, 0);
  306. break;
  307. }
  308. case __more__func:{
  309. GWARF_result reight_tmp, left_tmp;
  310. GWARF_value base_the_var = traverse(tmp_s->u.value, out_var, false).value; // 只有一个参数
  311. reight_tmp = get__value__(&base_the_var, the_var);
  312. left_tmp.value = find_var(login_var, 0, "value")->value;
  313. return_value = equal_func(left_tmp, reight_tmp, out_var, 1);
  314. break;
  315. }
  316. case __less__func:{
  317. GWARF_result reight_tmp, left_tmp;
  318. GWARF_value base_the_var = traverse(tmp_s->u.value, out_var, false).value; // 只有一个参数
  319. reight_tmp = get__value__(&base_the_var, the_var);
  320. left_tmp.value = find_var(login_var, 0, "value")->value;
  321. return_value = equal_func(left_tmp, reight_tmp, out_var, 2);
  322. break;
  323. }
  324. case __eqmore__func:{
  325. GWARF_result reight_tmp, left_tmp;
  326. GWARF_value base_the_var = traverse(tmp_s->u.value, out_var, false).value; // 只有一个参数
  327. reight_tmp = get__value__(&base_the_var, the_var);
  328. left_tmp.value = find_var(login_var, 0, "value")->value;
  329. return_value = equal_func(left_tmp, reight_tmp, out_var, 3);
  330. break;
  331. }
  332. case __eqless__func:{
  333. GWARF_result reight_tmp, left_tmp;
  334. GWARF_value base_the_var = traverse(tmp_s->u.value, out_var, false).value; // 只有一个参数
  335. reight_tmp = get__value__(&base_the_var, the_var);
  336. left_tmp.value = find_var(login_var, 0, "value")->value;
  337. return_value = equal_func(left_tmp, reight_tmp, out_var, 4);
  338. break;
  339. }
  340. case __noteq__func:{
  341. GWARF_result reight_tmp, left_tmp;
  342. GWARF_value base_the_var = traverse(tmp_s->u.value, out_var, false).value; // 只有一个参数
  343. reight_tmp = get__value__(&base_the_var, the_var);
  344. left_tmp.value = find_var(login_var, 0, "value")->value;
  345. return_value = equal_func(left_tmp, reight_tmp, out_var, 5);
  346. break;
  347. }
  348. case __pow__func:{
  349. GWARF_result reight_tmp, left_tmp;
  350. GWARF_value base_the_var = traverse(tmp_s->u.value, out_var, false).value; // 只有一个参数
  351. reight_tmp = get__value__(&base_the_var, the_var);
  352. left_tmp.value = find_var(login_var, 0, "value")->value;
  353. return_value = pow_func(left_tmp, reight_tmp, out_var);
  354. break;
  355. }
  356. case __log__func:{
  357. GWARF_result reight_tmp, left_tmp;
  358. GWARF_value base_the_var = traverse(tmp_s->u.value, out_var, false).value; // 只有一个参数
  359. reight_tmp = get__value__(&base_the_var, the_var);
  360. left_tmp.value = find_var(login_var, 0, "value")->value;
  361. return_value = log_func(left_tmp, reight_tmp, out_var);
  362. break;
  363. }
  364. case __sqrt__func:{
  365. GWARF_result reight_tmp, left_tmp;
  366. GWARF_value base_the_var = traverse(tmp_s->u.value, out_var, false).value; // 只有一个参数
  367. reight_tmp = get__value__(&base_the_var, the_var);
  368. left_tmp.value = find_var(login_var, 0, "value")->value;
  369. return_value = sqrt_func(left_tmp, reight_tmp, out_var);
  370. break;
  371. }
  372. case __negative__func:{
  373. GWARF_result left_tmp;
  374. left_tmp.value = find_var(login_var, 0, "value")->value;
  375. return_value = negative_func(left_tmp, out_var);
  376. break;
  377. }
  378. default:
  379. break;
  380. }
  381. return_result: return return_value;
  382. }
  383. class_object *int_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list){
  384. // 创建对象[空对象]
  385. puts("----set class----");
  386. GWARF_result class_value;
  387. class_object *class_tmp = malloc(sizeof(class_object));
  388. class_tmp->the_var = make_var_base(make_var()); // make class var list
  389. if(father_var_list != NULL){
  390. append_by_var_list(class_tmp->the_var, father_var_list); // int、double、str等内置类需要继承gobject类
  391. }
  392. class_tmp->out_var = append_by_var_list(class_tmp->the_var, copy_var_list(the_var)); // make class var list with out var
  393. class_value.value.type = CLASS_value;
  394. class_value.value.value.class_value = class_tmp;
  395. assigment_func("int", class_value, the_var, 0); // 注册class 的 位置
  396. puts("----stop set class----");
  397. // 注册函数
  398. int a[][2] = {{2,1}};
  399. char *name[] = {"__init__"};
  400. int lenth = sizeof(a)/sizeof(a[0]);
  401. for(int i = 0;i < lenth;i+=1){
  402. login_official_func(a[i][0], a[i][1], class_tmp->the_var, name[i], paser);
  403. }
  404. return class_tmp;
  405. }
  406. GWARF_result int_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var){ // out_var是外部环境
  407. GWARF_result return_value;
  408. var_list *login_var;
  409. return_value.u = return_def;
  410. return_value.return_times = 0;
  411. if(father.father->type == CLASS_value){ // is class so that can use "."
  412. login_var = father.father->value.class_value->the_var;
  413. }
  414. else if(father.father->type == OBJECT_value){
  415. login_var = father.father->value.object_value->the_var;
  416. }
  417. else{
  418. printf("NO login, father type = %d\n", father.father->type);
  419. }
  420. switch (the_func->official_func)
  421. {
  422. case __init__func:{ // printf something
  423. GWARF_result tmp;
  424. tmp.value = to_int(traverse(tmp_s->u.value, out_var, false).value, out_var); // 只有一个参数[要针对不同数据类型对此处作出处理]
  425. assigment_func("value", tmp, login_var, 0); // 注册到self
  426. return_value.u = statement_end; // __init__没有return
  427. break;
  428. }
  429. default:
  430. break;
  431. }
  432. return_result: return return_value;
  433. }
  434. // to int[底层实现]
  435. GWARF_value to_int(GWARF_value value, var_list *the_var){
  436. if((value.type == INT_value)){
  437. return value; // 直接返回数据
  438. }
  439. GWARF_value return_number;
  440. return_number.type = INT_value;
  441. if(value.type == OBJECT_value){ // 调用__value__方法
  442. return_number = to_int(get__value__(&value, the_var).value, the_var); // 递归
  443. }
  444. else{
  445. if(value.type == BOOL_value){
  446. return_number.value.int_value = value.value.bool_value;
  447. }
  448. else if(value.type == NUMBER_value){
  449. return_number.value.int_value = (int)value.value.double_value;
  450. }
  451. else if(value.type == STRING_value){
  452. return_number.value.int_value = atoi(value.value.string);
  453. }
  454. else{
  455. return_number.value.int_value = 0;
  456. }
  457. }
  458. return return_number;
  459. }
  460. class_object *double_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list){
  461. // 创建对象[空对象]
  462. puts("----set class----");
  463. GWARF_result class_value;
  464. class_object *class_tmp = malloc(sizeof(class_object));
  465. class_tmp->the_var = make_var_base(make_var()); // make class var list
  466. if(father_var_list != NULL){
  467. append_by_var_list(class_tmp->the_var, father_var_list); // 一切类都需要继承object类[包括set class如果tmp_s == NULL则需要继承object]
  468. }
  469. class_tmp->out_var = append_by_var_list(class_tmp->the_var, copy_var_list(the_var)); // make class var list with out var
  470. class_value.value.type = CLASS_value;
  471. class_value.value.value.class_value = class_tmp;
  472. assigment_func("double", class_value, the_var, 0); // 注册class 的 位置
  473. puts("----stop set class----");
  474. // 注册函数
  475. int a[][2] = {{2,1}};
  476. char *name[] = {"__init__"};
  477. int lenth = sizeof(a)/sizeof(a[0]);
  478. for(int i = 0;i < lenth;i+=1){
  479. login_official_func(a[i][0], a[i][1], class_tmp->the_var, name[i], paser);
  480. }
  481. return class_tmp;
  482. }
  483. GWARF_result double_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var){ // out_var是外部环境
  484. GWARF_result return_value;
  485. var_list *login_var;
  486. return_value.u = return_def;
  487. return_value.return_times = 0;
  488. if(father.father->type == CLASS_value){ // is class so that can use "."
  489. login_var = father.father->value.class_value->the_var;
  490. }
  491. else if(father.father->type == OBJECT_value){
  492. login_var = father.father->value.object_value->the_var;
  493. }
  494. else{
  495. printf("NO login, father type = %d\n", father.father->type);
  496. }
  497. switch (the_func->official_func)
  498. {
  499. case __init__func:{ // printf something
  500. GWARF_result tmp;
  501. tmp.value = to_double(traverse(tmp_s->u.value, out_var, false).value, out_var); // 只有一个参数[要针对不同数据类型对此处作出处理]
  502. assigment_func("value", tmp, login_var, 0); // 注册到self
  503. return_value.u = statement_end; // __init__没有return
  504. break;
  505. }
  506. default:
  507. break;
  508. }
  509. return_result: return return_value;
  510. }
  511. // to double[底层实现]
  512. GWARF_value to_double(GWARF_value value, var_list *the_var){
  513. if((value.type == NUMBER_value)){
  514. return value; // 直接返回数据
  515. }
  516. GWARF_value return_number;
  517. return_number.type = NUMBER_value;
  518. if(value.type == OBJECT_value){ // 调用__value__方法
  519. return_number = to_double(get__value__(&value, the_var).value, the_var); // 递归
  520. }
  521. else{
  522. if(value.type == BOOL_value){
  523. return_number.value.double_value = (double)value.value.bool_value;
  524. }
  525. else if(value.type == INT_value){
  526. return_number.value.double_value = (double)value.value.int_value;
  527. }
  528. else if(value.type == STRING_value){
  529. return_number.value.double_value = (double)atof(value.value.string);
  530. }
  531. else{
  532. return_number.value.double_value = 0;
  533. }
  534. }
  535. return return_number;
  536. }
  537. class_object *str_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list){
  538. // 创建对象[空对象]
  539. puts("----set class----");
  540. GWARF_result class_value;
  541. class_object *class_tmp = malloc(sizeof(class_object));
  542. class_tmp->the_var = make_var_base(make_var()); // make class var list
  543. if(father_var_list != NULL){
  544. append_by_var_list(class_tmp->the_var, father_var_list); // 一切类都需要继承object类[包括set class如果tmp_s == NULL则需要继承object]
  545. }
  546. class_tmp->out_var = append_by_var_list(class_tmp->the_var, copy_var_list(the_var)); // make class var list with out var
  547. class_value.value.type = CLASS_value;
  548. class_value.value.value.class_value = class_tmp;
  549. assigment_func("str", class_value, the_var, 0); // 注册class 的 位置
  550. puts("----stop set class----");
  551. // 注册函数
  552. int a[][2] = {{2,1}};
  553. char *name[] = {"__init__"};
  554. int lenth = sizeof(a)/sizeof(a[0]);
  555. for(int i = 0;i < lenth;i+=1){
  556. login_official_func(a[i][0], a[i][1], class_tmp->the_var, name[i], paser);
  557. }
  558. return class_tmp;
  559. }
  560. GWARF_result str_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var){ // out_var是外部环境
  561. GWARF_result return_value;
  562. var_list *login_var;
  563. return_value.u = return_def;
  564. return_value.return_times = 0;
  565. if(father.father->type == CLASS_value){ // is class so that can use "."
  566. login_var = father.father->value.class_value->the_var;
  567. }
  568. else if(father.father->type == OBJECT_value){
  569. login_var = father.father->value.object_value->the_var;
  570. }
  571. else{
  572. printf("NO login, father type = %d\n", father.father->type);
  573. }
  574. switch (the_func->official_func)
  575. {
  576. case __init__func:{ // printf something
  577. GWARF_result tmp;
  578. tmp.value = to_str(traverse(tmp_s->u.value, out_var, false).value, out_var); // 只有一个参数[要针对不同数据类型对此处作出处理]
  579. assigment_func("value", tmp, login_var, 0); // 注册到self
  580. return_value.u = statement_end; // __init__没有return
  581. break;
  582. }
  583. default:
  584. break;
  585. }
  586. return_result: return return_value;
  587. }
  588. // to str[底层实现]
  589. GWARF_value to_str(GWARF_value value, var_list *the_var){
  590. if((value.type == STRING_value)){
  591. return value; // 直接返回数据
  592. }
  593. GWARF_value return_number;
  594. return_number.type = STRING_value;
  595. if(value.type == OBJECT_value){ // 调用__value__方法
  596. return_number = to_str(get__value__(&value, the_var).value, the_var); // 递归
  597. }
  598. else{
  599. if(value.type == BOOL_value){
  600. if(value.value.bool_value){
  601. return_number.value.string = "true";
  602. }
  603. else{
  604. return_number.value.string = "false";
  605. }
  606. }
  607. else if(value.type == INT_value){
  608. size_t size = (size_t)(2 + len_int(value.value.int_value));
  609. return_number.value.string = (char *)malloc(size);
  610. snprintf(return_number.value.string, size, "%d", value.value.int_value);
  611. }
  612. else if(value.type == NUMBER_value){
  613. size_t size = (size_t)(2 + len_double(value.value.double_value));
  614. return_number.value.string = (char *)malloc(size);
  615. snprintf(return_number.value.string, size, "%f", value.value.double_value);
  616. }
  617. else if(value.type == NULL_value){
  618. return_number.value.string = "<-None->";
  619. }
  620. else if(value.type == FUNC_value){
  621. size_t size = (size_t)(20 + len_intx((unsigned int)value.value.func_value)); // 转换为无符号整形数字
  622. return_number.value.string = (char *)malloc(size);
  623. snprintf(return_number.value.string, size, "<-function on %u->", value.value.func_value);
  624. }
  625. else if(value.type == CLASS_value){
  626. size_t size = (size_t)(16 + len_intx((unsigned int)value.value.class_value));
  627. return_number.value.string = (char *)malloc(size);
  628. snprintf(return_number.value.string, size, "<-class on %u->", value.value.class_value);
  629. }
  630. else{
  631. printf("var value = other\n");
  632. }
  633. }
  634. return return_number;
  635. }
  636. class_object *bool_login_official(var_list *the_var, GWARF_result (*paser)(func *, parameter *, var_list *, GWARF_result, var_list *), var_list *father_var_list){
  637. // 创建对象[空对象]
  638. puts("----set class----");
  639. GWARF_result class_value;
  640. class_object *class_tmp = malloc(sizeof(class_object));
  641. class_tmp->the_var = make_var_base(make_var()); // make class var list
  642. if(father_var_list != NULL){
  643. append_by_var_list(class_tmp->the_var, father_var_list); // 一切类都需要继承object类[包括set class如果tmp_s == NULL则需要继承object]
  644. }
  645. class_tmp->out_var = append_by_var_list(class_tmp->the_var, copy_var_list(the_var)); // make class var list with out var
  646. class_value.value.type = CLASS_value;
  647. class_value.value.value.class_value = class_tmp;
  648. assigment_func("bool", class_value, the_var, 0); // 注册class 的 位置
  649. puts("----stop set class----");
  650. // 注册函数
  651. int a[][2] = {{2,1}};
  652. char *name[] = {"__init__"};
  653. int lenth = sizeof(a)/sizeof(a[0]);
  654. for(int i = 0;i < lenth;i+=1){
  655. login_official_func(a[i][0], a[i][1], class_tmp->the_var, name[i], paser);
  656. }
  657. return class_tmp;
  658. }
  659. GWARF_result bool_official_func(func *the_func, parameter *tmp_s, var_list *the_var, GWARF_result father, var_list *out_var){ // out_var是外部环境, the_var是self内部环境
  660. GWARF_result return_value;
  661. var_list *login_var;
  662. return_value.u = return_def;
  663. return_value.return_times = 0;
  664. if(father.father->type == CLASS_value){ // is class so that can use "."
  665. login_var = father.father->value.class_value->the_var;
  666. }
  667. else if(father.father->type == OBJECT_value){
  668. login_var = father.father->value.object_value->the_var;
  669. }
  670. else{
  671. printf("NO login, father type = %d\n", father.father->type);
  672. }
  673. switch (the_func->official_func)
  674. {
  675. case __init__func:{ // printf something
  676. GWARF_result tmp;
  677. tmp.value = to_bool_(traverse(tmp_s->u.value, out_var, false).value, out_var); // 只有一个参数[要针对不同数据类型对此处作出处理]
  678. assigment_func("value", tmp, login_var, 0); // 注册到self
  679. return_value.u = statement_end; // __init__没有return
  680. break;
  681. }
  682. default:
  683. break;
  684. }
  685. return_result: return return_value;
  686. }
  687. // to bool[底层实现]
  688. GWARF_value to_bool_(GWARF_value value, var_list *the_var){
  689. if((value.type == BOOL_value)){
  690. return value; // 直接返回数据
  691. }
  692. GWARF_value return_number;
  693. return_number.type = BOOL_value;
  694. if(value.type == OBJECT_value){ // 调用__value__方法
  695. return_number = to_bool_(get__value__(&value, the_var).value, the_var); // 递归
  696. }
  697. else{
  698. return_number.value.bool_value = to_bool(value); // 转换成bool
  699. }
  700. return return_number;
  701. }
  702. GWARF_result get__value__(GWARF_value *base_the_var, var_list *the_var){ // 用于计算的get__value__统一核心
  703. return run_func(base_the_var, the_var, "__value__");
  704. }
  705. GWARF_result get__bool__(GWARF_value *base_the_var, var_list *the_var){ // 用于计算的get__value__统一核心
  706. return run_func(base_the_var, the_var, "__bool__");
  707. }
  708. GWARF_result run_func(GWARF_value *base_the_var, var_list *the_var, char *name){ // 无参数func->直到返回GWARF_value[not class]
  709. GWARF_result reight_tmp, get;
  710. int times = 0;
  711. var_list *call_var;
  712. while(1){
  713. if(base_the_var->type == CLASS_value){ // is class so that can use "."
  714. call_var = base_the_var->value.class_value->the_var;
  715. }
  716. else if(base_the_var->type == OBJECT_value){
  717. call_var = base_the_var->value.object_value->the_var;
  718. }
  719. else{
  720. reight_tmp.u = return_def;
  721. reight_tmp.value = *base_the_var;
  722. reight_tmp.return_times = times;
  723. goto return_result; // 如果类型不是object或者class
  724. }
  725. get.value = find_var(call_var, 0, name)->value; // TODO:: 需要检查__value__是否存在
  726. get.father = base_the_var; // 设置father
  727. reight_tmp = call_back_core(get, the_var, NULL);
  728. times = reight_tmp.return_times;
  729. base_the_var = &(reight_tmp.value); // 重复获取__value__[直到类型不是object或class]
  730. }
  731. return_result: return reight_tmp;
  732. }