interpreter.hpp 22 KB

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