interpreter.hpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694
  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 INTERPRETER_HPP
  20. #define INTERPRETER_HPP
  21. #include <iostream>
  22. #include <deque>
  23. #include <map>
  24. #include <list>
  25. #include <vector>
  26. #include <dlfcn.h>
  27. #include <initializer_list>
  28. #include <cstdint>
  29. #include "dictionnary.hpp"
  30. #include "kernel.hpp"
  31. using namespace std;
  32. namespace Gomu{
  33. //*************
  34. //* Constants *
  35. //*************
  36. static const int assignement_precedence_level=98;
  37. static const int max_precedence_level=99;
  38. static const size_t max_nodes_number=1024;
  39. static const size_t max_arguments_number=8;
  40. //**********************
  41. //* Early declarations *
  42. //**********************
  43. class Completion;
  44. class Context;
  45. class ContextualFunction;
  46. class Function;
  47. class Node;
  48. class Interpreter;
  49. class OperatorInfo;
  50. class Symbol;
  51. //************
  52. //* Typedefs *
  53. //************
  54. typedef Value (*CFunc0)(Context&);
  55. typedef Value (*CFunc1)(Context&,Value&);
  56. typedef Value (*CFunc2)(Context&,Value&,Value&);
  57. typedef void* (*Func1)(void*);
  58. typedef const initializer_list<string>& string_list;
  59. //*********************
  60. //* Enumeration types *
  61. //*********************
  62. //! Enumeration of bracket types
  63. typedef enum{
  64. bCurly,
  65. bRound,
  66. bSquare
  67. } BracketType;
  68. //! Enumeration of expression types
  69. typedef enum{
  70. expArray,
  71. expArrayGet,
  72. expArraySet,
  73. expFunction,
  74. expMemberFunction,
  75. expLeaf,
  76. expTuple,
  77. expSet
  78. } ExpressionType;
  79. //! Enumeration of token types
  80. typedef enum{
  81. tCloseBracket,
  82. tComma,
  83. tDot,
  84. tEnd,
  85. tInteger,
  86. tName,
  87. tOpenBracket,
  88. tOperator,
  89. tString,
  90. tUnkown
  91. } TokenType;
  92. //! Enumeration of operator types
  93. typedef enum{
  94. opBinary,
  95. opPreUnitary,
  96. opPostUnitary
  97. } OperatorType;
  98. //**********************
  99. //* Class declarations *
  100. //**********************
  101. //------------
  102. // Completion
  103. //------------
  104. //! A class for completion information
  105. class Completion{
  106. public:
  107. //! Prefix to add to the word for looking symbol, used in case of a member call
  108. //! By example if s is ot type string then a.<tab> has prefix 'String.'
  109. string prefix;
  110. //! The word to complete
  111. string word;
  112. //! Type of the class for a member call, nullptr otherwise
  113. Type* type;
  114. //! An iterator for the context's symbols
  115. map<string,Symbol>::iterator it;
  116. };
  117. //---------
  118. // Context
  119. //---------
  120. //! A class for the interpretation context
  121. class Context{
  122. public:
  123. //! Map for association name <-> symbol
  124. map<string,Symbol> symbols;
  125. //! The unique constructor
  126. Context();
  127. //! Add a symbol for a contextual function
  128. //! \param name name of the symbol
  129. //! \param args list of the argument type strings
  130. //! \param ptr the contextual function pointer
  131. void add_contextual_function(string ret,string name,string_list args,void* ptr);
  132. //! Add a symbol for a function
  133. //! \param ret return type string
  134. //! \param name name of the symbol
  135. //! \param args list of the argument type strings
  136. //! \param ptr the standard function pointer
  137. void add_function(string ret,string name,string_list args,void* ptr);
  138. //! Add a member function to the class given by the first argument
  139. //! \param ret return type string
  140. //! \param name name of the symbol
  141. //! \param args list of the argument type strings
  142. //! \param ptr the standard function pointer
  143. void add_member_function(string ret,string name,string_list args,void* ptr);
  144. //! Add a symbol named name
  145. //! \param name name of the symbol
  146. //! \param type type of the symbol
  147. //! \param ptr pointer to the data
  148. //! \param lock specify if the symbol is locked or not
  149. //! \return Return the new added symbol
  150. Symbol* add_symbol(string name,Type* type,void* ptr,bool lock=true);
  151. //! Test if we can add a symbol named name
  152. //! \param name the symbol name
  153. //! \return true if we can add the symbol false otherwise
  154. bool can_add(string name);
  155. //! Test if we can add a contextual function called name
  156. //! \param name function name
  157. //! \param targs type of function arguments
  158. //! \return true if we can add the symbol false otherwise
  159. bool can_add_contextual_function(string name,string_list targs);
  160. //! Test if we can add a function called name
  161. //! \param name function name
  162. //! \param targs type of function arguments
  163. //! \return true if we can add the symbol false otherwise
  164. bool can_add_function(string name,string_list targs);
  165. //! Test if we can add a member function called name
  166. //! \param name function name
  167. //! \param targs type of function arguments
  168. //! \return true if we can add the symbol false otherwise
  169. bool can_add_member_function(string name,string_list targs);
  170. //! Evaluate an array node
  171. //! \param size size of the array
  172. //! \param current the node of the array
  173. //! \param nodes array of all nodes
  174. void eval_array(size_t size,Node& current,Node* nodes);
  175. //! Evaluate a contextual function
  176. //! \param contextual pointer to a contextual function
  177. //! \param args arguments of the function call
  178. //! \param nargs number of argument for the function call
  179. //! \return value returned by the contextual function
  180. Value eval_contextual_function(ContextualFunction* contextual,Value** args,size_t nargs);
  181. //! Evaluate a function
  182. //! \param function pointer to a function
  183. //! \param args arguments of the function call
  184. //! \param nargs number of argument for the function call
  185. //! \return value returned by the function
  186. Value eval_function(Function* function,Value** args,size_t nargs);
  187. //! Evaluate a function node (contextual or not)
  188. //! \param current the node of the function to evaluate
  189. //! \param nodes array of all nodes
  190. //! \param size size of the array
  191. void eval_function(Node& current,Node* nodes);
  192. //! Evaluate a function given by a symbol (contextual or not)
  193. //! \param symbol symbol for the function
  194. //! \param args arguments of the function call
  195. //! \param nargs number of argument for the function call
  196. //! \return value returned by the function
  197. Value eval_function(Symbol* symbol,Value** args,size_t nargs);
  198. //! Evaluate a member function node (contextual or not)
  199. //! \param current the node of the function to evaluate
  200. //! \param nodes array of all nodes
  201. //! \param size size of the array
  202. void eval_member_function(Node& current,Node* nodes);
  203. //! Evaluate a set node
  204. //! \param size size of the set
  205. //! \param current the node of the set
  206. //! \param nodes array of all nodes
  207. void eval_set(size_t size,Node& current,Node* nodes);
  208. //! Evaluate a tuple node
  209. //! \param size size of the tuple
  210. //! \param current the node of the tuple
  211. //! \param nodes array of all nodes
  212. void eval_tuple(size_t size,Node& current,Node* nodes);
  213. //! Return the signature of a string list of arguments
  214. //! \param args the string list
  215. //! \return The corresponding signature
  216. Signature get_signature(string_list args);
  217. //! Return the symbol named name
  218. //! \param name name to find
  219. //! \return Symbol corresponding to name if it exists, nullptr otherwise
  220. Symbol* get_symbol(string name);
  221. //! Return the symbol named name in class type
  222. //! \param ctype type to lookup
  223. //! \param name name to find
  224. //! \return Symbol corresponding to name if it exists, nullptr otherwise
  225. Symbol* get_symbol(Type* ctype,string name);
  226. //! Return the type named name
  227. //! \param name name to find
  228. //! \return Type corresponding to name if it exists, nullptr otherwise
  229. Type* get_type(string name);
  230. //! Load the module name
  231. //! \param name of the module to load
  232. void load_module(string name);
  233. //! Load functions of a module
  234. //! \param the module to load
  235. //! \param src specify the src to load 0:functions 1:member_functions 2:contextual_functions
  236. void load_module_functions(Module* module,int src);
  237. //! Unload a function
  238. //! \param name the name of the function to delete
  239. //! \param args the string list of the function arguments
  240. void unload_function(string name,string_list args);
  241. //! Unload a module
  242. //! \param module the module to unload
  243. void unload_module(Module* module);
  244. //! Unload module functions
  245. //! \param module the module containint functions to unload
  246. void unload_module_functions(Module* module);
  247. //! Unload a symbol
  248. //! \param name of the symbol to delete
  249. void unload_symbol(string name);
  250. //! Unload a type
  251. //! \param type the type tu unload
  252. void unload_type(Type* type);
  253. //! Reload a module
  254. //! \param module the module to reload
  255. void reload_module(Module* module);
  256. //! Set arguments of a function node
  257. //! \param current node of the function to consider
  258. //! \param nodes an array of all nodes
  259. //! \args an array of Value* to store the arguments of the function
  260. //! \return number of arguments of the function call
  261. size_t set_arguments(Node& current,Node* nodes,Value** args);
  262. };
  263. //--------------------
  264. // ContextualFunction
  265. //--------------------
  266. //! A class for contextual function
  267. class ContextualFunction{
  268. public:
  269. //! Return type of the contextual function
  270. Type* tr;
  271. //! Pointer to the contextual function
  272. void* ptr;
  273. //! Signature of the contextual function
  274. Signature signature;
  275. //! The unique constructor
  276. ContextualFunction(Type* tr,const Signature& signature,void* ptr);
  277. //! Eval the contextual function
  278. //! \param args function call arguments
  279. //! \papam number of arguments of the function call
  280. //! \param context the context of the contextual function evaluation
  281. //! \return The returned value
  282. Value eval(Value* args[8],size_t nargs,Context& context);
  283. };
  284. //----------
  285. // Function
  286. //----------
  287. //! A class for function
  288. class Function{
  289. public:
  290. //! Return type of the function
  291. Type* tr;
  292. //! Signature of the function
  293. Signature signature;
  294. //! Pointer to the function
  295. void* ptr;
  296. //! The unique constructor
  297. Function(Type* tr,const Signature& signature,void* ptr);
  298. //! Evaluate the function
  299. //! \param args function call arguments
  300. //! \papam number of arguments of the function call
  301. //! \return The returned value
  302. Value eval(Value* args[8],size_t nargs);
  303. };
  304. //------
  305. // Node
  306. //------
  307. //! Class for expression node
  308. class Node{
  309. public:
  310. //! Token type of the node
  311. TokenType tokenType;
  312. //! Expression type of the node
  313. ExpressionType expressionType;
  314. //! Position in the command of the fisrt letter of the substring representing the node
  315. size_t pos;
  316. //! Substring of the command representing the node
  317. string str;
  318. union{
  319. //! Bracket type of the node (if any)
  320. BracketType bracketType;
  321. //! Operator information of the node (if any)
  322. OperatorInfo* operatorInfo;
  323. };
  324. //! Value of the node after evaluation
  325. Value value;
  326. //! Index of the node son
  327. slong son;
  328. //! Index of the node brother
  329. slong bro;
  330. //! Specify if the node can be erased or not
  331. bool erase;
  332. };
  333. //-------------
  334. // Interpreter
  335. //-------------
  336. //! A class for the interpreter
  337. class Interpreter{
  338. protected:
  339. //! Information about completion
  340. Completion completion;
  341. //! Number of nodes in the expression
  342. size_t nodes_number;
  343. //! An array of nodes describing the expression obtained from the command string
  344. Node nodes[max_nodes_number];
  345. //! The dictionnary of all defined operator
  346. Dictionnary<OperatorInfo> operator_tree;
  347. public:
  348. //! The unique constructor
  349. Interpreter();
  350. //! Add an operator to the operator's dictionnary
  351. //! \param op operator identification (=,!=,...)
  352. //! \param name name of the operator function to call
  353. //! \param type operator type (binary,preunitary,postunitary)
  354. //! \pram p operator precedence
  355. void add_operator(const string& op,const string& name,OperatorType type,int p);
  356. //! Function called during completion. It return a non empty string for each
  357. //! possible coompleted word and an empty one if there is non more possibiliy.
  358. //! \param cmd command containing the word to complete
  359. //! \param word the word to complete
  360. //! \param pos position of the cursor when completion has been called
  361. //! \param state state of the completion process 0 for the first call and >0 for the others
  362. //! \param context context of the future command evaluation
  363. //! \return a possible completion or ""
  364. string complete(const string& cmd,const string& word,size_t pos,int state,Context& context);
  365. //! Construct a sequence from an expression
  366. //! \param first position of the first node of the sequence
  367. //! \param last position of the last node of the sequence
  368. //! \return size of the sequence
  369. size_t construct_sequence(size_t& first,size_t last);
  370. //! Construct the expression tree of the command from an array of tokens
  371. //! \param first position of the first token of the experssion
  372. //! \param last position of the last token of the expession
  373. //! \param precedence_level current precedence level
  374. //! \return position of the root of the constructed tree
  375. size_t construct_tree(size_t& first,size_t last,int precedence_level);
  376. //! Display an expression tree
  377. //! \param os the output stream for display
  378. //! \param i position of the tree root
  379. void display_expression_tree(ostream& os,size_t i) const;
  380. //! Display a token
  381. //! \param os the output stream for display
  382. //! \parap i index of the token to diplay
  383. void display_token(ostream& os,size_t i) const;
  384. //! Display the token array
  385. //! \param os the output stream for display
  386. void display_tokens(ostream& os) const;
  387. //! Evaluate a command
  388. //! \param cmd command to evaluate
  389. //! \param context context of the evaluation
  390. void eval(string cmd,Context& context);
  391. //! Evaluate an expression
  392. //! \param pos indice of the expression to evaluate
  393. //! \param context context of the evaluation
  394. void eval_expression(size_t pos,Context& context);
  395. //! Get an integer from a substring of a command
  396. //! \param pos indice of the substring of the command representing the integer
  397. //! \param cmd command
  398. //! \return pointer to the corresponding integer
  399. fmpz* get_integer(size_t& pos,const string& cmd);
  400. //! Get a name from a substring of a command
  401. //! \param pos indice of the substring of the command representing the name
  402. //! \param cmd command
  403. //! \return name
  404. string get_name(size_t& pos,const string& cmd);
  405. //! Try to get an operator from a substring of a command
  406. //! \param pos indice of the substring of the command representing the operator
  407. //! \param cmd command
  408. //! \return A pointer to the new created operator information, nullptr otherwise
  409. OperatorInfo* get_operator(size_t& pos,const string& cmd);
  410. //! Get a string from a substring of a command
  411. //! \param pos indice of the substring of the command representing the string
  412. //! \param cmd command
  413. //! \return string
  414. string get_string(size_t& pos,const string& cmd);
  415. //! Set node to be the token of command at position pos
  416. //! \param node destination node of the token
  417. //! \param pos position of the substring representing the token in comman
  418. //! \param command command to evaluate
  419. void set_token(Node& node,size_t& pos,const string& cmd);
  420. //! Create an array of tokens from a command
  421. //! \param command command to evaluate
  422. void split_to_tokens(const string& cmd);
  423. };
  424. //--------------
  425. // MetaFunction
  426. //--------------
  427. //! Class for meta function (used in case of overloaded functions)
  428. class MetaFunction{
  429. public:
  430. //! Fullname of overloaded functions
  431. set<string> functions;
  432. };
  433. //--------------
  434. // OperatorInfo
  435. //--------------
  436. //! Class for operator informtation
  437. class OperatorInfo{
  438. public:
  439. //! Name of the operator function to call
  440. string func;
  441. //! Type of the operator (unary,...)
  442. OperatorType type;
  443. //! Precedence of the operator
  444. int precedence;
  445. //! The unique constructor
  446. OperatorInfo(string func,OperatorType t,int p);
  447. };
  448. //--------
  449. // Symbol
  450. //--------
  451. //! Class for context symbol
  452. class Symbol:public Value{
  453. public:
  454. //! Specify if the symbol is hidden in completion
  455. bool hide;
  456. //! Specify if the symbol is locked (roughlt created by C++ call)
  457. bool locked;
  458. //! Name of the symbol
  459. string name;
  460. //! The unique constructor
  461. Symbol();
  462. };
  463. //***********************
  464. //* Auxiliary functions *
  465. //***********************
  466. //! Copy the value src to the value dst
  467. //! \param dst pointer to the destination value
  468. //! \param src pointer to the source value
  469. void copy_value(Value* dst,Value* src);
  470. //! Get the fullname of a function
  471. //! \param name short name of the function
  472. //! \param args string array of argument type
  473. //! \return fullname of the function
  474. string function_fullname(string name,string_list args);
  475. //! Get the fullname of a function call
  476. //! \param name short name of the function
  477. //! \param args arguments of function call
  478. //! \param nargs number of arguments of the function call
  479. //! \return fullname of the function
  480. string function_fullname(string name,Value** args,size_t nargs);
  481. //! Get the fullname of a function
  482. //! \param name short name of the function
  483. //! \param signature signature of the function
  484. //! \return fullname of the function
  485. string function_fullname(string name,const Signature& signature);
  486. //! Get the expression type of hthe sequence corresponding to the bracket type t
  487. //! \param t a bracket type
  488. //! \return Expression type cooresponding to t
  489. ExpressionType get_delimiter_expression_type(BracketType t);
  490. //! Set a signature for a list of argument type string
  491. //! \param signature signature to fullfill
  492. //! \param args list of argument type string
  493. void set_signature(Signature& signature,string_list args);
  494. ostream& operator<<(ostream& os,const BracketType& bt);
  495. ostream& operator<<(ostream& os,const OperatorInfo& oi);
  496. //**********************
  497. //* Inline definitions *
  498. //**********************
  499. //---------
  500. // Context
  501. //---------
  502. inline void
  503. Context::add_contextual_function(string ret,string name,string_list args,void* ptr){
  504. Signature signature=get_signature(args);
  505. Type* tr=get_type(ret);
  506. add_symbol(name,type_contextual_function,(void*)(new ContextualFunction(tr,signature,ptr)));
  507. }
  508. inline bool
  509. Context::can_add(string name){
  510. Symbol* symbol=get_symbol(name);
  511. return (symbol==nullptr or not symbol->locked);
  512. }
  513. inline bool
  514. Context::can_add_contextual_function(string name,string_list targs){
  515. return can_add(name);
  516. }
  517. inline Symbol*
  518. Context::get_symbol(Type* ctype,string name){
  519. return get_symbol(ctype->name+"."+name);
  520. }
  521. inline void
  522. Context::unload_symbol(string name){
  523. symbols.erase(name);
  524. }
  525. //--------------------
  526. // ContextualFunction
  527. //--------------------
  528. inline
  529. ContextualFunction::ContextualFunction(Type* t,const Signature& s,void* p):tr(t),signature(s),ptr(p){
  530. }
  531. //----------
  532. // Function
  533. //----------
  534. inline
  535. Function::Function(Type* t,const Signature& s,void* p):tr(t),signature(s),ptr(p){
  536. }
  537. //-------------
  538. // Interpreter
  539. //-------------
  540. inline
  541. Interpreter::Interpreter(){}
  542. inline OperatorInfo*
  543. Interpreter::get_operator(size_t& pos,const string& cmd){return operator_tree.find_at(pos,cmd);}
  544. //--------
  545. // Symbol
  546. //--------
  547. inline
  548. Symbol::Symbol():Value(),locked(false){
  549. }
  550. //--------------
  551. // OperatorInfo
  552. //--------------
  553. inline
  554. OperatorInfo::OperatorInfo(string str,OperatorType t,int p):func(str),type(t),precedence(p){}
  555. //---------------------
  556. // Auxiliray functions
  557. //---------------------
  558. inline void copyValue(Value* dst,Value* src){
  559. dst->type=src->type;
  560. dst->ptr=src->type->copy(src->ptr);
  561. }
  562. }
  563. #endif