kernel.cpp 8.8 KB

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