interpreter.cpp 44 KB

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