kernel.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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 "kernel.hpp"
  20. #include "interpreter.hpp"
  21. namespace Gomu{
  22. //*********
  23. //* Array *
  24. //*********
  25. string
  26. array_disp(void* v){
  27. string str;
  28. ArrayValue* arr=(ArrayValue*)v;
  29. if(arr->size==0)
  30. return "[]";
  31. str="[";
  32. str+=arr->type->disp(arr->tab[0]);
  33. for(size_t i=1;i<arr->size;++i){
  34. str+=", ";
  35. str+=arr->type->disp(arr->tab[i]);
  36. }
  37. str+=']';
  38. return str;
  39. }
  40. void
  41. array_del(void* v){
  42. ArrayValue* arr=(ArrayValue*)v;
  43. Value val;
  44. val.type=arr->type;
  45. for(size_t i=0;i<arr->size;++i){
  46. val.ptr=arr->tab[i];
  47. val.pdel();
  48. }
  49. delete[] arr->tab;
  50. delete arr;
  51. }
  52. void*
  53. array_copy(void* v){
  54. ArrayValue* arr=(ArrayValue*)v;
  55. size_t size=arr->size;
  56. ArrayValue* res=new ArrayValue(size);
  57. Type* type=res->type=arr->type;
  58. res->tab=new void*[size];
  59. for(size_t i=0;i<size;++i){
  60. res->tab[i]=type->copy(arr->tab[i]);
  61. }
  62. return (void*)res;
  63. }
  64. int
  65. array_comp(void* v1,void* v2){
  66. ArrayValue* arr1=(ArrayValue*)v1;
  67. ArrayValue* arr2=(ArrayValue*)v2;
  68. size_t size1=arr1->size;
  69. size_t size2=arr2->size;
  70. if(size1!=0 and size2!=0){
  71. if(arr1->type!=arr2->type) RuntimeError("Types of array mismatch for comparison");
  72. }
  73. if(size1==size2){
  74. Type* type=arr1->type;
  75. for(size_t i=0;i<size1;++i){
  76. int c=type->comp(arr1->tab[i],arr2->tab[i]);
  77. if(c!=0) return c;
  78. }
  79. return 0;
  80. }
  81. if(size1<size2) return -1;
  82. return 1;
  83. }
  84. //***********
  85. //* Boolean *
  86. //***********
  87. string
  88. boolean_disp(void* v){
  89. if((*(char*)v)==1)
  90. return "\033[35mtrue\033[0m";
  91. else
  92. return "\033[35mfalse\033[0m";
  93. }
  94. int
  95. boolean_comp(void* u,void* v){
  96. char a=*(char*)u;
  97. char b=*(char*)v;
  98. if(a==b) return 0;
  99. if(a<b) return 1;
  100. return -1;
  101. }
  102. //**********************
  103. //* ContextualFunction *
  104. //**********************
  105. string
  106. contextual_function_disp(void* v){
  107. ContextualFunction* f=(ContextualFunction*)v;
  108. return disp_signature(f->signature)+" -> "+type_disp(f->tr);
  109. }
  110. //************
  111. //* Function *
  112. //************
  113. string
  114. function_disp(void* val){
  115. Function* function=(Function*)(val);
  116. return disp_signature(function->signature)+" -> "+type_disp(function->tr);
  117. }
  118. //***********
  119. //* Integer *
  120. //***********
  121. string
  122. integer_disp(void* v){
  123. char* disp=fmpz_get_str(NULL,10,(fmpz*)v);
  124. string res="\033[34m";
  125. res+=disp;
  126. res+="\033[0m";
  127. free(disp);
  128. return res;
  129. }
  130. void*
  131. integer_copy(void* v){
  132. fmpz* res=new fmpz;
  133. fmpz_init_set(res,(fmpz*)v);
  134. return res;
  135. }
  136. //****************
  137. //* MetaFunction *
  138. //****************
  139. string
  140. meta_function_disp(void* v){
  141. MetaFunction* meta=(MetaFunction*)v;
  142. auto it=meta->functions.begin();
  143. string str=*it;
  144. for(++it;it!=meta->functions.end();++it){
  145. str+='\n';
  146. str+=*it;
  147. }
  148. return str;
  149. }
  150. //*******
  151. //* Set *
  152. //*******
  153. string
  154. set_disp(void* v){
  155. string str;
  156. SetValue* setval=(SetValue*)v;
  157. Type* type=setval->data.key_comp().type;
  158. if(setval->data.empty()) return "{}";
  159. str="{";
  160. auto it=setval->data.begin();
  161. str+=type->disp(*it);
  162. for(++it;it!=setval->data.end();++it){
  163. str+=',';
  164. str+=type->disp(*it);
  165. }
  166. str+='}';
  167. return str;
  168. }
  169. void
  170. set_del(void* v){
  171. SetValue* setval=(SetValue*)v;
  172. Type* type=setval->data.key_comp().type;
  173. for(auto it=setval->data.begin();it!=setval->data.end();++it){
  174. type->del(*it);
  175. }
  176. delete setval;
  177. }
  178. void*
  179. set_copy(void* v){
  180. SetValue* setval=(SetValue*)v;
  181. Type* type=setval->data.key_comp().type;
  182. SetValue* setres=new SetValue(type);
  183. for(auto it=setval->data.begin();it!=setval->data.end();++it){
  184. setres->data.insert(type->copy(*it));
  185. }
  186. return setres;
  187. }
  188. int
  189. set_comp(void* v1,void* v2){
  190. SetValue* set1=(SetValue*)v1;
  191. SetValue* set2=(SetValue*)v2;
  192. Type* type=set1->data.key_comp().type;
  193. if(type!=set2->data.key_comp().type) RuntimeError("Recurssive Types mismatch in set");
  194. if(set1->data.size()==set2->data.size()){
  195. auto it1=set1->data.begin();
  196. auto it2=set2->data.begin();
  197. for(;it1!=set1->data.end();++it1){
  198. int c=type->comp(*it1,*it2);
  199. if(c!=0) return c;
  200. }
  201. return 0;
  202. }
  203. else{
  204. if(set1->data.size()<set2->data.size()) return -1;
  205. return 1;
  206. }
  207. }
  208. //**********
  209. //* String *
  210. //**********
  211. int
  212. string_comp(void* u,void* v){
  213. string& a=*(string*)u;
  214. string& b=*(string*)v;
  215. if(a==b) return 0;
  216. if(a<b) return -1;
  217. return 1;
  218. }
  219. //*********
  220. //* Tuple *
  221. //*********
  222. string
  223. tuple_disp(void* v){
  224. TupleValue* t=(TupleValue*)v;
  225. if(t->size==0) return "()";
  226. string str="(";
  227. str+=t->tab[0].disp();
  228. for(size_t i=1;i<t->size;++i){
  229. str+=',';
  230. str+=t->tab[i].disp();
  231. }
  232. return str+')';
  233. }
  234. void
  235. tuple_del(void* v){
  236. TupleValue* t=(TupleValue*)v;
  237. for(size_t i=0;i<t->size;++i){
  238. t->tab[i].pdel();
  239. }
  240. }
  241. void*
  242. tuple_copy(void* v){
  243. TupleValue* t=(TupleValue*)v;
  244. TupleValue* res=new TupleValue(t->size);
  245. for(size_t i=0;i<t->size;++i){
  246. res->tab[i]=t->tab[i].copy();
  247. }
  248. return res;
  249. }
  250. int
  251. tuple_comp(void* v1,void *v2){
  252. TupleValue* t1=(TupleValue*)v1;
  253. TupleValue* t2=(TupleValue*)v2;
  254. if(t1->size==t2->size){
  255. for(size_t i=0;i<t1->size;++i){
  256. if(t1->tab[i].type==t2->tab[i].type){
  257. Type* type=t1->tab[i].type;
  258. int c=type->comp(t1->tab[i].ptr,t2->tab[i].ptr);
  259. if(c!=0) return c;
  260. }
  261. else{
  262. if(t1->tab[i].type->name<t2->tab[i].type->name) return -1;
  263. return 1;
  264. }
  265. }
  266. }
  267. else{
  268. if(t1->size<t2->size) return -1;
  269. return 1;
  270. }
  271. }
  272. //********
  273. //* Type *
  274. //********
  275. int
  276. type_comp(void* T1,void* T2){
  277. string s1=((Type*)T1)->name;
  278. string s2=((Type*)T2)->name;
  279. return string_comp(&s1,&s2);
  280. }
  281. //***********************
  282. //* Auxialiry functions *
  283. //***********************
  284. //----------------------------------
  285. // disp_signature(const Signature&)
  286. //----------------------------------
  287. string
  288. disp_signature(const Signature& s){
  289. if(s.size()==0) return "()";
  290. string str="(";
  291. str+=type_disp(s[0]);
  292. for(uint32_t i=1;i<s.size();++i){
  293. str+=','+type_disp(s[i]);
  294. }
  295. return str+')';
  296. }
  297. //------------------------------------
  298. // init_kernel(Context&,Interpreter&)
  299. //------------------------------------
  300. void init_kernel(Context& context,Interpreter& interpreter){
  301. //Operators
  302. interpreter.add_operator("=","operator=",opBinary,15);
  303. interpreter.add_operator("<","operator<",opBinary,8);
  304. interpreter.add_operator(">","operator>",opBinary,8);
  305. interpreter.add_operator("<=","operator<=",opBinary,8);
  306. interpreter.add_operator(">=","operator>=",opBinary,8);
  307. interpreter.add_operator("==","operator==",opBinary,8);
  308. interpreter.add_operator("!=","operator!=",opBinary,8);
  309. interpreter.add_operator("+","operator+",opBinary,4);
  310. interpreter.add_operator("-","operator-",opBinary,4);
  311. interpreter.add_operator("*","operator*",opBinary,3);
  312. interpreter.add_operator("::","operator::",opBinary,1);
  313. interpreter.add_operator("!","factorial",opPostUnitary,2);
  314. context.add_contextual_function("Void","load",{"String"},(void*)load_module);
  315. context.add_contextual_function("Void","unload",{"Module"},(void*)unload);
  316. context.add_contextual_function("Void","reload",{"Module"},(void*)reload);
  317. }
  318. //-----------------------
  319. // load(Context&,Value&)
  320. //-----------------------
  321. Value
  322. load_module(Context& context,Value& v){
  323. context.load_module(*((string*)(v.eval()->ptr)));
  324. return Value(type_void,nullptr);
  325. }
  326. //-------------------------
  327. // unload(Context&,Value&)
  328. //-------------------------
  329. Value
  330. unload(Context& context,Value& v){
  331. context.unload_module((Module*)(v.ptr));
  332. return Value(type_void,nullptr);
  333. }
  334. //-------------------------
  335. // reload(Context&,Value&)
  336. //-------------------------
  337. Value
  338. reload(Context& context,Value& v){
  339. context.reload_module((Module*)(v.ptr));
  340. return Value(type_void,nullptr);
  341. }
  342. }