module.hpp 9.2 KB

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