This commit is contained in:
blueloveTH 2023-02-01 15:30:57 +08:00
parent 5c1128d4ab
commit 2012e47507
4 changed files with 18 additions and 53 deletions

View File

@ -28,6 +28,8 @@ for n in [4,3,2,1]:
c_args = ['VM*'] + [mapping[i] for i in p_args]
c_ret = mapping[p_ret]
name = f'__f_{p_ret}__{"_".join(p_args)}'
# if c_ret == 'const char*':
# c_ret = 'char*'
s = f'typedef {c_ret} (*{name})({", ".join(c_args)});'
s += '\n'

View File

@ -270,42 +270,6 @@ class dict:
a.append(k.__json__()+': '+v.__json__())
return '{'+ ', '.join(a) + '}'
import ffi
def input():
return ffi.input()
class FileIO:
def __init__(self, path, mode):
assert type(path) is str
assert type(mode) is str
assert mode in ['r', 'w', 'rt', 'wt']
self.path = path
self.mode = mode
self.fp = ffi.fopen(path, mode)
def read(self):
assert self.mode in ['r', 'rt']
return ffi.fread(self.fp)
def write(self, s):
assert self.mode in ['w', 'wt']
assert type(s) is str
ffi.fwrite(self.fp, s)
def close(self):
ffi.fclose(self.fp)
def __enter__(self):
pass
def __exit__(self):
self.close()
def open(path, mode='r'):
return FileIO(path, mode)
class set:
def __init__(self, iterable=None):
iterable = iterable or []

View File

@ -3,31 +3,28 @@
#include "pocketpy.h"
#define PK_DEBUG_TIME
struct Timer{
const char* title;
Timer(const char* title) : title(title) {}
void run(std::function<void()> f){
#ifdef PK_DEBUG_TIME
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;
std::cout << title << ": " << elapsed << " s" << std::endl;
#else
f();
#endif
}
};
#ifndef __NO_MAIN
int main(int argc, char** argv){
VM* vm = pkpy_new_vm(true);
pkpy_vm_bind__f_str__(vm, "builtins", "input", [](VM* vm){
static std::string line;
std::getline(std::cin, line);
return line.c_str();
});
if(argc == 1){
VM* vm = pkpy_new_vm(true);
REPL repl(vm);
int result = -1;
while(true){
@ -36,6 +33,7 @@ int main(int argc, char** argv){
std::getline(std::cin, line);
result = pkpy_repl_input(&repl, line.c_str());
}
pkpy_delete(vm);
return 0;
}
@ -49,13 +47,17 @@ int main(int argc, char** argv){
return 1;
}
std::string src((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
PyVarOrNull ret = nullptr;
VM* vm = pkpy_new_vm(true);
Timer("Running time").run([=]{
vm->exec(src.c_str(), filename, EXEC_MODE);
});
if(filename[0] != '_'){
Timer("Running time").run([&]{
ret = vm->exec(src.c_str(), filename, EXEC_MODE);
});
}else{
ret = vm->exec(src.c_str(), filename, EXEC_MODE);
}
pkpy_delete(vm);
return 0;
return ret != nullptr ? 0 : 1;
}
__HELP:

View File

@ -766,9 +766,6 @@ extern "C" {
__add_module_re(vm);
__add_module_dis(vm);
vm->new_module("ffi");
// add builtins | no exception handler | must succeed
_Code code = vm->compile(__BUILTINS_CODE, "<builtins>", EXEC_MODE);
vm->_exec(code, vm->builtins, pkpy::make_shared<PyVarDict>());