main.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #include <iostream>
  2. #include <cstring>
  3. #include <readline/readline.h>
  4. #include <readline/history.h>
  5. #include <pthread.h>
  6. #include "interpreter.hpp"
  7. //#include "modules/base.hpp"
  8. //#include "modules/permutations.hpp"
  9. //#include "modules/matrix.hpp"
  10. //#include "modules/combinatorics.hpp"
  11. //#include "maths/polynomial.hpp"
  12. using namespace std;
  13. using namespace Gomu;
  14. static Context* completionContext;
  15. static size_t completionPos;
  16. static string completionString;
  17. static Interpreter* completionInterpreter;
  18. static char** completion(const char* str,int start,int end);
  19. static char* completionGenerator(const char* str,int state);
  20. int main(){
  21. Context context;
  22. Interpreter interpreter;
  23. completionContext=&context;
  24. completionInterpreter=&interpreter;
  25. // lexer.setContext(context);*/
  26. try{
  27. init_kernel(context,interpreter);
  28. }
  29. catch(Error err){
  30. err.disp(cout,"");
  31. }
  32. rl_basic_word_break_characters=(char*)" .,;:()[]{}=+-*<>/#@%$!?";
  33. // rl_completion_suppress_append=1;
  34. rl_completion_entry_function=completionGenerator;
  35. rl_filename_completion_desired=0;
  36. rl_attempted_completion_function = completion;
  37. string cmd;
  38. char* c_cmd;
  39. while((c_cmd = readline("> "))!=NULL){
  40. //enable autocomplete
  41. rl_bind_key('\t',rl_complete);
  42. cmd=c_cmd;
  43. free(c_cmd);
  44. if(cmd.compare("quit")==0)
  45. break;
  46. interpreter.eval(cmd,context);
  47. add_history(cmd.c_str());
  48. }
  49. return 0;
  50. }
  51. static char** completion(const char* str,int beg,int pos){
  52. completionPos=pos;
  53. //prevent adding extra character
  54. rl_completion_suppress_append=1;
  55. return rl_completion_matches(str,completionGenerator);
  56. }
  57. static char* completionGenerator(const char* str,int state){
  58. char* res=new char[1024];
  59. string comp;
  60. try{
  61. comp=completionInterpreter->complete(string(rl_line_buffer),string(str),completionPos,state,*completionContext);
  62. }
  63. catch(Error& err){
  64. return nullptr;
  65. }
  66. if(comp.empty()){
  67. rl_filename_completion_desired=0;
  68. //rl_bind_key('\t',rl_abort);
  69. return nullptr;
  70. }
  71. return strcpy(res,comp.c_str());
  72. }