module.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 "module.hpp"
  20. namespace Gomu{
  21. //**************
  22. //* ArrayValue *
  23. //**************
  24. ArrayValue::ArrayValue(size_t s){
  25. size=s;
  26. type=nullptr;
  27. if(s==0) tab=nullptr;
  28. else tab=new void*[size];
  29. }
  30. //*********
  31. //* Error *
  32. //*********
  33. void
  34. Error::disp(ostream& os,const string& cmd) const{
  35. switch(type){
  36. case errBug:
  37. os<<"\033[31mBug\033[0m : "<<msg;
  38. os<<" (\033[34m"<<file<<"\033[0m:\033[32m"<<line<<"\033[0m)";
  39. return;
  40. case errContext:
  41. os<<"\033[31mContext error\033[0m : "<<msg;
  42. os<<" (\033[34m"<<file<<"\033[0m:\033[32m"<<line<<"\033[0m)";
  43. return;
  44. case errRuntime:
  45. os<<"\033[31mRuntime error\033[0m : "<<msg;
  46. return;
  47. case errSyntax:
  48. os<<"\033[31mSyntax error\033[0m : "<<msg;
  49. os<<"(\033[34m"<<file<<"\033[0m:\033[32m"<<line<<"\033[0m)";
  50. break;
  51. default:
  52. os<<"\033[31mError\033[0m : ";
  53. break;
  54. }
  55. os<<endl<<"@ ";
  56. if(first>0) os<<cmd.substr(0,first);
  57. os<<"\033[32m"<<cmd.substr(first,last-first+1)<<"\033[0m";
  58. if(last+1<cmd.length()) os<<cmd.substr(last+1,string::npos);
  59. return;
  60. }
  61. //**********
  62. //* Module *
  63. //**********
  64. //------------------
  65. // Module::Module()
  66. //------------------
  67. Module::Module(){
  68. types=nullptr;
  69. functions=nullptr;
  70. member_functions=nullptr;
  71. contextual_functions=nullptr;
  72. }
  73. //---------------------
  74. // Module::init(Type*)
  75. //---------------------
  76. void Module::init(Type* types){
  77. size_t i=0;
  78. if(types==nullptr) ntype=0;
  79. else{
  80. while(types[i].disp!=nullptr) ++i;
  81. ntype=i;
  82. i=0;
  83. }
  84. if(functions==nullptr) nfunc=0;
  85. else{
  86. while(functions[i].ptr!=nullptr) ++i;
  87. nfunc=i;
  88. i=0;
  89. }
  90. if(member_functions==nullptr) nmfunc=0;
  91. else{
  92. while(member_functions[i].ptr!=nullptr) ++i;
  93. nmfunc=i;
  94. i=0;
  95. }
  96. if(contextual_functions==nullptr) ncfunc=0;
  97. else{
  98. while(contextual_functions[i].ptr!=nullptr) ++i;
  99. ncfunc=i;
  100. i=0;
  101. }
  102. if(symbols==nullptr) nsymb=0;
  103. else{
  104. while(symbols[i].ptr!=nullptr) ++i;
  105. nsymb=i;
  106. }
  107. }
  108. //********
  109. //* Type *
  110. //********
  111. //-------------------------------------------------------
  112. // Type::Type(string,DispFunc,DelFunc,CopyFunc,CompFunc)
  113. //-------------------------------------------------------
  114. Type::Type(string _name,DispFunc _disp,DelFunc _del,CopyFunc _copy,CompFunc _comp){
  115. name=_name;
  116. disp=_disp;
  117. del=_del;
  118. copy=_copy;
  119. comp=_comp;
  120. }
  121. //-------------------------
  122. // Type::Type(const Type&)
  123. //-------------------------
  124. Type::Type(const Type& t){
  125. name=t.name;
  126. disp=t.disp;
  127. del=t.del;
  128. copy=t.copy;
  129. comp=t.comp;
  130. }
  131. //---------------------------------
  132. // Type::Type(const Module::Type&)
  133. //---------------------------------
  134. Type::Type(const Module::Type& t){
  135. name=t.name;
  136. disp=t.disp;
  137. del=t.del;
  138. copy=t.copy;
  139. comp=t.comp;
  140. }
  141. //**************
  142. //* TupleValue *
  143. //**************
  144. TupleValue::TupleValue(size_t s){
  145. size=s;
  146. tab=(s==0)?nullptr:new Value[s];
  147. }
  148. //*********
  149. //* Value *
  150. //*********
  151. //---------------
  152. // Value::copy()
  153. //---------------
  154. Value
  155. Value::copy(){
  156. Value res;
  157. res.type=type;
  158. res.ptr=type->copy(ptr);
  159. return res;
  160. }
  161. //---------------
  162. // Value::disp()
  163. //---------------
  164. string
  165. Value::disp(){
  166. if(type==type_symbol) return ((Value*)ptr)->disp();
  167. if(ptr!=nullptr and type!=nullptr) return type->disp(ptr);
  168. else return "";
  169. }
  170. //***********************
  171. //* Auxiliary functions *
  172. //***********************
  173. //------------------
  174. // get_slong(void*)
  175. //------------------
  176. int64
  177. get_slong(void* v){
  178. fmpz* z=(fmpz*)v;
  179. if(fmpz_fits_si(z)) return fmpz_get_si(z);
  180. else RuntimeError("Integer too huge to fit slong");
  181. }
  182. //------------------
  183. // to_boolean(bool)
  184. //------------------
  185. void*
  186. to_boolean(bool b){
  187. char* res=new char;
  188. *res=(b?1:0);
  189. return res;
  190. }
  191. //-------------------
  192. // to_integer(slong)
  193. //-------------------
  194. void*
  195. to_integer(slong n){
  196. fmpz* z=new fmpz;
  197. fmpz_init(z);
  198. fmpz_set_si(z,n);
  199. return z;
  200. }
  201. //----------------
  202. // no_copy(void*)
  203. //----------------
  204. void*
  205. no_copy(void*){
  206. ContextError("Copy is undefined for this type");
  207. }
  208. //----------------------
  209. // no_comp(void*,void*)
  210. //----------------------
  211. int
  212. no_comp(void*,void*){
  213. ContextError("Comparison is undefined for this type");
  214. }
  215. }