module.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. }
  101. }
  102. //********
  103. //* Type *
  104. //********
  105. //-------------------------------------------------------
  106. // Type::Type(string,DispFunc,DelFunc,CopyFunc,CompFunc)
  107. //-------------------------------------------------------
  108. Type::Type(string _name,DispFunc _disp,DelFunc _del,CopyFunc _copy,CompFunc _comp){
  109. name=_name;
  110. disp=_disp;
  111. del=_del;
  112. copy=_copy;
  113. comp=_comp;
  114. }
  115. //-------------------------
  116. // Type::Type(const Type&)
  117. //-------------------------
  118. Type::Type(const Type& t){
  119. name=t.name;
  120. disp=t.disp;
  121. del=t.del;
  122. copy=t.copy;
  123. comp=t.comp;
  124. }
  125. //**************
  126. //* TupleValue *
  127. //**************
  128. TupleValue::TupleValue(size_t s){
  129. size=s;
  130. tab=(s==0)?nullptr:new Value[s];
  131. }
  132. //*********
  133. //* Value *
  134. //*********
  135. //---------------
  136. // Value::copy()
  137. //---------------
  138. Value
  139. Value::copy(){
  140. Value res;
  141. res.type=type;
  142. res.ptr=type->copy(ptr);
  143. return res;
  144. }
  145. //---------------
  146. // Value::disp()
  147. //---------------
  148. string
  149. Value::disp(){
  150. if(type==type_symbol) return ((Value*)ptr)->disp();
  151. if(ptr!=nullptr and type!=nullptr) return type->disp(ptr);
  152. else return "";
  153. }
  154. //***********************
  155. //* Auxiliary functions *
  156. //***********************
  157. //------------------
  158. // get_slong(void*)
  159. //------------------
  160. uint64
  161. get_slong(void* v){
  162. fmpz* z=(fmpz*)v;
  163. if(fmpz_fits_si(z)) return fmpz_get_si(z);
  164. else RuntimeError("Integer too huge to fit slong");
  165. }
  166. //------------------
  167. // to_boolean(bool)
  168. //------------------
  169. void*
  170. to_boolean(bool b){
  171. char* res=new char;
  172. *res=(b?1:0);
  173. return res;
  174. }
  175. //-------------------
  176. // to_integer(slong)
  177. //-------------------
  178. void*
  179. to_integer(slong n){
  180. fmpz* z=new fmpz;
  181. fmpz_init(z);
  182. fmpz_set_si(z,n);
  183. return z;
  184. }
  185. }