module.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. #ifndef MODULES_HPP
  20. #define MODULES_HPP
  21. #include <iostream>
  22. #include <cstdint>
  23. #include <map>
  24. #include <set>
  25. #include "array.hpp"
  26. #include "flint/fmpz.h"
  27. using namespace std;
  28. //*****************
  29. //* Integer types *
  30. //*****************
  31. typedef uint64_t uint64;
  32. typedef int64_t int64;
  33. //**********
  34. //* Macros *
  35. //**********
  36. #define FUNC(f) (void*)(f)
  37. #define CONT_FUNC_SENTINEL {"",{""},nullptr}
  38. #define FUNC_SENTINEL {"","",{""},nullptr}
  39. #define SYMB_SENTINEL {"","",nullptr}
  40. #define TYPE_SENTINEL {"",nullptr,nullptr,nullptr,nullptr}
  41. #define SyntaxError(msg,first,last) throw Error(errSyntax,(msg),(first),(last),__FILE__,__LINE__,__PRETTY_FUNCTION__);
  42. #define Bug(msg) throw Gomu::Error(Gomu::errBug,(msg),0,0,__FILE__,__LINE__,__PRETTY_FUNCTION__);
  43. #define ContextError(msg) throw Error(errContext,(msg),0,0,__FILE__,__LINE__,__PRETTY_FUNCTION__);
  44. #define RuntimeError(msg) throw Gomu::Error(Gomu::errRuntime,(msg),0,0,__FILE__,__LINE__,__PRETTY_FUNCTION__);
  45. namespace Gomu{
  46. //*********************
  47. //* Early declaration *
  48. //*********************
  49. class ArrayValue;
  50. class Context;
  51. class Interpreter;
  52. class Node;
  53. class SetValue;
  54. class SetValueComp;
  55. class Type;
  56. class Value;
  57. //************
  58. //* Typedefs *
  59. //************
  60. typedef string (*DispFunc)(void*);
  61. typedef void (*DelFunc)(void*);
  62. typedef void* (*CopyFunc)(void*);
  63. typedef int (*CompFunc)(void*,void*);
  64. //*********************
  65. //* Enumeration types *
  66. //*********************
  67. //! Enumeration of error type
  68. typedef enum{
  69. errBug,
  70. errSyntax,
  71. errContext,
  72. errRuntime,
  73. errUnkown
  74. } ErrorType;
  75. //******************
  76. //* Global objects *
  77. //******************
  78. extern Type *type_array;
  79. extern Type *type_boolean;
  80. extern Type *type_context;
  81. extern Type *type_generic;
  82. extern Type *type_integer;
  83. extern Type *type_function;
  84. extern Type *type_contextual_function;
  85. extern Type *type_meta_function;
  86. extern Type *type_module;
  87. extern Type *type_set;
  88. extern Type *type_string;
  89. extern Type *type_symbol;
  90. extern Type *type_tuple;
  91. extern Type *type_type;
  92. extern Type *type_void;
  93. //**********************
  94. //* Class declarations *
  95. //**********************
  96. //------------
  97. // ArrayValue
  98. //------------
  99. //! Class for array of value of same type
  100. class ArrayValue{
  101. public:
  102. //! size of the array
  103. size_t size;
  104. //! type of stored values
  105. Type* type;
  106. //! array of C++ pointer of value
  107. void** tab;
  108. //! Contrust an ArrayValue of a given size
  109. //! \param desired size
  110. ArrayValue(size_t s);
  111. };
  112. //! Class for interpreter error
  113. class Error{
  114. public:
  115. //! Type of the error
  116. ErrorType type;
  117. //! Error message
  118. string msg;
  119. //! Filename of the source file that have launched the error
  120. string file;
  121. //! Name of the function that have launched the error
  122. string function;
  123. //! First character of the corresponding token
  124. size_t first;
  125. //! Last character of the corresponding token
  126. size_t last;
  127. //! Number of the line that have launched the error
  128. size_t line;
  129. //! The empty constructor
  130. Error();
  131. //! The full constructor
  132. Error(ErrorType type,string msg,size_t first,size_t last,string file,int line,string function);
  133. //! Display the error
  134. //! \param os output streamm for display
  135. //! \param cmd command where error was found
  136. void disp(ostream& os,const string& cmd) const;
  137. };
  138. //--------
  139. // Module
  140. //--------
  141. //! Class for module
  142. class Module{
  143. public:
  144. class Function;
  145. class Symbol;
  146. class Type;
  147. //! Name of the module
  148. string name;
  149. //! Number of defined types
  150. size_t ntype;
  151. //! Number of defined functions
  152. size_t nfunc;
  153. //! Number of defined member functions
  154. size_t nmfunc;
  155. //! Number of defined contextual functions
  156. size_t ncfunc;
  157. //! Nomber of defined symbols
  158. size_t nsymb;
  159. //! Handle to the dinamic library object associated to the module
  160. void* handle;
  161. //! Name of defined types
  162. string* types;
  163. //! Defined functions
  164. Function *functions;
  165. //! Defined member functions
  166. Function *member_functions;
  167. //! Defined contextual function
  168. Function* contextual_functions;
  169. //! Defined symbols
  170. Symbol* symbols;
  171. //! Destructor
  172. ~Module();
  173. //! Unique constructor
  174. Module();
  175. //! Init module from types
  176. //! \param types types defined in the library
  177. void init(Module::Type* types);
  178. };
  179. //------------------
  180. // Module::Function
  181. //------------------
  182. //! Class for the module function declarations
  183. class Module::Function{
  184. public:
  185. //! Returned type
  186. string tr;
  187. //! Name of the function
  188. string name;
  189. //! Arguments type of the fnction
  190. initializer_list<string> targs;
  191. //! Pointer to the C++ function
  192. void* ptr;
  193. //! Specify if the function is loaded or not
  194. bool loaded;
  195. };
  196. //----------------
  197. // Module::Symbol
  198. //----------------
  199. //! Class for the module symbol declarations
  200. class Module::Symbol{
  201. public:
  202. //! Name of the symbol
  203. string name;
  204. //! Typre of the symbol
  205. string type;
  206. //! Pointer to the C++ data
  207. void* ptr;
  208. //! Specify if the symbol is loaded or not
  209. bool loaded;
  210. };
  211. //--------------
  212. // Module::Type
  213. //--------------
  214. //! Class for the module type declarations
  215. class Module::Type{
  216. public:
  217. //! Name of the type
  218. string name;
  219. //! Display function of the type
  220. DispFunc disp;
  221. //! Delete function of the type
  222. DelFunc del;
  223. //! Copy function of the type
  224. CopyFunc copy;
  225. //! Compeare function of the type
  226. CompFunc comp;
  227. //! Poniter to the type
  228. Gomu::Type** ptr;
  229. };
  230. //--------------
  231. // SetValueComp
  232. //--------------
  233. //! Class for comparison of SetValue objects
  234. class SetValueComp{
  235. public:
  236. //! Type of values in SetValue
  237. Type* type;
  238. //! The unsique constructor
  239. SetValueComp(Type* type);
  240. //! Function called for the comparison
  241. //! \param lhs pointer to a SetValue of type type
  242. //! \param rhs pointer to a SetValue of type type
  243. //! \return true if lhs<rhs and false otherwise
  244. bool operator()(void* lhs,void* rhs) const;
  245. };
  246. //----------
  247. // SetValue
  248. //----------
  249. //! Class for set of value of same type
  250. class SetValue{
  251. public:
  252. //! Set of value
  253. set<void*,SetValueComp> data;
  254. //! Construct a set of value of specified type
  255. //! \param type desired type
  256. SetValue(Type* type);
  257. };
  258. //------------
  259. // TupleValue
  260. //------------
  261. //! Class for tuple of Values
  262. class TupleValue{
  263. public:
  264. //! Tuple size
  265. size_t size;
  266. //! An array of Value
  267. Value* tab;
  268. //! Construt a TupleValue of size s
  269. //! \param s the desired size
  270. TupleValue(size_t s);
  271. };
  272. //------
  273. // Type
  274. //------
  275. //! Class for type
  276. class Type{
  277. public:
  278. //! Name of the type
  279. string name;
  280. //! Display function of the type
  281. DispFunc disp;
  282. //! Delete function of the type
  283. DelFunc del;
  284. //! Copy function of the type
  285. CopyFunc copy;
  286. //! Compeare function of the type
  287. CompFunc comp;
  288. //! Empty constructor
  289. Type();
  290. //! Full constructor
  291. Type(string,DispFunc disp,DelFunc del,CopyFunc copy,CompFunc comp);
  292. //! Recopy constructor
  293. Type(const Type&);
  294. //! Construct a type from a Module::Type
  295. Type(const Module::Type&);
  296. };
  297. //-------
  298. // Value
  299. //-------
  300. //! Class for value of the intepreter
  301. class Value{
  302. public:
  303. //! Type of the value
  304. Type* type;
  305. //! Pointer to theCc++ value
  306. void* ptr;
  307. //! Empty constructor
  308. Value();
  309. //! Constructor
  310. //! \param type type of the value
  311. //! \param ptr pointer to C++ value
  312. Value(Type* type,void* ptr);
  313. //! Delete the current value with protextions
  314. void pdel();
  315. //! Display the current value
  316. //! \return a string of the diplay
  317. string disp();
  318. //! Evaluate the current value (use for Symbolic value)
  319. //! \return pointed value if the current one is symbolic or the the current ont otherwise
  320. Value* eval();
  321. //! Return a copy of the current value
  322. Value copy();
  323. };
  324. //***********************
  325. //* Auxiliary functions *
  326. //***********************
  327. //! Return an slong from a value ptr
  328. int64 get_slong(void* v);
  329. //! Return a value ptr for a bool
  330. void* to_boolean(bool n);
  331. //! Return a value ptr from a slong integer
  332. void* to_integer(slong s);
  333. //! Undefined copy function for type
  334. void* no_copy(void*);
  335. //! Undefined compare function for type
  336. int no_comp(void*,void*);
  337. //**********************
  338. //* Inline definitions *
  339. //**********************
  340. //-------
  341. // Error
  342. //-------
  343. inline
  344. Error::Error(){}
  345. inline
  346. Error::Error(ErrorType _type,string _msg,size_t _first,size_t _last,string _file,int _line,string _function):type(_type),msg(_msg),first(_first),last(_last),file(_file),line(_line),function(_function){}
  347. //------
  348. // Type
  349. //------
  350. inline
  351. Type::Type(){}
  352. //--------
  353. // Module
  354. //--------
  355. inline
  356. Module::~Module(){}
  357. //--------------
  358. // SetValueComp
  359. //--------------
  360. inline
  361. SetValueComp::SetValueComp(Type* t){
  362. type=t;
  363. }
  364. inline bool
  365. SetValueComp::operator()(void* lhs,void* rhs) const{return type->comp(lhs,rhs)==-1;}
  366. //----------
  367. // SetValue
  368. //----------
  369. inline
  370. SetValue::SetValue(Type* typeinfo):data(SetValueComp(typeinfo)){}
  371. //-------
  372. // Value
  373. //-------
  374. inline
  375. Value::Value():type(nullptr),ptr(nullptr){}
  376. inline
  377. Value::Value(Type* t,void* p):type(t),ptr(p){}
  378. inline void
  379. Value::pdel(){
  380. if(ptr!=nullptr and type!=nullptr and type!=type_symbol){
  381. // cout<<"Delete value of type "<<type->name<<endl;
  382. type->del(ptr);
  383. }
  384. ptr=nullptr;
  385. }
  386. inline Value*
  387. Value::eval(){
  388. if(type!=type_symbol) return this;
  389. return (Value*)ptr;
  390. }
  391. }
  392. #endif