main.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. #include <fstream>
  2. #include "pocketpy.h"
  3. //#define PK_DEBUG_TIME
  4. #define PK_DEBUG_THREADED_REPL
  5. struct Timer{
  6. const char* title;
  7. Timer(const char* title) : title(title) {}
  8. void run(std::function<void()> f){
  9. auto start = std::chrono::high_resolution_clock::now();
  10. f();
  11. auto end = std::chrono::high_resolution_clock::now();
  12. double elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / 1000000.0;
  13. #ifdef PK_DEBUG_TIME
  14. std::cout << title << ": " << elapsed << " s" << std::endl;
  15. #endif
  16. }
  17. };
  18. #if defined(__EMSCRIPTEN__) || defined(__wasm__) || defined(__wasm32__) || defined(__wasm64__)
  19. // these code is for demo use, feel free to modify it
  20. REPL* _repl;
  21. extern "C" {
  22. __EXPORT
  23. void repl_start(){
  24. _repl = pkpy_new_repl(pkpy_new_vm(true));
  25. }
  26. __EXPORT
  27. bool repl_input(const char* line){
  28. return pkpy_repl_input(_repl, line) == NEED_MORE_LINES;
  29. }
  30. }
  31. #else
  32. void _tvm_dispatch(ThreadedVM* vm){
  33. while(pkpy_tvm_get_state(vm) != THREAD_FINISHED){
  34. if(pkpy_tvm_get_state(vm) == THREAD_SUSPENDED){
  35. PyObjectDump* obj = pkpy_tvm_read_json(vm);
  36. bool is_input_call = INPUT_JSONRPC_STR == obj->json;
  37. if(is_input_call){
  38. std::string line;
  39. std::getline(std::cin, line);
  40. pkpy_tvm_resume(vm, line.c_str());
  41. }else{
  42. std::cout << "unknown jsonrpc call" << std::endl;
  43. std::cout << obj->json << std::endl;
  44. exit(3);
  45. }
  46. pkpy_delete(obj);
  47. }
  48. }
  49. }
  50. int main(int argc, char** argv){
  51. if(argc == 1){
  52. #ifndef PK_DEBUG_THREADED_REPL
  53. VM* vm = pkpy_new_vm(true);
  54. #else
  55. ThreadedVM* vm = pkpy_new_tvm(true);
  56. #endif
  57. REPL repl(vm);
  58. while(true){
  59. (*vm->_stdout) << (repl.is_need_more_lines() ? "... " : ">>> ");
  60. std::string line;
  61. std::getline(std::cin, line);
  62. int result = pkpy_repl_input(&repl, line.c_str());
  63. #ifdef PK_DEBUG_THREADED_REPL
  64. if(result == (int)EXEC_DONE){
  65. _tvm_dispatch(vm);
  66. pkpy_tvm_reset_state(vm);
  67. }
  68. #endif
  69. }
  70. return 0;
  71. }
  72. if(argc == 2){
  73. std::string filename = argv[1];
  74. if(filename == "-h" || filename == "--help") goto __HELP;
  75. std::ifstream file(filename);
  76. if(!file.is_open()){
  77. std::cerr << "File not found: " << filename << std::endl;
  78. return 1;
  79. }
  80. std::string src((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
  81. ThreadedVM* vm = pkpy_new_tvm(true);
  82. _Code code;
  83. Timer("Compile time").run([&]{
  84. code = compile(vm, src.c_str(), filename);
  85. });
  86. if(code == nullptr) return 1;
  87. //std::cout << code->toString() << std::endl;
  88. // Timer("Running time").run([=]{
  89. // vm->exec(code);
  90. // });
  91. // for(auto& kv : _strIntern)
  92. // std::cout << kv.first << ", ";
  93. Timer("Running time").run([=]{
  94. vm->execAsync(code);
  95. _tvm_dispatch(vm);
  96. });
  97. return 0;
  98. }
  99. __HELP:
  100. std::cout << "Usage: pocketpy [filename]" << std::endl;
  101. return 0;
  102. }
  103. #endif