| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- #include <iostream>
- #include <fstream>
- #include "pocketpy.h"
- //#define PK_DEBUG_TIME
- struct Timer{
- const char* title;
- Timer(const char* title) : title(title) {}
- void run(std::function<void()> f){
- auto start = std::chrono::high_resolution_clock::now();
- f();
- auto end = std::chrono::high_resolution_clock::now();
- double elapsed = std::chrono::duration_cast<std::chrono::microseconds>(end - start).count() / 1000000.0;
- #ifdef PK_DEBUG_TIME
- std::cout << title << ": " << elapsed << " s" << std::endl;
- #endif
- }
- };
- VM* newVM(){
- VM* vm = pkpy_new_vm([](const VM* vm, const char* str) {
- std::cout << str;
- std::cout.flush();
- }, [](const VM* vm, const char* str) {
- std::cerr << str;
- std::cerr.flush();
- });
- return vm;
- }
- #if defined(__EMSCRIPTEN__) || defined(__wasm__) || defined(__wasm32__) || defined(__wasm64__)
- // these code is for demo use, feel free to modify it
- REPL* _repl;
- extern "C" {
- __EXPORT
- void repl_start(){
- _repl = pkpy_new_repl(newVM(), false);
- }
- __EXPORT
- bool repl_input(const char* line){
- return pkpy_input_repl(_repl, line);
- }
- }
- #else
- int main(int argc, char** argv){
- if(argc == 1){
- REPL repl(newVM());
- while(true){
- std::string line;
- std::getline(std::cin, line);
- repl.input(line);
- }
- return 0;
- }
-
- if(argc == 2){
- std::string filename = argv[1];
- if(filename == "-h" || filename == "--help") goto __HELP;
- std::ifstream file(filename);
- if(!file.is_open()){
- std::cerr << "File not found: " << filename << std::endl;
- return 1;
- }
- std::string src((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
- VM* vm = newVM();
- _Code code;
- Timer("Compile time").run([&]{
- code = compile(vm, src.c_str(), filename);
- });
- if(code == nullptr) return 1;
- //std::cout << code->toString() << std::endl;
- Timer("Running time").run([=]{
- vm->exec(code);
- });
- return 0;
- }
- __HELP:
- std::cout << "Usage: pocketpy [filename]" << std::endl;
- return 0;
- }
- #endif
|