interpreter.cpp 48 KB

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