interpreter.hpp 21 KB

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