interpreter.c 114 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4. #include "interpreter.h"
  5. double log_(double base, double num){ // 自己定义一次log
  6. return log(num) / log(base);
  7. }
  8. double sqrt_(double base, double num){ // 定义根号sqrt
  9. return pow(base, (1 / num));
  10. }
  11. bool is_space(GWARF_result *tmp){ // 使用指针是不想复制数据
  12. if(tmp->u == cycle_break || tmp->u == code_broken || tmp->u == cycle_continue || tmp->u == code_continued ||
  13. tmp->u == cycle_restart || tmp->u == code_restarted || tmp->u == code_rego || tmp->u == code_rewent){
  14. return true;
  15. }
  16. if(tmp->u == return_def && tmp->return_times != 0){ // return def
  17. return true;
  18. }
  19. return false;
  20. }
  21. bool is_error(GWARF_result *tmp){ // 判断是否为error
  22. if(tmp->u == error){
  23. return true;
  24. }
  25. return false;
  26. }
  27. bool to_bool(GWARF_value value){
  28. double bool_double = 1; // if bool_double == 0则返回false其他返回true
  29. if(value.type == OBJECT_value){ // 调用左add方法
  30. bool_double = to_bool(get__bool__(&value, NULL).value); // 实际上不需要the_var参数链[没有实参]
  31. }
  32. else{
  33. if(value.type == INT_value || value.type == BOOL_value){
  34. bool_double = (double)value.value.int_value;
  35. }
  36. else if(value.type == NUMBER_value){
  37. bool_double = value.value.double_value;
  38. }
  39. else if(value.type == STRING_value){
  40. bool_double = (double)strlen(value.value.string);
  41. }
  42. else if(value.type == NULL_value){
  43. bool_double = 0;
  44. }
  45. }
  46. // 返回值
  47. if(bool_double){
  48. return true;
  49. }
  50. else{
  51. return false;
  52. }
  53. }
  54. GWARF_result read_statement(statement *the_statement, var_list *the_var, var_list *login_var){ // read the statement list with case to run by func
  55. if(login_var == NULL){
  56. login_var = the_var;
  57. }
  58. GWARF_result return_value;
  59. return_value.u = statement_end; // 正常设置[正常语句结束]
  60. return_value.value.type = NULL_value; // 默认设置
  61. return_value.value.value.int_value = 0; // 默认设置
  62. switch (the_statement->type)
  63. {
  64. case operation: // 表达式运算
  65. puts("----code----");
  66. return_value = operation_func(the_statement, the_var, login_var);
  67. if((return_value.value.type == INT_value)){
  68. printf("operation value = %d\n", return_value.value.value.int_value);
  69. }
  70. else if(return_value.value.type == BOOL_value){
  71. if(return_value.value.value.bool_value){
  72. printf("operation value = true\n");
  73. }
  74. else{
  75. printf("operation value = false\n");
  76. }
  77. }
  78. else if(return_value.value.type == NUMBER_value){
  79. printf("operation value = %f\n", return_value.value.value.double_value);
  80. }
  81. else if(return_value.value.type == NULL_value){
  82. printf("operation value = None\n");
  83. }
  84. else if(return_value.value.type == STRING_value){
  85. printf("operation value = %s\n", return_value.value.value.string);
  86. }
  87. else{
  88. printf("operation value = other[%d]\n", return_value.value.type);
  89. }
  90. puts("----stop code----");
  91. break;
  92. case call:
  93. return_value = call_back(the_statement, the_var);
  94. break;
  95. case while_cycle:
  96. puts("----while code----");
  97. return_value = while_func(the_statement, the_var);
  98. puts("----stop while code----");
  99. if(return_value.u == statement_end){ // while循环不需要返回值[避免GWARF_value 进入 the_var]
  100. return_value.value.type = NULL_value;
  101. return_value.value.value.int_value = 0;
  102. }
  103. break;
  104. case for_cycle:
  105. puts("----for code----");
  106. return_value = for_func(the_statement, the_var);
  107. puts("----for while code----");
  108. if(return_value.u == statement_end){ // while循环不需要返回值[避免GWARF_value 进入 the_var]
  109. return_value.value.type = NULL_value;
  110. return_value.value.value.int_value = 0;
  111. }
  112. break;
  113. case if_branch:
  114. puts("----if code----");
  115. return_value = if_func(the_statement->code.if_branch.done, the_var);
  116. puts("----stop if code----");
  117. if(return_value.u == statement_end){ // while循环不需要返回值[避免GWARF_value 进入 the_var]
  118. return_value.value.type = NULL_value;
  119. return_value.value.value.int_value = 0;
  120. }
  121. break;
  122. case base_value: // get value[所有字面量均为这个表达式]
  123. return_value.value = (the_statement->code).base_value.value; // code
  124. if((return_value.value.type == INT_value) || (return_value.value.type == BOOL_value)){
  125. printf("get value = %d\n", return_value.value.value.int_value);
  126. }
  127. else if(return_value.value.type == NUMBER_value){
  128. printf("get value = %f\n", return_value.value.value.double_value);
  129. }
  130. else if(return_value.value.type == NULL_value){
  131. printf("get value = None\n");
  132. }
  133. else if(return_value.value.type == STRING_value){
  134. printf("get value = %s\n", return_value.value.value.string);
  135. }
  136. else{
  137. printf("get value = other[%d]\n", return_value.value.type);
  138. }
  139. // base_value返回字面量 -> 主要返回object类型,还会返回GWARF_value的其他类型供生成object类型
  140. break;
  141. case base_list: // get value[所有字面量均为这个表达式]
  142. return_value.value = parameter_to_list(the_statement->code.base_list.value, the_var); // code
  143. break;
  144. case slice:{ // get value[所有字面量均为这个表达式]
  145. GWARF_result tmp_result = traverse((the_statement->code).slice.base_var, the_var, false), get; // 把a[1:2:3]的a取出来
  146. if(is_error(&tmp_result)){ // Name Error错误
  147. return_value = tmp_result;
  148. goto the_break_slice;
  149. }
  150. else if(is_space(&tmp_result)){
  151. return_value = tmp_result;
  152. goto the_break_slice;
  153. }
  154. GWARF_value base_the_var = tmp_result.value;
  155. if(base_the_var.type == CLASS_value){ // is class so that can use "."
  156. var *tmp = find_var(base_the_var.value.class_value->the_var, 0, "__slice__");
  157. if(tmp != NULL){
  158. get.value = tmp->value;
  159. get.father = &base_the_var; // 设置father
  160. return_value = call_back_core(get, the_var, the_statement->code.slice.value);
  161. goto the_break_slice;
  162. }
  163. }
  164. else if(base_the_var.type == OBJECT_value){
  165. var *tmp = find_var(base_the_var.value.object_value->the_var, 0, "__slice__");
  166. if(tmp != NULL){
  167. get.value = tmp->value;
  168. get.father = &base_the_var; // 设置father
  169. return_value = call_back_core(get, the_var, the_statement->code.slice.value);
  170. goto the_break_slice;
  171. }
  172. }
  173. the_break_slice:
  174. return_value.value = to_object(return_value.value, the_var);
  175. // printf("return_value.value.type = %d\n", return_value.value);
  176. break;
  177. }
  178. case base_var:{ // because the var tmp, we should ues a {} to make a block[name space] for the tmp var;
  179. int from = 0;
  180. if(the_statement->code.base_var.from == NULL){
  181. from = 0;
  182. }
  183. else{
  184. GWARF_result tmp_result, tmp_object = traverse(the_statement->code.base_var.from, the_var, false);
  185. if(is_error(&tmp_object)){ // Name Error错误
  186. // puts("STOP:: Name No Found!");
  187. from = 0;
  188. }
  189. else if(is_space(&tmp_object)){
  190. from = 0;
  191. }
  192. else{
  193. tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
  194. if(tmp_result.value.type == INT_value){
  195. from = tmp_result.value.value.int_value;
  196. }
  197. else if(tmp_result.value.type == NUMBER_value){
  198. from = (int)tmp_result.value.value.double_value;
  199. }
  200. else{
  201. from = 0;
  202. }
  203. }
  204. }
  205. var *tmp = find_var(the_var, from, (the_statement->code).base_var.var_name);
  206. if(tmp == NULL){ // 输出name error[共两处会输出]
  207. char *tmp = malloc((size_t)( 21 + strlen(the_statement->code.base_var.var_name) ));
  208. sprintf(tmp, "name not found [%s]\n", the_statement->code.base_var.var_name);
  209. return_value = to_error(tmp, "NameException", the_var);
  210. }
  211. else
  212. {
  213. return_value.value = tmp->value; // get_var
  214. if((return_value.value.type == INT_value) || (return_value.value.type == BOOL_value)){
  215. printf("var value = %d\n", return_value.value.value.int_value);
  216. }
  217. else if(return_value.value.type == NUMBER_value){
  218. printf("var value = %f\n", return_value.value.value.double_value);
  219. }
  220. else if(return_value.value.type == NULL_value){
  221. printf("var value = None\n");
  222. }
  223. else if(return_value.value.type == STRING_value){
  224. printf("var value = %s\n", return_value.value.value.string);
  225. }
  226. else{
  227. printf("var value = other[%d]\n", return_value.value.type);
  228. }
  229. }
  230. break;
  231. }
  232. case point:{
  233. GWARF_result tmp_result = traverse((the_statement->code).point.base_var, the_var, false);
  234. if(is_error(&tmp_result)){ // Name Error错误
  235. // puts("STOP:: Name No Found!");
  236. return_value = tmp_result;
  237. goto the_break;
  238. }
  239. else if(is_space(&tmp_result)){
  240. return_value = tmp_result;
  241. goto the_break;
  242. }
  243. GWARF_value base_the_var = tmp_result.value;
  244. if(base_the_var.type == CLASS_value){ // is class so that can use "."
  245. return_value = traverse((the_statement->code).point.child_var, base_the_var.value.class_value->the_var, false);
  246. }
  247. else if(base_the_var.type == OBJECT_value){
  248. return_value = traverse((the_statement->code).point.child_var, base_the_var.value.object_value->the_var, false);
  249. }
  250. else{ // 其他类型
  251. goto the_break;
  252. }
  253. if(is_error(&return_value)){ // Name Error错误
  254. // puts("STOP:: Name No Found!");
  255. goto the_break;
  256. }
  257. else if(is_space(&return_value)){
  258. goto the_break;
  259. }
  260. return_value.father = malloc(sizeof(return_value.father)); // 记录father的值
  261. *(return_value.father) = base_the_var;
  262. the_break:
  263. return_value.value = to_object(return_value.value, the_var);
  264. break;
  265. }
  266. case down:{
  267. GWARF_result tmp_result = traverse((the_statement->code).down.base_var, the_var, false), get;
  268. if(is_error(&tmp_result)){ // Name Error错误
  269. // puts("STOP:: Name No Found!");
  270. return_value = tmp_result;
  271. goto the_break_down;
  272. }
  273. else if(is_space(&tmp_result)){
  274. return_value = tmp_result;
  275. goto the_break_down;
  276. }
  277. GWARF_value base_the_var = tmp_result.value;
  278. if(base_the_var.type == CLASS_value){ // is class so that can use "."
  279. GWARF_result child_value = traverse((the_statement->code).down.child_var, the_var, false); // 作为参数
  280. var *tmp = find_var(base_the_var.value.class_value->the_var, 0, "__down__");
  281. if(tmp != NULL){
  282. get.value = tmp->value;
  283. get.father = &base_the_var; // 设置father
  284. return_value = call_back_core(get, the_var, pack_value_parameter(child_value.value));
  285. goto the_break_down;
  286. }
  287. }
  288. else if(base_the_var.type == OBJECT_value){
  289. GWARF_result child_value = traverse((the_statement->code).down.child_var, the_var, false); // 作为参数
  290. var *tmp = find_var(base_the_var.value.object_value->the_var, 0, "__down__");
  291. if(tmp != NULL){
  292. get.value = tmp->value;
  293. get.father = &base_the_var; // 设置father
  294. return_value = call_back_core(get, the_var, pack_value_parameter(child_value.value));
  295. goto the_break_down;
  296. }
  297. }
  298. the_break_down:
  299. return_value.value = to_object(return_value.value, the_var); // call_back_core 返回值是object
  300. break;
  301. }
  302. case def:{
  303. GWARF_result func_value;
  304. func *func_tmp = malloc(sizeof(func));
  305. func_tmp->done = the_statement->code.def.done;
  306. func_tmp->parameter_list = the_statement->code.def.parameter_list;
  307. func_tmp->the_var = copy_var_list(the_var);
  308. func_tmp->type = customize; // func by user
  309. if(login_var != the_var){ // 定义为类方法
  310. func_tmp->is_class = 1;
  311. }
  312. else{
  313. func_tmp->is_class = 0;
  314. }
  315. func_value.value.type = FUNC_value;
  316. func_value.value.value.func_value = func_tmp;
  317. int from;
  318. if(the_statement->code.def.from == NULL){
  319. from = 0;
  320. }
  321. else{
  322. GWARF_result tmp_result, tmp_object = traverse(the_statement->code.def.from, the_var, false);
  323. if(is_error(&tmp_object)){ // Name Error错误
  324. // puts("STOP:: Name No Found!");
  325. from = 0;
  326. }
  327. else if(is_space(&tmp_object)){
  328. from = 0;
  329. }
  330. else{
  331. tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
  332. if(tmp_result.value.type == INT_value){
  333. from = tmp_result.value.value.int_value;
  334. }
  335. else if(tmp_result.value.type == NUMBER_value){
  336. from = (int)tmp_result.value.value.double_value;
  337. }
  338. else{
  339. from = 0;
  340. }
  341. }
  342. }
  343. assigment_func(the_statement->code.def.name, func_value, login_var, from); // 注册函数到指定的位置
  344. // 无返回值
  345. break;
  346. }
  347. case set_class:{
  348. puts("----set class----");
  349. GWARF_result class_value;
  350. class_object *class_tmp = malloc(sizeof(class_object));
  351. class_tmp->the_var = make_var_base(make_var()); // make class var list
  352. class_value.value.type = CLASS_value;
  353. class_value.value.value.class_value = class_tmp;
  354. // 获取father -- append_by_var_list[拼凑]
  355. GWARF_result father_tmp;
  356. parameter *tmp_s = the_statement->code.set_class.father_list;
  357. if(tmp_s != NULL){
  358. while(1){
  359. father_tmp = traverse(tmp_s->u.value, the_var, false); // 执行
  360. if(is_error(&father_tmp)){ // Name Error错误
  361. // puts("STOP:: Name No Found!");
  362. return_value = father_tmp;
  363. break;
  364. }
  365. else if(is_space(&father_tmp)){
  366. return_value = father_tmp;
  367. break;
  368. }
  369. if(father_tmp.value.type == CLASS_value){ // 可以通过class继承, 也可以通过object.cls继承
  370. append_by_var_list(class_tmp->the_var, father_tmp.value.value.class_value->the_var);
  371. }
  372. else if(father_tmp.value.type == OBJECT_value){
  373. append_by_var_list(class_tmp->the_var, father_tmp.value.value.object_value->cls);
  374. }
  375. if (tmp_s->next == NULL){ // the last
  376. break;
  377. }
  378. tmp_s = tmp_s->next;
  379. }
  380. }
  381. else{
  382. var *object_tmp = find_var(the_var, 0, "object");
  383. if(object_tmp != NULL){
  384. father_tmp.value = object_tmp->value;
  385. append_by_var_list(class_tmp->the_var, father_tmp.value.value.class_value->the_var);
  386. }
  387. else{
  388. // TODO::返回error -> var_list错误
  389. }
  390. }
  391. class_tmp->out_var = append_by_var_list(class_tmp->the_var, copy_var_list(the_var)); //TODO::class_tmp->out_var = copy_var_list(the_var);
  392. // 执行done
  393. statement *tmp = the_statement->code.set_class.done;
  394. GWARF_result result;
  395. while(1){
  396. if(tmp == NULL){
  397. break; // off
  398. }
  399. read_statement(tmp, the_var, class_tmp->the_var);
  400. tmp = tmp->next;
  401. }
  402. int from;
  403. if(the_statement->code.set_class.from == NULL){
  404. from = 0;
  405. }
  406. else{
  407. GWARF_result tmp_result, tmp_object = traverse(the_statement->code.set_class.from, the_var, false);
  408. if(is_error(&tmp_object)){ // Name Error错误
  409. // puts("STOP:: Name No Found!");
  410. from = 0;
  411. }
  412. else if(is_space(&tmp_object)){
  413. from = 0;
  414. }
  415. else{
  416. tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
  417. if(tmp_result.value.type == INT_value){
  418. from = tmp_result.value.value.int_value;
  419. }
  420. else if(tmp_result.value.type == NUMBER_value){
  421. from = (int)tmp_result.value.value.double_value;
  422. }
  423. else{
  424. from = 0;
  425. }
  426. }
  427. }
  428. assigment_func(the_statement->code.set_class.name, class_value, login_var, from); // 注册class 的 位置
  429. puts("----stop set class----");
  430. // 无返回值
  431. break;
  432. }
  433. case break_cycle:
  434. return_value.u = cycle_break;
  435. return_value.value.type = INT_value;
  436. if(the_statement->code.break_cycle.times == NULL){
  437. return_value.value.value.int_value = 0;
  438. }
  439. else{
  440. int int_tmp;
  441. GWARF_result tmp_result, tmp_object = traverse(the_statement->code.break_cycle.times, the_var, false);
  442. if(is_error(&tmp_object)){ // Name Error错误
  443. // puts("STOP:: Name No Found!");
  444. return_value = tmp_object;
  445. break;
  446. }
  447. else if(is_space(&tmp_object)){
  448. return_value = tmp_object;
  449. break;
  450. }
  451. tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
  452. if(tmp_result.value.type == INT_value){
  453. int_tmp = tmp_result.value.value.int_value;
  454. }
  455. else if(tmp_result.value.type == NUMBER_value){
  456. int_tmp = (int)tmp_result.value.value.double_value;
  457. }
  458. else{
  459. int_tmp = 0;
  460. }
  461. return_value.value.value.int_value = int_tmp;
  462. }
  463. break;
  464. case broken:
  465. return_value.u = code_broken;
  466. return_value.value.type = INT_value;
  467. if(the_statement->code.broken.times == NULL){
  468. return_value.value.value.int_value = 0;
  469. }
  470. else{
  471. int int_tmp;
  472. GWARF_result tmp_result, tmp_object = traverse(the_statement->code.broken.times, the_var, false);
  473. if(is_error(&tmp_object)){ // Name Error错误
  474. // puts("STOP:: Name No Found!");
  475. return_value = tmp_object;
  476. break;
  477. }
  478. else if(is_space(&tmp_object)){
  479. return_value = tmp_object;
  480. break;
  481. }
  482. tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
  483. if(tmp_result.value.type == INT_value){
  484. int_tmp = tmp_result.value.value.int_value;
  485. }
  486. else if(tmp_result.value.type == NUMBER_value){
  487. int_tmp = (int)tmp_result.value.value.double_value;
  488. }
  489. else{
  490. int_tmp = 0;
  491. }
  492. return_value.value.value.int_value = int_tmp;
  493. }
  494. break;
  495. case continue_cycle:
  496. return_value.u = cycle_continue;
  497. return_value.value.type = INT_value;
  498. if(the_statement->code.continue_cycle.times == NULL){
  499. return_value.value.value.int_value = 0;
  500. }
  501. else{
  502. int int_tmp;
  503. GWARF_result tmp_result, tmp_object = traverse(the_statement->code.continue_cycle.times, the_var, false);
  504. if(is_error(&tmp_object)){ // Name Error错误
  505. // puts("STOP:: Name No Found!");
  506. return_value = tmp_object;
  507. break;
  508. }
  509. else if(is_space(&tmp_object)){
  510. return_value = tmp_object;
  511. break;
  512. }
  513. tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
  514. if(tmp_result.value.type == INT_value){
  515. int_tmp = tmp_result.value.value.int_value;
  516. }
  517. else if(tmp_result.value.type == NUMBER_value){
  518. int_tmp = (int)tmp_result.value.value.double_value;
  519. }
  520. else{
  521. int_tmp = 0;
  522. }
  523. return_value.value.value.int_value = int_tmp;
  524. }
  525. break;
  526. case continued:
  527. return_value.u = code_continued;
  528. return_value.value.type = INT_value;
  529. if(the_statement->code.continued.times == NULL){
  530. return_value.value.value.int_value = 0;
  531. }
  532. else{
  533. int int_tmp;
  534. GWARF_result tmp_result, tmp_object = traverse(the_statement->code.continued.times, the_var, false);
  535. if(is_error(&tmp_object)){ // Name Error错误
  536. // puts("STOP:: Name No Found!");
  537. return_value = tmp_object;
  538. break;
  539. }
  540. else if(is_space(&tmp_object)){
  541. return_value = tmp_object;
  542. break;
  543. }
  544. tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
  545. if(tmp_result.value.type == INT_value){
  546. int_tmp = tmp_result.value.value.int_value;
  547. }
  548. else if(tmp_result.value.type == NUMBER_value){
  549. int_tmp = (int)tmp_result.value.value.double_value;
  550. }
  551. else{
  552. int_tmp = 0;
  553. }
  554. return_value.value.value.int_value = int_tmp;
  555. }
  556. break;
  557. case restart:
  558. return_value.u = cycle_restart;
  559. return_value.value.type = INT_value;
  560. if(the_statement->code.restart.times == NULL){
  561. return_value.value.value.int_value = 0;
  562. }
  563. else{
  564. int int_tmp;
  565. GWARF_result tmp_result, tmp_object = traverse(the_statement->code.restart.times, the_var, false);
  566. if(is_error(&tmp_object)){ // Name Error错误
  567. // puts("STOP:: Name No Found!");
  568. return_value = tmp_object;
  569. break;
  570. }
  571. else if(is_space(&tmp_object)){
  572. return_value = tmp_object;
  573. break;
  574. }
  575. tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
  576. if(tmp_result.value.type == INT_value){
  577. int_tmp = tmp_result.value.value.int_value;
  578. }
  579. else if(tmp_result.value.type == NUMBER_value){
  580. int_tmp = (int)tmp_result.value.value.double_value;
  581. }
  582. else{
  583. int_tmp = 0;
  584. }
  585. return_value.value.value.int_value = int_tmp;
  586. }
  587. break;
  588. case restarted:
  589. return_value.u = code_restarted;
  590. return_value.value.type = INT_value;
  591. if(the_statement->code.restarted.times == NULL){
  592. return_value.value.value.int_value = 0;
  593. }
  594. else{
  595. int int_tmp;
  596. GWARF_result tmp_result, tmp_object = traverse(the_statement->code.restarted.times, the_var, false);
  597. if(is_error(&tmp_object)){ // Name Error错误
  598. // puts("STOP:: Name No Found!");
  599. return_value = tmp_object;
  600. break;
  601. }
  602. else if(is_space(&tmp_object)){
  603. return_value = tmp_object;
  604. break;
  605. }
  606. tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
  607. if(tmp_result.value.type == INT_value){
  608. int_tmp = tmp_result.value.value.int_value;
  609. }
  610. else if(tmp_result.value.type == NUMBER_value){
  611. int_tmp = (int)tmp_result.value.value.double_value;
  612. }
  613. else{
  614. int_tmp = 0;
  615. }
  616. return_value.value.value.int_value = int_tmp;
  617. }
  618. break;
  619. case return_code:
  620. return_value.u = return_def;
  621. if(the_statement->code.return_code.times == NULL){
  622. return_value.return_times = 0;
  623. }
  624. else{
  625. int int_tmp;
  626. GWARF_result tmp_result, tmp_object = traverse(the_statement->code.return_code.times, the_var, false);
  627. if(is_error(&tmp_object)){ // Name Error错误
  628. // puts("STOP:: Name No Found!");
  629. return_value = tmp_object;
  630. break;
  631. }
  632. else if(is_space(&tmp_object)){
  633. return_value = tmp_object;
  634. break;
  635. }
  636. tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
  637. if(tmp_result.value.type == INT_value){
  638. int_tmp = tmp_result.value.value.int_value;
  639. }
  640. else if(tmp_result.value.type == NUMBER_value){
  641. int_tmp = (int)tmp_result.value.value.double_value;
  642. }
  643. else{
  644. int_tmp = 0;
  645. }
  646. return_value.return_times = int_tmp;
  647. }
  648. if(the_statement->code.return_code.value == NULL){ // return NULL
  649. return_value.value.type = NULL_value;
  650. return_value.value.value.double_value = 0;
  651. }
  652. else{
  653. GWARF_result tmp_result = traverse(the_statement->code.return_code.value, the_var, false);
  654. if(is_error(&tmp_result)){ // Name Error错误
  655. // puts("STOP:: Name No Found!");
  656. return_value = tmp_result;
  657. break;
  658. }
  659. else if(is_space(&tmp_result)){
  660. return_value = tmp_result;
  661. break;
  662. }
  663. return_value.value = to_object(tmp_result.value, the_var);
  664. }
  665. break;
  666. case rewent:
  667. return_value.u = code_rewent; // rego but not now
  668. break;
  669. case rego:
  670. return_value.u = code_rego; // rego now
  671. break;
  672. case set_default:{
  673. char *name = the_statement->code.set_default.name;
  674. int base_from = 0;
  675. GWARF_result tmp_result, tmp_object = traverse(the_statement->code.set_default.times, the_var, false);
  676. if(is_error(&tmp_object)){ // Name Error错误
  677. // puts("STOP:: Name No Found!");
  678. return_value = tmp_object;
  679. break;
  680. }
  681. else if(is_space(&tmp_object)){
  682. return_value = tmp_object;
  683. break;
  684. }
  685. tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
  686. if(tmp_result.value.type == INT_value){
  687. base_from = tmp_result.value.value.int_value;
  688. }
  689. else if(tmp_result.value.type == NUMBER_value){
  690. base_from = (int)tmp_result.value.value.double_value;
  691. }
  692. else{
  693. base_from = 0;
  694. }
  695. append_default_var_base(name, base_from, the_var->default_list);
  696. printf("set_default for %s\n", name);
  697. break;
  698. }
  699. case set_global:{
  700. char *name = the_statement->code.set_global.name;
  701. int base_from = get_var_list_len(the_var);
  702. append_default_var_base(name, base_from, the_var->default_list);
  703. printf("global for %s\n", name);
  704. break;
  705. }
  706. case set_nonlocal:{
  707. char *name = the_statement->code.set_global.name;
  708. append_default_var_base(name, 1, the_var->default_list);
  709. printf("nonlocal for %s\n", name);
  710. break;
  711. }
  712. case code_block:
  713. puts("----block code----");
  714. return_value = block_func(the_statement, the_var);
  715. puts("----stop block code----");
  716. break;
  717. case try_code:
  718. return_value = try_func(the_statement, the_var);
  719. break;
  720. case raise_e:
  721. return_value = raise_func(the_statement, the_var, true);
  722. break;
  723. case throw_e:
  724. return_value = raise_func(the_statement, the_var, false);
  725. break;
  726. case import_class: // import xxx as xxx 语句
  727. return_value = import_func(the_statement, the_var);
  728. break;
  729. case include_import: // import xxx as xxx 语句
  730. return_value = include_func(the_statement, the_var);
  731. break;
  732. default:
  733. puts("default");
  734. break;
  735. }
  736. return return_value;
  737. }
  738. // -----------------include func
  739. GWARF_result include_func(statement *the_statement, var_list *the_var){
  740. GWARF_result return_value;
  741. statement *file_statement = the_statement->code.include_import.file;
  742. GWARF_value file = to_str(traverse(file_statement, the_var, false).value, the_var);
  743. // TODO:: 使用链表来存储
  744. inter *old_global = global_inter; // 保存旧迭代器
  745. statement_list *old_statement_base = statement_base; // 保存statement_base
  746. global_inter = get_inter(); // 拿全局解释器[并声明全局变量]
  747. free(global_inter->global_var); // 不需要新的the_var
  748. global_inter->global_var = the_var->var_base;
  749. statement_base = make_statement_base(global_inter->global_code);
  750. parser(file.value.string);
  751. printf("----start run----\n");
  752. traverse_global(global_inter->global_code, the_var);
  753. printf("code end...\n");
  754. global_inter = old_global; // 变回旧迭代器
  755. statement_base = old_statement_base; // 变回statement_base
  756. return_value.u = statement_end;
  757. return_value.value.type = NULL_value;
  758. return_value.value.value.int_value = 0;
  759. return return_value;
  760. }
  761. // -----------------import func
  762. GWARF_result import_func(statement *the_statement, var_list *the_var){
  763. GWARF_result return_value;
  764. statement *file_statement = the_statement->code.import_class.file;
  765. char *name = the_statement->code.import_class.name;
  766. GWARF_value file = to_str(traverse(file_statement, the_var, false).value, the_var);
  767. // TODO:: 使用链表来存储
  768. inter *old_global = global_inter; // 保存旧迭代器
  769. statement_list *old_statement_base = statement_base; // 保存statement_base
  770. global_inter = get_inter(); // 拿全局解释器[并声明全局变量]
  771. var_list *new_the_var = make_var_base(global_inter->global_var);
  772. statement_base = make_statement_base(global_inter->global_code);
  773. login(new_the_var);
  774. parser(file.value.string);
  775. printf("----start run----\n");
  776. traverse_global(global_inter->global_code, new_the_var);
  777. printf("code end...\n");
  778. global_inter = old_global; // 保存旧迭代器
  779. statement_base = old_statement_base; // 保存statement_base
  780. GWARF_result import_result;
  781. import_result.value.type = CLASS_value;
  782. class_object *class_tmp = malloc(sizeof(class_object));
  783. class_tmp->the_var = new_the_var; // make class var list
  784. class_tmp->out_var = append_by_var_list(class_tmp->the_var, copy_var_list(the_var)); // make class var list with out var
  785. int from;
  786. if(the_statement->code.import_class.from == NULL){
  787. from = 0;
  788. }
  789. else{
  790. GWARF_result tmp_result, tmp_object = traverse(the_statement->code.import_class.from, the_var, false);
  791. if(is_error(&tmp_object)){ // Name Error错误
  792. // puts("STOP:: Name No Found!");
  793. from = 0;
  794. }
  795. else if(is_space(&tmp_object)){
  796. from = 0;
  797. }
  798. else{
  799. tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
  800. if(tmp_result.value.type == INT_value){
  801. from = tmp_result.value.value.int_value;
  802. }
  803. else if(tmp_result.value.type == NUMBER_value){
  804. from = (int)tmp_result.value.value.double_value;
  805. }
  806. else{
  807. from = 0;
  808. }
  809. }
  810. }
  811. import_result.value.value.class_value = class_tmp;
  812. assigment_func(name, import_result, the_var, from);
  813. return_value.u = statement_end;
  814. return_value.value.type = NULL_value;
  815. return_value.value.value.int_value = 0;
  816. return return_value;
  817. }
  818. // -----------------if func
  819. GWARF_result if_func(if_list *if_base, var_list *the_var){ // read the statement list with case to run by func
  820. GWARF_result value;
  821. if_list *start;
  822. again: start = if_base;
  823. bool rego = false; // switch...case...
  824. while(1){
  825. if(start->condition == NULL){ // else
  826. else_restart:
  827. puts("----else----");
  828. value = traverse(start->done, the_var, true);
  829. puts("----stop else----");
  830. // restarted操作
  831. // TODO::可以检查值类型,如果不正确则是解释器错误
  832. if(value.u == code_restarted){
  833. if(value.value.type != INT_value){
  834. // printf("Code Warrning: Bad Type Number for restart(ed), reset to zero");
  835. value.value.type = INT_value;
  836. value.value.value.int_value = 0;
  837. }
  838. if(value.value.value.int_value <= 0){
  839. puts("----restarted real----");
  840. value.u = statement_end;
  841. goto else_restart;
  842. }
  843. else{
  844. value.value.value.int_value -= 1;
  845. break;
  846. }
  847. }
  848. // continued操作
  849. if(value.u == code_continued){
  850. if(value.value.type != INT_value){
  851. // printf("Code Warrning: Bad Type Number for continue(ed), reset to zero");
  852. value.value.type = INT_value;
  853. value.value.value.int_value = 0;
  854. }
  855. if(value.value.value.int_value <= 0){
  856. puts("----if continue real----");
  857. value.u = statement_end;
  858. goto again;
  859. }
  860. else{
  861. value.value.value.int_value -= 1;
  862. }
  863. break;
  864. }
  865. // broken操作
  866. if(value.u == code_broken){
  867. if(value.value.type != INT_value){
  868. // printf("Code Warrning: Bad Type Number for break(broken), reset to zero");
  869. value.value.type = INT_value;
  870. value.value.value.int_value = 0;
  871. }
  872. if(value.value.value.int_value <= 0){
  873. value.u = statement_end; // 正常设置[正常语句结束]
  874. }
  875. else{
  876. value.value.value.int_value -= 1;
  877. }
  878. break;
  879. }
  880. // rego操作
  881. // else层的rego和rewent是可以往上层遗传的[也就是else如果显式指定rego和rewent是会遗传的,但是如果是if或elif指定rego是不会遗传的]
  882. if((value.u == code_rewent) || (value.u == code_rego)){
  883. ;
  884. }
  885. break; // else not next and don't need rego
  886. }
  887. else{ // not else
  888. bool condition;
  889. GWARF_result tmp_result = traverse(start->condition, the_var, false);
  890. if(is_error(&tmp_result)){ // Name Error错误
  891. // puts("STOP:: Name No Found!");
  892. value = tmp_result;
  893. break;
  894. }
  895. else if(is_space(&tmp_result)){
  896. value = tmp_result;
  897. break;
  898. }
  899. condition = to_bool(tmp_result.value);
  900. printf("if condition = %d\n", condition);
  901. if(rego || (condition)){ // condition run success or rego(condition won't do) bug rewent can
  902. if_restart:
  903. puts("----if----");
  904. value = traverse(start->done, the_var, true);
  905. puts("----stop if----");
  906. // restarted操作
  907. if(value.u == code_restarted){
  908. if(value.value.type != INT_value){
  909. // printf("Code Warrning: Bad Type Number for restart(ed), reset to zero");
  910. value.value.type = INT_value;
  911. value.value.value.int_value = 0;
  912. }
  913. if(value.value.value.int_value <= 0){
  914. puts("----restarted real----");
  915. value.u = statement_end;
  916. goto if_restart;
  917. }
  918. else{
  919. value.value.value.int_value -= 1;
  920. break;
  921. }
  922. }
  923. // continued操作 [设在在rewent和rego前面]
  924. if(value.u == code_continued){
  925. if(value.value.type != INT_value){
  926. // printf("Code Warrning: Bad Type Number for continue(ed), reset to zero");
  927. value.value.type = INT_value;
  928. value.value.value.int_value = 0;
  929. }
  930. if(value.value.value.int_value <= 0){
  931. puts("----if continue real----");
  932. value.u = statement_end;
  933. goto again;
  934. }
  935. else{
  936. value.value.value.int_value -= 1;
  937. }
  938. break;
  939. }
  940. // broken操作
  941. if(value.u == code_broken){
  942. if(value.value.type != INT_value){
  943. // printf("Code Warrning: Bad Type Number for break(broken), reset to zero");
  944. value.value.type = INT_value;
  945. value.value.value.int_value = 0;
  946. }
  947. value.value.value.int_value -= 1;
  948. if(value.value.value.int_value < 0){
  949. value.u = statement_end; // 正常设置[正常语句结束]
  950. }
  951. break;
  952. }
  953. // rego操作
  954. if((value.u == code_rewent) || (value.u == code_rego)){
  955. value.u = statement_end; // 设置为正常语句
  956. rego = true;
  957. }
  958. // not restarted -> if is rego
  959. if(!rego){
  960. break; // don't rego
  961. }
  962. }
  963. }
  964. if(start->next == NULL){ // not next
  965. break;
  966. }
  967. start = start->next;
  968. }
  969. if((value.u == cycle_continue) || (value.u == cycle_restart) || (value.u == cycle_break)){ // if不处理也不计入层次 同break一样
  970. ;
  971. }
  972. return value;
  973. }
  974. // -----------------for func
  975. GWARF_result for_func(statement *the_statement, var_list *the_var){ // read the statement list with case to run by func
  976. GWARF_result value;
  977. var *tmp = make_var(); // base_var
  978. the_var = append_var_list(tmp, the_var);
  979. bool condition;
  980. if(the_statement->code.for_cycle.first != NULL){
  981. GWARF_result tmp_result = traverse(the_statement->code.for_cycle.first, the_var, false); // first to do
  982. if(is_error(&tmp_result)){ // Name Error错误
  983. // puts("STOP:: Name No Found!");
  984. value = tmp_result;
  985. return value;
  986. }
  987. else if(is_space(&tmp_result)){
  988. value = tmp_result;
  989. return value;
  990. }
  991. }
  992. while (1){
  993. if(the_statement->code.for_cycle.condition != NULL){ // 检查是否存在循环条件
  994. GWARF_result tmp_result = traverse(the_statement->code.for_cycle.condition, the_var, false);
  995. if(is_error(&tmp_result)){ // Name Error错误
  996. // puts("STOP:: Name No Found!");
  997. value = tmp_result;
  998. goto return_value;
  999. }
  1000. else if(is_space(&tmp_result)){
  1001. value = tmp_result;
  1002. goto return_value;
  1003. }
  1004. condition = to_bool(tmp_result.value);
  1005. printf("for condition = %d\n", condition);
  1006. if(!condition){
  1007. break;
  1008. }
  1009. }
  1010. restart_again:
  1011. puts("----for----");
  1012. value = traverse(the_statement->code.for_cycle.done, the_var, false);
  1013. //break操作
  1014. if((value.u == cycle_break) || (value.u == code_broken)){
  1015. if(value.value.type != INT_value){
  1016. value.value.type = INT_value;
  1017. value.value.value.int_value = 0;
  1018. }
  1019. if(value.value.value.int_value <= 0){
  1020. value.u = statement_end; // 正常设置[正常语句结束]
  1021. }
  1022. else{
  1023. value.value.value.int_value -= 1;
  1024. }
  1025. break; // break don't need after do
  1026. }
  1027. puts("----stop for----");
  1028. // after do
  1029. if(the_statement->code.for_cycle.after != NULL){
  1030. GWARF_result tmp_result = traverse(the_statement->code.for_cycle.after, the_var, false);
  1031. if(is_error(&tmp_result)){ // Name Error错误
  1032. // puts("STOP:: Name No Found!");
  1033. value = tmp_result;
  1034. goto return_value;
  1035. }
  1036. else if(is_space(&tmp_result)){
  1037. value = tmp_result;
  1038. goto return_value;
  1039. }
  1040. }
  1041. // continue操作
  1042. if((value.u == cycle_continue) || (value.u == code_continued)){
  1043. if(value.value.type != INT_value){
  1044. value.value.type = INT_value;
  1045. value.value.value.int_value = 0;
  1046. }
  1047. if(value.value.value.int_value <= 0){
  1048. puts("----continue real----");
  1049. value.u = statement_end;
  1050. continue;
  1051. }
  1052. else{
  1053. value.value.value.int_value -= 1;
  1054. break;
  1055. }
  1056. }
  1057. // restart操作
  1058. if((value.u == cycle_restart) || (value.u == code_restarted)){
  1059. if(value.value.type != INT_value){
  1060. value.value.type = INT_value;
  1061. value.value.value.int_value = 0;
  1062. }
  1063. if(value.value.value.int_value <= 0){
  1064. puts("----restart real----");
  1065. value.u = statement_end;
  1066. goto restart_again;
  1067. }
  1068. else{
  1069. value.value.value.int_value -= 1;
  1070. break;
  1071. }
  1072. }
  1073. }
  1074. return_value:
  1075. the_var = free_var_list(the_var); // free the new var
  1076. return value;
  1077. }
  1078. // -----------------raise func
  1079. GWARF_result raise_func(statement *the_statement, var_list *the_var, bool not_class){ // read the statement list with case to run by func
  1080. GWARF_result error_value;
  1081. GWARF_value info;
  1082. if(not_class){ // raise
  1083. error_value = traverse(the_statement->code.raise_e.done, the_var, false);
  1084. info = to_str(traverse(the_statement->code.raise_e.info, the_var, false).value, the_var);
  1085. }
  1086. else{ // raise
  1087. GWARF_value tmp;
  1088. tmp.type = STRING_value;
  1089. tmp.value.string = "throw by user";
  1090. error_value = traverse(the_statement->code.throw_e.done, the_var, false);
  1091. info = to_str(tmp, the_var);
  1092. }
  1093. // restart操作[和continue效果相同]
  1094. if(is_error(&error_value)){ // 遇到错误->执行except语句[不需要再检查break...]
  1095. goto return_value; // raise执行时发生错误
  1096. }
  1097. if(is_space(&error_value)){
  1098. goto return_value;
  1099. }
  1100. error_value.u = error;
  1101. error_value.error_info = info.value.string;
  1102. if(error_value.value.type == OBJECT_value){
  1103. ; // 本身就是object
  1104. }
  1105. else if(error_value.value.type == CLASS_value && not_class){ // class类型
  1106. error_value.value = call_back_core(error_value, the_var, pack_value_parameter(info)).value;
  1107. }
  1108. else{
  1109. error_value.value = to_object(error_value.value, the_var); // 直接返回
  1110. }
  1111. return_value: return error_value;
  1112. }
  1113. // -----------------try func
  1114. GWARF_result try_func(statement *the_statement, var_list *the_var){ // read the statement list with case to run by func
  1115. GWARF_result value;
  1116. var *tmp = make_var(); // base_var
  1117. the_var = append_var_list(tmp, the_var);
  1118. again:
  1119. puts("----try----");
  1120. value = traverse(the_statement->code.try_code.try, the_var, false);
  1121. puts("----stop try----");
  1122. // restart操作[和continue效果相同]
  1123. if(is_error(&value)){ // 遇到错误->执行except语句[不需要再检查break...]
  1124. assigment_func(the_statement->code.try_code.name, value, the_var, 0);
  1125. puts("----except----");
  1126. value = traverse(the_statement->code.try_code.except, the_var, false);
  1127. puts("----stop except----");
  1128. }
  1129. if(value.u == code_restarted){
  1130. if(value.value.type != INT_value){
  1131. value.value.type = INT_value;
  1132. value.value.value.int_value = 0;
  1133. }
  1134. if(value.value.value.int_value <= 0){
  1135. puts("----restarted real----");
  1136. value.u = statement_end;
  1137. goto again;
  1138. }
  1139. else{
  1140. value.value.value.int_value -= 1;
  1141. }
  1142. }
  1143. // continued操作
  1144. if(value.u == code_continued){
  1145. if(value.value.type != INT_value){
  1146. value.value.type = INT_value;
  1147. value.value.value.int_value = 0;
  1148. }
  1149. if(value.value.value.int_value <= 0){
  1150. puts("----block continue real----");
  1151. value.u = statement_end;
  1152. goto again;
  1153. }
  1154. else{
  1155. value.value.value.int_value -= 1;
  1156. }
  1157. }
  1158. // broken操作
  1159. if(value.u == code_broken){
  1160. if(value.value.type != INT_value){
  1161. value.value.type = INT_value;
  1162. value.value.value.int_value = 0;
  1163. }
  1164. if(value.value.value.int_value <= 0){
  1165. value.u = statement_end; // 正常设置[正常语句结束]
  1166. }
  1167. else{
  1168. value.value.value.int_value -= 1;
  1169. }
  1170. }
  1171. the_var = free_var_list(the_var); // free the new var
  1172. return value;
  1173. }
  1174. // -----------------block func
  1175. GWARF_result block_func(statement *the_statement, var_list *the_var){ // read the statement list with case to run by func
  1176. GWARF_result value;
  1177. again:
  1178. puts("----block----");
  1179. value = traverse(the_statement->code.code_block.done, the_var, true);
  1180. puts("----stop block----");
  1181. // restart操作[和continue效果相同]
  1182. if(value.u == code_restarted){
  1183. if(value.value.type != INT_value){
  1184. value.value.type = INT_value;
  1185. value.value.value.int_value = 0;
  1186. }
  1187. if(value.value.value.int_value <= 0){
  1188. puts("----restarted real----");
  1189. value.u = statement_end;
  1190. goto again;
  1191. }
  1192. else{
  1193. value.value.value.int_value -= 1;
  1194. }
  1195. }
  1196. // continued操作
  1197. if(value.u == code_continued){
  1198. if(value.value.type != INT_value){
  1199. value.value.type = INT_value;
  1200. value.value.value.int_value = 0;
  1201. }
  1202. if(value.value.value.int_value <= 0){
  1203. puts("----block continue real----");
  1204. value.u = statement_end;
  1205. goto again;
  1206. }
  1207. else{
  1208. value.value.value.int_value -= 1;
  1209. }
  1210. }
  1211. // broken操作
  1212. if(value.u == code_broken){
  1213. if(value.value.type != INT_value){
  1214. value.value.type = INT_value;
  1215. value.value.value.int_value = 0;
  1216. }
  1217. if(value.value.value.int_value <= 0){
  1218. value.u = statement_end; // 正常设置[正常语句结束]
  1219. }
  1220. else{
  1221. value.value.value.int_value -= 1;
  1222. }
  1223. }
  1224. return value;
  1225. }
  1226. // -----------------while func
  1227. GWARF_result while_func(statement *the_statement, var_list *the_var){ // read the statement list with case to run by func
  1228. GWARF_result value;
  1229. // printf("----address = %d----\n", the_var);
  1230. var *tmp = make_var(); // base_var
  1231. the_var = append_var_list(tmp, the_var);
  1232. // printf("----new address = %d----\n", the_var);
  1233. bool condition;
  1234. while (1){
  1235. GWARF_result tmp_result = traverse(the_statement->code.while_cycle.condition, the_var, false);
  1236. if(is_error(&tmp_result)){ // Name Error错误
  1237. // puts("STOP:: Name No Found!");
  1238. value = tmp_result;
  1239. goto return_value;
  1240. }
  1241. else if(is_space(&tmp_result)){
  1242. value = tmp_result;
  1243. goto return_value;
  1244. }
  1245. condition = to_bool(tmp_result.value);
  1246. printf("while condition = %d\n", condition);
  1247. if(!condition){
  1248. break;
  1249. }
  1250. restart_again:
  1251. puts("----while----");
  1252. value = traverse(the_statement->code.while_cycle.done, the_var, false);
  1253. puts("----stop while----");
  1254. // break的操作
  1255. if((value.u == cycle_break) || (value.u == code_broken)){
  1256. if(value.value.type != INT_value){
  1257. value.value.type = INT_value;
  1258. value.value.value.int_value = 0;
  1259. }
  1260. if(value.value.value.int_value <= 0){
  1261. value.u = statement_end; // 正常设置[正常语句结束]
  1262. }
  1263. else{
  1264. value.value.value.int_value -= 1;
  1265. }
  1266. break;
  1267. }
  1268. // continue的操作
  1269. if((value.u == cycle_continue) || (value.u == code_continued)){
  1270. if(value.value.type != INT_value){
  1271. value.value.type = INT_value;
  1272. value.value.value.int_value = 0;
  1273. }
  1274. if(value.value.value.int_value <= 0){
  1275. value.u = statement_end;
  1276. continue;
  1277. }
  1278. else{
  1279. value.value.value.int_value -= 1;
  1280. break;
  1281. }
  1282. }
  1283. // restart的操作
  1284. if((value.u == cycle_restart) || (value.u == code_restarted)){
  1285. if(value.value.type != INT_value){
  1286. value.value.type = INT_value;
  1287. value.value.value.int_value = 0;
  1288. }
  1289. if(value.value.value.int_value <= 0){
  1290. value.u = statement_end;
  1291. goto restart_again;
  1292. }
  1293. else{
  1294. value.value.value.int_value -= 1;
  1295. break;
  1296. }
  1297. }
  1298. }
  1299. return_value:
  1300. the_var = free_var_list(the_var); // free the new var
  1301. return value;
  1302. }
  1303. // -----------------operation func
  1304. GWARF_result operation_func(statement *the_statement, var_list *the_var, var_list *login_var){ // read the statement list with case to run by func
  1305. GWARF_result value, left_result, right_result;
  1306. int func_type = the_statement->code.operation.type;
  1307. if((func_type != ASSIGMENT_func) && (func_type != NEGATIVE_func)){ // don't run because I don't need[if it's and func ,it will be run twice]
  1308. left_result = traverse((*the_statement).code.operation.left_exp, the_var, false);
  1309. if(is_error(&left_result)){ // Name Error错误
  1310. // puts("STOP:: Name No Found!");
  1311. return left_result;
  1312. }
  1313. else if(is_space(&left_result)){
  1314. return left_result;
  1315. }
  1316. }
  1317. right_result = traverse((*the_statement).code.operation.right_exp, the_var, false);
  1318. if(is_error(&right_result)){ // Name Error错误
  1319. return right_result;
  1320. }
  1321. else if(right_result.u != return_def && is_space(&right_result)){
  1322. return right_result;
  1323. }
  1324. switch (func_type) // 获取运算类型
  1325. {
  1326. case ADD_func:
  1327. value = add_func(left_result, right_result, the_var);
  1328. break;
  1329. case SUB_func:
  1330. value = sub_func(left_result, right_result, the_var);
  1331. break;
  1332. case MUL_func:
  1333. value = mul_func(left_result, right_result, the_var);
  1334. break;
  1335. case DIV_func:
  1336. value = div_func(left_result, right_result, the_var);
  1337. break;
  1338. case NEGATIVE_func:
  1339. value = negative_func(right_result, the_var);
  1340. break;
  1341. case ASSIGMENT_func:{ // because the var char, we should ues a {} to make a block[name space] for the tmp var;
  1342. if((the_statement->code.operation.left_exp)->type == base_var){ // 通过base_var赋值
  1343. char *left = (the_statement->code.operation.left_exp)->code.base_var.var_name; // get var name but not value
  1344. int from = 0;
  1345. if((the_statement->code.operation.left_exp)->code.base_var.from == NULL){
  1346. from = 0;
  1347. }
  1348. else{
  1349. GWARF_result tmp_result, tmp_object = traverse((the_statement->code.operation.left_exp)->code.base_var.from, the_var, false);
  1350. if(is_error(&tmp_object)){ // Name Error错误
  1351. // puts("STOP:: Name No Found!");
  1352. return tmp_object;
  1353. }
  1354. else if(is_space(&tmp_object)){
  1355. return tmp_object;
  1356. }
  1357. tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
  1358. if(tmp_result.value.type == INT_value){
  1359. from = tmp_result.value.value.int_value;
  1360. }
  1361. else if(tmp_result.value.type == NUMBER_value){
  1362. from = (int)tmp_result.value.value.double_value;
  1363. }
  1364. else{
  1365. from = 0;
  1366. }
  1367. }
  1368. value = assigment_func(left, right_result, login_var, from);
  1369. }
  1370. else if((the_statement->code.operation.left_exp)->type == point){ // 通过point赋值
  1371. GWARF_result tmp_result = traverse((the_statement->code.operation.left_exp)->code.point.base_var, the_var, false); // 不用取value
  1372. if(is_error(&tmp_result)){ // Name Error错误
  1373. // puts("STOP:: Name No Found!");
  1374. return tmp_result;
  1375. }
  1376. else if(is_space(&tmp_result)){
  1377. return tmp_result;
  1378. }
  1379. GWARF_value base_the_var = tmp_result.value; // 不用取value
  1380. if(((the_statement->code.operation.left_exp)->code.point.child_var)->type == base_var){
  1381. char *left = ((the_statement->code.operation.left_exp)->code.point.child_var)->code.base_var.var_name;
  1382. int from = 0;
  1383. if(((the_statement->code.operation.left_exp)->code.point.child_var)->code.base_var.from == NULL){
  1384. from = 0;
  1385. }
  1386. else{
  1387. GWARF_result tmp_result, tmp_object = traverse(((the_statement->code.operation.left_exp)->code.point.child_var)->code.base_var.from, the_var, false);
  1388. if(is_error(&tmp_object)){ // Name Error错误
  1389. // puts("STOP:: Name No Found!");
  1390. return tmp_object;
  1391. }
  1392. else if(is_space(&tmp_object)){
  1393. return tmp_object;
  1394. }
  1395. tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
  1396. if(tmp_result.value.type == INT_value){
  1397. from = tmp_result.value.value.int_value;
  1398. }
  1399. else if(tmp_result.value.type == NUMBER_value){
  1400. from = (int)tmp_result.value.value.double_value;
  1401. }
  1402. else{
  1403. from = 0;
  1404. }
  1405. }
  1406. value = assigment_func(left, right_result, base_the_var.value.object_value->the_var, from);
  1407. }
  1408. else{
  1409. goto the_else;
  1410. }
  1411. }
  1412. else if((the_statement->code.operation.left_exp)->type == down){ // 通过down赋值
  1413. GWARF_result tmp_result = traverse((the_statement->code.operation.left_exp)->code.down.base_var, the_var, false), get; // 不用取value
  1414. if(is_error(&tmp_result)){ // Name Error错误
  1415. // puts("STOP:: Name No Found!");
  1416. return tmp_result;
  1417. }
  1418. else if(is_space(&tmp_result)){
  1419. return tmp_result;
  1420. }
  1421. GWARF_value base_the_var = tmp_result.value; // 不用取value
  1422. if(base_the_var.type == CLASS_value){ // is class so that can use "."
  1423. GWARF_result child_value = traverse((the_statement->code.operation.left_exp)->code.down.child_var, the_var, false); // 作为参数
  1424. var *tmp = find_var(base_the_var.value.class_value->the_var, 0, "__set__");
  1425. if(tmp != NULL){
  1426. get.value = tmp->value;
  1427. get.father = &base_the_var; // 设置father
  1428. parameter *tmp = pack_value_parameter(child_value.value);
  1429. tmp->next = pack_value_parameter(right_result.value);
  1430. value = call_back_core(get, the_var, tmp);
  1431. goto the_else;
  1432. }
  1433. }
  1434. else if(base_the_var.type == OBJECT_value){
  1435. GWARF_result child_value = traverse((the_statement->code.operation.left_exp)->code.down.child_var, the_var, false); // 作为参数
  1436. var *tmp = find_var(base_the_var.value.object_value->the_var, 0, "__set__");
  1437. if(tmp != NULL){
  1438. get.value = tmp->value;
  1439. get.father = &base_the_var; // 设置father
  1440. parameter *tmp = pack_value_parameter(child_value.value);
  1441. tmp->next = pack_value_parameter(right_result.value);
  1442. value = call_back_core(get, the_var, tmp);
  1443. goto the_else;
  1444. }
  1445. }
  1446. else{
  1447. goto the_else;
  1448. }
  1449. }
  1450. else{
  1451. the_else: puts("Bad Assigment");
  1452. }
  1453. break;
  1454. }
  1455. case EQUAL_func:
  1456. value = equal_func(left_result, right_result, the_var, 0);
  1457. break;
  1458. case MORE_func:
  1459. value = equal_func(left_result, right_result, the_var, 1);
  1460. break;
  1461. case LESS_func:
  1462. value = equal_func(left_result, right_result, the_var, 2);
  1463. break;
  1464. case MOREEQ_func:
  1465. value = equal_func(left_result, right_result, the_var, 3);
  1466. break;
  1467. case LESSEQ_func:
  1468. value = equal_func(left_result, right_result, the_var, 4);
  1469. break;
  1470. case NOTEQ_func:
  1471. value = equal_func(left_result, right_result, the_var, 5);
  1472. break;
  1473. case POW_func:
  1474. value = pow_func(left_result, right_result, the_var);
  1475. break;
  1476. case LOG_func:
  1477. value = log_func(left_result, right_result, the_var);
  1478. break;
  1479. case SQRT_func:
  1480. value = sqrt_func(left_result, right_result, the_var);
  1481. break;
  1482. default:
  1483. break;
  1484. }
  1485. value.u = statement_end; // 正常设置[正常语句结束]
  1486. value.value = to_object(value.value, the_var); // 返回类型是object[不下放到add func等]
  1487. return value;
  1488. }
  1489. GWARF_result call_back(statement *the_statement, var_list *the_var){ // the func for add and call from read_statement_list
  1490. GWARF_result get = traverse(the_statement->code.call.func, the_var, false), result;
  1491. if(is_error(&get)){ // Name Error错误
  1492. // puts("STOP:: Name No Found!");
  1493. return get;
  1494. }
  1495. else if(is_space(&get)){
  1496. return get;
  1497. }
  1498. result = call_back_core(get, the_var, the_statement->code.call.parameter_list);
  1499. result.value = to_object(result.value, the_var); // 返回类型是object
  1500. return result;
  1501. }
  1502. GWARF_result call_back_core(GWARF_result get, var_list *the_var, parameter *tmp_s){ // the func for add and call from read_statement_list
  1503. GWARF_result result;
  1504. var_list *old_var_list = the_var;
  1505. if(get.value.type == FUNC_value){
  1506. func *func_ = get.value.value.func_value;
  1507. parameter *tmp_x = func_->parameter_list;
  1508. the_var = func_->the_var;
  1509. // tmp_x:形参,tmp_s:实参
  1510. // printf("----address = %d----\n", the_var);
  1511. var *tmp = make_var(); // base_var
  1512. the_var = append_var_list(tmp, the_var);
  1513. // printf("----new address = %d----\n", the_var);
  1514. if(func_->type == customize){ // 用户定义的方法
  1515. if(tmp_x == NULL){
  1516. goto no_tmp_x; // 无形参
  1517. }
  1518. GWARF_result father;
  1519. if(func_->is_class == 1){
  1520. father.value = *(get.father);
  1521. assigment_func(tmp_x->u.name, father, the_var, 0);
  1522. if (tmp_x->next == NULL){ // the last
  1523. goto no_tmp_x;
  1524. }
  1525. tmp_x = tmp_x->next; // get the next to iter
  1526. }
  1527. while(1){
  1528. GWARF_result tmp = traverse(tmp_s->u.value, the_var, false); // 不需要取__value__
  1529. if(is_error(&tmp)){ // Name Error错误
  1530. // puts("STOP:: Name No Found!");
  1531. the_var = free_var_list(the_var); // free the new var
  1532. return tmp;
  1533. }
  1534. else if(is_space(&tmp)){
  1535. the_var = free_var_list(the_var); // free the new var
  1536. return tmp;
  1537. }
  1538. assigment_func(tmp_x->u.name, tmp, the_var, 0);
  1539. if ((tmp_x->next == NULL)||(tmp_s->next == NULL)){ // the last
  1540. break;
  1541. }
  1542. tmp_x = tmp_x->next; // get the next to iter
  1543. tmp_s = tmp_s->next;
  1544. }
  1545. no_tmp_x:
  1546. puts("----start func----");
  1547. result = traverse(func_->done, the_var, false); // 执行func_value->done
  1548. if(is_error(&result)){ // Name Error错误
  1549. // puts("STOP:: Name No Found!");
  1550. the_var = free_var_list(the_var); // free the new var
  1551. return result;
  1552. }
  1553. else if(is_space(&result)){
  1554. the_var = free_var_list(the_var); // free the new var
  1555. return result;
  1556. }
  1557. puts("----stop start func----");
  1558. }
  1559. else{
  1560. result = func_->paser(func_, tmp_s, the_var, get, old_var_list);
  1561. }
  1562. the_var = free_var_list(the_var); // free the new var
  1563. }
  1564. else if(get.value.type == CLASS_value){ // 生成实例
  1565. the_object *object_tmp = malloc(sizeof(the_object)); // 生成object的空间
  1566. object_tmp->cls = get.value.value.class_value->the_var;
  1567. object_tmp->the_var = append_by_var_list(make_var_base(make_var()), object_tmp->cls);
  1568. GWARF_value tmp;
  1569. tmp.type = OBJECT_value;
  1570. tmp.value.object_value = object_tmp;
  1571. // 执行__init__
  1572. var *init_tmp = find_var(object_tmp->cls, 0, "__init__");
  1573. if(init_tmp != NULL){ // 找到了__init__
  1574. func *func_ = init_tmp->value.value.func_value;
  1575. parameter *tmp_x = func_->parameter_list;
  1576. the_var = func_->the_var;
  1577. // tmp_x:形参,tmp_s:实参
  1578. // // printf("----address = %d----\n", the_var);
  1579. var *tmp = make_var(); // base_var
  1580. the_var = append_var_list(tmp, the_var);
  1581. // // printf("----new address = %d----\n", the_var);
  1582. if(func_->type == customize){ // 用户定义的方法
  1583. if(tmp_x == NULL){
  1584. puts("No tmp_x");
  1585. goto no_tmp_x_init; // 无形参
  1586. }
  1587. GWARF_result father;
  1588. father.value.type = OBJECT_value;
  1589. father.value.value.object_value = object_tmp;
  1590. if(func_->is_class == 1){
  1591. assigment_func(tmp_x->u.name, father, the_var, 0);
  1592. if (tmp_x->next == NULL){ // the last
  1593. goto no_tmp_x_init;
  1594. }
  1595. tmp_x = tmp_x->next; // get the next to iter
  1596. }
  1597. while(1){
  1598. GWARF_result tmp = traverse(tmp_s->u.value, the_var, false);
  1599. if(is_error(&tmp)){ // Name Error错误
  1600. // puts("STOP:: Name No Found!");
  1601. the_var = free_var_list(the_var); // free the new var
  1602. return tmp;
  1603. }
  1604. else if(is_space(&tmp)){
  1605. the_var = free_var_list(the_var); // free the new var
  1606. return tmp;
  1607. }
  1608. assigment_func(tmp_x->u.name, tmp, the_var, 0);
  1609. if ((tmp_x->next == NULL)||(tmp_s->next == NULL)){ // the last
  1610. break;
  1611. }
  1612. tmp_x = tmp_x->next; // get the next to iter
  1613. tmp_s = tmp_s->next;
  1614. }
  1615. no_tmp_x_init:
  1616. puts("----start func----");
  1617. {
  1618. GWARF_result tmp = traverse(func_->done, the_var, false); // 执行func_value->done
  1619. if(is_error(&tmp)){ // Name Error错误
  1620. // puts("STOP:: Name No Found!");\
  1621. the_var = free_var_list(the_var); // free the new var
  1622. return tmp;
  1623. }
  1624. else if(is_space(&tmp)){
  1625. the_var = free_var_list(the_var); // free the new var
  1626. return tmp;
  1627. }
  1628. }
  1629. puts("----stop start func----");
  1630. }
  1631. else{
  1632. GWARF_result tmp_get;
  1633. GWARF_value father;
  1634. father.type = OBJECT_value;
  1635. father.value.object_value = object_tmp;
  1636. tmp_get.father = &father;
  1637. GWARF_result tmp = func_->paser(func_, tmp_s, the_var, tmp_get, old_var_list); // 返回值不记录
  1638. if(is_error(&tmp)){ // Name Error错误
  1639. the_var = free_var_list(the_var); // free the new var
  1640. // puts("STOP:: Name No Found!");
  1641. return tmp;
  1642. }
  1643. else if(is_space(&tmp)){
  1644. the_var = free_var_list(the_var); // free the new var
  1645. return tmp;
  1646. }
  1647. }
  1648. the_var = free_var_list(the_var); // free the new var
  1649. }
  1650. // 记录返回值
  1651. result.u = statement_end;
  1652. result.value = tmp;
  1653. }
  1654. else if(get.value.type == OBJECT_value){ // 调用__call__方法
  1655. // 执行__init__
  1656. var *call_tmp = find_var(get.value.value.object_value->the_var, 0, "__call__");
  1657. if(call_tmp != NULL){ // 找到了__init__
  1658. func *func_ = call_tmp->value.value.func_value;
  1659. parameter *tmp_x = func_->parameter_list;
  1660. the_var = func_->the_var;
  1661. // tmp_x:形参,tmp_s:实参
  1662. // // printf("----address = %d----\n", the_var);
  1663. var *tmp = make_var(); // base_var
  1664. the_var = append_var_list(tmp, the_var);
  1665. // // printf("----new address = %d----\n", the_var);
  1666. if(func_->type == customize){ // 用户定义的方法
  1667. if(tmp_x == NULL){
  1668. puts("No tmp_x");
  1669. goto no_tmp_x_call; // 无形参
  1670. }
  1671. GWARF_result father;
  1672. father.value.type = OBJECT_value;
  1673. father.value.value.object_value = get.value.value.object_value;
  1674. if(func_->is_class == 1){
  1675. assigment_func(tmp_x->u.name, father, the_var, 0);
  1676. if (tmp_x->next == NULL){ // the last
  1677. goto no_tmp_x_call;
  1678. }
  1679. tmp_x = tmp_x->next; // get the next to iter
  1680. }
  1681. while(1){
  1682. GWARF_result tmp = traverse(tmp_s->u.value, the_var, false);
  1683. if(is_error(&tmp)){ // Name Error错误
  1684. // puts("STOP:: Name No Found!");
  1685. the_var = free_var_list(the_var); // free the new var
  1686. return tmp;
  1687. }
  1688. else if(is_space(&tmp)){
  1689. the_var = free_var_list(the_var); // free the new var
  1690. return tmp;
  1691. }
  1692. assigment_func(tmp_x->u.name, tmp, the_var, 0);
  1693. if ((tmp_x->next == NULL)||(tmp_s->next == NULL)){ // the last
  1694. break;
  1695. }
  1696. tmp_x = tmp_x->next; // get the next to iter
  1697. tmp_s = tmp_s->next;
  1698. }
  1699. no_tmp_x_call:
  1700. puts("----start func----");
  1701. {
  1702. GWARF_result tmp = traverse(func_->done, the_var, false); // 执行func_value->done
  1703. if(is_error(&tmp)){ // Name Error错误
  1704. // puts("STOP:: Name No Found!");\
  1705. the_var = free_var_list(the_var); // free the new var
  1706. return tmp;
  1707. }
  1708. else if(is_space(&tmp)){
  1709. the_var = free_var_list(the_var); // free the new var
  1710. return tmp;
  1711. }
  1712. }
  1713. puts("----stop start func----");
  1714. }
  1715. else{
  1716. GWARF_result tmp_get;
  1717. GWARF_value father;
  1718. father.type = OBJECT_value;
  1719. father.value.object_value = get.value.value.object_value;
  1720. tmp_get.father = &father;
  1721. GWARF_result tmp = func_->paser(func_, tmp_s, the_var, tmp_get, old_var_list); // 返回值不记录
  1722. if(is_error(&tmp)){ // Name Error错误
  1723. the_var = free_var_list(the_var); // free the new var
  1724. // puts("STOP:: Name No Found!");
  1725. return tmp;
  1726. }
  1727. else if(is_space(&tmp)){
  1728. the_var = free_var_list(the_var); // free the new var
  1729. return tmp;
  1730. }
  1731. }
  1732. the_var = free_var_list(the_var); // free the new var
  1733. }
  1734. }
  1735. if(result.u == return_def){
  1736. if(result.return_times <= 0){
  1737. result.u = statement_end;
  1738. }
  1739. else{
  1740. result.return_times -= 1;
  1741. }
  1742. }
  1743. return result;
  1744. }
  1745. // --------- ADD
  1746. GWARF_result add_func(GWARF_result left_result, GWARF_result right_result, var_list *the_var){ // the func for add and call from read_statement_list
  1747. GWARF_result return_value, get; // the result by call read_statement_list with left and right; value is the result for add
  1748. if(left_result.value.type == OBJECT_value){ // 调用左add方法
  1749. GWARF_result get;
  1750. GWARF_value base_the_var = left_result.value; // 只有一个参数
  1751. var_list *call_var = base_the_var.value.object_value->the_var;
  1752. var *tmp = find_var(call_var, 0, "__add__");
  1753. if(tmp != NULL){
  1754. get.value = tmp->value;
  1755. get.father = &base_the_var; // 设置father
  1756. return_value = call_back_core(get, the_var, pack_value_parameter(right_result.value));
  1757. goto return_back;
  1758. }
  1759. // goto next if
  1760. }
  1761. if(right_result.value.type == OBJECT_value){ // 调用右add方法
  1762. GWARF_result get;
  1763. GWARF_value base_the_var = right_result.value; // 只有一个参数
  1764. var_list *call_var = base_the_var.value.object_value->the_var;
  1765. var *tmp = find_var(call_var, 0, "__add__");
  1766. if(tmp != NULL){
  1767. get.value = tmp->value;
  1768. get.father = &base_the_var; // 设置father
  1769. return_value = call_back_core(get, the_var, pack_value_parameter(left_result.value));
  1770. }
  1771. else{
  1772. return_value.u = statement_end;
  1773. return_value.value = left_result.value;
  1774. }
  1775. goto return_back;
  1776. }
  1777. {
  1778. // 理论上用户是不可以直接调用下面的基类的,计算过程中万物皆类
  1779. if(left_result.value.type == NULL_value){
  1780. return_value.u = statement_end;
  1781. return_value.value = right_result.value; // NULL加法相当于0
  1782. }
  1783. else if(right_result.value.type == NULL_value){
  1784. return_value.u = statement_end;
  1785. return_value.value = left_result.value; // NULL加法相当于0
  1786. }
  1787. else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is INT
  1788. return_value.u = statement_end;
  1789. return_value.value.type = INT_value;
  1790. return_value.value.value.int_value = (int)(left_result.value.value.int_value + right_result.value.value.int_value);
  1791. }
  1792. else if((left_result.value.type == NUMBER_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
  1793. return_value.u = statement_end;
  1794. return_value.value.type = NUMBER_value;
  1795. return_value.value.value.double_value = (double)(left_result.value.value.double_value + right_result.value.value.double_value);
  1796. }
  1797. else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
  1798. return_value.u = statement_end;
  1799. return_value.value.type = NUMBER_value;
  1800. return_value.value.value.double_value = (double)(left_result.value.value.int_value + right_result.value.value.double_value);
  1801. }
  1802. else if((left_result.value.type == NUMBER_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is NUMBER
  1803. return_value.u = statement_end;
  1804. return_value.value.type = NUMBER_value;
  1805. return_value.value.value.double_value = (double)(left_result.value.value.double_value + right_result.value.value.int_value);
  1806. }
  1807. else if((left_result.value.type == STRING_value) && (right_result.value.type == STRING_value)){ // all is NUMBER
  1808. return_value.u = statement_end;
  1809. return_value.value.type = STRING_value;
  1810. char *l = left_result.value.value.string;
  1811. char *r = right_result.value.value.string;
  1812. return_value.value.value.string = malloc(strlen(l) + strlen(r)); // 创建新空间
  1813. strcpy(return_value.value.value.string, l); // 复制字符串
  1814. strcat(return_value.value.value.string, r); // 追加字符串
  1815. }
  1816. }
  1817. return_back: return return_value;
  1818. }
  1819. // --------- SUB
  1820. GWARF_result sub_func(GWARF_result left_result, GWARF_result right_result, var_list *the_var){ // the func for sub and call from read_statement_list
  1821. GWARF_result return_value; // the result by call read_statement_list with left and right; value is the result for sub
  1822. if(left_result.value.type == OBJECT_value){ // 调用左sub方法
  1823. GWARF_result get;
  1824. GWARF_value base_the_var = left_result.value; // 只有一个参数
  1825. var_list *call_var = base_the_var.value.object_value->the_var;
  1826. var *tmp = find_var(call_var, 0, "__sub__");
  1827. if(tmp != NULL){
  1828. get.value = tmp->value;
  1829. get.father = &base_the_var; // 设置father
  1830. return_value = call_back_core(get, the_var, pack_value_parameter(right_result.value));
  1831. goto return_back;
  1832. }
  1833. }
  1834. if(right_result.value.type == OBJECT_value){ // 调用右sub方法
  1835. GWARF_result get;
  1836. GWARF_value base_the_var = right_result.value; // 只有一个参数
  1837. var_list *call_var = base_the_var.value.object_value->the_var;
  1838. var *tmp = find_var(call_var, 0, "__sub__");
  1839. if(tmp != NULL){
  1840. get.value = tmp->value;
  1841. get.father = &base_the_var; // 设置father
  1842. return_value = call_back_core(get, the_var, pack_value_parameter(left_result.value));
  1843. }
  1844. else{
  1845. return_value.u = statement_end;
  1846. return_value.value = left_result.value;
  1847. }
  1848. goto return_back;
  1849. }
  1850. {
  1851. if(left_result.value.type == NULL_value){
  1852. return_value.u = statement_end;
  1853. return negative_func(right_result, the_var); // NULL减法相当于0
  1854. }
  1855. else if(right_result.value.type == NULL_value){
  1856. return_value.u = statement_end;
  1857. return_value.value = left_result.value; // NULL减法相当于0
  1858. }
  1859. else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is INT
  1860. return_value.u = statement_end;
  1861. return_value.value.type = INT_value;
  1862. return_value.value.value.int_value = (int)(left_result.value.value.int_value - right_result.value.value.int_value);
  1863. }
  1864. else if((left_result.value.type == NUMBER_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
  1865. return_value.u = statement_end;
  1866. return_value.value.type = NUMBER_value;
  1867. return_value.value.value.double_value = (double)(left_result.value.value.double_value - right_result.value.value.double_value);
  1868. }
  1869. else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
  1870. return_value.u = statement_end;
  1871. return_value.value.type = NUMBER_value;
  1872. return_value.value.value.double_value = (double)(left_result.value.value.int_value - right_result.value.value.double_value);
  1873. }
  1874. else if((left_result.value.type == NUMBER_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is NUMBER
  1875. return_value.u = statement_end;
  1876. return_value.value.type = NUMBER_value;
  1877. return_value.value.value.double_value = (double)(left_result.value.value.double_value - right_result.value.value.int_value);
  1878. }
  1879. }
  1880. return_back: return return_value;
  1881. }
  1882. // --------- negative
  1883. GWARF_result negative_func(GWARF_result right_result, var_list *the_var){ // the func for negative and call from read_statement_list
  1884. GWARF_result return_value; // the result by call read_statement_list with left and right; value is the result for negative
  1885. if(right_result.value.type == OBJECT_value){ // 调用右sub方法
  1886. GWARF_result get;
  1887. GWARF_value base_the_var = right_result.value; // 只有一个参数
  1888. var_list *call_var = base_the_var.value.object_value->the_var;
  1889. var *tmp = find_var(call_var, 0, "__negative__");
  1890. if(tmp != NULL){
  1891. get.value = tmp->value;
  1892. get.father = &base_the_var; // 设置father
  1893. return_value = call_back_core(get, the_var, pack_value_parameter(right_result.value));
  1894. }
  1895. else{
  1896. return_value.u = statement_end;
  1897. return_value.value.type = BOOL_value;
  1898. return_value.value.value.bool_value = true;
  1899. }
  1900. }
  1901. else{
  1902. if(right_result.value.type == NULL_value){ // 返回bool true
  1903. return_value.u = statement_end;
  1904. return_value.value.type = BOOL_value;
  1905. return_value.value.value.bool_value = true;
  1906. }
  1907. else if(right_result.value.type == INT_value){ // all is INT
  1908. return_value.u = statement_end;
  1909. return_value.value.type = INT_value;
  1910. return_value.value.value.int_value = (int)(-1 * right_result.value.value.int_value);
  1911. }
  1912. else if(right_result.value.type == BOOL_value){
  1913. return_value.u = statement_end;
  1914. return_value.value.type = BOOL_value;
  1915. if(right_result.value.value.bool_value)
  1916. {
  1917. return_value.value.value.bool_value = false;
  1918. }
  1919. else{
  1920. return_value.value.value.bool_value = true;
  1921. }
  1922. }
  1923. else if(right_result.value.type == NUMBER_value){ // all is NUMBER
  1924. return_value.u = statement_end;
  1925. return_value.value.type = NUMBER_value;
  1926. return_value.value.value.double_value = (double)(-1 * right_result.value.value.double_value);
  1927. }
  1928. else if(right_result.value.type == STRING_value){ // 字符串
  1929. return_value.u = statement_end;
  1930. return_value.value.type = STRING_value;
  1931. char *r = right_result.value.value.string;
  1932. return_value.value.value.string = malloc(strlen(r)); // 创建新空间
  1933. char *tmp = malloc(strlen(r));
  1934. strcpy(tmp, r); // 复制字符串
  1935. for(int i=0;i<strlen(tmp);i += 1){
  1936. return_value.value.value.string[i] = tmp[strlen(tmp) - i - 1]; // 反转
  1937. }
  1938. }
  1939. }
  1940. return_back: return return_value;
  1941. }
  1942. // --------- MUL
  1943. GWARF_result mul_func(GWARF_result left_result, GWARF_result right_result, var_list *the_var){ // the func for mul and call from read_statement_list
  1944. GWARF_result return_value; // the result by call read_statement_list with left and right; value is the result for mul
  1945. if(left_result.value.type == OBJECT_value){ // 调用左mul方法
  1946. GWARF_result get;
  1947. GWARF_value base_the_var = left_result.value; // 只有一个参数
  1948. var_list *call_var = base_the_var.value.object_value->the_var;
  1949. var *tmp = find_var(call_var, 0, "__mul__");
  1950. if(tmp != NULL){
  1951. get.value = tmp->value;
  1952. get.father = &base_the_var; // 设置father
  1953. return_value = call_back_core(get, the_var, pack_value_parameter(right_result.value));
  1954. goto return_back;
  1955. }
  1956. }
  1957. if(right_result.value.type == OBJECT_value){ // 调用右mul方法
  1958. GWARF_result get;
  1959. GWARF_value base_the_var = right_result.value; // 只有一个参数
  1960. var_list *call_var = base_the_var.value.object_value->the_var;
  1961. var *tmp = find_var(call_var, 0, "__mul__");
  1962. if(tmp != NULL){
  1963. get.value = tmp->value;
  1964. get.father = &base_the_var; // 设置father
  1965. return_value = call_back_core(get, the_var, pack_value_parameter(left_result.value));
  1966. }
  1967. else{
  1968. return_value.u = statement_end;
  1969. return_value.value = left_result.value;
  1970. }
  1971. goto return_back;
  1972. }
  1973. {
  1974. if(left_result.value.type == NULL_value){
  1975. return_value.value = right_result.value; // NULL乘法相当于1
  1976. }
  1977. else if(right_result.value.type == NULL_value){
  1978. return_value.value = left_result.value; // NULL乘法相当于1
  1979. }
  1980. else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is INT
  1981. return_value.u = statement_end;
  1982. return_value.value.type = INT_value;
  1983. return_value.value.value.int_value = (int)(left_result.value.value.int_value * right_result.value.value.int_value);
  1984. }
  1985. else if((left_result.value.type == NUMBER_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
  1986. return_value.u = statement_end;
  1987. return_value.value.type = NUMBER_value;
  1988. return_value.value.value.double_value = (double)(left_result.value.value.double_value * right_result.value.value.double_value);
  1989. }
  1990. else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
  1991. return_value.u = statement_end;
  1992. return_value.value.type = NUMBER_value;
  1993. return_value.value.value.double_value = (double)(left_result.value.value.int_value * right_result.value.value.double_value);
  1994. }
  1995. else if((left_result.value.type == NUMBER_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is NUMBER
  1996. return_value.u = statement_end;
  1997. return_value.value.type = NUMBER_value;
  1998. return_value.value.value.double_value = (double)(left_result.value.value.double_value * right_result.value.value.int_value);
  1999. }
  2000. else if((left_result.value.type == INT_value) && (right_result.value.type == STRING_value)){ // all is NUMBER
  2001. return_value.u = statement_end;
  2002. return_value.value.type = STRING_value;
  2003. int l = left_result.value.value.int_value;
  2004. char *r = right_result.value.value.string;
  2005. if(l == 0){
  2006. return_value.value.value.string = (char *)malloc(0); // NULL string
  2007. }
  2008. else if(l > 0){
  2009. return_value.value.value.string = malloc(strlen(r) * l); // 创建新空间
  2010. strcpy(return_value.value.value.string, r); // 复制字符串
  2011. l -= 1;
  2012. for(;l>0;l -= 1){
  2013. strcat(return_value.value.value.string, r); // 追加字符串
  2014. }
  2015. }
  2016. else{
  2017. return_value.value.value.string = malloc(strlen(r) * (-l)); // 创建新空间
  2018. char *tmp = malloc(strlen(r) * (-l));
  2019. strcpy(tmp, r); // 复制字符串
  2020. l += 1;
  2021. for(;l<0;l += 1){
  2022. strcat(tmp, r); // 追加字符串
  2023. }
  2024. for(int i=0;i<strlen(tmp);i += 1){
  2025. return_value.value.value.string[i] = tmp[strlen(tmp) - i - 1]; // 反转
  2026. }
  2027. free(tmp);
  2028. }
  2029. }
  2030. else if((left_result.value.type == STRING_value) && (right_result.value.type == INT_value)){ // all is NUMBER
  2031. return_value.u = statement_end;
  2032. return_value.value.type = STRING_value;
  2033. int l = right_result.value.value.int_value;
  2034. char *r = left_result.value.value.string;
  2035. if(l == 0){
  2036. return_value.value.value.string = (char *)malloc(0); // NULL string
  2037. }
  2038. else if(l > 0){
  2039. return_value.value.value.string = malloc(strlen(r) * l); // 创建新空间
  2040. strcpy(return_value.value.value.string, r); // 复制字符串
  2041. l -= 1;
  2042. for(;l>0;l -= 1){
  2043. strcat(return_value.value.value.string, r); // 追加字符串
  2044. }
  2045. }
  2046. else{
  2047. return_value.value.value.string = malloc(strlen(r) * (-l)); // 创建新空间
  2048. char *tmp = malloc(strlen(r) * (-l));
  2049. strcpy(tmp, r); // 复制字符串
  2050. l += 1;
  2051. for(;l<0;l += 1){
  2052. strcat(tmp, r); // 追加字符串
  2053. }
  2054. for(int i=0;i<strlen(tmp);i += 1){
  2055. return_value.value.value.string[i] = tmp[strlen(tmp) - i - 1]; // 反转
  2056. }
  2057. free(tmp);
  2058. }
  2059. }
  2060. }
  2061. return_back: return return_value;
  2062. }
  2063. // --------- DIV
  2064. GWARF_result div_func(GWARF_result left_result, GWARF_result right_result, var_list *the_var){ // the func for div and call from read_statement_list
  2065. GWARF_result return_value; // the result by call read_statement_list with left and right; value is the result for div
  2066. if(left_result.value.type == OBJECT_value){ // 调用左div方法
  2067. GWARF_result get;
  2068. GWARF_value base_the_var = left_result.value; // 只有一个参数
  2069. var_list *call_var = base_the_var.value.object_value->the_var;
  2070. var *tmp = find_var(call_var, 0, "__div__");
  2071. if(tmp != NULL){
  2072. get.value = tmp->value;
  2073. get.father = &base_the_var; // 设置father
  2074. return_value = call_back_core(get, the_var, pack_value_parameter(right_result.value));
  2075. goto return_back;
  2076. }
  2077. }
  2078. if(right_result.value.type == OBJECT_value){ // 调用右div方法
  2079. GWARF_result get;
  2080. GWARF_value base_the_var = right_result.value; // 只有一个参数
  2081. var_list *call_var = base_the_var.value.object_value->the_var;
  2082. var *tmp = find_var(call_var, 0, "__div__");
  2083. if(tmp != NULL){
  2084. get.value = tmp->value;
  2085. get.father = &base_the_var; // 设置father
  2086. return_value = call_back_core(get, the_var, pack_value_parameter(left_result.value));
  2087. }
  2088. else{
  2089. return_value.u = statement_end;
  2090. return_value.value = left_result.value;
  2091. }
  2092. goto return_back;
  2093. }
  2094. {
  2095. if(left_result.value.type == NULL_value){
  2096. left_result.value.type = INT_value;
  2097. left_result.value.value.int_value = 1;
  2098. }
  2099. else if(right_result.value.type == NULL_value){
  2100. return_value.value = left_result.value; // NULL除发相当于1
  2101. goto return_result;
  2102. }
  2103. // 此处不是else if
  2104. if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is INT
  2105. return_value.u = statement_end;
  2106. return_value.value.type = NUMBER_value; // 除 无int
  2107. return_value.value.value.double_value = ((double)left_result.value.value.int_value / (double)right_result.value.value.int_value);
  2108. }
  2109. else if((left_result.value.type == NUMBER_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
  2110. return_value.u = statement_end;
  2111. return_value.value.type = NUMBER_value;
  2112. return_value.value.value.double_value = (left_result.value.value.double_value / right_result.value.value.double_value);
  2113. }
  2114. else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
  2115. return_value.u = statement_end;
  2116. return_value.value.type = NUMBER_value;
  2117. return_value.value.value.double_value = ((double)left_result.value.value.int_value / right_result.value.value.double_value);
  2118. }
  2119. else if((left_result.value.type == NUMBER_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is NUMBER
  2120. return_value.u = statement_end;
  2121. return_value.value.type = NUMBER_value;
  2122. return_value.value.value.double_value = (left_result.value.value.double_value / (double)right_result.value.value.int_value);
  2123. }
  2124. }
  2125. return_back: return_result: return return_value;
  2126. }
  2127. // --------- POW
  2128. GWARF_result pow_func(GWARF_result left_result, GWARF_result right_result, var_list *the_var){ // the func for div and call from read_statement_list
  2129. GWARF_result return_value; // the result by call read_statement_list with left and right; value is the result for div
  2130. if(left_result.value.type == OBJECT_value){ // 调用左pow方法
  2131. GWARF_result get;
  2132. GWARF_value base_the_var = left_result.value; // 只有一个参数
  2133. var_list *call_var = base_the_var.value.object_value->the_var;
  2134. var *tmp = find_var(call_var, 0, "__pow__");
  2135. if(tmp != NULL){
  2136. get.value = tmp->value;
  2137. get.father = &base_the_var; // 设置father
  2138. return_value = call_back_core(get, the_var, pack_value_parameter(right_result.value));
  2139. goto return_back;
  2140. }
  2141. }
  2142. if(right_result.value.type == OBJECT_value){ // 调用右pow方法
  2143. GWARF_result get;
  2144. GWARF_value base_the_var = right_result.value; // 只有一个参数
  2145. var_list *call_var = base_the_var.value.object_value->the_var;
  2146. var *tmp = find_var(call_var, 0, "__pow__");
  2147. if(tmp != NULL){
  2148. get.value = tmp->value;
  2149. get.father = &base_the_var; // 设置father
  2150. return_value = call_back_core(get, the_var, pack_value_parameter(left_result.value));
  2151. }
  2152. else{
  2153. return_value.u = statement_end;
  2154. return_value.value = left_result.value;
  2155. }
  2156. goto return_back;
  2157. }
  2158. {
  2159. if(left_result.value.type == NULL_value){
  2160. return_value.u = statement_end;
  2161. return_value.value.type = INT_value;
  2162. return_value.value.value.int_value = 1;
  2163. }
  2164. else if(right_result.value.type == NULL_value){
  2165. return_value.value = left_result.value; // NULL乘方相当于1
  2166. }
  2167. else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is INT
  2168. return_value.u = statement_end;
  2169. return_value.value.type = INT_value;
  2170. return_value.value.value.int_value = (int)pow((double)left_result.value.value.int_value, (double)right_result.value.value.int_value);
  2171. }
  2172. else if((left_result.value.type == NUMBER_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
  2173. return_value.u = statement_end;
  2174. return_value.value.type = NUMBER_value;
  2175. return_value.value.value.double_value = (double)pow(left_result.value.value.double_value, right_result.value.value.double_value);
  2176. }
  2177. else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
  2178. return_value.u = statement_end;
  2179. return_value.value.type = NUMBER_value;
  2180. return_value.value.value.double_value = (double)pow((double)left_result.value.value.int_value, (double)right_result.value.value.double_value);
  2181. }
  2182. else if((left_result.value.type == NUMBER_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is NUMBER
  2183. return_value.u = statement_end;
  2184. return_value.value.type = NUMBER_value;
  2185. return_value.value.value.double_value = (double)pow((double)left_result.value.value.double_value, (double)right_result.value.value.int_value);
  2186. }
  2187. }
  2188. return_back: return return_value;
  2189. }
  2190. // --------- LOG
  2191. GWARF_result log_func(GWARF_result left_result, GWARF_result right_result, var_list *the_var){ // the func for div and call from read_statement_list
  2192. GWARF_result return_value; // the result by call read_statement_list with left and right; value is the result for div
  2193. if(left_result.value.type == OBJECT_value){ // 调用左log方法
  2194. GWARF_result get;
  2195. GWARF_value base_the_var = left_result.value; // 只有一个参数
  2196. var_list *call_var = base_the_var.value.object_value->the_var;
  2197. var *tmp = find_var(call_var, 0, "__log__");
  2198. if(tmp != NULL){
  2199. get.value = tmp->value;
  2200. get.father = &base_the_var; // 设置father
  2201. return_value = call_back_core(get, the_var, pack_value_parameter(right_result.value));
  2202. goto return_back;
  2203. }
  2204. }
  2205. if(right_result.value.type == OBJECT_value){ // 调用右log方法
  2206. GWARF_result get;
  2207. GWARF_value base_the_var = right_result.value; // 只有一个参数
  2208. var_list *call_var = base_the_var.value.object_value->the_var;
  2209. var *tmp = find_var(call_var, 0, "__log__");
  2210. if(tmp != NULL){
  2211. get.value = tmp->value;
  2212. get.father = &base_the_var; // 设置father
  2213. return_value = call_back_core(get, the_var, pack_value_parameter(left_result.value));
  2214. }
  2215. else{
  2216. return_value.u = statement_end;
  2217. return_value.value = left_result.value;
  2218. }
  2219. goto return_back;
  2220. }
  2221. {
  2222. if(left_result.value.type == NULL_value){
  2223. return_value.value = left_result.value; // 返回NULL
  2224. }
  2225. else if(right_result.value.type == NULL_value){
  2226. return_value.u = statement_end;
  2227. return_value.value.type = INT_value;
  2228. return_value.value.value.int_value = 0;
  2229. }
  2230. else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is INT
  2231. return_value.u = statement_end;
  2232. return_value.value.type = INT_value;
  2233. return_value.value.value.int_value = (int)log_((double)left_result.value.value.int_value, (double)right_result.value.value.int_value);
  2234. }
  2235. else if((left_result.value.type == NUMBER_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
  2236. return_value.u = statement_end;
  2237. return_value.value.type = NUMBER_value;
  2238. return_value.value.value.double_value = (double)log_(left_result.value.value.double_value, right_result.value.value.double_value);
  2239. }
  2240. else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
  2241. return_value.u = statement_end;
  2242. return_value.value.type = NUMBER_value;
  2243. return_value.value.value.double_value = (double)log_((double)left_result.value.value.int_value, (double)right_result.value.value.double_value);
  2244. }
  2245. else if((left_result.value.type == NUMBER_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is NUMBER
  2246. return_value.u = statement_end;
  2247. return_value.value.type = NUMBER_value;
  2248. return_value.value.value.double_value = (double)log_((double)left_result.value.value.double_value, (double)right_result.value.value.int_value);
  2249. }
  2250. }
  2251. return_back: return return_value;
  2252. }
  2253. // --------- SQRT
  2254. GWARF_result sqrt_func(GWARF_result left_result, GWARF_result right_result, var_list *the_var){ // the func for div and call from read_statement_list
  2255. GWARF_result return_value; // the result by call read_statement_list with left and right; value is the result for div
  2256. if(left_result.value.type == OBJECT_value){ // 调用左sqrt方法
  2257. GWARF_result get;
  2258. GWARF_value base_the_var = left_result.value; // 只有一个参数
  2259. var_list *call_var = base_the_var.value.object_value->the_var;
  2260. var *tmp = find_var(call_var, 0, "__sqrt__");
  2261. if(tmp != NULL){
  2262. get.value = tmp->value;
  2263. get.father = &base_the_var; // 设置father
  2264. return_value = call_back_core(get, the_var, pack_value_parameter(right_result.value));
  2265. goto return_back;
  2266. }
  2267. }
  2268. if(right_result.value.type == OBJECT_value){ // 调用右sqrt方法
  2269. GWARF_result get;
  2270. GWARF_value base_the_var = right_result.value; // 只有一个参数
  2271. var_list *call_var = base_the_var.value.object_value->the_var;
  2272. var *tmp = find_var(call_var, 0, "__sqrt__");
  2273. if(tmp != NULL){
  2274. get.value = tmp->value;
  2275. get.father = &base_the_var; // 设置father
  2276. return_value = call_back_core(get, the_var, pack_value_parameter(left_result.value));
  2277. }
  2278. else{
  2279. return_value.u = statement_end;
  2280. return_value.value = left_result.value;
  2281. }
  2282. goto return_back;
  2283. }
  2284. {
  2285. if(left_result.value.type == NULL_value){
  2286. return_value.u = statement_end;
  2287. return_value.value.type = INT_value;
  2288. return_value.value.value.int_value = 0;
  2289. }
  2290. else if(right_result.value.type == NULL_value){
  2291. return_value.value = right_result.value; // 返回NULL
  2292. }
  2293. else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is INT
  2294. return_value.u = statement_end;
  2295. return_value.value.type = INT_value;
  2296. return_value.value.value.int_value = (int)sqrt_((double)left_result.value.value.int_value, (double)right_result.value.value.int_value);
  2297. }
  2298. else if((left_result.value.type == NUMBER_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
  2299. return_value.u = statement_end;
  2300. return_value.value.type = NUMBER_value;
  2301. return_value.value.value.double_value = (double)sqrt_(left_result.value.value.double_value, right_result.value.value.double_value);
  2302. }
  2303. else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
  2304. return_value.u = statement_end;
  2305. return_value.value.type = NUMBER_value;
  2306. return_value.value.value.double_value = (double)sqrt_((double)left_result.value.value.int_value, (double)right_result.value.value.double_value);
  2307. }
  2308. else if((left_result.value.type == NUMBER_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is NUMBER
  2309. return_value.u = statement_end;
  2310. return_value.value.type = NUMBER_value;
  2311. return_value.value.value.double_value = (double)sqrt_((double)left_result.value.value.double_value, (double)right_result.value.value.int_value);
  2312. }
  2313. }
  2314. return_back: return return_value;
  2315. }
  2316. // --------- ASSIGMENT
  2317. GWARF_result assigment_func(char *left, GWARF_result right_result, var_list *the_var, int from){ // the func for assigment and call from read_statement_list
  2318. add_var(the_var, from, left, right_result.value);
  2319. return right_result;
  2320. }
  2321. // --------- EQUAL
  2322. GWARF_result equal_func(GWARF_result left_result, GWARF_result right_result, var_list *the_var, int type){ // the func for equal and call from read_statement_list
  2323. GWARF_result return_value;
  2324. int return_bool = false;
  2325. char *func_list[] = {"__eq__", "__more__", "__less__", "__eqmore__", "__eqless__","__noteq__"}; // bool 运算符
  2326. if(left_result.value.type == OBJECT_value){ // 调用左div方法
  2327. GWARF_result get;
  2328. GWARF_value base_the_var = left_result.value; // 只有一个参数
  2329. var_list *call_var = base_the_var.value.object_value->the_var;
  2330. var *tmp = find_var(call_var, 0, func_list[type]);
  2331. if(tmp != NULL){
  2332. get.value = tmp->value;
  2333. get.father = &base_the_var; // 设置father
  2334. return_value = call_back_core(get, the_var, pack_value_parameter(right_result.value));
  2335. goto return_back;
  2336. }
  2337. }
  2338. if(right_result.value.type == OBJECT_value){ // 调用右div方法
  2339. GWARF_result get;
  2340. GWARF_value base_the_var = right_result.value; // 只有一个参数
  2341. var_list *call_var = base_the_var.value.object_value->the_var;
  2342. var *tmp = find_var(call_var, 0, func_list[type]);
  2343. if(tmp != NULL){
  2344. get.value = tmp->value;
  2345. get.father = &base_the_var; // 设置father
  2346. return_value = call_back_core(get, the_var, pack_value_parameter(left_result.value));
  2347. }
  2348. else{
  2349. return_value.u = statement_end;
  2350. return_value.value.value.bool_value = true;
  2351. return_value.value.type = BOOL_value;
  2352. }
  2353. goto return_back;
  2354. }
  2355. else{
  2356. return_value.u = statement_end;
  2357. if(left_result.value.type == NULL_value || right_result.value.type == NULL_value){
  2358. return_bool = false; // 无论什么都返回false NULL != NULL
  2359. }
  2360. else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is INT
  2361. if ((left_result.value.value.int_value == right_result.value.value.int_value) && (type == 0)){ // 如果相等:: __eq__
  2362. return_bool = true; // 返回1 否则(默认)为0
  2363. }
  2364. if ((left_result.value.value.int_value > right_result.value.value.int_value) && (type == 1)){ // 如果大于::__more__
  2365. return_bool = true; // 返回1 否则(默认)为0
  2366. }
  2367. if ((left_result.value.value.int_value < right_result.value.value.int_value) && (type == 2)){ // 如果小于::__less__
  2368. return_bool = true; // 返回1 否则(默认)为0
  2369. }
  2370. if ((left_result.value.value.int_value >= right_result.value.value.int_value) && (type == 3)){ // 如果大于等于::__eqmore__
  2371. return_bool = true; // 返回1 否则(默认)为0
  2372. }
  2373. if ((left_result.value.value.int_value <= right_result.value.value.int_value) && (type == 4)){ // 如果小于等于::__eqless__
  2374. return_bool = true; // 返回1 否则(默认)为0
  2375. }
  2376. if ((left_result.value.value.int_value != right_result.value.value.int_value) && (type == 5)){ // 如果不相等::__noteq__
  2377. return_bool = true; // 返回1 否则(默认)为0
  2378. }
  2379. }
  2380. else if((left_result.value.type == NUMBER_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
  2381. if ((left_result.value.value.double_value == right_result.value.value.double_value) && (type == 0)){ // 如果相等
  2382. return_bool = true; // 返回1 否则(默认)为0
  2383. }
  2384. if ((left_result.value.value.double_value > right_result.value.value.double_value) && (type == 1)){ // 如果大于
  2385. return_bool = true; // 返回1 否则(默认)为0
  2386. }
  2387. if ((left_result.value.value.double_value < right_result.value.value.double_value) && (type == 2)){ // 如果小于
  2388. return_bool = true; // 返回1 否则(默认)为0
  2389. }
  2390. if ((left_result.value.value.double_value >= right_result.value.value.double_value) && (type == 3)){ // 如果大于等于
  2391. return_bool = true; // 返回1 否则(默认)为0
  2392. }
  2393. if ((left_result.value.value.double_value <= right_result.value.value.double_value) && (type == 4)){ // 如果小于等于
  2394. return_bool = true; // 返回1 否则(默认)为0
  2395. }
  2396. if ((left_result.value.value.double_value != right_result.value.value.double_value) && (type == 5)){ // 如果不相等
  2397. return_bool = true; // 返回1 否则(默认)为0
  2398. }
  2399. }
  2400. else if((left_result.value.type == NUMBER_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is NUMBER
  2401. if ((left_result.value.value.double_value == right_result.value.value.int_value) && (type == 0)){ // 如果相等
  2402. return_bool = true; // 返回1 否则(默认)为0
  2403. }
  2404. if ((left_result.value.value.double_value > right_result.value.value.int_value) && (type == 1)){ // 如果大于
  2405. return_bool = true; // 返回1 否则(默认)为0
  2406. }
  2407. if ((left_result.value.value.double_value < right_result.value.value.int_value) && (type == 2)){ // 如果小于
  2408. return_bool = true; // 返回1 否则(默认)为0
  2409. }
  2410. if ((left_result.value.value.double_value >= right_result.value.value.int_value) && (type == 3)){ // 如果大于等于
  2411. return_bool = true; // 返回1 否则(默认)为0
  2412. }
  2413. if ((left_result.value.value.double_value <= right_result.value.value.int_value) && (type == 4)){ // 如果小于等于
  2414. return_bool = true; // 返回1 否则(默认)为0
  2415. }
  2416. if ((left_result.value.value.double_value != right_result.value.value.int_value) && (type == 5)){ // 如果不相等
  2417. return_bool = true; // 返回1 否则(默认)为0
  2418. }
  2419. }
  2420. else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
  2421. if ((left_result.value.value.int_value == right_result.value.value.double_value) && (type == 0)){ // 如果相等
  2422. return_bool = true; // 返回1 否则(默认)为0
  2423. }
  2424. if ((left_result.value.value.int_value > right_result.value.value.double_value) && (type == 1)){ // 如果大于
  2425. return_bool = true; // 返回1 否则(默认)为0
  2426. }
  2427. if ((left_result.value.value.int_value < right_result.value.value.double_value) && (type == 2)){ // 如果小于
  2428. return_bool = true; // 返回1 否则(默认)为0
  2429. }
  2430. if ((left_result.value.value.int_value >= right_result.value.value.double_value) && (type == 3)){ // 如果大于等于
  2431. return_bool = true; // 返回1 否则(默认)为0
  2432. }
  2433. if ((left_result.value.value.int_value <= right_result.value.value.double_value) && (type == 4)){ // 如果小于等于
  2434. return_bool = true; // 返回1 否则(默认)为0
  2435. }
  2436. if ((left_result.value.value.int_value != right_result.value.value.double_value) && (type == 5)){ // 如果不相等
  2437. return_bool = true; // 返回1 否则(默认)为0
  2438. }
  2439. }
  2440. return_value.value.value.bool_value = return_bool;
  2441. return_value.value.type = BOOL_value;
  2442. }
  2443. return_back: return return_value;
  2444. }
  2445. // --------- traverse[iter]
  2446. GWARF_result traverse(statement *the_statement, var_list *the_var, bool new){ // traverse the statement
  2447. statement *tmp = the_statement;
  2448. GWARF_result result, result2;
  2449. if(the_statement == NULL){
  2450. result.u = statement_end; // 正常设置[正常语句结束]
  2451. result.value.type = NULL_value; // 默认设置
  2452. result.value.value.int_value = 0; // 默认设置
  2453. goto return_back;
  2454. }
  2455. bool lock = false;
  2456. if(new){ // need to make new var
  2457. var *tmp = make_var(); // base_var
  2458. the_var = append_var_list(tmp, the_var);
  2459. }
  2460. while(1){
  2461. if(tmp == NULL){
  2462. break; // off
  2463. }
  2464. result2 = read_statement_list(tmp, the_var);
  2465. // 错误停止
  2466. if(result2.u == error){ // Name Error错误
  2467. // puts("STOP:: Name No Found!");
  2468. result = result2;
  2469. break;
  2470. }
  2471. // TODO::rewent语句得优化一下 设置result2.is_rewent值
  2472. if(result2.u == return_def && result2.return_times != 0){ // return def
  2473. result2.return_times -= 1;
  2474. }
  2475. else if(result2.u == code_rewent){
  2476. lock = true; // keep the result is rewent for return
  2477. result = result2;
  2478. }
  2479. else if(is_space(&result2)){
  2480. result = result2;
  2481. break;
  2482. }
  2483. if(!lock){
  2484. result = result2;
  2485. }
  2486. else{
  2487. puts("local");
  2488. }
  2489. tmp = tmp->next;
  2490. }
  2491. if(new){ // need to make new var
  2492. the_var = free_var_list(the_var); // free the new var
  2493. }
  2494. return_back: return result;
  2495. }
  2496. GWARF_result traverse_global(statement *the_statement, var_list *the_var){ // traverse the statement[not break、broken、and others] but error
  2497. statement *tmp = the_statement;
  2498. GWARF_result result;
  2499. while(1){
  2500. if(tmp == NULL){
  2501. break; // off
  2502. }
  2503. result = read_statement_list(tmp, the_var);
  2504. if(result.u == error){ // Name Error错误
  2505. printf("%s", result.error_info);
  2506. break;
  2507. }
  2508. tmp = tmp->next;
  2509. }
  2510. return result;
  2511. }
  2512. inter *get_inter(){
  2513. inter *tmp;
  2514. tmp = malloc(sizeof(inter)); // get an address for base var
  2515. tmp->global_var = make_var();
  2516. tmp->global_code = make_statement();
  2517. return tmp;
  2518. }