123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446 |
- #include "__virtualmath.h"
- Statement *makeStatement(long int line, char *file) {
- Statement *tmp = memCalloc(1, sizeof(Statement));
- tmp->type = start;
- tmp->next = NULL;
- tmp->line = line;
- tmp->code_file = memStrcpy(file, 0, false, false);
- return tmp;
- }
- Token *setOperationFromToken(Statement **st_ad, struct Token *left, struct Token *right, int type, bool is_right) {
- Token *new_token = NULL;
- Statement *st = *st_ad, *left_st = left->data.st;
- if (is_right && left->data.st->type == operation &&
- left_st->u.operation.OperationType == st->u.operation.OperationType){
- st->u.operation.left = left_st->u.operation.right;
- left_st->u.operation.right = st;
- st->u.operation.right = right->data.st;
- st = left_st; // left_st是主中心
- }
- else{
- st->u.operation.left = left_st;
- st->u.operation.right = right->data.st;
- }
- new_token = makeToken(0);
- new_token->token_type = type;
- new_token->data.st = st;
- st->line = left->line;
- freeToken(left, true, false);
- freeToken(right, true, false);
- *st_ad = st;
- return new_token;
- }
- Statement *makeBaseValueStatement(LinkValue *value, long int line, char *file) {
- Statement *tmp = makeStatement(line, file);
- tmp->type = base_value;
- tmp->u.base_value.value = value;
- gcAddStatementLink(&value->gc_status);
- return tmp;
- }
- Statement *makeBaseVarStatement(char *name, Statement *times, long int line, char *file) {
- Statement *tmp = makeStatement(line, file);
- tmp->type = base_var;
- tmp->u.base_var.name = memStrcpy(name, 0, false, false);
- tmp->u.base_var.times = times;
- return tmp;
- }
- Statement *makeBaseSVarStatement(Statement *name, Statement *times){
- Statement *tmp = makeStatement(name->line, name->code_file);
- tmp->type = base_svar;
- tmp->u.base_svar.name = name;
- tmp->u.base_svar.times = times;
- return tmp;
- }
- Statement *makeBaseDictStatement(Parameter *pt, long int line, char *file) {
- Statement *tmp = makeStatement(line, file);
- tmp->type = base_dict;
- tmp->u.base_dict.dict = pt;
- return tmp;
- }
- Statement *makeOperationStatement(int type, long int line, char *file) {
- Statement *tmp = makeStatement(line, file);
- tmp->type = operation;
- tmp->u.operation.OperationType = type;
- tmp->u.operation.left = NULL;
- tmp->u.operation.right = NULL;
- return tmp;
- }
- Statement *makeTupleStatement(Parameter *pt, enum ListType type, long int line, char *file) {
- Statement *tmp = makeStatement(line, file);
- tmp->type = base_list;
- tmp->u.base_list.type = type;
- tmp->u.base_list.list = pt;
- return tmp;
- }
- Statement *makeFunctionStatement(Statement *name, Statement *function, Parameter *pt) {
- Statement *tmp = makeStatement(name->line, name->code_file);
- tmp->type = set_function;
- tmp->u.set_function.name = name;
- tmp->u.set_function.function = function;
- tmp->u.set_function.parameter = pt;
- return tmp;
- }
- Statement *makeCallStatement(Statement *function, Parameter *pt) {
- Statement *tmp = makeStatement(function->line, function->code_file);
- tmp->type = call_function;
- tmp->u.call_function.function = function;
- tmp->u.call_function.parameter = pt;
- return tmp;
- }
- Statement *makeIfStatement(long int line, char *file) {
- Statement *tmp = makeStatement(line, file);
- tmp->type = if_branch;
- tmp->u.if_branch.if_list = NULL;
- tmp->u.if_branch.else_list = NULL;
- tmp->u.if_branch.finally = NULL;
- return tmp;
- }
- Statement *makeWhileStatement(long int line, char *file) {
- Statement *tmp = makeStatement(line, file);
- tmp->type = while_branch;
- tmp->u.while_branch.type = while_;
- tmp->u.while_branch.while_list = NULL;
- tmp->u.while_branch.else_list = NULL;
- tmp->u.while_branch.finally = NULL;
- tmp->u.while_branch.first = NULL;
- tmp->u.while_branch.after = NULL;
- return tmp;
- }
- Statement *makeTryStatement(long int line, char *file) {
- Statement *tmp = makeStatement(line, file);
- tmp->type = try_branch;
- tmp->u.try_branch.except_list = NULL;
- tmp->u.try_branch.else_list = NULL;
- tmp->u.try_branch.finally = NULL;
- tmp->u.try_branch.try = NULL;
- return tmp;
- }
- Statement *makeBreakStatement(Statement *times, long int line, char *file){
- Statement *tmp = makeStatement(line, file);
- tmp->type = break_cycle;
- tmp->u.break_cycle.times = times;
- return tmp;
- }
- Statement *makeContinueStatement(Statement *times, long int line, char *file){
- Statement *tmp = makeStatement(line, file);
- tmp->type = continue_cycle;
- tmp->u.continue_cycle.times = times;
- return tmp;
- }
- Statement *makeRegoStatement(Statement *times, long int line, char *file){
- Statement *tmp = makeStatement(line, file);
- tmp->type = rego_if;
- tmp->u.rego_if.times = times;
- return tmp;
- }
- Statement *makeRestartStatement(Statement *times, long int line, char *file){
- Statement *tmp = makeStatement(line, file);
- tmp->type = restart;
- tmp->u.restart.times = times;
- return tmp;
- }
- Statement *makeReturnStatement(Statement *value, long int line, char *file){
- Statement *tmp = makeStatement(line, file);
- tmp->type = return_code;
- tmp->u.return_code.value = value;
- return tmp;
- }
- Statement *makeRaiseStatement(Statement *value, long int line, char *file){
- Statement *tmp = makeStatement(line, file);
- tmp->type = raise_code;
- tmp->u.raise_code.value = value;
- return tmp;
- }
- Statement *makeIncludeStatement(Statement *file, long int line, char *file_dir){
- Statement *tmp = makeStatement(line, file_dir);
- tmp->type = include_file;
- tmp->u.include_file.file = file;
- return tmp;
- }
- void connectStatement(Statement *base, Statement *new){
- while (base->next != NULL){
- base = base->next;
- }
- base->next = new;
- }
- void freeStatement(Statement *st){
- freeBase(st, return_);
- Statement *next_tmp;
- while (st != NULL){
- switch (st->type) {
- case operation:
- freeStatement(st->u.operation.right);
- freeStatement(st->u.operation.left);
- break;
- case base_value:
- gcFreeStatementLink(&st->u.base_value.value->gc_status);
- break;
- case base_var:
- memFree(st->u.base_var.name);
- freeStatement(st->u.base_var.times);
- break;
- case base_svar:
- freeStatement(st->u.base_svar.name);
- freeStatement(st->u.base_svar.times);
- break;
- case set_function:
- freeStatement(st->u.set_function.name);
- freeStatement(st->u.set_function.function);
- freeParameter(st->u.set_function.parameter, true);
- break;
- case call_function:
- freeStatement(st->u.call_function.function);
- freeParameter(st->u.call_function.parameter, true);
- break;
- case base_list:
- freeParameter(st->u.base_list.list, true);
- break;
- case base_dict:
- freeParameter(st->u.base_dict.dict, true);
- break;
- case if_branch:
- freeStatementList(st->u.if_branch.if_list);
- freeStatement(st->u.if_branch.finally);
- freeStatement(st->u.if_branch.else_list);
- break;
- case while_branch:
- freeStatementList(st->u.while_branch.while_list);
- freeStatement(st->u.while_branch.first);
- freeStatement(st->u.while_branch.after);
- freeStatement(st->u.while_branch.else_list);
- freeStatement(st->u.while_branch.finally);
- break;
- case for_branch:
- freeStatementList(st->u.for_branch.for_list);
- freeStatement(st->u.for_branch.var);
- freeStatement(st->u.for_branch.iter);
- freeStatement(st->u.for_branch.else_list);
- freeStatement(st->u.for_branch.finally);
- break;
- case try_branch:
- freeStatementList(st->u.try_branch.except_list);
- freeStatement(st->u.try_branch.try);
- freeStatement(st->u.try_branch.else_list);
- freeStatement(st->u.try_branch.finally);
- break;
- case with_branch:
- freeStatementList(st->u.with_branch.with_list);
- freeStatement(st->u.with_branch.else_list);
- freeStatement(st->u.with_branch.finally);
- break;
- case break_cycle:
- freeStatement(st->u.break_cycle.times);
- break;
- case continue_cycle:
- freeStatement(st->u.continue_cycle.times);
- break;
- case rego_if:
- freeStatement(st->u.rego_if.times);
- break;
- case restart:
- freeStatement(st->u.restart.times);
- break;
- case return_code:
- freeStatement(st->u.return_code.value);
- break;
- case raise_code:
- freeStatement(st->u.raise_code.value);
- break;
- case include_file:
- freeStatement(st->u.include_file.file);
- break;
- default:
- break;
- }
- memFree(st->code_file);
- next_tmp = st->next;
- memFree(st);
- st = next_tmp;
- }
- return_:
- return;
- }
- Statement *copyStatement(Statement *st){
- if (st == NULL)
- return NULL;
- Statement *tmp = copyStatementCore(st);
- Statement *base_tmp = tmp;
- while (st->next != NULL){
- tmp->next = copyStatementCore(st->next);
- tmp = tmp->next;
- st = st->next;
- }
- return base_tmp;
- }
- Statement *copyStatementCore(Statement *st){
- Statement *new = makeStatement(st->line, st->code_file);
- new->type = st->type;
- new->next = NULL;
- switch (st->type) {
- case base_value:
- new->u.base_value.value = st->u.base_value.value;
- gcAddStatementLink(&new->u.base_value.value->gc_status);
- break;
- case operation:
- new->u.operation.OperationType = st->u.operation.OperationType;
- new->u.operation.right = copyStatement(st->u.operation.right);
- new->u.operation.left = copyStatement(st->u.operation.left);
- break;
- case base_var:
- new->u.base_var.name = memStrcpy(st->u.base_var.name, 0, false, false);
- new->u.base_var.times = copyStatement(st->u.base_var.times);
- break;
- case base_svar:
- new->u.base_svar.name = copyStatement(st->u.base_svar.name);
- new->u.base_svar.times = copyStatement(st->u.base_svar.times);
- break;
- case set_function:
- new->u.set_function.name = copyStatement(st->u.set_function.name);
- new->u.set_function.function = copyStatement(st->u.set_function.function);
- new->u.set_function.parameter = copyParameter(st->u.set_function.parameter);
- break;
- case call_function:
- new->u.call_function.function = copyStatement(st->u.call_function.function);
- new->u.call_function.parameter = copyParameter(st->u.call_function.parameter);
- break;
- case base_list:
- new->u.base_list.type = st->u.base_list.type;
- new->u.base_list.list = copyParameter(st->u.base_list.list);
- break;
- case base_dict:
- new->u.base_dict.dict = copyParameter(st->u.base_dict.dict);
- break;
- case if_branch:
- new->u.if_branch.if_list = copyStatementList(st->u.if_branch.if_list);
- new->u.if_branch.finally = copyStatement(st->u.if_branch.finally);
- new->u.if_branch.else_list = copyStatement(st->u.if_branch.else_list);
- break;
- case while_branch:
- new->u.while_branch.type = st->u.while_branch.type;
- new->u.while_branch.while_list = copyStatementList(st->u.while_branch.while_list);
- new->u.while_branch.first = copyStatement(st->u.while_branch.first);
- new->u.while_branch.after = copyStatement(st->u.while_branch.after);
- new->u.while_branch.else_list = copyStatement(st->u.while_branch.else_list);
- new->u.while_branch.finally = copyStatement(st->u.while_branch.finally);
- break;
- case for_branch:
- new->u.for_branch.for_list = copyStatementList(st->u.for_branch.for_list);
- new->u.for_branch.var = copyStatement(st->u.for_branch.var);
- new->u.for_branch.iter = copyStatement(st->u.for_branch.iter);
- new->u.for_branch.else_list = copyStatement(st->u.for_branch.else_list);
- new->u.for_branch.finally = copyStatement(st->u.for_branch.finally);
- break;
- case try_branch:
- new->u.try_branch.except_list = copyStatementList(st->u.try_branch.except_list);
- new->u.try_branch.try = copyStatement(st->u.try_branch.try);
- new->u.try_branch.else_list = copyStatement(st->u.try_branch.else_list);
- new->u.try_branch.finally = copyStatement(st->u.try_branch.finally);
- break;
- case with_branch:
- new->u.with_branch.with_list = copyStatementList(st->u.with_branch.with_list);
- new->u.with_branch.else_list = copyStatement(st->u.with_branch.else_list);
- new->u.with_branch.finally = copyStatement(st->u.with_branch.finally);
- break;
- case break_cycle:
- new->u.break_cycle.times = copyStatement(st->u.break_cycle.times);
- break;
- case continue_cycle:
- new->u.continue_cycle.times = copyStatement(st->u.continue_cycle.times);
- break;
- case rego_if:
- new->u.rego_if.times = copyStatement(st->u.rego_if.times);
- break;
- case restart:
- new->u.restart.times = copyStatement(st->u.restart.times);
- break;
- case return_code:
- new->u.return_code.value = copyStatement(st->u.return_code.value);
- break;
- case raise_code:
- new->u.raise_code.value = copyStatement(st->u.raise_code.value);
- break;
- case include_file:
- new->u.include_file.file = copyStatement(st->u.include_file.file);
- break;
- default:
- break;
- }
- return new;
- }
- StatementList *makeStatementList(Statement *condition, Statement *var, Statement *code, int type) {
- StatementList *tmp = memCalloc(1, sizeof(StatementList));
- tmp->condition = condition;
- tmp->var = var;
- tmp->code = code;
- tmp->type = type;
- tmp->next = NULL;
- return tmp;
- }
- StatementList *connectStatementList(StatementList *base, StatementList *new){
- StatementList *tmp = base;
- if (base == NULL)
- return new;
- while (tmp->next != NULL){
- tmp = tmp->next;
- }
- tmp->next = new;
- return base;
- }
- void freeStatementList(StatementList *base){
- while (base != NULL){
- freeStatement(base->condition);
- freeStatement(base->code);
- freeStatement(base->var);
- StatementList *tmp = base;
- base = base->next;
- memFree(tmp);
- }
- }
- StatementList *copyStatementList(StatementList *sl){
- if (sl == NULL)
- return NULL;
- StatementList *tmp = makeStatementList(copyStatement(sl->condition), copyStatement(sl->var),
- copyStatement(sl->code), sl->type);
- StatementList *base_tmp = tmp;
- while (sl->next != NULL){
- tmp->next = makeStatementList(copyStatement(sl->condition), copyStatement(sl->var),
- copyStatement(sl->code), sl->type);
- tmp = tmp->next;
- sl = sl->next;
- }
- return base_tmp;
- }
|