main.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. Interpreter interpreter;
  39. Context context(&interpreter);
  40. completion_context=&context;
  41. completion_interpreter=&interpreter;
  42. try{
  43. init_kernel(context,interpreter);
  44. context.load_module("base");
  45. }
  46. catch(Error err){
  47. err.disp(cout,"");
  48. }
  49. rl_basic_word_break_characters=(char*)" .,;:()[]{}=+-*<>/#@%$!?";
  50. rl_completion_entry_function=completion_generator;
  51. rl_filename_completion_desired=0;
  52. rl_attempted_completion_function = completion;
  53. string cmd;
  54. char* c_cmd;
  55. while((c_cmd = readline("> "))!=NULL){
  56. //enable autocomplete
  57. rl_bind_key('\t',rl_complete);
  58. cmd=c_cmd;
  59. free(c_cmd);
  60. if(cmd.compare("quit")==0)
  61. break;
  62. interpreter.eval(cmd,context);
  63. add_history(cmd.c_str());
  64. }
  65. return 0;
  66. }
  67. static char** completion(const char* str,int beg,int pos){
  68. completion_pos=pos;
  69. rl_completion_suppress_append=1;
  70. return rl_completion_matches(str,completion_generator);
  71. }
  72. static char* completion_generator(const char* str,int state){
  73. char* res=new char[1024];
  74. string comp;
  75. try{
  76. comp=completion_interpreter->complete(string(rl_line_buffer),string(str),completion_pos,state,*completion_context);
  77. }
  78. catch(Error& err){
  79. return nullptr;
  80. }
  81. if(comp.empty()){
  82. rl_filename_completion_desired=0;
  83. return nullptr;
  84. }
  85. return strcpy(res,comp.c_str());
  86. }