interpreter.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501
  1. /**
  2. * This file is part of Gomu.
  3. *
  4. * Copyright 2016 by Jean Fromentin <jean.fromentin@math.cnrs.fr>
  5. *
  6. * Gomu is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Gomu is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Gomu. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "interpreter.hpp"
  20. namespace Gomu{
  21. //************************************************************
  22. //* Definition of fundamental types declared in modules.hpp *
  23. //************************************************************
  24. Type *type_array=new Type("Array",array_disp,array_del,array_copy,array_comp);
  25. Type *type_boolean=new Type("Boolean",boolean_disp,boolean_del,boolean_copy,boolean_comp);
  26. Type *type_context=new Type("Context",context_disp,context_del,context_copy,context_comp);
  27. Type *type_generic=new Type("Generic",nullptr,nullptr,nullptr,nullptr);
  28. Type *type_integer=new Type("Integer",integer_disp,integer_del,integer_copy,integer_comp);
  29. Type *type_function=new Type("Function",function_disp,function_del,function_copy,function_comp);
  30. Type *type_contextual_function=new Type("ContextualFunction",contextual_function_disp,contextual_function_del,contextual_function_copy,contextual_function_comp);
  31. Type *type_meta_function=new Type("MetaFunction",meta_function_disp,meta_function_del,meta_function_copy,meta_function_comp);
  32. Type *type_module=new Type("Module",module_disp,module_del,module_copy,module_comp);
  33. Type *type_set=new Type("Set",set_disp,set_del,set_copy,set_comp);
  34. Type *type_string=new Type("String",string_disp,string_del,string_copy,string_comp);
  35. Type *type_symbol=new Type("Symbol",nullptr,nullptr,nullptr,nullptr);
  36. Type *type_tuple=new Type("Tuple",tuple_disp,tuple_del,tuple_copy,tuple_comp);
  37. Type *type_type=new Type("Type",type_disp,type_del,type_copy,type_comp);
  38. Type *type_void=new Type("Void",void_disp,void_del,void_copy,void_comp);
  39. //***********************
  40. //* Context definitions *
  41. //***********************
  42. //--------------------
  43. // Context::Context()
  44. //--------------------
  45. Context::Context(Interpreter* inter){
  46. interpreter=inter;
  47. add_symbol("context",type_context,this)->hide=true;
  48. add_symbol("Array",type_type,type_array);
  49. add_symbol("Boolean",type_type,type_boolean)->hide=true;
  50. add_symbol("Context",type_type,type_context)->hide=true;
  51. add_symbol("Generic",type_type,type_generic)->hide=true;
  52. add_symbol("Integer",type_type,type_integer)->hide=true;
  53. add_symbol("Function",type_type,type_function)->hide=true;
  54. add_symbol("ContextualFunction",type_type,type_contextual_function)->hide=true;
  55. add_symbol("Module",type_type,type_module);
  56. add_symbol("Set",type_type,type_set);
  57. add_symbol("String",type_type,type_string);
  58. add_symbol("Symbol",type_type,type_symbol)->hide=true;
  59. add_symbol("Tuple",type_type,type_tuple);
  60. add_symbol("Type",type_type,type_type)->hide=true;
  61. add_symbol("Void",type_type,type_void)->hide=true;
  62. }
  63. //--------------------------------------------------------
  64. // Context::add_function(string,string,string_list,void*)
  65. //--------------------------------------------------------
  66. void
  67. Context::add_function(string ret,string name,string_list args,void* ptr){
  68. Type* tr=get_type(ret);
  69. Signature signature=get_signature(args);
  70. Symbol* symbol=get_symbol(name);
  71. if(symbol==nullptr or (symbol!=nullptr and
  72. not symbol->locked and
  73. symbol->type!=type_meta_function and
  74. symbol->type!=type_function)){
  75. Function* function=new Function(tr,signature,ptr);
  76. add_symbol(name,type_function,function,true);
  77. return;
  78. }
  79. string fullname=function_fullname(name,args);
  80. if(symbol->type==type_meta_function){
  81. if(not can_add(fullname))
  82. ContextError("A locked symbol named "+fullname+" already exists");
  83. MetaFunction* meta_function=(MetaFunction*)symbol->ptr;
  84. meta_function->functions.insert(fullname);
  85. Function* function=new Function(tr,signature,ptr);
  86. add_symbol(fullname,type_function,function,true);
  87. return;
  88. }
  89. if(symbol->type==type_function){
  90. Function* function=new Function(tr,signature,ptr);
  91. add_symbol(fullname,type_function,function,true);
  92. MetaFunction* meta_function=new MetaFunction;
  93. meta_function->functions.insert(fullname);
  94. function=(Function*)symbol->ptr;
  95. fullname=function_fullname(name,function->signature);
  96. add_symbol(fullname,type_function,function,true);
  97. meta_function->functions.insert(fullname);
  98. symbol->type=type_meta_function;
  99. symbol->ptr=(void*)meta_function;
  100. return;
  101. }
  102. ContextError("A locked symbol named "+name+" already exist");
  103. }
  104. //-------------------------------------------------------------
  105. // Context::add_member_function(string,string,string_list,ptr)
  106. //-------------------------------------------------------------
  107. void
  108. Context::add_member_function(string ret,string name,string_list args,void* ptr){
  109. if(args.size()==0) ContextError("Member function must have at least one argument");
  110. string cname=*args.begin();
  111. Type* ctype=get_type(cname);
  112. if(ctype==nullptr) ContextError("The class type "+cname+" does not exist.");
  113. add_function(ret,cname+"."+name,args,ptr);
  114. }
  115. //--------------------------------------------
  116. // Context::add_symbol(name,Type*,void*,bool)
  117. //--------------------------------------------
  118. Symbol*
  119. Context::add_symbol(string name,Type* type,void* ptr,bool lock){
  120. Symbol& s=symbols[name];
  121. if(s.locked) ContextError("A locked symbol named "+name+" already exist");
  122. s.del();s.type=type;
  123. s.ptr=ptr;s.locked=lock;
  124. s.name=name;
  125. s.hide=false;
  126. return &s;
  127. }
  128. //-----------------------------------------------
  129. // Context::can_add_function(string,string_list)
  130. //-----------------------------------------------
  131. bool
  132. Context::can_add_function(string name,string_list targs){
  133. if(can_add(name)) return true;
  134. if(not can_add(function_fullname(name,targs))) return false;
  135. Symbol* symbol=get_symbol(name);
  136. return symbol->type==type_meta_function or symbol->type==type_function;
  137. }
  138. //-----------------------------------------------
  139. // Context::can_add_member_function(string,string_list)
  140. //-----------------------------------------------
  141. bool
  142. Context::can_add_member_function(string name,string_list targs){
  143. if(targs.size()==0) ContextError("A member function must have at least one argument");
  144. string cname=*(targs.begin());
  145. Type* ctype=get_type(cname);
  146. if(ctype==nullptr) ContextError("The class type "+cname+" does not exist");
  147. return can_add(cname+"."+name);
  148. }
  149. //-----------------------------------------
  150. // Context::eval_array(size_t,Node&,Node*)
  151. //-----------------------------------------
  152. void
  153. Context::eval_array(size_t size,Node& current, Node* nodes){
  154. ArrayValue* array;
  155. if(size==0) array=new ArrayValue(0);
  156. else{
  157. size_t j=current.son;
  158. int k=0;
  159. Type* type=nodes[j].value.eval()->type;
  160. if(type==nullptr or type==type_void)
  161. ContextError("Non empty Array of undefinded type is forbidden");
  162. array=new ArrayValue(size);
  163. array->type=type;
  164. while(j!=-1){
  165. Node* node=&nodes[j];
  166. Value* val=node->value.eval();
  167. //Test if the type is correct
  168. if(val->type!=type){
  169. //No -> delete array
  170. for(int i=0;i<k;++i) type->del(array->tab[i]);
  171. delete[] array->tab;
  172. delete array;
  173. current.value.ptr=nullptr;
  174. current.value.type=type_void;
  175. ContextError("Elements of an Array must have same type");
  176. }
  177. //We cannot stole pointer of value for a symbol
  178. if(node->value.type==type_symbol){
  179. array->tab[k++]=type->copy(val->ptr);
  180. }
  181. else{
  182. array->tab[k++]=val->ptr;
  183. val->type=type_void;
  184. val->ptr=nullptr;
  185. }
  186. j=node->bro;
  187. }
  188. }
  189. current.value.ptr=array;
  190. current.value.type=type_array;
  191. }
  192. //-----------------------------------------------------------------------
  193. // Context::eval_contextual_function(ContextualFunction*,Value**,size_t)
  194. //-----------------------------------------------------------------------
  195. Value
  196. Context::eval_contextual_function(ContextualFunction* func,Value** args,size_t nargs){
  197. const Signature& signature=func->signature;
  198. if(nargs!=func->signature.size())
  199. ContextError("The number of arguments is incorect ("+to_string(nargs)+" instead of "+to_string(func->signature.size())+").");
  200. for(size_t i=0;i<nargs;++i){
  201. if(args[i]->type==type_symbol and signature[i]!=type_symbol){
  202. args[i]=(Value*)args[i]->ptr;
  203. }
  204. if(signature[i]!=type_generic and signature[i]!=args[i]->type)
  205. ContextError("Argument "+to_string(i+1)+" type is incorrect ("+type_disp(args[i]->type)+" instead of "+type_disp(signature[i])+").");
  206. }
  207. return func->eval(args,nargs,*this);
  208. }
  209. //--------------------------------------------------
  210. // Context::eval_function(Function*,Value**,size_t)
  211. //--------------------------------------------------
  212. Value
  213. Context::eval_function(Function* function,Value** args,size_t nargs){
  214. if(nargs!=function->signature.size())
  215. ContextError("Number of argument is "+to_string(nargs)+" instead of "+to_string(function->signature.size()));
  216. Type* targ;
  217. for(size_t i=0;i<nargs;++i){
  218. if(args[i]->type==type_symbol){
  219. targ=((Value*)(args[i]->ptr))->type;
  220. }
  221. else{
  222. targ=args[i]->type;
  223. }
  224. if(targ!=function->signature[i])
  225. ContextError("Argument "+to_string(i)+" is of type "+type_disp(targ)+" instead of "+type_disp(function->signature[i]));
  226. }
  227. return function->eval(args,nargs);
  228. }
  229. //-------------------------------------
  230. // Context::eval_function(Node&,Node*)
  231. //-------------------------------------
  232. void
  233. Context::eval_function(Node& current,Node* nodes){
  234. string name=current.str;
  235. Symbol* symbol=get_symbol(name);
  236. if(symbol==nullptr)
  237. ContextError("There is no symbol named "+name);
  238. Value* args[max_arguments_number];
  239. size_t nargs=set_arguments(current,nodes,args);
  240. current.value=eval_function(symbol,args,nargs);
  241. }
  242. //------------------------------------------------
  243. // Context::eval_function(Symbol*,Value**,size_t)
  244. //------------------------------------------------
  245. Value
  246. Context::eval_function(Symbol* symbol,Value** args,size_t nargs){
  247. if(symbol->type==type_contextual_function){
  248. return eval_contextual_function((ContextualFunction*)symbol->ptr,args,nargs);
  249. }
  250. else if(symbol->type==type_function){
  251. return eval_function((Function*)symbol->ptr,args,nargs);
  252. }
  253. else if(symbol->type==type_meta_function){
  254. string fullname=function_fullname(symbol->name,args,nargs);
  255. Value *val=get_symbol(fullname);
  256. if(val==nullptr) ContextError("There is no function "+fullname);
  257. if(val->type!=type_function) Bug("This case shall be impossible!");
  258. return eval_function((Function*)val->ptr,args,nargs);
  259. }
  260. else{
  261. Bug("The symbol named "+symbol->name+" is not callable");
  262. }
  263. }
  264. //--------------------------------------------
  265. // Context::eval_member_function(Node&,Node*)
  266. //--------------------------------------------
  267. void
  268. Context::eval_member_function(Node& current,Node* nodes){
  269. Value* args[max_arguments_number];
  270. size_t nargs=set_arguments(current,nodes,args);
  271. Value* first=args[0]->eval();
  272. Type* type=first->type;
  273. Value* val=get_symbol(type,current.str);
  274. if(val==nullptr){
  275. if(type==type_type){
  276. type=(Type*)first->ptr;
  277. val=get_symbol(type,current.str);
  278. //current.son=nodes[current.son].bro;
  279. if(val==nullptr)
  280. ContextError("No member function "+current.str+" for tyipe "+type->name);
  281. current.value=eval_function((Function*)val->ptr,&args[1],nargs-1);
  282. return;
  283. }
  284. else{
  285. ContextError("No member function "+current.str+" for type "+type->name);
  286. }
  287. }
  288. if(val->type!=type_function)
  289. ContextError("The symbol named "+current.str+" is not callable");
  290. current.value=eval_function((Function*)val->ptr,args,nargs);
  291. }
  292. //---------------------------------------
  293. // Context::eval_set(size_t,Node&,Node*)
  294. //---------------------------------------
  295. void
  296. Context::eval_set(size_t size,Node& current, Node* nodes){
  297. SetValue* set;
  298. if(size==0) set=new SetValue(type_generic);
  299. else{
  300. size_t j=current.son;
  301. int k=0;
  302. Type* type=nodes[j].value.eval()->type;
  303. if(type==nullptr or type==type_void)
  304. ContextError("Non empty Set of undefinded type is forbidden");
  305. set=new SetValue(type);
  306. while(j!=-1){
  307. Node* node=&nodes[j];
  308. Value* val=node->value.eval();
  309. //Test if the type is correct
  310. if(val->type!=type){
  311. //No -> delete set
  312. for(auto it=set->data.begin();it!=set->data.end();++it) type->del(*it);
  313. delete set;
  314. current.value.ptr=nullptr;
  315. current.value.type=type_void;
  316. ContextError("Elements of a Set must have same type");
  317. }
  318. //Check if the value is not in the set otherwise do nothing
  319. if(set->data.find(val->ptr)==set->data.end()){
  320. if(node->value.type==type_symbol){
  321. //We cannot stole pointer of value for a symbol
  322. set->data.insert(type->copy(val->ptr));
  323. }
  324. else{
  325. set->data.insert(val->ptr);
  326. val->type=type_void;
  327. val->ptr=nullptr;
  328. }
  329. }
  330. j=node->bro;
  331. }
  332. }
  333. current.value.ptr=set;
  334. current.value.type=type_set;
  335. }
  336. //-----------------------------------------
  337. // Context::eval_tuple(size_t,Node&,Node*)
  338. //-----------------------------------------
  339. void
  340. Context::eval_tuple(size_t size,Node& current, Node* nodes){
  341. TupleValue* tuple;
  342. if(size==0) tuple=new TupleValue(0);
  343. else{
  344. size_t j=current.son;
  345. int k=0;
  346. tuple=new TupleValue(size);
  347. while(j!=-1){
  348. Node* node=&nodes[j];
  349. Value* val=node->value.eval();
  350. //We cannot stole pointer of value for a symbol
  351. if(node->value.type==type_symbol){
  352. tuple->tab[k++]=val->copy();
  353. }
  354. else{
  355. tuple->tab[k++]=*val;
  356. val->type=type_void;
  357. val->ptr=nullptr;
  358. }
  359. j=node->bro;
  360. }
  361. }
  362. current.value.ptr=tuple;
  363. current.value.type=type_tuple;
  364. }
  365. //-------------------------------------
  366. // Context::get_signature(string_list)
  367. //-------------------------------------
  368. Signature
  369. Context::get_signature(string_list targs){
  370. Signature s(targs.size());
  371. size_t i=0;
  372. for(auto its=targs.begin();its!=targs.end();++its) s[i++]=get_type(*its);
  373. return s;
  374. }
  375. //---------------------------
  376. // Context::get_symbol(name)
  377. //---------------------------
  378. Symbol*
  379. Context::get_symbol(string name){
  380. auto it=symbols.find(name);
  381. if(it==symbols.end()) return nullptr;
  382. return &it->second;
  383. }
  384. //-------------------------
  385. // Context::get_type(name)
  386. //-------------------------
  387. Type*
  388. Context::get_type(string name){
  389. Value* v=get_symbol(name);
  390. if(v==nullptr) ContextError("Type "+name+" does not exist");
  391. if(v->type!=type_type) ContextError(name+" does not design a type");
  392. return (Type*)v->ptr;
  393. }
  394. //----------------------------
  395. // Context::load_module(name)
  396. //----------------------------
  397. void
  398. Context::load_module(string name){
  399. string filename="ext/"+name+".so";
  400. Symbol* symbol=get_symbol(name);
  401. if(symbol!=nullptr and symbol->locked==true) ContextError("A locked symbol "+name+" already exist");
  402. void* handle=dlopen(filename.c_str(),RTLD_NOW);
  403. if(not handle) RuntimeError("In loading module "+name+" : "+dlerror());
  404. Module* module=new Module;
  405. symbol=add_symbol(name,type_module,module);
  406. module->handle=handle;
  407. module->name=name;
  408. //Loading types
  409. Type* types=(Type*)dlsym(handle,"types");
  410. module->functions=(Module::Function*)dlsym(handle,"functions");
  411. module->member_functions=(Module::Function*)dlsym(handle,"member_functions");
  412. module->contextual_functions=(Module::Function*)dlsym(handle,"contextual_functions");
  413. module->init(types);
  414. //Test if we can load all types
  415. for(size_t i=0;i<module->ntype;++i){
  416. if(not can_add(types[i].name)){
  417. dlclose(handle);
  418. delete(module);
  419. ContextError("The type "+name+" cannot be added because a locked symbol has the same name");
  420. }
  421. }
  422. module->types=new string[module->ntype];
  423. for(size_t i=0;i<module->ntype;++i){
  424. Type* type=new Type(types[i]);
  425. module->types[i]=type->name;
  426. add_symbol(type->name,type_type,type);
  427. }
  428. load_module_functions(module,0);
  429. load_module_functions(module,1);
  430. load_module_functions(module,2);
  431. }
  432. //---------------------------------------------
  433. // Context::load_module_functions(Module*,int)
  434. //---------------------------------------------
  435. void Context::load_module_functions(Module* module,int src){
  436. bool (Context::*can_add_func)(string,string_list);
  437. void (Context::*add_func)(string,string,string_list,void*);
  438. Module::Function* module_func_ptr;
  439. size_t n;
  440. switch(src){
  441. case 0:
  442. can_add_func=&Context::can_add_function;
  443. add_func=&Context::add_function;
  444. module_func_ptr=module->functions;
  445. n=module->nfunc;
  446. break;
  447. case 1:
  448. can_add_func=&Context::can_add_member_function;
  449. add_func=&Context::add_member_function;
  450. module_func_ptr=module->member_functions;
  451. n=module->nmfunc;
  452. break;
  453. case 2:
  454. can_add_func=&Context::can_add_contextual_function;
  455. add_func=&Context::add_contextual_function;
  456. module_func_ptr=module->contextual_functions;
  457. n=module->ncfunc;
  458. break;
  459. default:
  460. Bug("Not yet implemented");
  461. }
  462. for(size_t i=0;i<n;++i){
  463. Module::Function& function=module_func_ptr[i];
  464. try{
  465. function.loaded=(this->*can_add_func)(function.name,function.targs);
  466. }
  467. catch(Error& err){
  468. err.disp(cout,"");
  469. function.loaded=false;
  470. }
  471. if(function.loaded){
  472. (this->*add_func)(function.tr,function.name,function.targs,function.ptr);
  473. }
  474. else{
  475. cout<<"Warning : ";
  476. if(src==0) cout<<"Function";
  477. else if(src==1) cout<<"Member function";
  478. else cout<<"Contextual function";
  479. cout<<" "<<function.name<<" not loaded"<<endl;
  480. }
  481. }
  482. }
  483. //----------------------------------------------
  484. // Context::unload_function(string,string_list)
  485. //----------------------------------------------
  486. void
  487. Context::unload_function(string name,string_list args){
  488. string fullname=function_fullname(name,args);
  489. unload_symbol(fullname);
  490. Symbol* symbol=get_symbol(name);
  491. if(symbol==nullptr) Bug("The meta function "+name+" does not exist");
  492. if(symbol->type==type_function){
  493. unload_symbol(name);
  494. return;
  495. }
  496. if(symbol->type!=type_meta_function) Bug("The symbol "+name+" is not of type Function or MetaFunction");
  497. MetaFunction* meta=(MetaFunction*)(symbol->ptr);
  498. meta->functions.erase(fullname);
  499. if(meta->functions.empty()) //There is no more function called name
  500. unload_symbol(name);
  501. }
  502. //---------------------------------
  503. // Context::unload_module(Module*)
  504. //---------------------------------
  505. void
  506. Context::unload_module(Module* module){
  507. unload_module_functions(module);
  508. for(size_t i=0;i<module->ntype;++i){
  509. unload_type(get_type(module->types[i]));
  510. }
  511. dlclose(module->handle);
  512. unload_symbol(module->name);
  513. }
  514. //-------------------------------------------
  515. // Context::unload_module_functions(Module*)
  516. //-------------------------------------------
  517. void
  518. Context::unload_module_functions(Module* module){
  519. for(size_t i=0;i<module->nfunc;++i){
  520. if(module->functions[i].loaded)
  521. unload_function(module->functions[i].name,module->functions[i].targs);
  522. }
  523. for(size_t i=0;i<module->nmfunc;++i){
  524. Module::Function& func=module->member_functions[i];
  525. if(func.loaded)
  526. unload_function(*func.targs.begin()+"."+func.name,func.targs);
  527. }
  528. for(size_t i=0;i<module->ncfunc;++i){
  529. if(module->contextual_functions[i].loaded)
  530. unload_symbol(module->contextual_functions[i].name);
  531. }
  532. }
  533. //-----------------------------
  534. // Context::unload_type(Type*)
  535. //-----------------------------
  536. void
  537. Context::unload_type(Type* type){
  538. for(auto it=symbols.begin();it!=symbols.end();++it){
  539. if(it->second.type==type)
  540. unload_symbol(it->first);
  541. }
  542. unload_symbol(type->name);
  543. }
  544. //--------------------------
  545. // Context::reload(Module*)
  546. //--------------------------
  547. void
  548. Context::reload_module(Module* module){
  549. //** Unload some module member
  550. unload_module_functions(module); //unload functions
  551. if(module->handle!=nullptr){
  552. dlclose(module->handle); //close previous module handle
  553. module->handle=nullptr;
  554. }
  555. //we postpone the unload of module->types
  556. //** Load the new version of the library
  557. string filename="ext/"+module->name+".so";
  558. void* handle=dlopen(filename.c_str(),RTLD_NOW);
  559. if(not handle){
  560. handle=nullptr;
  561. RuntimeError("In loading module "+module->name+" : "+dlerror());
  562. }
  563. Module nmod; //A temporary module for the newly loaded data
  564. Type* types=(Type*)dlsym(handle,"types");
  565. nmod.functions=(Module::Function*)dlsym(handle,"functions");
  566. nmod.member_functions=(Module::Function*)dlsym(handle,"member_functions");
  567. nmod.contextual_functions=(Module::Function*)dlsym(handle,"contextual_functions");
  568. nmod.init(types);//init the module, ie, compute size of arrays
  569. //Test if we can load the new types
  570. int link[nmod.ntype];
  571. for(size_t i=0;i<nmod.ntype;++i){
  572. string name=types[i].name;
  573. link[i]=-1;
  574. if(not can_add(name)){
  575. for(size_t j=0;j<module->ntype;++j){
  576. if(module->types[j]==name){
  577. link[i]=j;
  578. break;
  579. }
  580. }
  581. if(link[i]==-1){//We can't load a type
  582. //close the library
  583. dlclose(handle);
  584. ContextError("The type "+name+" cannot be added because a locked symbol has the same name");
  585. }
  586. }
  587. }
  588. //Look for old types to unload, ie, types in the old version of the library
  589. //that are not present in the new version
  590. bool to_unload[module->ntype];
  591. for(size_t i=0;i<module->ntype;++i){
  592. to_unload[i]=true;
  593. for(size_t j=0;j<nmod.ntype;++j){
  594. if(link[j]==i){
  595. to_unload[i]=false;
  596. break;
  597. }
  598. }
  599. }
  600. //Unload marked types
  601. for(size_t i=0;i<module->ntype;++i){
  602. if(to_unload[i]) unload_type(get_type(module->types[i]));
  603. }
  604. //Update or load the types in the new version of the library
  605. delete[] module->types; //unload types array
  606. module->types=new string[nmod.ntype];
  607. for(size_t i=0;i<nmod.ntype;++i){
  608. Type& nmod_type=types[i];
  609. module->types[i]=nmod_type.name;
  610. if(link[i]==-1){
  611. Type *type=new Type(nmod_type);
  612. add_symbol(type->name,type_type,type);
  613. }
  614. else{
  615. Type* type=get_type(nmod_type.name);
  616. type->disp=nmod_type.disp;
  617. type->del=nmod_type.del;
  618. type->copy=nmod_type.copy;
  619. type->comp=nmod_type.comp;
  620. }
  621. }
  622. module->ntype=nmod.ntype;
  623. module->functions=nmod.functions;
  624. module->nfunc=nmod.nfunc;
  625. module->member_functions=nmod.member_functions;
  626. module->nmfunc=nmod.nmfunc;
  627. module->contextual_functions=nmod.contextual_functions;
  628. module->ncfunc=nmod.ncfunc;
  629. module->handle=handle;
  630. load_module_functions(module,0);
  631. load_module_functions(module,1);
  632. load_module_functions(module,2);
  633. }
  634. //---------------------------------------------
  635. // Context::set_arguments(Node&,Node*,Value**)
  636. //---------------------------------------------
  637. size_t
  638. Context::set_arguments(Node& current,Node* nodes,Value** args){
  639. size_t j=current.son;
  640. size_t nargs=0;
  641. while(j!=-1){
  642. args[nargs++]=&nodes[j].value;
  643. j=nodes[j].bro;
  644. }
  645. return nargs;
  646. }
  647. //**********************
  648. //* ContextualFunction *
  649. //**********************
  650. //---------------------------------------------------
  651. // ContextualFunction::eval(Value**,size_t,Context&)
  652. //---------------------------------------------------
  653. Value ContextualFunction::eval(Value* args[8],size_t nargs,Context& context){
  654. switch(nargs){
  655. case 0:
  656. return (*((CFunc0)ptr))(context);
  657. case 1:
  658. return (*((CFunc1)ptr))(context,*args[0]);
  659. case 2:
  660. return (*((CFunc2)ptr))(context,*args[0],*args[1]);
  661. default:
  662. Bug("Not yet implemented");
  663. break;
  664. }
  665. }
  666. //************
  667. //* Function *
  668. //************
  669. //--------------------------------
  670. // Function::eval(Value**,size_t)
  671. //--------------------------------
  672. Value Function::eval(Value* args[8],size_t nargs){
  673. Value res;
  674. res.type=tr;
  675. switch(nargs){
  676. case 1:
  677. res.ptr=(*((Func1)ptr))(args[0]->eval()->ptr);
  678. break;
  679. default:
  680. Bug("Not yet implemented");
  681. }
  682. return res;
  683. }
  684. //***************
  685. //* Interpreter *
  686. //***************
  687. //-------------------------------------------------------------------------
  688. // Interperter::add_operator(const string&,const string&,OperatorType,int)
  689. //-------------------------------------------------------------------------
  690. void
  691. Interpreter::add_operator(const string& op,const string& name,OperatorType type,int priority){
  692. OperatorInfo* opInfo=new OperatorInfo(name,type,priority);
  693. operator_tree.add(op,opInfo);
  694. }
  695. //------------------------------------------------------------------------
  696. // Interpreter::complete(const string&,const string&,size_t,int,Context&)
  697. //------------------------------------------------------------------------
  698. string
  699. Interpreter::complete(const string& cmd,const string& word,size_t pos,int state,Context& context){
  700. if(state==0){
  701. split_to_tokens(cmd);
  702. size_t i;
  703. for(i=0;i<nodes_number;++i){
  704. if(nodes[i].pos>pos) break;
  705. }
  706. if(i!=0) --i;
  707. if(i>0 and nodes[i-1].tokenType==tDot) --i;
  708. if(nodes[i].tokenType==tDot){
  709. //*.word
  710. if(i>0 and nodes[i-1].tokenType==tName){
  711. Symbol* symbol=context.get_symbol(nodes[i-1].str);
  712. if(symbol==nullptr) return "";
  713. if(symbol->type==type_type){
  714. completion.type=context.get_type(nodes[i-1].str);
  715. }
  716. else{
  717. completion.type=symbol->type;
  718. }
  719. completion.prefix=completion.type->name+".";
  720. completion.word=completion.prefix+word;
  721. completion.it=context.symbols.lower_bound(completion.word);
  722. }
  723. else return "";
  724. }
  725. else{
  726. //word
  727. completion.prefix="";
  728. completion.word=word;
  729. completion.it=context.symbols.lower_bound(completion.word);
  730. completion.type=nullptr;
  731. }
  732. }
  733. //Omit function fullname, hidden symbol and member symbols if no member call
  734. while((completion.it!=context.symbols.end()) and
  735. ((completion.it->second.hide) or
  736. (completion.it->first.find('(')!=string::npos) or
  737. (completion.type==nullptr and completion.it->first.find('.')!=string::npos)
  738. )
  739. )
  740. ++completion.it;
  741. if(completion.it==context.symbols.end()) return "";
  742. if(completion.it->first.compare(0,completion.word.size(),completion.word)==0){
  743. string res=completion.it->first;
  744. ++completion.it;
  745. return res.substr(completion.prefix.size());
  746. }
  747. return "";
  748. }
  749. //-------------------------------------------------
  750. // Interpreter::construct_sequence(size_t&,size_t)
  751. //-------------------------------------------------
  752. size_t
  753. Interpreter::construct_sequence(size_t& pos,size_t last){
  754. Node& node=nodes[pos];
  755. BracketType bt=node.bracketType;
  756. node.expressionType=get_delimiter_expression_type(bt);
  757. int depth=1;
  758. size_t begin=pos;
  759. slong* next=&node.son;
  760. size_t i=++pos;
  761. for(;pos<=last and depth!=0;++pos){
  762. switch(nodes[pos].tokenType){
  763. case tOpenBracket:
  764. ++depth;
  765. break;
  766. case tCloseBracket:
  767. --depth;
  768. break;
  769. case tComma:
  770. if(depth==1){
  771. if(i>pos-1)//Empty
  772. SyntaxError("An Expression is needed",nodes[i-1].pos,nodes[pos].pos);
  773. (*next)=construct_tree(i,pos-1,max_precedence_level);
  774. next=&(nodes[*next].bro);
  775. i=pos+1;
  776. }
  777. break;
  778. default:
  779. break;
  780. }
  781. }
  782. //Close bracket not found
  783. if(depth!=0){
  784. switch(bt){
  785. case bRound:
  786. SyntaxError("Tuple or list has not ending",nodes[begin].pos,nodes[last].pos);
  787. case bSquare:
  788. SyntaxError("Array has not ending",nodes[begin].pos,nodes[last].pos);
  789. case bCurly:
  790. SyntaxError("Set has not ending",nodes[begin].pos,nodes[last].pos);
  791. default:
  792. Bug("Bracket type unkown");
  793. break;
  794. }
  795. }
  796. size_t end=pos-1;
  797. //Close bracket mismatch
  798. if(nodes[end].bracketType!=bt)
  799. SyntaxError("Close bracket mismatch the opening one",nodes[begin].pos,nodes[end].pos);
  800. //The sequence is empty
  801. if(end==begin+1) return begin;
  802. //Add the last element
  803. if(i>=end)//Empty
  804. SyntaxError("An expression is needed",nodes[i-1].pos,nodes[end].pos);
  805. (*next)=construct_tree(i,end-1,max_precedence_level);
  806. return begin;
  807. }
  808. //-------------------------------------------------
  809. // Interpreter::construct_tree(size_t&,size_t,int)
  810. //-------------------------------------------------
  811. size_t
  812. Interpreter::construct_tree(size_t& first,size_t last,int precedence_level){
  813. Node& node=nodes[first];
  814. size_t root=first;
  815. switch(node.tokenType){
  816. case tInteger:
  817. case tString:
  818. node.expressionType=expLeaf;
  819. ++first;
  820. break;
  821. case tName:
  822. ++first;
  823. if(first<=last and nodes[first].tokenType==tOpenBracket){
  824. if(nodes[first].bracketType==bRound){
  825. //Function call
  826. node.son=nodes[construct_sequence(first,last)].son;
  827. node.expressionType=expFunction;
  828. break;
  829. }
  830. else if(nodes[first].bracketType==bSquare){
  831. //Get or set call
  832. node.bro=nodes[construct_sequence(first,last)].son;
  833. Node& nnode=nodes[first];
  834. nnode.son=root;
  835. nnode.str="get";
  836. root=first;
  837. nnode.expressionType=expArrayGet;
  838. node.expressionType=expLeaf;
  839. break;
  840. }
  841. else{
  842. Bug("Not yet implemented");
  843. }
  844. }
  845. else if(first<=last and (nodes[first].tokenType==tDot)){
  846. //Member function call
  847. ++first;
  848. if(first>last or nodes[first].tokenType!=tName){
  849. if(nodes[first-1].tokenType==tDot) SyntaxError("Name missing after dot",nodes[first-1].pos,nodes[first-1].pos);
  850. SyntaxError("Name missing after ::",nodes[first-1].pos,nodes[first-1].pos+1);
  851. }
  852. string name=nodes[first].str;
  853. ++first;
  854. if(first<=last and nodes[first].tokenType==tOpenBracket and nodes[first].bracketType==bRound){
  855. Node& nnode=nodes[first-2];
  856. nnode.son=root;
  857. nnode.str=name;
  858. nnode.expressionType=expMemberFunction;
  859. root=first-2;
  860. node.bro=nodes[construct_sequence(first,last)].son;
  861. node.expressionType=expLeaf;
  862. break;
  863. }
  864. else{
  865. Node& nnode=nodes[first-1];
  866. nnode.son=root;
  867. nnode.expressionType=expMemberFunction;
  868. nnode.str=name;
  869. node.expressionType=expLeaf;
  870. root=first-1;
  871. break;
  872. }
  873. }
  874. else{
  875. node.expressionType=expLeaf;
  876. }
  877. break;
  878. case tOpenBracket:
  879. root=construct_sequence(first,last);
  880. //(Expr) -> Expr
  881. if(node.bracketType==bRound and node.son!=-1 and nodes[node.son].bro==-1){
  882. root=node.son;
  883. }
  884. break;
  885. case tOperator:
  886. if(node.operatorInfo->type!=opPreUnitary and node.str!="-")
  887. SyntaxError("Operator not allowed here",node.pos,node.pos+node.str.length()-1);
  888. node.expressionType=expFunction;
  889. if(node.str=="-"){
  890. node.str="negate";
  891. node.son=construct_tree(++first,last,1);
  892. }
  893. else{
  894. node.str=node.operatorInfo->func;
  895. node.son=construct_tree(++first,last,node.operatorInfo->precedence);
  896. }
  897. break;
  898. case tCloseBracket:
  899. SyntaxError("Missing openning bracket",node.pos,node.pos);
  900. case tComma:
  901. SyntaxError("Unexecepted comma outside a sequence",node.pos,node.pos);
  902. case tUnkown:
  903. SyntaxError("Get unkown token",node.pos,node.pos);
  904. case tEnd:
  905. //Ignore this token
  906. break;
  907. default:
  908. Bug("not yet implemented");
  909. break;
  910. }
  911. size_t lhs;
  912. //We already read an expression
  913. while(first<=last){//There is something on the right
  914. //At this time inly operator are allowed
  915. lhs=root;
  916. root=first;
  917. Node& node=nodes[root];
  918. if(node.tokenType==tOperator){
  919. if(node.operatorInfo->type==opPreUnitary){
  920. SyntaxError("A prefixed unitary operator is not allowed here",node.pos,node.pos+node.str.length()-1);
  921. }
  922. if(node.operatorInfo->precedence>precedence_level) return lhs;
  923. if(node.operatorInfo->type==opPostUnitary){
  924. node.expressionType=expFunction;
  925. node.son=lhs;
  926. ++first;
  927. }
  928. else if(node.operatorInfo->type==opBinary){
  929. if(first==last) SyntaxError("An expression is needed after binary operator",node.pos,node.pos+node.str.length()-1);
  930. size_t rhs=construct_tree(++first,last,node.operatorInfo->precedence);
  931. node.str=node.operatorInfo->func;
  932. node.expressionType=expFunction;
  933. node.son=lhs;
  934. nodes[lhs].bro=rhs;
  935. }
  936. }
  937. else SyntaxError("Only operator are allowed after such expression",node.pos,node.pos+node.str.length()-1);
  938. }
  939. return root;
  940. }
  941. //-------------------------------------------------------
  942. // Interpreter::display_expression_tree(ostream&,size_t)
  943. //-------------------------------------------------------
  944. void
  945. Interpreter::display_expression_tree(ostream& os,size_t i) const{
  946. const Node& node=nodes[i];
  947. switch(node.expressionType){
  948. case expLeaf:
  949. os<<'[';
  950. display_token(os,i);
  951. os<<']';
  952. return;
  953. case expTuple:
  954. os<<"[tuple";
  955. break;
  956. case expArray:
  957. os<<"[array";
  958. break;
  959. case expSet:
  960. os<<"[set";
  961. break;
  962. case expFunction:
  963. os<<"[function '"<<node.str<<"'";
  964. break;
  965. case expMemberFunction:
  966. os<<"[member function '"<<node.str<<"'";
  967. break;
  968. default:
  969. Bug("ExpressionNode type unkown");
  970. }
  971. slong j=node.son;
  972. if(j>=0){//There is at least a son
  973. do{
  974. os<<',';
  975. display_expression_tree(os,j);
  976. j=nodes[j].bro;
  977. }while(j>=0);
  978. }
  979. os<<']';
  980. }
  981. //---------------------------------------------
  982. // Interpreter::display_token(ostream&,size_t)
  983. //---------------------------------------------
  984. void
  985. Interpreter::display_token(ostream& os,size_t i) const{
  986. const Node& node=nodes[i];
  987. switch(node.tokenType){
  988. case tInteger:
  989. os<<"(integer,"<<integer_disp(node.value.ptr)<<')';
  990. return;
  991. case tString:
  992. os<<"(string,"<<node.str<<')';
  993. return;
  994. case tName:
  995. os<<"(name,"<<node.str<<')';
  996. return;
  997. case tEnd:
  998. os<<"(end)";
  999. return;
  1000. case tComma:
  1001. os<<"(comma)";
  1002. return;
  1003. case tOpenBracket:
  1004. os<<"(open "<<node.bracketType<<" bracket)";
  1005. return;
  1006. case tCloseBracket:
  1007. os<<"(close "<<node.bracketType<<" bracket)";
  1008. return;
  1009. case tOperator:
  1010. os<<"(operator, "<<*node.operatorInfo<<")";
  1011. return;
  1012. case tUnkown:
  1013. os<<"(?,"<<node.str<<')';
  1014. return;
  1015. default:
  1016. Bug("Token type unkown");
  1017. return;
  1018. }
  1019. }
  1020. //---------------------------------------
  1021. // Interpreter::display_tokens(ostream&)
  1022. //---------------------------------------
  1023. void
  1024. Interpreter::display_tokens(ostream& os) const{
  1025. for(size_t i=0;i<nodes_number;++i){
  1026. display_token(os,i);
  1027. os<<endl;
  1028. }
  1029. }
  1030. //---------------------------
  1031. // Interpreter::eval(string)
  1032. //---------------------------
  1033. void Interpreter::eval(string cmd,Context& context,bool display){
  1034. size_t root;
  1035. Node* node;
  1036. bool error=false;
  1037. try{
  1038. split_to_tokens(cmd);
  1039. size_t first=0;
  1040. root=construct_tree(first,nodes_number-1,max_precedence_level);
  1041. eval_expression(root,context);
  1042. node=&nodes[root];
  1043. Value* value=node->value.eval();
  1044. if(value->type!=nullptr and value->type!=type_void and display){
  1045. cout<<value->disp()<<endl;
  1046. }
  1047. }
  1048. catch(Error err){
  1049. err.disp(cout,cmd);
  1050. cout<<endl;
  1051. error=true;
  1052. }
  1053. //Delete the return value
  1054. if(not error){
  1055. if(node->erase){
  1056. node->value.del();
  1057. node->erase=false;
  1058. }
  1059. }
  1060. }
  1061. //-----------------------------------------------
  1062. // Interpreter::eval_expression(size_t,Context&)
  1063. //-----------------------------------------------
  1064. void Interpreter::eval_expression(size_t pos,Context& context){
  1065. Node &node=nodes[pos];
  1066. if(node.expressionType==expLeaf){
  1067. if(node.tokenType==tName){
  1068. node.value.type=type_symbol;
  1069. Symbol* symbol=context.get_symbol(node.str);
  1070. if(symbol!=nullptr){
  1071. node.value.ptr=symbol;
  1072. }
  1073. else{
  1074. node.value.ptr=context.add_symbol(node.str,type_void,nullptr,false);
  1075. }
  1076. return;
  1077. }
  1078. return;
  1079. }
  1080. size_t size=0;
  1081. slong j=node.son;
  1082. while(j!=-1){
  1083. ++size;
  1084. eval_expression(j,context);
  1085. j=nodes[j].bro;
  1086. }
  1087. switch(node.expressionType){
  1088. case expFunction:
  1089. context.eval_function(node,nodes);
  1090. break;
  1091. case expMemberFunction:
  1092. context.eval_member_function(node,nodes);
  1093. break;
  1094. case expArray:
  1095. context.eval_array(size,node,nodes);
  1096. break;
  1097. case expSet:
  1098. context.eval_set(size,node,nodes);
  1099. break;
  1100. case expTuple:
  1101. context.eval_tuple(size,node,nodes);
  1102. break;
  1103. case expArrayGet:
  1104. Bug("Exp array get not yet implemented");
  1105. break;
  1106. default:
  1107. Bug("Not yet implemented");
  1108. }
  1109. j=node.son;
  1110. while(j!=-1){
  1111. if(nodes[j].erase){
  1112. nodes[j].value.del();
  1113. nodes[j].erase=false;
  1114. }
  1115. j=nodes[j].bro;
  1116. }
  1117. }
  1118. //-------------------------------------------------
  1119. // Interpreter::get_integer(size_t&,const string&)
  1120. //-------------------------------------------------
  1121. fmpz*
  1122. Interpreter::get_integer(size_t& pos,const string& cmd){
  1123. size_t oldPos=pos;
  1124. size_t end=cmd.length();
  1125. char l;
  1126. do{
  1127. l=cmd[++pos];
  1128. }while(pos<end and '0'<=l and l<='9');
  1129. string sint=cmd.substr(oldPos,pos-oldPos);
  1130. fmpz* res=new fmpz;
  1131. fmpz_init(res);
  1132. fmpz_set_str(res,sint.c_str(),10);
  1133. return res;
  1134. }
  1135. //----------------------------------------------
  1136. // Interpreter::get_name(size_t&,const string&)
  1137. //----------------------------------------------
  1138. string
  1139. Interpreter::get_name(size_t& pos,const string& cmd){
  1140. size_t end=cmd.length();
  1141. size_t npos=pos;
  1142. char l=cmd[npos];
  1143. do{
  1144. ++npos;
  1145. l=cmd[npos];
  1146. }while(npos<end and (('a'<=l and l<='z') or
  1147. ('A'<=l and l<='Z') or
  1148. ('0'<=l and l<='9') or
  1149. l=='_'));
  1150. string str=cmd.substr(pos,npos-pos);
  1151. pos=npos;
  1152. return str;
  1153. }
  1154. //------------------------------------------------
  1155. // Interpreter::get_string(size_t&,const string&)
  1156. //------------------------------------------------
  1157. string
  1158. Interpreter::get_string(size_t& pos,const string& cmd){
  1159. size_t npos=cmd.find('"',pos+1);
  1160. if(npos==string::npos){
  1161. SyntaxError("the string does not end by \"",pos,string::npos);
  1162. }
  1163. string str=cmd.substr(pos+1,npos-pos-1);
  1164. pos=npos+1;
  1165. return str;
  1166. }
  1167. //-----------------------------------------------------
  1168. // Interpreter::set_token(Node&,size_t&,const string&)
  1169. //-----------------------------------------------------
  1170. void
  1171. Interpreter::set_token(Node& node,size_t& pos,const string& cmd){
  1172. node.value.type=nullptr;
  1173. node.erase=false;
  1174. size_t oldPos;
  1175. node.pos=pos;
  1176. node.son=-1;
  1177. node.bro=-1;
  1178. size_t end=cmd.length();
  1179. if(pos>=end){
  1180. node.tokenType=tEnd;
  1181. return;
  1182. }
  1183. char l=cmd[pos];
  1184. if('0'<=l and l<='9'){
  1185. node.tokenType=tInteger;
  1186. oldPos=pos;
  1187. node.value.type=type_integer;
  1188. node.value.ptr=(void*)(get_integer(pos,cmd));
  1189. node.erase=true;
  1190. node.str=cmd.substr(oldPos,pos-oldPos+1);
  1191. return;
  1192. }
  1193. if(l=='"'){
  1194. node.tokenType=tString;
  1195. node.str=get_string(pos,cmd);
  1196. node.value.type=type_string;
  1197. string* pstr=new string;
  1198. *pstr=node.str;
  1199. node.value.ptr=(void*)pstr;
  1200. node.erase=true;
  1201. return;
  1202. }
  1203. if(('a'<=l and l<='z') or ('A'<=l and l<='Z') or l=='_'){
  1204. node.tokenType=tName;
  1205. node.str=get_name(pos,cmd);
  1206. return;
  1207. }
  1208. switch(l){
  1209. case ',':
  1210. node.str=',';
  1211. node.tokenType=tComma;
  1212. ++pos;
  1213. return;
  1214. case '.':
  1215. node.str='.';
  1216. node.tokenType=tDot;
  1217. ++pos;
  1218. return;
  1219. case '(':
  1220. node.tokenType=tOpenBracket;
  1221. node.bracketType=bRound;
  1222. node.str=l;++pos;
  1223. return;
  1224. case ')':
  1225. node.tokenType=tCloseBracket;
  1226. node.bracketType=bRound;
  1227. node.str=l;++pos;
  1228. return;
  1229. case '[':
  1230. node.tokenType=tOpenBracket;
  1231. node.bracketType=bSquare;
  1232. node.str=l;++pos;
  1233. return;
  1234. case ']':
  1235. node.tokenType=tCloseBracket;
  1236. node.bracketType=bSquare;
  1237. node.str=l;++pos;
  1238. return;
  1239. case '{':
  1240. node.tokenType=tOpenBracket;
  1241. node.bracketType=bCurly;
  1242. node.str=l;++pos;
  1243. return;
  1244. case '}':
  1245. node.tokenType=tCloseBracket;
  1246. node.bracketType=bCurly;
  1247. node.str=l;++pos;
  1248. return;
  1249. }
  1250. //Try to find operator
  1251. oldPos=pos;
  1252. OperatorInfo* operatorInfo=get_operator(pos,cmd);
  1253. if(operatorInfo!=nullptr){//Operator found
  1254. node.tokenType=tOperator;
  1255. node.operatorInfo=operatorInfo;
  1256. node.str=cmd.substr(oldPos,pos-oldPos);
  1257. return;
  1258. }
  1259. //Operator not found
  1260. SyntaxError("Token unkown",oldPos,oldPos);
  1261. return;
  1262. }
  1263. //---------------------------------------------
  1264. // Interpreter::split_to_tokens(const string&)
  1265. //---------------------------------------------
  1266. void Interpreter::split_to_tokens(const string& cmd){
  1267. nodes_number=0;
  1268. size_t pos=0;
  1269. while(true){
  1270. if(nodes_number>=max_nodes_number) Bug("The command is too huge");
  1271. Node& node=nodes[nodes_number++];
  1272. set_token(node,pos,cmd);
  1273. if(node.tokenType==tEnd) break;
  1274. }
  1275. //Remove node of token type tEnd
  1276. --nodes_number;
  1277. }
  1278. //***********************
  1279. //* Auxiliary functions *
  1280. //***********************
  1281. //--------------------------------------------
  1282. // get_delimiter_expression_type(BracketType)
  1283. //--------------------------------------------
  1284. ExpressionType
  1285. get_delimiter_expression_type(BracketType type){
  1286. switch(type){
  1287. case bRound:
  1288. return expTuple;
  1289. case bSquare:
  1290. return expArray;
  1291. case bCurly:
  1292. return expSet;
  1293. default:
  1294. cout<<"BUG from getDelimiterExpressionType"<<endl;
  1295. return expTuple;
  1296. }
  1297. }
  1298. //---------------------------------
  1299. // close_bracket_char(BracketType)
  1300. //---------------------------------
  1301. char
  1302. close_bracket_char(BracketType type){
  1303. switch(type){
  1304. case bRound:
  1305. return ')';
  1306. case bSquare:
  1307. return ']';
  1308. case bCurly:
  1309. return '}';
  1310. default:
  1311. cout<<"BUG from closeBracketChar"<<endl;
  1312. return ')';
  1313. }
  1314. }
  1315. //---------------------------------------
  1316. // function_fullname(string,string_list)
  1317. //---------------------------------------
  1318. string
  1319. function_fullname(string name,string_list args){
  1320. string res=name+"(";
  1321. if(args.size()==0) return res+")";
  1322. auto it=args.begin();
  1323. res+=*it;
  1324. for(++it;it!=args.end();++it){
  1325. res+=",";
  1326. res+=*it;
  1327. }
  1328. return res+")";
  1329. }
  1330. //------------------------------------------
  1331. // function_fullname(string,Vlaue**,size_t)
  1332. //------------------------------------------
  1333. string
  1334. function_fullname(string name,Value** args,size_t nargs){
  1335. string res=name+"(";
  1336. if(nargs==0) return res+")";
  1337. res+=args[0]->type->name;
  1338. for(size_t i=1;i<nargs;++i){
  1339. res+=",";
  1340. res+=args[i]->type->name;
  1341. }
  1342. return res+")";
  1343. }
  1344. //--------------------------------------------
  1345. // function_fullname(string,const Signature&)
  1346. //--------------------------------------------
  1347. string
  1348. function_fullname(string name,const Signature& signature){
  1349. string res=name+"(";
  1350. if(signature.is_empty()) return res+")";
  1351. res+=signature[0]->name;
  1352. for(size_t i=1;i<signature.size();++i){
  1353. res+=",";
  1354. res+=signature[i]->name;
  1355. }
  1356. return res+")";
  1357. }
  1358. ostream&
  1359. operator<<(ostream& os,const BracketType& bt){
  1360. switch(bt){
  1361. case bRound:
  1362. return os<<"round";
  1363. break;
  1364. case bSquare:
  1365. return os<<"square";
  1366. break;
  1367. case bCurly:
  1368. return os<<"curly";
  1369. break;
  1370. default:
  1371. return os<<"?";
  1372. break;
  1373. }
  1374. }
  1375. ostream&
  1376. operator<<(ostream& os,const OperatorInfo& info){
  1377. switch(info.type){
  1378. case opPreUnitary:
  1379. return os<<"_ "<<info.func;
  1380. case opPostUnitary:
  1381. return os<<info.func<<" _";
  1382. case opBinary:
  1383. return os<<"_ "<<info.func<<" _";
  1384. default:
  1385. return os<<"Operator type unkown";
  1386. }
  1387. }
  1388. }