main.cpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #include <iostream>
  20. #include <cstring>
  21. #include <readline/readline.h>
  22. #include <readline/history.h>
  23. #include <pthread.h>
  24. #include "interpreter.hpp"
  25. using namespace std;
  26. using namespace Gomu;
  27. //! Context for completion
  28. static Context* completion_context;
  29. //! Interpreter for complerion
  30. static Interpreter* completion_interpreter;
  31. //! Position of completed word
  32. static size_t completion_pos;
  33. //! Completion functions called by readline
  34. static char** completion(const char* str,int start,int end);
  35. static char* completion_generator(const char* str,int state);
  36. //! Main function
  37. int main(){
  38. Context context;
  39. Interpreter interpreter;
  40. completion_context=&context;
  41. completion_interpreter=&interpreter;
  42. try{
  43. init_kernel(context,interpreter);
  44. }
  45. catch(Error err){
  46. err.disp(cout,"");
  47. }
  48. rl_basic_word_break_characters=(char*)" .,;:()[]{}=+-*<>/#@%$!?";
  49. rl_completion_entry_function=completion_generator;
  50. rl_filename_completion_desired=0;
  51. rl_attempted_completion_function = completion;
  52. string cmd;
  53. char* c_cmd;
  54. while((c_cmd = readline("> "))!=NULL){
  55. //enable autocomplete
  56. rl_bind_key('\t',rl_complete);
  57. cmd=c_cmd;
  58. free(c_cmd);
  59. if(cmd.compare("quit")==0)
  60. break;
  61. interpreter.eval(cmd,context);
  62. add_history(cmd.c_str());
  63. }
  64. return 0;
  65. }
  66. static char** completion(const char* str,int beg,int pos){
  67. completion_pos=pos;
  68. rl_completion_suppress_append=1;
  69. return rl_completion_matches(str,completion_generator);
  70. }
  71. static char* completion_generator(const char* str,int state){
  72. char* res=new char[1024];
  73. string comp;
  74. try{
  75. comp=completion_interpreter->complete(string(rl_line_buffer),string(str),completion_pos,state,*completion_context);
  76. }
  77. catch(Error& err){
  78. return nullptr;
  79. }
  80. if(comp.empty()){
  81. rl_filename_completion_desired=0;
  82. return nullptr;
  83. }
  84. return strcpy(res,comp.c_str());
  85. }