123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655 |
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- #include "interpreter.h"
- double log_(double base, double num){ // 自己定义一次log
- return log(num) / log(base);
- }
- double sqrt_(double base, double num){ // 定义根号sqrt
- return pow(base, (1 / num));
- }
- bool is_space(GWARF_result *tmp){ // 使用指针是不想复制数据
- if(tmp->u == cycle_break || tmp->u == code_broken || tmp->u == cycle_continue || tmp->u == code_continued ||
- tmp->u == cycle_restart || tmp->u == code_restarted || tmp->u == code_rego || tmp->u == code_rewent){
- return true;
- }
- if(tmp->u == return_def && tmp->return_times != 0){ // return def
- return true;
- }
- return false;
- }
- bool is_error(GWARF_result *tmp){ // 判断是否为error
- if(tmp->u == error){
- return true;
- }
- return false;
- }
- bool to_bool(GWARF_value value){
- double bool_double = 1; // if bool_double == 0则返回false其他返回true
- if(value.type == OBJECT_value){ // 调用左add方法
- bool_double = to_bool(get__bool__(&value, NULL).value); // 实际上不需要the_var参数链[没有实参]
- }
- else{
- if(value.type == INT_value || value.type == BOOL_value){
- bool_double = (double)value.value.int_value;
- }
- else if(value.type == NUMBER_value){
- bool_double = value.value.double_value;
- }
- else if(value.type == STRING_value){
- bool_double = (double)strlen(value.value.string);
- }
- else if(value.type == NULL_value){
- bool_double = 0;
- }
- }
- // 返回值
- if(bool_double){
- return true;
- }
- else{
- return false;
- }
- }
- 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
- if(login_var == NULL){
- login_var = the_var;
- }
- GWARF_result return_value;
- return_value.u = statement_end; // 正常设置[正常语句结束]
- return_value.value.type = NULL_value; // 默认设置
- return_value.value.value.int_value = 0; // 默认设置
- switch (the_statement->type)
- {
- case operation: // 表达式运算
- puts("----code----");
- return_value = operation_func(the_statement, the_var, login_var);
- if((return_value.value.type == INT_value)){
- printf("operation value = %d\n", return_value.value.value.int_value);
- }
- else if(return_value.value.type == BOOL_value){
- if(return_value.value.value.bool_value){
- printf("operation value = true\n");
- }
- else{
- printf("operation value = false\n");
- }
- }
- else if(return_value.value.type == NUMBER_value){
- printf("operation value = %f\n", return_value.value.value.double_value);
- }
- else if(return_value.value.type == NULL_value){
- printf("operation value = None\n");
- }
- else if(return_value.value.type == STRING_value){
- printf("operation value = %s\n", return_value.value.value.string);
- }
- else{
- printf("operation value = other[%d]\n", return_value.value.type);
- }
- puts("----stop code----");
- break;
- case call:
- return_value = call_back(the_statement, the_var);
- break;
- case while_cycle:
- puts("----while code----");
- return_value = while_func(the_statement, the_var);
- puts("----stop while code----");
- if(return_value.u == statement_end){ // while循环不需要返回值[避免GWARF_value 进入 the_var]
- return_value.value.type = NULL_value;
- return_value.value.value.int_value = 0;
- }
- break;
- case for_cycle:
- puts("----for code----");
- return_value = for_func(the_statement, the_var);
- puts("----for while code----");
- if(return_value.u == statement_end){ // while循环不需要返回值[避免GWARF_value 进入 the_var]
- return_value.value.type = NULL_value;
- return_value.value.value.int_value = 0;
- }
- break;
- case if_branch:
- puts("----if code----");
- return_value = if_func(the_statement->code.if_branch.done, the_var);
- puts("----stop if code----");
- if(return_value.u == statement_end){ // while循环不需要返回值[避免GWARF_value 进入 the_var]
- return_value.value.type = NULL_value;
- return_value.value.value.int_value = 0;
- }
- break;
- case base_value: // get value[所有字面量均为这个表达式]
- return_value.value = (the_statement->code).base_value.value; // code
- if((return_value.value.type == INT_value) || (return_value.value.type == BOOL_value)){
- printf("get value = %d\n", return_value.value.value.int_value);
- }
- else if(return_value.value.type == NUMBER_value){
- printf("get value = %f\n", return_value.value.value.double_value);
- }
- else if(return_value.value.type == NULL_value){
- printf("get value = None\n");
- }
- else if(return_value.value.type == STRING_value){
- printf("get value = %s\n", return_value.value.value.string);
- }
- else{
- printf("get value = other[%d]\n", return_value.value.type);
- }
- // base_value返回字面量 -> 主要返回object类型,还会返回GWARF_value的其他类型供生成object类型
- break;
- case base_list: // get value[所有字面量均为这个表达式]
- return_value.value = parameter_to_list(the_statement->code.base_list.value, the_var); // code
- break;
- case slice:{ // get value[所有字面量均为这个表达式]
- GWARF_result tmp_result = traverse((the_statement->code).slice.base_var, the_var, false), get; // 把a[1:2:3]的a取出来
- if(is_error(&tmp_result)){ // Name Error错误
- return_value = tmp_result;
- goto the_break_slice;
- }
- else if(is_space(&tmp_result)){
- return_value = tmp_result;
- goto the_break_slice;
- }
- GWARF_value base_the_var = tmp_result.value;
- if(base_the_var.type == CLASS_value){ // is class so that can use "."
- var *tmp = find_var(base_the_var.value.class_value->the_var, 0, "__slice__");
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- return_value = call_back_core(get, the_var, the_statement->code.slice.value);
- goto the_break_slice;
- }
- }
- else if(base_the_var.type == OBJECT_value){
- var *tmp = find_var(base_the_var.value.object_value->the_var, 0, "__slice__");
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- return_value = call_back_core(get, the_var, the_statement->code.slice.value);
- goto the_break_slice;
- }
- }
- the_break_slice:
- return_value.value = to_object(return_value.value, the_var);
- // printf("return_value.value.type = %d\n", return_value.value);
- break;
- }
- case base_var:{ // because the var tmp, we should ues a {} to make a block[name space] for the tmp var;
- int from = 0;
- if(the_statement->code.base_var.from == NULL){
- from = 0;
- }
- else{
- GWARF_result tmp_result, tmp_object = traverse(the_statement->code.base_var.from, the_var, false);
- if(is_error(&tmp_object)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- from = 0;
- }
- else if(is_space(&tmp_object)){
- from = 0;
- }
- else{
- tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
- if(tmp_result.value.type == INT_value){
- from = tmp_result.value.value.int_value;
- }
- else if(tmp_result.value.type == NUMBER_value){
- from = (int)tmp_result.value.value.double_value;
- }
- else{
- from = 0;
- }
- }
- }
- var *tmp = find_var(the_var, from, (the_statement->code).base_var.var_name);
- if(tmp == NULL){ // 输出name error[共两处会输出]
- char *tmp = malloc((size_t)( 21 + strlen(the_statement->code.base_var.var_name) ));
- sprintf(tmp, "name not found [%s]\n", the_statement->code.base_var.var_name);
- return_value = to_error(tmp, "NameException", the_var);
- }
- else
- {
- return_value.value = tmp->value; // get_var
- if((return_value.value.type == INT_value) || (return_value.value.type == BOOL_value)){
- printf("var value = %d\n", return_value.value.value.int_value);
- }
- else if(return_value.value.type == NUMBER_value){
- printf("var value = %f\n", return_value.value.value.double_value);
- }
- else if(return_value.value.type == NULL_value){
- printf("var value = None\n");
- }
- else if(return_value.value.type == STRING_value){
- printf("var value = %s\n", return_value.value.value.string);
- }
- else{
- printf("var value = other[%d]\n", return_value.value.type);
- }
- }
- break;
- }
- case point:{
- GWARF_result tmp_result = traverse((the_statement->code).point.base_var, the_var, false);
- if(is_error(&tmp_result)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- return_value = tmp_result;
- goto the_break;
- }
- else if(is_space(&tmp_result)){
- return_value = tmp_result;
- goto the_break;
- }
- GWARF_value base_the_var = tmp_result.value;
- if(base_the_var.type == CLASS_value){ // is class so that can use "."
- return_value = traverse((the_statement->code).point.child_var, base_the_var.value.class_value->the_var, false);
- }
- else if(base_the_var.type == OBJECT_value){
- return_value = traverse((the_statement->code).point.child_var, base_the_var.value.object_value->the_var, false);
- }
- else{ // 其他类型
- goto the_break;
- }
- if(is_error(&return_value)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- goto the_break;
- }
- else if(is_space(&return_value)){
- goto the_break;
- }
- return_value.father = malloc(sizeof(return_value.father)); // 记录father的值
- *(return_value.father) = base_the_var;
- the_break:
- return_value.value = to_object(return_value.value, the_var);
- break;
- }
- case down:{
- GWARF_result tmp_result = traverse((the_statement->code).down.base_var, the_var, false), get;
- if(is_error(&tmp_result)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- return_value = tmp_result;
- goto the_break_down;
- }
- else if(is_space(&tmp_result)){
- return_value = tmp_result;
- goto the_break_down;
- }
- GWARF_value base_the_var = tmp_result.value;
- if(base_the_var.type == CLASS_value){ // is class so that can use "."
- GWARF_result child_value = traverse((the_statement->code).down.child_var, the_var, false); // 作为参数
- var *tmp = find_var(base_the_var.value.class_value->the_var, 0, "__down__");
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- return_value = call_back_core(get, the_var, pack_value_parameter(child_value.value));
- goto the_break_down;
- }
- }
- else if(base_the_var.type == OBJECT_value){
- GWARF_result child_value = traverse((the_statement->code).down.child_var, the_var, false); // 作为参数
- var *tmp = find_var(base_the_var.value.object_value->the_var, 0, "__down__");
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- return_value = call_back_core(get, the_var, pack_value_parameter(child_value.value));
- goto the_break_down;
- }
- }
- the_break_down:
- return_value.value = to_object(return_value.value, the_var); // call_back_core 返回值是object
- break;
- }
- case def:{
- GWARF_result func_value;
- func *func_tmp = malloc(sizeof(func));
- func_tmp->done = the_statement->code.def.done;
- func_tmp->parameter_list = the_statement->code.def.parameter_list;
- func_tmp->the_var = copy_var_list(the_var);
- func_tmp->type = customize; // func by user
- if(login_var != the_var){ // 定义为类方法
- func_tmp->is_class = 1;
- }
- else{
- func_tmp->is_class = 0;
- }
- func_value.value.type = FUNC_value;
- func_value.value.value.func_value = func_tmp;
- int from;
- if(the_statement->code.def.from == NULL){
- from = 0;
- }
- else{
- GWARF_result tmp_result, tmp_object = traverse(the_statement->code.def.from, the_var, false);
- if(is_error(&tmp_object)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- from = 0;
- }
- else if(is_space(&tmp_object)){
- from = 0;
- }
- else{
- tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
- if(tmp_result.value.type == INT_value){
- from = tmp_result.value.value.int_value;
- }
- else if(tmp_result.value.type == NUMBER_value){
- from = (int)tmp_result.value.value.double_value;
- }
- else{
- from = 0;
- }
- }
- }
- assigment_func(the_statement->code.def.name, func_value, login_var, from); // 注册函数到指定的位置
- // 无返回值
- break;
- }
- case set_class:{
- puts("----set class----");
- GWARF_result class_value;
- class_object *class_tmp = malloc(sizeof(class_object));
- class_tmp->the_var = make_var_base(make_var()); // make class var list
- class_value.value.type = CLASS_value;
- class_value.value.value.class_value = class_tmp;
- // 获取father -- append_by_var_list[拼凑]
- GWARF_result father_tmp;
- parameter *tmp_s = the_statement->code.set_class.father_list;
- if(tmp_s != NULL){
- while(1){
- father_tmp = traverse(tmp_s->u.value, the_var, false); // 执行
- if(is_error(&father_tmp)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- return_value = father_tmp;
- break;
- }
- else if(is_space(&father_tmp)){
- return_value = father_tmp;
- break;
- }
- if(father_tmp.value.type == CLASS_value){ // 可以通过class继承, 也可以通过object.cls继承
- append_by_var_list(class_tmp->the_var, father_tmp.value.value.class_value->the_var);
- }
- else if(father_tmp.value.type == OBJECT_value){
- append_by_var_list(class_tmp->the_var, father_tmp.value.value.object_value->cls);
- }
- if (tmp_s->next == NULL){ // the last
- break;
- }
- tmp_s = tmp_s->next;
- }
- }
- else{
- var *object_tmp = find_var(the_var, 0, "object");
- if(object_tmp != NULL){
- father_tmp.value = object_tmp->value;
- append_by_var_list(class_tmp->the_var, father_tmp.value.value.class_value->the_var);
- }
- else{
- // TODO::返回error -> var_list错误
- }
- }
- 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);
- // 执行done
- statement *tmp = the_statement->code.set_class.done;
- GWARF_result result;
- while(1){
- if(tmp == NULL){
- break; // off
- }
- read_statement(tmp, the_var, class_tmp->the_var);
- tmp = tmp->next;
- }
- int from;
- if(the_statement->code.set_class.from == NULL){
- from = 0;
- }
- else{
- GWARF_result tmp_result, tmp_object = traverse(the_statement->code.set_class.from, the_var, false);
- if(is_error(&tmp_object)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- from = 0;
- }
- else if(is_space(&tmp_object)){
- from = 0;
- }
- else{
- tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
- if(tmp_result.value.type == INT_value){
- from = tmp_result.value.value.int_value;
- }
- else if(tmp_result.value.type == NUMBER_value){
- from = (int)tmp_result.value.value.double_value;
- }
- else{
- from = 0;
- }
- }
- }
- assigment_func(the_statement->code.set_class.name, class_value, login_var, from); // 注册class 的 位置
- puts("----stop set class----");
- // 无返回值
- break;
- }
- case break_cycle:
- return_value.u = cycle_break;
- return_value.value.type = INT_value;
- if(the_statement->code.break_cycle.times == NULL){
- return_value.value.value.int_value = 0;
- }
- else{
- int int_tmp;
- GWARF_result tmp_result, tmp_object = traverse(the_statement->code.break_cycle.times, the_var, false);
- if(is_error(&tmp_object)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- return_value = tmp_object;
- break;
- }
- else if(is_space(&tmp_object)){
- return_value = tmp_object;
- break;
- }
- tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
- if(tmp_result.value.type == INT_value){
- int_tmp = tmp_result.value.value.int_value;
- }
- else if(tmp_result.value.type == NUMBER_value){
- int_tmp = (int)tmp_result.value.value.double_value;
- }
- else{
- int_tmp = 0;
- }
- return_value.value.value.int_value = int_tmp;
- }
- break;
- case broken:
- return_value.u = code_broken;
- return_value.value.type = INT_value;
- if(the_statement->code.broken.times == NULL){
- return_value.value.value.int_value = 0;
- }
- else{
- int int_tmp;
- GWARF_result tmp_result, tmp_object = traverse(the_statement->code.broken.times, the_var, false);
- if(is_error(&tmp_object)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- return_value = tmp_object;
- break;
- }
- else if(is_space(&tmp_object)){
- return_value = tmp_object;
- break;
- }
- tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
- if(tmp_result.value.type == INT_value){
- int_tmp = tmp_result.value.value.int_value;
- }
- else if(tmp_result.value.type == NUMBER_value){
- int_tmp = (int)tmp_result.value.value.double_value;
- }
- else{
- int_tmp = 0;
- }
- return_value.value.value.int_value = int_tmp;
- }
- break;
- case continue_cycle:
- return_value.u = cycle_continue;
- return_value.value.type = INT_value;
- if(the_statement->code.continue_cycle.times == NULL){
- return_value.value.value.int_value = 0;
- }
- else{
- int int_tmp;
- GWARF_result tmp_result, tmp_object = traverse(the_statement->code.continue_cycle.times, the_var, false);
- if(is_error(&tmp_object)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- return_value = tmp_object;
- break;
- }
- else if(is_space(&tmp_object)){
- return_value = tmp_object;
- break;
- }
- tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
- if(tmp_result.value.type == INT_value){
- int_tmp = tmp_result.value.value.int_value;
- }
- else if(tmp_result.value.type == NUMBER_value){
- int_tmp = (int)tmp_result.value.value.double_value;
- }
- else{
- int_tmp = 0;
- }
- return_value.value.value.int_value = int_tmp;
- }
- break;
- case continued:
- return_value.u = code_continued;
- return_value.value.type = INT_value;
- if(the_statement->code.continued.times == NULL){
- return_value.value.value.int_value = 0;
- }
- else{
- int int_tmp;
- GWARF_result tmp_result, tmp_object = traverse(the_statement->code.continued.times, the_var, false);
- if(is_error(&tmp_object)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- return_value = tmp_object;
- break;
- }
- else if(is_space(&tmp_object)){
- return_value = tmp_object;
- break;
- }
- tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
- if(tmp_result.value.type == INT_value){
- int_tmp = tmp_result.value.value.int_value;
- }
- else if(tmp_result.value.type == NUMBER_value){
- int_tmp = (int)tmp_result.value.value.double_value;
- }
- else{
- int_tmp = 0;
- }
- return_value.value.value.int_value = int_tmp;
- }
- break;
- case restart:
- return_value.u = cycle_restart;
- return_value.value.type = INT_value;
- if(the_statement->code.restart.times == NULL){
- return_value.value.value.int_value = 0;
- }
- else{
- int int_tmp;
- GWARF_result tmp_result, tmp_object = traverse(the_statement->code.restart.times, the_var, false);
- if(is_error(&tmp_object)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- return_value = tmp_object;
- break;
- }
- else if(is_space(&tmp_object)){
- return_value = tmp_object;
- break;
- }
- tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
- if(tmp_result.value.type == INT_value){
- int_tmp = tmp_result.value.value.int_value;
- }
- else if(tmp_result.value.type == NUMBER_value){
- int_tmp = (int)tmp_result.value.value.double_value;
- }
- else{
- int_tmp = 0;
- }
- return_value.value.value.int_value = int_tmp;
- }
- break;
- case restarted:
- return_value.u = code_restarted;
- return_value.value.type = INT_value;
- if(the_statement->code.restarted.times == NULL){
- return_value.value.value.int_value = 0;
- }
- else{
- int int_tmp;
- GWARF_result tmp_result, tmp_object = traverse(the_statement->code.restarted.times, the_var, false);
- if(is_error(&tmp_object)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- return_value = tmp_object;
- break;
- }
- else if(is_space(&tmp_object)){
- return_value = tmp_object;
- break;
- }
- tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
- if(tmp_result.value.type == INT_value){
- int_tmp = tmp_result.value.value.int_value;
- }
- else if(tmp_result.value.type == NUMBER_value){
- int_tmp = (int)tmp_result.value.value.double_value;
- }
- else{
- int_tmp = 0;
- }
- return_value.value.value.int_value = int_tmp;
- }
- break;
- case return_code:
- return_value.u = return_def;
- if(the_statement->code.return_code.times == NULL){
- return_value.return_times = 0;
- }
- else{
- int int_tmp;
- GWARF_result tmp_result, tmp_object = traverse(the_statement->code.return_code.times, the_var, false);
- if(is_error(&tmp_object)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- return_value = tmp_object;
- break;
- }
- else if(is_space(&tmp_object)){
- return_value = tmp_object;
- break;
- }
- tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
- if(tmp_result.value.type == INT_value){
- int_tmp = tmp_result.value.value.int_value;
- }
- else if(tmp_result.value.type == NUMBER_value){
- int_tmp = (int)tmp_result.value.value.double_value;
- }
- else{
- int_tmp = 0;
- }
- return_value.return_times = int_tmp;
- }
- if(the_statement->code.return_code.value == NULL){ // return NULL
- return_value.value.type = NULL_value;
- return_value.value.value.double_value = 0;
- }
- else{
- GWARF_result tmp_result = traverse(the_statement->code.return_code.value, the_var, false);
- if(is_error(&tmp_result)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- return_value = tmp_result;
- break;
- }
- else if(is_space(&tmp_result)){
- return_value = tmp_result;
- break;
- }
- return_value.value = to_object(tmp_result.value, the_var);
- }
- break;
- case rewent:
- return_value.u = code_rewent; // rego but not now
- break;
- case rego:
- return_value.u = code_rego; // rego now
- break;
- case set_default:{
- char *name = the_statement->code.set_default.name;
- int base_from = 0;
- GWARF_result tmp_result, tmp_object = traverse(the_statement->code.set_default.times, the_var, false);
- if(is_error(&tmp_object)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- return_value = tmp_object;
- break;
- }
- else if(is_space(&tmp_object)){
- return_value = tmp_object;
- break;
- }
- tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
- if(tmp_result.value.type == INT_value){
- base_from = tmp_result.value.value.int_value;
- }
- else if(tmp_result.value.type == NUMBER_value){
- base_from = (int)tmp_result.value.value.double_value;
- }
- else{
- base_from = 0;
- }
- append_default_var_base(name, base_from, the_var->default_list);
- printf("set_default for %s\n", name);
- break;
- }
- case set_global:{
- char *name = the_statement->code.set_global.name;
- int base_from = get_var_list_len(the_var);
- append_default_var_base(name, base_from, the_var->default_list);
- printf("global for %s\n", name);
- break;
- }
- case set_nonlocal:{
- char *name = the_statement->code.set_global.name;
- append_default_var_base(name, 1, the_var->default_list);
- printf("nonlocal for %s\n", name);
- break;
- }
- case code_block:
- puts("----block code----");
- return_value = block_func(the_statement, the_var);
- puts("----stop block code----");
- break;
- case try_code:
- return_value = try_func(the_statement, the_var);
- break;
- case raise_e:
- return_value = raise_func(the_statement, the_var, true);
- break;
- case throw_e:
- return_value = raise_func(the_statement, the_var, false);
- break;
- case import_class: // import xxx as xxx 语句
- return_value = import_func(the_statement, the_var);
- break;
- case include_import: // import xxx as xxx 语句
- return_value = include_func(the_statement, the_var);
- break;
- default:
- puts("default");
- break;
- }
- return return_value;
- }
- // -----------------include func
- GWARF_result include_func(statement *the_statement, var_list *the_var){
- GWARF_result return_value;
- statement *file_statement = the_statement->code.include_import.file;
- GWARF_value file = to_str(traverse(file_statement, the_var, false).value, the_var);
- // TODO:: 使用链表来存储
- inter *old_global = global_inter; // 保存旧迭代器
- statement_list *old_statement_base = statement_base; // 保存statement_base
- global_inter = get_inter(); // 拿全局解释器[并声明全局变量]
- free(global_inter->global_var); // 不需要新的the_var
- global_inter->global_var = the_var->var_base;
- statement_base = make_statement_base(global_inter->global_code);
-
- parser(file.value.string);
- printf("----start run----\n");
- traverse_global(global_inter->global_code, the_var);
- printf("code end...\n");
- global_inter = old_global; // 变回旧迭代器
- statement_base = old_statement_base; // 变回statement_base
- return_value.u = statement_end;
- return_value.value.type = NULL_value;
- return_value.value.value.int_value = 0;
- return return_value;
- }
- // -----------------import func
- GWARF_result import_func(statement *the_statement, var_list *the_var){
- GWARF_result return_value;
- statement *file_statement = the_statement->code.import_class.file;
-
- char *name = the_statement->code.import_class.name;
- GWARF_value file = to_str(traverse(file_statement, the_var, false).value, the_var);
- // TODO:: 使用链表来存储
- inter *old_global = global_inter; // 保存旧迭代器
- statement_list *old_statement_base = statement_base; // 保存statement_base
- global_inter = get_inter(); // 拿全局解释器[并声明全局变量]
- var_list *new_the_var = make_var_base(global_inter->global_var);
- statement_base = make_statement_base(global_inter->global_code);
-
- login(new_the_var);
-
- parser(file.value.string);
- printf("----start run----\n");
- traverse_global(global_inter->global_code, new_the_var);
- printf("code end...\n");
- global_inter = old_global; // 保存旧迭代器
- statement_base = old_statement_base; // 保存statement_base
- GWARF_result import_result;
- import_result.value.type = CLASS_value;
-
- class_object *class_tmp = malloc(sizeof(class_object));
- class_tmp->the_var = new_the_var; // make class var list
- class_tmp->out_var = append_by_var_list(class_tmp->the_var, copy_var_list(the_var)); // make class var list with out var
- int from;
- if(the_statement->code.import_class.from == NULL){
- from = 0;
- }
- else{
- GWARF_result tmp_result, tmp_object = traverse(the_statement->code.import_class.from, the_var, false);
- if(is_error(&tmp_object)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- from = 0;
- }
- else if(is_space(&tmp_object)){
- from = 0;
- }
- else{
- tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
- if(tmp_result.value.type == INT_value){
- from = tmp_result.value.value.int_value;
- }
- else if(tmp_result.value.type == NUMBER_value){
- from = (int)tmp_result.value.value.double_value;
- }
- else{
- from = 0;
- }
- }
- }
- import_result.value.value.class_value = class_tmp;
- assigment_func(name, import_result, the_var, from);
- return_value.u = statement_end;
- return_value.value.type = NULL_value;
- return_value.value.value.int_value = 0;
- return return_value;
- }
- // -----------------if func
- GWARF_result if_func(if_list *if_base, var_list *the_var){ // read the statement list with case to run by func
- GWARF_result value;
- if_list *start;
- again: start = if_base;
- bool rego = false; // switch...case...
- while(1){
- if(start->condition == NULL){ // else
- else_restart:
- puts("----else----");
- value = traverse(start->done, the_var, true);
- puts("----stop else----");
-
- // restarted操作
- // TODO::可以检查值类型,如果不正确则是解释器错误
- if(value.u == code_restarted){
- if(value.value.type != INT_value){
- // printf("Code Warrning: Bad Type Number for restart(ed), reset to zero");
- value.value.type = INT_value;
- value.value.value.int_value = 0;
- }
- if(value.value.value.int_value <= 0){
- puts("----restarted real----");
- value.u = statement_end;
- goto else_restart;
- }
- else{
- value.value.value.int_value -= 1;
- break;
- }
- }
- // continued操作
- if(value.u == code_continued){
- if(value.value.type != INT_value){
- // printf("Code Warrning: Bad Type Number for continue(ed), reset to zero");
- value.value.type = INT_value;
- value.value.value.int_value = 0;
- }
- if(value.value.value.int_value <= 0){
- puts("----if continue real----");
- value.u = statement_end;
- goto again;
- }
- else{
- value.value.value.int_value -= 1;
- }
- break;
- }
- // broken操作
- if(value.u == code_broken){
- if(value.value.type != INT_value){
- // printf("Code Warrning: Bad Type Number for break(broken), reset to zero");
- value.value.type = INT_value;
- value.value.value.int_value = 0;
- }
- if(value.value.value.int_value <= 0){
- value.u = statement_end; // 正常设置[正常语句结束]
- }
- else{
- value.value.value.int_value -= 1;
- }
- break;
- }
- // rego操作
- // else层的rego和rewent是可以往上层遗传的[也就是else如果显式指定rego和rewent是会遗传的,但是如果是if或elif指定rego是不会遗传的]
- if((value.u == code_rewent) || (value.u == code_rego)){
- ;
- }
- break; // else not next and don't need rego
- }
- else{ // not else
- bool condition;
- GWARF_result tmp_result = traverse(start->condition, the_var, false);
- if(is_error(&tmp_result)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- value = tmp_result;
- break;
- }
- else if(is_space(&tmp_result)){
- value = tmp_result;
- break;
- }
- condition = to_bool(tmp_result.value);
- printf("if condition = %d\n", condition);
- if(rego || (condition)){ // condition run success or rego(condition won't do) bug rewent can
- if_restart:
- puts("----if----");
- value = traverse(start->done, the_var, true);
- puts("----stop if----");
- // restarted操作
- if(value.u == code_restarted){
- if(value.value.type != INT_value){
- // printf("Code Warrning: Bad Type Number for restart(ed), reset to zero");
- value.value.type = INT_value;
- value.value.value.int_value = 0;
- }
- if(value.value.value.int_value <= 0){
- puts("----restarted real----");
- value.u = statement_end;
- goto if_restart;
- }
- else{
- value.value.value.int_value -= 1;
- break;
- }
- }
-
- // continued操作 [设在在rewent和rego前面]
- if(value.u == code_continued){
- if(value.value.type != INT_value){
- // printf("Code Warrning: Bad Type Number for continue(ed), reset to zero");
- value.value.type = INT_value;
- value.value.value.int_value = 0;
- }
- if(value.value.value.int_value <= 0){
- puts("----if continue real----");
- value.u = statement_end;
- goto again;
- }
- else{
- value.value.value.int_value -= 1;
- }
- break;
- }
- // broken操作
- if(value.u == code_broken){
- if(value.value.type != INT_value){
- // printf("Code Warrning: Bad Type Number for break(broken), reset to zero");
- value.value.type = INT_value;
- value.value.value.int_value = 0;
- }
- value.value.value.int_value -= 1;
- if(value.value.value.int_value < 0){
- value.u = statement_end; // 正常设置[正常语句结束]
- }
- break;
- }
-
- // rego操作
- if((value.u == code_rewent) || (value.u == code_rego)){
- value.u = statement_end; // 设置为正常语句
- rego = true;
- }
- // not restarted -> if is rego
- if(!rego){
- break; // don't rego
- }
- }
- }
- if(start->next == NULL){ // not next
- break;
- }
- start = start->next;
- }
- if((value.u == cycle_continue) || (value.u == cycle_restart) || (value.u == cycle_break)){ // if不处理也不计入层次 同break一样
- ;
- }
- return value;
- }
- // -----------------for func
- GWARF_result for_func(statement *the_statement, var_list *the_var){ // read the statement list with case to run by func
- GWARF_result value;
- var *tmp = make_var(); // base_var
- the_var = append_var_list(tmp, the_var);
- bool condition;
- if(the_statement->code.for_cycle.first != NULL){
- GWARF_result tmp_result = traverse(the_statement->code.for_cycle.first, the_var, false); // first to do
- if(is_error(&tmp_result)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- value = tmp_result;
- return value;
- }
- else if(is_space(&tmp_result)){
- value = tmp_result;
- return value;
- }
- }
- while (1){
- if(the_statement->code.for_cycle.condition != NULL){ // 检查是否存在循环条件
- GWARF_result tmp_result = traverse(the_statement->code.for_cycle.condition, the_var, false);
- if(is_error(&tmp_result)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- value = tmp_result;
- goto return_value;
- }
- else if(is_space(&tmp_result)){
- value = tmp_result;
- goto return_value;
- }
- condition = to_bool(tmp_result.value);
- printf("for condition = %d\n", condition);
- if(!condition){
- break;
- }
- }
- restart_again:
- puts("----for----");
- value = traverse(the_statement->code.for_cycle.done, the_var, false);
- //break操作
- if((value.u == cycle_break) || (value.u == code_broken)){
- if(value.value.type != INT_value){
- value.value.type = INT_value;
- value.value.value.int_value = 0;
- }
- if(value.value.value.int_value <= 0){
- value.u = statement_end; // 正常设置[正常语句结束]
- }
- else{
- value.value.value.int_value -= 1;
- }
- break; // break don't need after do
- }
- puts("----stop for----");
- // after do
- if(the_statement->code.for_cycle.after != NULL){
- GWARF_result tmp_result = traverse(the_statement->code.for_cycle.after, the_var, false);
- if(is_error(&tmp_result)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- value = tmp_result;
- goto return_value;
- }
- else if(is_space(&tmp_result)){
- value = tmp_result;
- goto return_value;
- }
- }
- // continue操作
- if((value.u == cycle_continue) || (value.u == code_continued)){
- if(value.value.type != INT_value){
- value.value.type = INT_value;
- value.value.value.int_value = 0;
- }
- if(value.value.value.int_value <= 0){
- puts("----continue real----");
- value.u = statement_end;
- continue;
- }
- else{
- value.value.value.int_value -= 1;
- break;
- }
- }
- // restart操作
- if((value.u == cycle_restart) || (value.u == code_restarted)){
- if(value.value.type != INT_value){
- value.value.type = INT_value;
- value.value.value.int_value = 0;
- }
- if(value.value.value.int_value <= 0){
- puts("----restart real----");
- value.u = statement_end;
- goto restart_again;
- }
- else{
- value.value.value.int_value -= 1;
- break;
- }
- }
- }
- return_value:
- the_var = free_var_list(the_var); // free the new var
- return value;
- }
- // -----------------raise func
- GWARF_result raise_func(statement *the_statement, var_list *the_var, bool not_class){ // read the statement list with case to run by func
- GWARF_result error_value;
- GWARF_value info;
- if(not_class){ // raise
- error_value = traverse(the_statement->code.raise_e.done, the_var, false);
- info = to_str(traverse(the_statement->code.raise_e.info, the_var, false).value, the_var);
- }
- else{ // raise
- GWARF_value tmp;
- tmp.type = STRING_value;
- tmp.value.string = "throw by user";
- error_value = traverse(the_statement->code.throw_e.done, the_var, false);
- info = to_str(tmp, the_var);
- }
- // restart操作[和continue效果相同]
- if(is_error(&error_value)){ // 遇到错误->执行except语句[不需要再检查break...]
- goto return_value; // raise执行时发生错误
- }
- if(is_space(&error_value)){
- goto return_value;
- }
- error_value.u = error;
- error_value.error_info = info.value.string;
- if(error_value.value.type == OBJECT_value){
- ; // 本身就是object
- }
- else if(error_value.value.type == CLASS_value && not_class){ // class类型
- error_value.value = call_back_core(error_value, the_var, pack_value_parameter(info)).value;
- }
- else{
- error_value.value = to_object(error_value.value, the_var); // 直接返回
- }
- return_value: return error_value;
- }
- // -----------------try func
- GWARF_result try_func(statement *the_statement, var_list *the_var){ // read the statement list with case to run by func
- GWARF_result value;
- var *tmp = make_var(); // base_var
- the_var = append_var_list(tmp, the_var);
- again:
- puts("----try----");
- value = traverse(the_statement->code.try_code.try, the_var, false);
- puts("----stop try----");
- // restart操作[和continue效果相同]
- if(is_error(&value)){ // 遇到错误->执行except语句[不需要再检查break...]
- assigment_func(the_statement->code.try_code.name, value, the_var, 0);
- puts("----except----");
- value = traverse(the_statement->code.try_code.except, the_var, false);
- puts("----stop except----");
- }
- if(value.u == code_restarted){
- if(value.value.type != INT_value){
- value.value.type = INT_value;
- value.value.value.int_value = 0;
- }
- if(value.value.value.int_value <= 0){
- puts("----restarted real----");
- value.u = statement_end;
- goto again;
- }
- else{
- value.value.value.int_value -= 1;
- }
- }
-
- // continued操作
- if(value.u == code_continued){
- if(value.value.type != INT_value){
- value.value.type = INT_value;
- value.value.value.int_value = 0;
- }
- if(value.value.value.int_value <= 0){
- puts("----block continue real----");
- value.u = statement_end;
- goto again;
- }
- else{
- value.value.value.int_value -= 1;
- }
- }
- // broken操作
- if(value.u == code_broken){
- if(value.value.type != INT_value){
- value.value.type = INT_value;
- value.value.value.int_value = 0;
- }
- if(value.value.value.int_value <= 0){
- value.u = statement_end; // 正常设置[正常语句结束]
- }
- else{
- value.value.value.int_value -= 1;
- }
- }
- the_var = free_var_list(the_var); // free the new var
- return value;
- }
- // -----------------block func
- GWARF_result block_func(statement *the_statement, var_list *the_var){ // read the statement list with case to run by func
- GWARF_result value;
- again:
- puts("----block----");
- value = traverse(the_statement->code.code_block.done, the_var, true);
- puts("----stop block----");
- // restart操作[和continue效果相同]
- if(value.u == code_restarted){
- if(value.value.type != INT_value){
- value.value.type = INT_value;
- value.value.value.int_value = 0;
- }
- if(value.value.value.int_value <= 0){
- puts("----restarted real----");
- value.u = statement_end;
- goto again;
- }
- else{
- value.value.value.int_value -= 1;
- }
- }
-
- // continued操作
- if(value.u == code_continued){
- if(value.value.type != INT_value){
- value.value.type = INT_value;
- value.value.value.int_value = 0;
- }
- if(value.value.value.int_value <= 0){
- puts("----block continue real----");
- value.u = statement_end;
- goto again;
- }
- else{
- value.value.value.int_value -= 1;
- }
- }
- // broken操作
- if(value.u == code_broken){
- if(value.value.type != INT_value){
- value.value.type = INT_value;
- value.value.value.int_value = 0;
- }
- if(value.value.value.int_value <= 0){
- value.u = statement_end; // 正常设置[正常语句结束]
- }
- else{
- value.value.value.int_value -= 1;
- }
- }
- return value;
- }
- // -----------------while func
- GWARF_result while_func(statement *the_statement, var_list *the_var){ // read the statement list with case to run by func
- GWARF_result value;
- // printf("----address = %d----\n", the_var);
- var *tmp = make_var(); // base_var
- the_var = append_var_list(tmp, the_var);
- // printf("----new address = %d----\n", the_var);
- bool condition;
- while (1){
- GWARF_result tmp_result = traverse(the_statement->code.while_cycle.condition, the_var, false);
- if(is_error(&tmp_result)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- value = tmp_result;
- goto return_value;
- }
- else if(is_space(&tmp_result)){
- value = tmp_result;
- goto return_value;
- }
- condition = to_bool(tmp_result.value);
- printf("while condition = %d\n", condition);
- if(!condition){
- break;
- }
- restart_again:
- puts("----while----");
- value = traverse(the_statement->code.while_cycle.done, the_var, false);
- puts("----stop while----");
- // break的操作
- if((value.u == cycle_break) || (value.u == code_broken)){
- if(value.value.type != INT_value){
- value.value.type = INT_value;
- value.value.value.int_value = 0;
- }
- if(value.value.value.int_value <= 0){
- value.u = statement_end; // 正常设置[正常语句结束]
- }
- else{
- value.value.value.int_value -= 1;
- }
- break;
- }
- // continue的操作
- if((value.u == cycle_continue) || (value.u == code_continued)){
- if(value.value.type != INT_value){
- value.value.type = INT_value;
- value.value.value.int_value = 0;
- }
- if(value.value.value.int_value <= 0){
- value.u = statement_end;
- continue;
- }
- else{
- value.value.value.int_value -= 1;
- break;
- }
- }
- // restart的操作
- if((value.u == cycle_restart) || (value.u == code_restarted)){
- if(value.value.type != INT_value){
- value.value.type = INT_value;
- value.value.value.int_value = 0;
- }
- if(value.value.value.int_value <= 0){
- value.u = statement_end;
- goto restart_again;
- }
- else{
- value.value.value.int_value -= 1;
- break;
- }
- }
- }
- return_value:
- the_var = free_var_list(the_var); // free the new var
- return value;
- }
- // -----------------operation func
- 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
- GWARF_result value, left_result, right_result;
- int func_type = the_statement->code.operation.type;
- 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]
- left_result = traverse((*the_statement).code.operation.left_exp, the_var, false);
- if(is_error(&left_result)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- return left_result;
- }
- else if(is_space(&left_result)){
- return left_result;
- }
- }
- right_result = traverse((*the_statement).code.operation.right_exp, the_var, false);
- if(is_error(&right_result)){ // Name Error错误
- return right_result;
- }
- else if(right_result.u != return_def && is_space(&right_result)){
- return right_result;
- }
- switch (func_type) // 获取运算类型
- {
- case ADD_func:
- value = add_func(left_result, right_result, the_var);
- break;
- case SUB_func:
- value = sub_func(left_result, right_result, the_var);
- break;
- case MUL_func:
- value = mul_func(left_result, right_result, the_var);
- break;
- case DIV_func:
- value = div_func(left_result, right_result, the_var);
- break;
- case NEGATIVE_func:
- value = negative_func(right_result, the_var);
- break;
- case ASSIGMENT_func:{ // because the var char, we should ues a {} to make a block[name space] for the tmp var;
- if((the_statement->code.operation.left_exp)->type == base_var){ // 通过base_var赋值
- char *left = (the_statement->code.operation.left_exp)->code.base_var.var_name; // get var name but not value
- int from = 0;
- if((the_statement->code.operation.left_exp)->code.base_var.from == NULL){
- from = 0;
- }
- else{
- GWARF_result tmp_result, tmp_object = traverse((the_statement->code.operation.left_exp)->code.base_var.from, the_var, false);
- if(is_error(&tmp_object)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- return tmp_object;
- }
- else if(is_space(&tmp_object)){
- return tmp_object;
- }
- tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
- if(tmp_result.value.type == INT_value){
- from = tmp_result.value.value.int_value;
- }
- else if(tmp_result.value.type == NUMBER_value){
- from = (int)tmp_result.value.value.double_value;
- }
- else{
- from = 0;
- }
- }
- value = assigment_func(left, right_result, login_var, from);
- }
- else if((the_statement->code.operation.left_exp)->type == point){ // 通过point赋值
- GWARF_result tmp_result = traverse((the_statement->code.operation.left_exp)->code.point.base_var, the_var, false); // 不用取value
- if(is_error(&tmp_result)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- return tmp_result;
- }
- else if(is_space(&tmp_result)){
- return tmp_result;
- }
- GWARF_value base_the_var = tmp_result.value; // 不用取value
- if(((the_statement->code.operation.left_exp)->code.point.child_var)->type == base_var){
- char *left = ((the_statement->code.operation.left_exp)->code.point.child_var)->code.base_var.var_name;
- int from = 0;
- if(((the_statement->code.operation.left_exp)->code.point.child_var)->code.base_var.from == NULL){
- from = 0;
- }
- else{
- GWARF_result tmp_result, tmp_object = traverse(((the_statement->code.operation.left_exp)->code.point.child_var)->code.base_var.from, the_var, false);
- if(is_error(&tmp_object)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- return tmp_object;
- }
- else if(is_space(&tmp_object)){
- return tmp_object;
- }
- tmp_result = get__value__(&(tmp_object.value), the_var); // 从object中提取value
- if(tmp_result.value.type == INT_value){
- from = tmp_result.value.value.int_value;
- }
- else if(tmp_result.value.type == NUMBER_value){
- from = (int)tmp_result.value.value.double_value;
- }
- else{
- from = 0;
- }
- }
- value = assigment_func(left, right_result, base_the_var.value.object_value->the_var, from);
- }
- else{
- goto the_else;
- }
- }
- else if((the_statement->code.operation.left_exp)->type == down){ // 通过down赋值
- GWARF_result tmp_result = traverse((the_statement->code.operation.left_exp)->code.down.base_var, the_var, false), get; // 不用取value
- if(is_error(&tmp_result)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- return tmp_result;
- }
- else if(is_space(&tmp_result)){
- return tmp_result;
- }
- GWARF_value base_the_var = tmp_result.value; // 不用取value
- if(base_the_var.type == CLASS_value){ // is class so that can use "."
- GWARF_result child_value = traverse((the_statement->code.operation.left_exp)->code.down.child_var, the_var, false); // 作为参数
- var *tmp = find_var(base_the_var.value.class_value->the_var, 0, "__set__");
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- parameter *tmp = pack_value_parameter(child_value.value);
- tmp->next = pack_value_parameter(right_result.value);
- value = call_back_core(get, the_var, tmp);
- goto the_else;
- }
- }
- else if(base_the_var.type == OBJECT_value){
- GWARF_result child_value = traverse((the_statement->code.operation.left_exp)->code.down.child_var, the_var, false); // 作为参数
- var *tmp = find_var(base_the_var.value.object_value->the_var, 0, "__set__");
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- parameter *tmp = pack_value_parameter(child_value.value);
- tmp->next = pack_value_parameter(right_result.value);
- value = call_back_core(get, the_var, tmp);
- goto the_else;
- }
- }
- else{
- goto the_else;
- }
- }
- else{
- the_else: puts("Bad Assigment");
- }
- break;
- }
- case EQUAL_func:
- value = equal_func(left_result, right_result, the_var, 0);
- break;
- case MORE_func:
- value = equal_func(left_result, right_result, the_var, 1);
- break;
- case LESS_func:
- value = equal_func(left_result, right_result, the_var, 2);
- break;
- case MOREEQ_func:
- value = equal_func(left_result, right_result, the_var, 3);
- break;
- case LESSEQ_func:
- value = equal_func(left_result, right_result, the_var, 4);
- break;
- case NOTEQ_func:
- value = equal_func(left_result, right_result, the_var, 5);
- break;
- case POW_func:
- value = pow_func(left_result, right_result, the_var);
- break;
- case LOG_func:
- value = log_func(left_result, right_result, the_var);
- break;
- case SQRT_func:
- value = sqrt_func(left_result, right_result, the_var);
- break;
- default:
- break;
- }
- value.u = statement_end; // 正常设置[正常语句结束]
- value.value = to_object(value.value, the_var); // 返回类型是object[不下放到add func等]
- return value;
- }
- GWARF_result call_back(statement *the_statement, var_list *the_var){ // the func for add and call from read_statement_list
- GWARF_result get = traverse(the_statement->code.call.func, the_var, false), result;
- if(is_error(&get)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- return get;
- }
- else if(is_space(&get)){
- return get;
- }
- result = call_back_core(get, the_var, the_statement->code.call.parameter_list);
- result.value = to_object(result.value, the_var); // 返回类型是object
- return result;
- }
- 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
- GWARF_result result;
- var_list *old_var_list = the_var;
- if(get.value.type == FUNC_value){
- func *func_ = get.value.value.func_value;
- parameter *tmp_x = func_->parameter_list;
- the_var = func_->the_var;
- // tmp_x:形参,tmp_s:实参
- // printf("----address = %d----\n", the_var);
- var *tmp = make_var(); // base_var
- the_var = append_var_list(tmp, the_var);
- // printf("----new address = %d----\n", the_var);
- if(func_->type == customize){ // 用户定义的方法
- if(tmp_x == NULL){
- goto no_tmp_x; // 无形参
- }
- GWARF_result father;
- if(func_->is_class == 1){
- father.value = *(get.father);
- assigment_func(tmp_x->u.name, father, the_var, 0);
- if (tmp_x->next == NULL){ // the last
- goto no_tmp_x;
- }
- tmp_x = tmp_x->next; // get the next to iter
- }
- while(1){
- GWARF_result tmp = traverse(tmp_s->u.value, the_var, false); // 不需要取__value__
- if(is_error(&tmp)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- the_var = free_var_list(the_var); // free the new var
- return tmp;
- }
- else if(is_space(&tmp)){
- the_var = free_var_list(the_var); // free the new var
- return tmp;
- }
- assigment_func(tmp_x->u.name, tmp, the_var, 0);
- if ((tmp_x->next == NULL)||(tmp_s->next == NULL)){ // the last
- break;
- }
- tmp_x = tmp_x->next; // get the next to iter
- tmp_s = tmp_s->next;
- }
- no_tmp_x:
- puts("----start func----");
- result = traverse(func_->done, the_var, false); // 执行func_value->done
- if(is_error(&result)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- the_var = free_var_list(the_var); // free the new var
- return result;
- }
- else if(is_space(&result)){
- the_var = free_var_list(the_var); // free the new var
- return result;
- }
- puts("----stop start func----");
- }
- else{
- result = func_->paser(func_, tmp_s, the_var, get, old_var_list);
- }
- the_var = free_var_list(the_var); // free the new var
- }
- else if(get.value.type == CLASS_value){ // 生成实例
- the_object *object_tmp = malloc(sizeof(the_object)); // 生成object的空间
- object_tmp->cls = get.value.value.class_value->the_var;
- object_tmp->the_var = append_by_var_list(make_var_base(make_var()), object_tmp->cls);
- GWARF_value tmp;
- tmp.type = OBJECT_value;
- tmp.value.object_value = object_tmp;
- // 执行__init__
- var *init_tmp = find_var(object_tmp->cls, 0, "__init__");
- if(init_tmp != NULL){ // 找到了__init__
- func *func_ = init_tmp->value.value.func_value;
- parameter *tmp_x = func_->parameter_list;
- the_var = func_->the_var;
- // tmp_x:形参,tmp_s:实参
- // // printf("----address = %d----\n", the_var);
- var *tmp = make_var(); // base_var
- the_var = append_var_list(tmp, the_var);
- // // printf("----new address = %d----\n", the_var);
- if(func_->type == customize){ // 用户定义的方法
- if(tmp_x == NULL){
- puts("No tmp_x");
- goto no_tmp_x_init; // 无形参
- }
- GWARF_result father;
- father.value.type = OBJECT_value;
- father.value.value.object_value = object_tmp;
- if(func_->is_class == 1){
- assigment_func(tmp_x->u.name, father, the_var, 0);
- if (tmp_x->next == NULL){ // the last
- goto no_tmp_x_init;
- }
- tmp_x = tmp_x->next; // get the next to iter
- }
- while(1){
- GWARF_result tmp = traverse(tmp_s->u.value, the_var, false);
- if(is_error(&tmp)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- the_var = free_var_list(the_var); // free the new var
- return tmp;
- }
- else if(is_space(&tmp)){
- the_var = free_var_list(the_var); // free the new var
- return tmp;
- }
- assigment_func(tmp_x->u.name, tmp, the_var, 0);
- if ((tmp_x->next == NULL)||(tmp_s->next == NULL)){ // the last
- break;
- }
- tmp_x = tmp_x->next; // get the next to iter
- tmp_s = tmp_s->next;
- }
- no_tmp_x_init:
- puts("----start func----");
- {
- GWARF_result tmp = traverse(func_->done, the_var, false); // 执行func_value->done
- if(is_error(&tmp)){ // Name Error错误
- // puts("STOP:: Name No Found!");\
- the_var = free_var_list(the_var); // free the new var
- return tmp;
- }
- else if(is_space(&tmp)){
- the_var = free_var_list(the_var); // free the new var
- return tmp;
- }
- }
- puts("----stop start func----");
- }
- else{
- GWARF_result tmp_get;
- GWARF_value father;
- father.type = OBJECT_value;
- father.value.object_value = object_tmp;
- tmp_get.father = &father;
- GWARF_result tmp = func_->paser(func_, tmp_s, the_var, tmp_get, old_var_list); // 返回值不记录
- if(is_error(&tmp)){ // Name Error错误
- the_var = free_var_list(the_var); // free the new var
- // puts("STOP:: Name No Found!");
- return tmp;
- }
- else if(is_space(&tmp)){
- the_var = free_var_list(the_var); // free the new var
- return tmp;
- }
- }
- the_var = free_var_list(the_var); // free the new var
- }
- // 记录返回值
- result.u = statement_end;
- result.value = tmp;
- }
- else if(get.value.type == OBJECT_value){ // 调用__call__方法
- // 执行__init__
- var *call_tmp = find_var(get.value.value.object_value->the_var, 0, "__call__");
- if(call_tmp != NULL){ // 找到了__init__
- func *func_ = call_tmp->value.value.func_value;
- parameter *tmp_x = func_->parameter_list;
- the_var = func_->the_var;
- // tmp_x:形参,tmp_s:实参
- // // printf("----address = %d----\n", the_var);
- var *tmp = make_var(); // base_var
- the_var = append_var_list(tmp, the_var);
- // // printf("----new address = %d----\n", the_var);
- if(func_->type == customize){ // 用户定义的方法
- if(tmp_x == NULL){
- puts("No tmp_x");
- goto no_tmp_x_call; // 无形参
- }
- GWARF_result father;
- father.value.type = OBJECT_value;
- father.value.value.object_value = get.value.value.object_value;
- if(func_->is_class == 1){
- assigment_func(tmp_x->u.name, father, the_var, 0);
- if (tmp_x->next == NULL){ // the last
- goto no_tmp_x_call;
- }
- tmp_x = tmp_x->next; // get the next to iter
- }
- while(1){
- GWARF_result tmp = traverse(tmp_s->u.value, the_var, false);
- if(is_error(&tmp)){ // Name Error错误
- // puts("STOP:: Name No Found!");
- the_var = free_var_list(the_var); // free the new var
- return tmp;
- }
- else if(is_space(&tmp)){
- the_var = free_var_list(the_var); // free the new var
- return tmp;
- }
- assigment_func(tmp_x->u.name, tmp, the_var, 0);
- if ((tmp_x->next == NULL)||(tmp_s->next == NULL)){ // the last
- break;
- }
- tmp_x = tmp_x->next; // get the next to iter
- tmp_s = tmp_s->next;
- }
- no_tmp_x_call:
- puts("----start func----");
- {
- GWARF_result tmp = traverse(func_->done, the_var, false); // 执行func_value->done
- if(is_error(&tmp)){ // Name Error错误
- // puts("STOP:: Name No Found!");\
- the_var = free_var_list(the_var); // free the new var
- return tmp;
- }
- else if(is_space(&tmp)){
- the_var = free_var_list(the_var); // free the new var
- return tmp;
- }
- }
- puts("----stop start func----");
- }
- else{
- GWARF_result tmp_get;
- GWARF_value father;
- father.type = OBJECT_value;
- father.value.object_value = get.value.value.object_value;
- tmp_get.father = &father;
- GWARF_result tmp = func_->paser(func_, tmp_s, the_var, tmp_get, old_var_list); // 返回值不记录
- if(is_error(&tmp)){ // Name Error错误
- the_var = free_var_list(the_var); // free the new var
- // puts("STOP:: Name No Found!");
- return tmp;
- }
- else if(is_space(&tmp)){
- the_var = free_var_list(the_var); // free the new var
- return tmp;
- }
- }
- the_var = free_var_list(the_var); // free the new var
- }
- }
- if(result.u == return_def){
- if(result.return_times <= 0){
- result.u = statement_end;
- }
- else{
- result.return_times -= 1;
- }
- }
- return result;
- }
- // --------- ADD
- 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
- GWARF_result return_value, get; // the result by call read_statement_list with left and right; value is the result for add
- if(left_result.value.type == OBJECT_value){ // 调用左add方法
- GWARF_result get;
- GWARF_value base_the_var = left_result.value; // 只有一个参数
- var_list *call_var = base_the_var.value.object_value->the_var;
- var *tmp = find_var(call_var, 0, "__add__");
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- return_value = call_back_core(get, the_var, pack_value_parameter(right_result.value));
- goto return_back;
- }
- // goto next if
- }
- if(right_result.value.type == OBJECT_value){ // 调用右add方法
- GWARF_result get;
- GWARF_value base_the_var = right_result.value; // 只有一个参数
- var_list *call_var = base_the_var.value.object_value->the_var;
- var *tmp = find_var(call_var, 0, "__add__");
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- return_value = call_back_core(get, the_var, pack_value_parameter(left_result.value));
- }
- else{
- return_value.u = statement_end;
- return_value.value = left_result.value;
- }
- goto return_back;
- }
- {
- // 理论上用户是不可以直接调用下面的基类的,计算过程中万物皆类
- if(left_result.value.type == NULL_value){
- return_value.u = statement_end;
- return_value.value = right_result.value; // NULL加法相当于0
- }
- else if(right_result.value.type == NULL_value){
- return_value.u = statement_end;
- return_value.value = left_result.value; // NULL加法相当于0
- }
- 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
- return_value.u = statement_end;
- return_value.value.type = INT_value;
- return_value.value.value.int_value = (int)(left_result.value.value.int_value + right_result.value.value.int_value);
- }
- else if((left_result.value.type == NUMBER_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value;
- return_value.value.value.double_value = (double)(left_result.value.value.double_value + right_result.value.value.double_value);
- }
- else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value;
- return_value.value.value.double_value = (double)(left_result.value.value.int_value + right_result.value.value.double_value);
- }
- else if((left_result.value.type == NUMBER_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value;
- return_value.value.value.double_value = (double)(left_result.value.value.double_value + right_result.value.value.int_value);
- }
- else if((left_result.value.type == STRING_value) && (right_result.value.type == STRING_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = STRING_value;
- char *l = left_result.value.value.string;
- char *r = right_result.value.value.string;
- return_value.value.value.string = malloc(strlen(l) + strlen(r)); // 创建新空间
- strcpy(return_value.value.value.string, l); // 复制字符串
- strcat(return_value.value.value.string, r); // 追加字符串
- }
-
- }
- return_back: return return_value;
- }
- // --------- SUB
- 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
- GWARF_result return_value; // the result by call read_statement_list with left and right; value is the result for sub
- if(left_result.value.type == OBJECT_value){ // 调用左sub方法
- GWARF_result get;
- GWARF_value base_the_var = left_result.value; // 只有一个参数
- var_list *call_var = base_the_var.value.object_value->the_var;
- var *tmp = find_var(call_var, 0, "__sub__");
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- return_value = call_back_core(get, the_var, pack_value_parameter(right_result.value));
- goto return_back;
- }
- }
- if(right_result.value.type == OBJECT_value){ // 调用右sub方法
- GWARF_result get;
- GWARF_value base_the_var = right_result.value; // 只有一个参数
- var_list *call_var = base_the_var.value.object_value->the_var;
- var *tmp = find_var(call_var, 0, "__sub__");
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- return_value = call_back_core(get, the_var, pack_value_parameter(left_result.value));
- }
- else{
- return_value.u = statement_end;
- return_value.value = left_result.value;
- }
- goto return_back;
- }
- {
- if(left_result.value.type == NULL_value){
- return_value.u = statement_end;
- return negative_func(right_result, the_var); // NULL减法相当于0
- }
- else if(right_result.value.type == NULL_value){
- return_value.u = statement_end;
- return_value.value = left_result.value; // NULL减法相当于0
- }
- 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
- return_value.u = statement_end;
- return_value.value.type = INT_value;
- return_value.value.value.int_value = (int)(left_result.value.value.int_value - right_result.value.value.int_value);
- }
- else if((left_result.value.type == NUMBER_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value;
- return_value.value.value.double_value = (double)(left_result.value.value.double_value - right_result.value.value.double_value);
- }
- else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value;
- return_value.value.value.double_value = (double)(left_result.value.value.int_value - right_result.value.value.double_value);
- }
- else if((left_result.value.type == NUMBER_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value;
- return_value.value.value.double_value = (double)(left_result.value.value.double_value - right_result.value.value.int_value);
- }
- }
- return_back: return return_value;
- }
- // --------- negative
- GWARF_result negative_func(GWARF_result right_result, var_list *the_var){ // the func for negative and call from read_statement_list
- GWARF_result return_value; // the result by call read_statement_list with left and right; value is the result for negative
- if(right_result.value.type == OBJECT_value){ // 调用右sub方法
- GWARF_result get;
- GWARF_value base_the_var = right_result.value; // 只有一个参数
- var_list *call_var = base_the_var.value.object_value->the_var;
- var *tmp = find_var(call_var, 0, "__negative__");
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- return_value = call_back_core(get, the_var, pack_value_parameter(right_result.value));
- }
- else{
- return_value.u = statement_end;
- return_value.value.type = BOOL_value;
- return_value.value.value.bool_value = true;
- }
- }
- else{
- if(right_result.value.type == NULL_value){ // 返回bool true
- return_value.u = statement_end;
- return_value.value.type = BOOL_value;
- return_value.value.value.bool_value = true;
- }
- else if(right_result.value.type == INT_value){ // all is INT
- return_value.u = statement_end;
- return_value.value.type = INT_value;
- return_value.value.value.int_value = (int)(-1 * right_result.value.value.int_value);
- }
- else if(right_result.value.type == BOOL_value){
- return_value.u = statement_end;
- return_value.value.type = BOOL_value;
- if(right_result.value.value.bool_value)
- {
- return_value.value.value.bool_value = false;
- }
- else{
- return_value.value.value.bool_value = true;
- }
- }
- else if(right_result.value.type == NUMBER_value){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value;
- return_value.value.value.double_value = (double)(-1 * right_result.value.value.double_value);
- }
- else if(right_result.value.type == STRING_value){ // 字符串
- return_value.u = statement_end;
- return_value.value.type = STRING_value;
- char *r = right_result.value.value.string;
- return_value.value.value.string = malloc(strlen(r)); // 创建新空间
- char *tmp = malloc(strlen(r));
- strcpy(tmp, r); // 复制字符串
- for(int i=0;i<strlen(tmp);i += 1){
- return_value.value.value.string[i] = tmp[strlen(tmp) - i - 1]; // 反转
- }
- }
- }
- return_back: return return_value;
- }
- // --------- MUL
- 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
- GWARF_result return_value; // the result by call read_statement_list with left and right; value is the result for mul
- if(left_result.value.type == OBJECT_value){ // 调用左mul方法
- GWARF_result get;
- GWARF_value base_the_var = left_result.value; // 只有一个参数
- var_list *call_var = base_the_var.value.object_value->the_var;
- var *tmp = find_var(call_var, 0, "__mul__");
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- return_value = call_back_core(get, the_var, pack_value_parameter(right_result.value));
- goto return_back;
- }
- }
- if(right_result.value.type == OBJECT_value){ // 调用右mul方法
- GWARF_result get;
- GWARF_value base_the_var = right_result.value; // 只有一个参数
- var_list *call_var = base_the_var.value.object_value->the_var;
- var *tmp = find_var(call_var, 0, "__mul__");
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- return_value = call_back_core(get, the_var, pack_value_parameter(left_result.value));
- }
- else{
- return_value.u = statement_end;
- return_value.value = left_result.value;
- }
- goto return_back;
- }
- {
- if(left_result.value.type == NULL_value){
- return_value.value = right_result.value; // NULL乘法相当于1
- }
- else if(right_result.value.type == NULL_value){
- return_value.value = left_result.value; // NULL乘法相当于1
- }
- 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
- return_value.u = statement_end;
- return_value.value.type = INT_value;
- return_value.value.value.int_value = (int)(left_result.value.value.int_value * right_result.value.value.int_value);
- }
- else if((left_result.value.type == NUMBER_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value;
- return_value.value.value.double_value = (double)(left_result.value.value.double_value * right_result.value.value.double_value);
- }
- else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value;
- return_value.value.value.double_value = (double)(left_result.value.value.int_value * right_result.value.value.double_value);
- }
- else if((left_result.value.type == NUMBER_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value;
- return_value.value.value.double_value = (double)(left_result.value.value.double_value * right_result.value.value.int_value);
- }
- else if((left_result.value.type == INT_value) && (right_result.value.type == STRING_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = STRING_value;
- int l = left_result.value.value.int_value;
- char *r = right_result.value.value.string;
- if(l == 0){
- return_value.value.value.string = (char *)malloc(0); // NULL string
- }
- else if(l > 0){
- return_value.value.value.string = malloc(strlen(r) * l); // 创建新空间
- strcpy(return_value.value.value.string, r); // 复制字符串
- l -= 1;
- for(;l>0;l -= 1){
- strcat(return_value.value.value.string, r); // 追加字符串
- }
- }
- else{
- return_value.value.value.string = malloc(strlen(r) * (-l)); // 创建新空间
- char *tmp = malloc(strlen(r) * (-l));
- strcpy(tmp, r); // 复制字符串
- l += 1;
- for(;l<0;l += 1){
- strcat(tmp, r); // 追加字符串
- }
- for(int i=0;i<strlen(tmp);i += 1){
- return_value.value.value.string[i] = tmp[strlen(tmp) - i - 1]; // 反转
- }
- free(tmp);
- }
- }
- else if((left_result.value.type == STRING_value) && (right_result.value.type == INT_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = STRING_value;
- int l = right_result.value.value.int_value;
- char *r = left_result.value.value.string;
- if(l == 0){
- return_value.value.value.string = (char *)malloc(0); // NULL string
- }
- else if(l > 0){
- return_value.value.value.string = malloc(strlen(r) * l); // 创建新空间
- strcpy(return_value.value.value.string, r); // 复制字符串
- l -= 1;
- for(;l>0;l -= 1){
- strcat(return_value.value.value.string, r); // 追加字符串
- }
- }
- else{
- return_value.value.value.string = malloc(strlen(r) * (-l)); // 创建新空间
- char *tmp = malloc(strlen(r) * (-l));
- strcpy(tmp, r); // 复制字符串
- l += 1;
- for(;l<0;l += 1){
- strcat(tmp, r); // 追加字符串
- }
- for(int i=0;i<strlen(tmp);i += 1){
- return_value.value.value.string[i] = tmp[strlen(tmp) - i - 1]; // 反转
- }
- free(tmp);
- }
-
- }
- }
- return_back: return return_value;
- }
- // --------- DIV
- 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
- GWARF_result return_value; // the result by call read_statement_list with left and right; value is the result for div
- if(left_result.value.type == OBJECT_value){ // 调用左div方法
- GWARF_result get;
- GWARF_value base_the_var = left_result.value; // 只有一个参数
- var_list *call_var = base_the_var.value.object_value->the_var;
- var *tmp = find_var(call_var, 0, "__div__");
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- return_value = call_back_core(get, the_var, pack_value_parameter(right_result.value));
- goto return_back;
- }
- }
- if(right_result.value.type == OBJECT_value){ // 调用右div方法
- GWARF_result get;
- GWARF_value base_the_var = right_result.value; // 只有一个参数
- var_list *call_var = base_the_var.value.object_value->the_var;
- var *tmp = find_var(call_var, 0, "__div__");
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- return_value = call_back_core(get, the_var, pack_value_parameter(left_result.value));
- }
- else{
- return_value.u = statement_end;
- return_value.value = left_result.value;
- }
- goto return_back;
- }
- {
- if(left_result.value.type == NULL_value){
- left_result.value.type = INT_value;
- left_result.value.value.int_value = 1;
- }
- else if(right_result.value.type == NULL_value){
- return_value.value = left_result.value; // NULL除发相当于1
- goto return_result;
- }
- // 此处不是else if
- 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
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value; // 除 无int
- return_value.value.value.double_value = ((double)left_result.value.value.int_value / (double)right_result.value.value.int_value);
- }
- else if((left_result.value.type == NUMBER_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value;
- return_value.value.value.double_value = (left_result.value.value.double_value / right_result.value.value.double_value);
- }
- else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value;
- return_value.value.value.double_value = ((double)left_result.value.value.int_value / right_result.value.value.double_value);
- }
- else if((left_result.value.type == NUMBER_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value;
- return_value.value.value.double_value = (left_result.value.value.double_value / (double)right_result.value.value.int_value);
- }
- }
- return_back: return_result: return return_value;
- }
- // --------- POW
- 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
- GWARF_result return_value; // the result by call read_statement_list with left and right; value is the result for div
- if(left_result.value.type == OBJECT_value){ // 调用左pow方法
- GWARF_result get;
- GWARF_value base_the_var = left_result.value; // 只有一个参数
- var_list *call_var = base_the_var.value.object_value->the_var;
- var *tmp = find_var(call_var, 0, "__pow__");
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- return_value = call_back_core(get, the_var, pack_value_parameter(right_result.value));
- goto return_back;
- }
- }
- if(right_result.value.type == OBJECT_value){ // 调用右pow方法
- GWARF_result get;
- GWARF_value base_the_var = right_result.value; // 只有一个参数
- var_list *call_var = base_the_var.value.object_value->the_var;
- var *tmp = find_var(call_var, 0, "__pow__");
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- return_value = call_back_core(get, the_var, pack_value_parameter(left_result.value));
- }
- else{
- return_value.u = statement_end;
- return_value.value = left_result.value;
- }
- goto return_back;
- }
- {
- if(left_result.value.type == NULL_value){
- return_value.u = statement_end;
- return_value.value.type = INT_value;
- return_value.value.value.int_value = 1;
- }
- else if(right_result.value.type == NULL_value){
- return_value.value = left_result.value; // NULL乘方相当于1
- }
- 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
- return_value.u = statement_end;
- return_value.value.type = INT_value;
- return_value.value.value.int_value = (int)pow((double)left_result.value.value.int_value, (double)right_result.value.value.int_value);
- }
- else if((left_result.value.type == NUMBER_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value;
- return_value.value.value.double_value = (double)pow(left_result.value.value.double_value, right_result.value.value.double_value);
- }
- else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value;
- return_value.value.value.double_value = (double)pow((double)left_result.value.value.int_value, (double)right_result.value.value.double_value);
- }
- else if((left_result.value.type == NUMBER_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value;
- return_value.value.value.double_value = (double)pow((double)left_result.value.value.double_value, (double)right_result.value.value.int_value);
- }
- }
- return_back: return return_value;
- }
- // --------- LOG
- 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
- GWARF_result return_value; // the result by call read_statement_list with left and right; value is the result for div
- if(left_result.value.type == OBJECT_value){ // 调用左log方法
- GWARF_result get;
- GWARF_value base_the_var = left_result.value; // 只有一个参数
- var_list *call_var = base_the_var.value.object_value->the_var;
- var *tmp = find_var(call_var, 0, "__log__");
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- return_value = call_back_core(get, the_var, pack_value_parameter(right_result.value));
- goto return_back;
- }
- }
- if(right_result.value.type == OBJECT_value){ // 调用右log方法
- GWARF_result get;
- GWARF_value base_the_var = right_result.value; // 只有一个参数
- var_list *call_var = base_the_var.value.object_value->the_var;
- var *tmp = find_var(call_var, 0, "__log__");
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- return_value = call_back_core(get, the_var, pack_value_parameter(left_result.value));
- }
- else{
- return_value.u = statement_end;
- return_value.value = left_result.value;
- }
- goto return_back;
- }
- {
- if(left_result.value.type == NULL_value){
- return_value.value = left_result.value; // 返回NULL
- }
- else if(right_result.value.type == NULL_value){
- return_value.u = statement_end;
- return_value.value.type = INT_value;
- return_value.value.value.int_value = 0;
- }
- 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
- return_value.u = statement_end;
- return_value.value.type = INT_value;
- return_value.value.value.int_value = (int)log_((double)left_result.value.value.int_value, (double)right_result.value.value.int_value);
- }
- else if((left_result.value.type == NUMBER_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value;
- return_value.value.value.double_value = (double)log_(left_result.value.value.double_value, right_result.value.value.double_value);
- }
- else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value;
- return_value.value.value.double_value = (double)log_((double)left_result.value.value.int_value, (double)right_result.value.value.double_value);
- }
- else if((left_result.value.type == NUMBER_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value;
- return_value.value.value.double_value = (double)log_((double)left_result.value.value.double_value, (double)right_result.value.value.int_value);
- }
- }
- return_back: return return_value;
- }
- // --------- SQRT
- 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
- GWARF_result return_value; // the result by call read_statement_list with left and right; value is the result for div
- if(left_result.value.type == OBJECT_value){ // 调用左sqrt方法
- GWARF_result get;
- GWARF_value base_the_var = left_result.value; // 只有一个参数
- var_list *call_var = base_the_var.value.object_value->the_var;
- var *tmp = find_var(call_var, 0, "__sqrt__");
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- return_value = call_back_core(get, the_var, pack_value_parameter(right_result.value));
- goto return_back;
- }
- }
- if(right_result.value.type == OBJECT_value){ // 调用右sqrt方法
- GWARF_result get;
- GWARF_value base_the_var = right_result.value; // 只有一个参数
- var_list *call_var = base_the_var.value.object_value->the_var;
- var *tmp = find_var(call_var, 0, "__sqrt__");
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- return_value = call_back_core(get, the_var, pack_value_parameter(left_result.value));
- }
- else{
- return_value.u = statement_end;
- return_value.value = left_result.value;
- }
- goto return_back;
- }
- {
- if(left_result.value.type == NULL_value){
- return_value.u = statement_end;
- return_value.value.type = INT_value;
- return_value.value.value.int_value = 0;
- }
- else if(right_result.value.type == NULL_value){
- return_value.value = right_result.value; // 返回NULL
- }
- 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
- return_value.u = statement_end;
- return_value.value.type = INT_value;
- return_value.value.value.int_value = (int)sqrt_((double)left_result.value.value.int_value, (double)right_result.value.value.int_value);
- }
- else if((left_result.value.type == NUMBER_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value;
- return_value.value.value.double_value = (double)sqrt_(left_result.value.value.double_value, right_result.value.value.double_value);
- }
- else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value;
- return_value.value.value.double_value = (double)sqrt_((double)left_result.value.value.int_value, (double)right_result.value.value.double_value);
- }
- else if((left_result.value.type == NUMBER_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is NUMBER
- return_value.u = statement_end;
- return_value.value.type = NUMBER_value;
- return_value.value.value.double_value = (double)sqrt_((double)left_result.value.value.double_value, (double)right_result.value.value.int_value);
- }
- }
- return_back: return return_value;
- }
- // --------- ASSIGMENT
- 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
- add_var(the_var, from, left, right_result.value);
- return right_result;
- }
- // --------- EQUAL
- 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
- GWARF_result return_value;
- int return_bool = false;
- char *func_list[] = {"__eq__", "__more__", "__less__", "__eqmore__", "__eqless__","__noteq__"}; // bool 运算符
- if(left_result.value.type == OBJECT_value){ // 调用左div方法
- GWARF_result get;
- GWARF_value base_the_var = left_result.value; // 只有一个参数
- var_list *call_var = base_the_var.value.object_value->the_var;
- var *tmp = find_var(call_var, 0, func_list[type]);
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- return_value = call_back_core(get, the_var, pack_value_parameter(right_result.value));
- goto return_back;
- }
- }
- if(right_result.value.type == OBJECT_value){ // 调用右div方法
- GWARF_result get;
- GWARF_value base_the_var = right_result.value; // 只有一个参数
- var_list *call_var = base_the_var.value.object_value->the_var;
- var *tmp = find_var(call_var, 0, func_list[type]);
- if(tmp != NULL){
- get.value = tmp->value;
- get.father = &base_the_var; // 设置father
- return_value = call_back_core(get, the_var, pack_value_parameter(left_result.value));
- }
- else{
- return_value.u = statement_end;
- return_value.value.value.bool_value = true;
- return_value.value.type = BOOL_value;
- }
- goto return_back;
- }
- else{
- return_value.u = statement_end;
- if(left_result.value.type == NULL_value || right_result.value.type == NULL_value){
- return_bool = false; // 无论什么都返回false NULL != NULL
- }
- 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
- if ((left_result.value.value.int_value == right_result.value.value.int_value) && (type == 0)){ // 如果相等:: __eq__
- return_bool = true; // 返回1 否则(默认)为0
- }
- if ((left_result.value.value.int_value > right_result.value.value.int_value) && (type == 1)){ // 如果大于::__more__
- return_bool = true; // 返回1 否则(默认)为0
- }
- if ((left_result.value.value.int_value < right_result.value.value.int_value) && (type == 2)){ // 如果小于::__less__
- return_bool = true; // 返回1 否则(默认)为0
- }
- if ((left_result.value.value.int_value >= right_result.value.value.int_value) && (type == 3)){ // 如果大于等于::__eqmore__
- return_bool = true; // 返回1 否则(默认)为0
- }
- if ((left_result.value.value.int_value <= right_result.value.value.int_value) && (type == 4)){ // 如果小于等于::__eqless__
- return_bool = true; // 返回1 否则(默认)为0
- }
- if ((left_result.value.value.int_value != right_result.value.value.int_value) && (type == 5)){ // 如果不相等::__noteq__
- return_bool = true; // 返回1 否则(默认)为0
- }
- }
- else if((left_result.value.type == NUMBER_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
- if ((left_result.value.value.double_value == right_result.value.value.double_value) && (type == 0)){ // 如果相等
- return_bool = true; // 返回1 否则(默认)为0
- }
- if ((left_result.value.value.double_value > right_result.value.value.double_value) && (type == 1)){ // 如果大于
- return_bool = true; // 返回1 否则(默认)为0
- }
- if ((left_result.value.value.double_value < right_result.value.value.double_value) && (type == 2)){ // 如果小于
- return_bool = true; // 返回1 否则(默认)为0
- }
- if ((left_result.value.value.double_value >= right_result.value.value.double_value) && (type == 3)){ // 如果大于等于
- return_bool = true; // 返回1 否则(默认)为0
- }
- if ((left_result.value.value.double_value <= right_result.value.value.double_value) && (type == 4)){ // 如果小于等于
- return_bool = true; // 返回1 否则(默认)为0
- }
- if ((left_result.value.value.double_value != right_result.value.value.double_value) && (type == 5)){ // 如果不相等
- return_bool = true; // 返回1 否则(默认)为0
- }
- }
- else if((left_result.value.type == NUMBER_value) && (right_result.value.type == INT_value || right_result.value.type == BOOL_value)){ // all is NUMBER
- if ((left_result.value.value.double_value == right_result.value.value.int_value) && (type == 0)){ // 如果相等
- return_bool = true; // 返回1 否则(默认)为0
- }
- if ((left_result.value.value.double_value > right_result.value.value.int_value) && (type == 1)){ // 如果大于
- return_bool = true; // 返回1 否则(默认)为0
- }
- if ((left_result.value.value.double_value < right_result.value.value.int_value) && (type == 2)){ // 如果小于
- return_bool = true; // 返回1 否则(默认)为0
- }
- if ((left_result.value.value.double_value >= right_result.value.value.int_value) && (type == 3)){ // 如果大于等于
- return_bool = true; // 返回1 否则(默认)为0
- }
- if ((left_result.value.value.double_value <= right_result.value.value.int_value) && (type == 4)){ // 如果小于等于
- return_bool = true; // 返回1 否则(默认)为0
- }
- if ((left_result.value.value.double_value != right_result.value.value.int_value) && (type == 5)){ // 如果不相等
- return_bool = true; // 返回1 否则(默认)为0
- }
- }
- else if((left_result.value.type == INT_value || left_result.value.type == BOOL_value) && (right_result.value.type == NUMBER_value)){ // all is NUMBER
- if ((left_result.value.value.int_value == right_result.value.value.double_value) && (type == 0)){ // 如果相等
- return_bool = true; // 返回1 否则(默认)为0
- }
- if ((left_result.value.value.int_value > right_result.value.value.double_value) && (type == 1)){ // 如果大于
- return_bool = true; // 返回1 否则(默认)为0
- }
- if ((left_result.value.value.int_value < right_result.value.value.double_value) && (type == 2)){ // 如果小于
- return_bool = true; // 返回1 否则(默认)为0
- }
- if ((left_result.value.value.int_value >= right_result.value.value.double_value) && (type == 3)){ // 如果大于等于
- return_bool = true; // 返回1 否则(默认)为0
- }
- if ((left_result.value.value.int_value <= right_result.value.value.double_value) && (type == 4)){ // 如果小于等于
- return_bool = true; // 返回1 否则(默认)为0
- }
- if ((left_result.value.value.int_value != right_result.value.value.double_value) && (type == 5)){ // 如果不相等
- return_bool = true; // 返回1 否则(默认)为0
- }
- }
- return_value.value.value.bool_value = return_bool;
- return_value.value.type = BOOL_value;
- }
- return_back: return return_value;
- }
- // --------- traverse[iter]
- GWARF_result traverse(statement *the_statement, var_list *the_var, bool new){ // traverse the statement
- statement *tmp = the_statement;
- GWARF_result result, result2;
- if(the_statement == NULL){
- result.u = statement_end; // 正常设置[正常语句结束]
- result.value.type = NULL_value; // 默认设置
- result.value.value.int_value = 0; // 默认设置
- goto return_back;
- }
- bool lock = false;
- if(new){ // need to make new var
- var *tmp = make_var(); // base_var
- the_var = append_var_list(tmp, the_var);
- }
- while(1){
- if(tmp == NULL){
- break; // off
- }
- result2 = read_statement_list(tmp, the_var);
- // 错误停止
- if(result2.u == error){ // Name Error错误
- // puts("STOP:: Name No Found!");
- result = result2;
- break;
- }
- // TODO::rewent语句得优化一下 设置result2.is_rewent值
- if(result2.u == return_def && result2.return_times != 0){ // return def
- result2.return_times -= 1;
- }
- else if(result2.u == code_rewent){
- lock = true; // keep the result is rewent for return
- result = result2;
- }
- else if(is_space(&result2)){
- result = result2;
- break;
- }
-
- if(!lock){
- result = result2;
- }
- else{
- puts("local");
- }
- tmp = tmp->next;
- }
- if(new){ // need to make new var
- the_var = free_var_list(the_var); // free the new var
- }
- return_back: return result;
- }
- GWARF_result traverse_global(statement *the_statement, var_list *the_var){ // traverse the statement[not break、broken、and others] but error
- statement *tmp = the_statement;
- GWARF_result result;
- while(1){
- if(tmp == NULL){
- break; // off
- }
- result = read_statement_list(tmp, the_var);
- if(result.u == error){ // Name Error错误
- printf("%s", result.error_info);
- break;
- }
- tmp = tmp->next;
- }
- return result;
- }
- inter *get_inter(){
- inter *tmp;
- tmp = malloc(sizeof(inter)); // get an address for base var
- tmp->global_var = make_var();
- tmp->global_code = make_statement();
- return tmp;
- }
|