main.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. pkpy_delete(obj);
  38. if(is_input_call){
  39. std::string line;
  40. std::getline(std::cin, line);
  41. pkpy_tvm_resume(vm, line.c_str());
  42. }else{
  43. exit(999);
  44. pkpy_tvm_resume(vm, nullptr);
  45. }
  46. }
  47. }
  48. }
  49. int main(int argc, char** argv){
  50. if(argc == 1){
  51. #ifndef PK_DEBUG_THREADED_REPL
  52. VM* vm = pkpy_new_vm(true);
  53. #else
  54. ThreadedVM* vm = pkpy_new_tvm(true);
  55. #endif
  56. REPL repl(vm);
  57. while(true){
  58. (*vm->_stdout) << (repl.is_need_more_lines() ? "... " : ">>> ");
  59. std::string line;
  60. std::getline(std::cin, line);
  61. int result = pkpy_repl_input(&repl, line.c_str());
  62. #ifdef PK_DEBUG_THREADED_REPL
  63. if(result == (int)EXEC_DONE){
  64. _tvm_dispatch(vm);
  65. pkpy_tvm_reset_state(vm);
  66. }
  67. #endif
  68. }
  69. return 0;
  70. }
  71. if(argc == 2){
  72. std::string filename = argv[1];
  73. if(filename == "-h" || filename == "--help") goto __HELP;
  74. std::ifstream file(filename);
  75. if(!file.is_open()){
  76. std::cerr << "File not found: " << filename << std::endl;
  77. return 1;
  78. }
  79. std::string src((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
  80. ThreadedVM* vm = pkpy_new_tvm(true);
  81. _Code code;
  82. Timer("Compile time").run([&]{
  83. code = compile(vm, src.c_str(), filename);
  84. });
  85. if(code == nullptr) return 1;
  86. //std::cout << code->toString() << std::endl;
  87. // Timer("Running time").run([=]{
  88. // vm->exec(code);
  89. // });
  90. // for(auto& kv : _strIntern)
  91. // std::cout << kv.first << ", ";
  92. Timer("Running time").run([=]{
  93. vm->execAsync(code);
  94. _tvm_dispatch(vm);
  95. });
  96. return 0;
  97. }
  98. __HELP:
  99. std::cout << "Usage: pocketpy [filename]" << std::endl;
  100. return 0;
  101. }
  102. #endif