#include "tlang.h" #include #include #include tobject_t* __node_method_call_execute(node_t* n,scope_t* s,retarg_t* retArg) { node_t* parent=n->data.variable_node.parent; tobject_t* parent_obj = parent->execute(parent,s,retArg); tobject_t* o=NULL; char* name = string_dupp(n->data.function_call_node.name); if(parent_obj->type == tdict && dict_haskey(parent_obj->data.dict,name)) { tobject_t* fname = dict_getkvp(parent_obj->data.dict,name)->value; if(fname->type == texternalmethod || fname->type == tinternalmethod) { list_tobject_t args; list_create(s->rt,&args,0); int i; for(i = 0;i< n->data.function_call_node.count;i++) { node_t* a =n->data.function_call_node.args[i]; retarg_t _arg; _arg.isContinue=false; _arg.isBreaking=false; _arg.isReturning=false; list_add(&args,a->execute(a,s,&_arg)); } o = tobject_call(s,fname,&args); o->count++; //ensures this object is not freed when freeing list list_free(&args); o->count--; } } if(parent_obj->type == tbool) { if(strcmp(name,"toChar") == 0) { o = tobject_create(s->rt); o->type = tchar; o->data.chr= parent_obj->data.boolean ? 'y' : 'n'; } if(strcmp(name,"toNumber") == 0) { o = tobject_create(s->rt); o->type = tnumber; o->data.number= parent_obj->data.boolean ? 1 : 0; } } if(parent_obj->type == tchar) { if(strcmp(name,"toUpper") == 0) { o = tobject_create(s->rt); o->type = tchar; o->data.chr=(char)toupper(parent_obj->data.chr); } if(strcmp(name,"toLower") == 0) { o = tobject_create(s->rt); o->type = tchar; o->data.chr=(char)tolower(parent_obj->data.chr); } if(strcmp(name,"toNumber") == 0) { o = tobject_create(s->rt); o->type = tnumber; o->data.number = parent_obj->data.chr; } } if(parent_obj->type == tstring) { int argcount = n->data.function_call_node.count; node_t** args=n->data.function_call_node.args; if(strcmp(name,"replace") == 0) { if(argcount >= 2) { tobject_t* a0=args[0]->execute(args[0],s,retArg); tobject_t* a1=args[1]->execute(args[1],s,retArg); string_t* s1=tobject_tostring(s,a0); string_t* s2=tobject_tostring(s,a1); string_t* s3=string_replace(parent_obj->data.string,s1,s2); string_free(s1); string_free(s2); o=tobject_string(s->rt,s3); } }else if(strcmp(name,"substring") == 0) { if(argcount>=1) { tobject_t* a0=args[0]->execute(args[0],s,retArg); int i = 0; int length = parent_obj->data.string->length; if(a0->type == tnumber) { i = (int)a0->data.number; if(i<0) i = 0; if(i>length) i = length-1; length = length - i; } if(argcount >= 2) { tobject_t* a1=args[1]->execute(args[1],s,retArg); if(a1->type == tnumber) { int len = (int)a1->data.number; if(len < length) length = len; } } string_t* s3=string_substring(parent_obj->data.string,i,length); o=tobject_string(s->rt,s3); } }else if(strcmp(name,"remove") == 0) { if(argcount>=1) { tobject_t* a0=args[0]->execute(args[0],s,retArg); int i = 0; int length = parent_obj->data.string->length; if(a0->type == tnumber) { i = (int)a0->data.number; if(i<0) i = 0; if(i>length) i = length-1; length = length - i; } if(argcount >= 2) { tobject_t* a1=args[1]->execute(args[1],s,retArg); if(a1->type == tnumber) { int len = (int)a1->data.number; if(len < length) length = len; } } string_t* s3=string_remove(parent_obj->data.string,i,length); o=tobject_string(s->rt,s3); } }else if(strcmp(name,"split") == 0) { if(argcount >= 1) { tobject_t* sOn=args[0]->execute(args[0],s,retArg); string_t* splitOn=tobject_tostring(s,sOn); int count = 2147483647; bool splitOp=false; if(argcount >= 2) { tobject_t* sOn=args[1]->execute(args[1],s,retArg); splitOp = tobject_tobool(sOn); //tobject_freeifzero(sOn); } if(argcount >= 3) { tobject_t* _count=args[2]->execute(args[2],s,retArg); if(_count->type == tnumber) { count = (int)_count->data.number; } //tobject_freeifzero(_count); } tobject_t* s3=string_split(s->rt,parent_obj->data.string,splitOn,count,!splitOp); string_free(splitOn); o=s3; } } if(strcmp(name,"startsWith") == 0) { if(argcount >= 1) { tobject_t* sOn=args[0]->execute(args[0],s,retArg); string_t* starts=tobject_tostring(s,sOn); o = tobject_bool(s->rt,string_startswith(parent_obj->data.string,starts)); string_free(starts); // tobject_freeifzero(sOn); } } if(strcmp(name,"endsWith") == 0) { if(argcount >= 1) { tobject_t* sOn=args[0]->execute(args[0],s,retArg); string_t* starts=tobject_tostring(s,sOn); o = tobject_bool(s->rt,string_endswith(parent_obj->data.string,starts)); if(o->data.boolean) { int i = 0; } string_free(starts); // tobject_freeifzero(sOn); } } if(strcmp(name,"trimStart") == 0) { char c = ' '; if(argcount >= 1) { tobject_t* sOn=args[0]->execute(args[0],s,retArg); if(sOn->type == tchar) { c = sOn->data.chr; } tobject_freeifzero(sOn); } o = tobject_string(s->rt,string_trimstarts(parent_obj->data.string,c)); } if(strcmp(name,"trimEnd") == 0) { char c = ' '; if(argcount >= 1) { tobject_t* sOn=args[0]->execute(args[0],s,retArg); if(sOn->type == tchar) { c = sOn->data.chr; } tobject_freeifzero(sOn); } o = tobject_string(s->rt,string_trimends(parent_obj->data.string,c)); } } if(parent_obj->type == tnumber) { if(strcmp(name,"abs") == 0) { double val=fabs(parent_obj->data.number); o = tobject_create(s->rt); o->type = tnumber; o->data.number = val; } if(strcmp(name,"int") == 0) { int64_t val=(int64_t)parent_obj->data.number; o = tobject_create(s->rt); o->type = tnumber; o->data.number = val; } if(strcmp(name,"toChar") == 0) { o = tobject_create(s->rt); o->type = tchar; o->data.chr = (char)parent_obj->data.number; } } if(parent_obj->type == tlist) { int argcount = n->data.function_call_node.count; node_t** args=n->data.function_call_node.args; if(strcmp(name,"add") == 0) { if(argcount >= 1) { tobject_t* o2=args[0]->execute(args[0],s,retArg); list_add(&parent_obj->data.list,o2); o=parent_obj; } } if(strcmp(name,"remove") == 0) { if(argcount >= 1) { tobject_t* o2=args[0]->execute(args[0],s,retArg); list_remove(&parent_obj->data.list,o2); o=parent_obj; } } if(strcmp(name,"at")==0) { if(argcount >= 1) { tobject_t* o2=args[0]->execute(args[0],s,retArg); if(o2->type != tnumber) { int index=(int)o2->data.number; if(index < parent_obj->data.list.length) { o = parent_obj->data.list.items[index]; } } tobject_freeifzero(o2); } } if(strcmp(name,"setAt") == 0) { tobject_t* val = NULL; if(argcount >= 2) { val = args[1]->execute(args[1],s,retArg); o=val; } else if(argcount == 1) { val = tobject_basic(s->rt,tnull); o = val; } if(argcount >= 1) { tobject_t* o2=args[0]->execute(args[0],s,retArg); if(o2->type != tnumber) { int index=(int)o2->data.number; if(index < parent_obj->data.list.length) { list_set(&parent_obj->data.list,index,o2); //o = parent_obj->data.list.items[index]; } } tobject_freeifzero(o2); } } } if(o == NULL) { if(strcmp(name,"toString") == 0) { o = tobject_string(s->rt,tobject_tostring(s,parent_obj)); } else { o = tobject_basic(s->rt,tundef); } } free(name); o->count++; tobject_freeifzero(parent_obj); o->count--; return o; } void __node_method_call_free(node_t* node) { node_t** args= node->data.function_call_node.args; int i; for(i=0;idata.function_call_node.count;i++) { node_free(args[i]); } free(args); node_free(node->data.function_call_node.parent); string_free(node->data.function_call_node.name); } node_t* node_method_call_create(node_t* parent,string_t* name) { node_t* n = (node_t*)malloc(sizeof(node_t)); n->type = methodcallnode; n->data.function_call_node.count = 0; n->data.function_call_node.name = string_dups(name); n->data.function_call_node.capacity=64; n->data.function_call_node.args=(node_t**)malloc(sizeof(node_t*) * n->data.scope_node.capacity); n->data.function_call_node.parent = parent; n->execute = __node_method_call_execute; n->free = __node_method_call_free; return n; } void node_method_call_add(node_t* fc,node_t* arg) { if(fc->data.function_call_node.count + 128 >fc->data.scope_node.capacity) { fc->data.function_call_node.capacity = fc->data.function_call_node.count + 128; fc->data.function_call_node.args=(node_t**)realloc(fc->data.function_call_node.args,fc->data.function_call_node.capacity * sizeof(node_t*)); } fc->data.function_call_node.args[fc->data.function_call_node.count++]=arg; }