This commit is contained in:
blueloveTH 2023-02-06 17:13:22 +08:00
parent e81c1c4491
commit 802e5f8b20
8 changed files with 154 additions and 4538 deletions

View File

@ -1,7 +1,5 @@
with open("src/opcodes.h", "rt", encoding='utf-8') as f: with open("src/opcodes.h", "rt", encoding='utf-8') as f:
OPCODES_TEXT = f.read() OPCODES_TEXT = f.read()
with open("src/_bindings.h", "rt", encoding='utf-8') as f:
_BINDINGS_TEXT = f.read()
pipeline = [ pipeline = [
["hash_table8.hpp", "common.h", "memory.h", "str.h", "safestl.h", "builtins.h", "error.h"], ["hash_table8.hpp", "common.h", "memory.h", "str.h", "safestl.h", "builtins.h", "error.h"],
@ -32,7 +30,6 @@ def remove_copied_include(text):
text text
) )
text = text.replace('#include "opcodes.h"', OPCODES_TEXT) text = text.replace('#include "opcodes.h"', OPCODES_TEXT)
text = text.replace('#include "_bindings.h"', _BINDINGS_TEXT)
return text return text
for seq in pipeline: for seq in pipeline:

File diff suppressed because it is too large Load Diff

@ -1 +1 @@
Subproject commit 6df3b2fdfe9b1da642c18bfbd937b94541bf89bc Subproject commit 5a56f312c7e20d3356eaa0ac2711c1abd0da802c

File diff suppressed because it is too large Load Diff

View File

@ -1,57 +0,0 @@
mapping = {
'int': 'i64',
'float': 'f64',
'str': 'const char*',
'bool': 'bool',
'None': 'void'
}
def args(n, a: list, res: list, first=True):
if n == 0:
# 3 args must be the same type
if len(a) == 4 and len(set(a[1:])) != 1:
return
res.append(tuple(a))
return
for p_ret, c_ret in mapping.items():
if not first and p_ret == 'None':
continue
a.append(p_ret)
args(n-1, a, res, first=False)
a.pop()
data = []
for n in [4,3,2,1]:
res = []
args(n, [], res)
for p_ret,*p_args in res:
c_args = [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'
impl = []
for i, p_arg in enumerate(p_args):
impl.append( f'{mapping[p_arg]} _{i} = vm->Py{p_arg.capitalize()}_AS_C(args[{i}]);' )
call_impl = f'f({", ".join([f"_{j}" for j in range(len(p_args))])})';
if p_ret == 'None':
impl.append( call_impl + ';' )
impl.append( 'return vm->None;' )
else:
impl.append ( f'{mapping[p_ret]} ret = {call_impl};' )
impl.append( f'return vm->Py{p_ret.capitalize()}(ret);' )
impl = '\n'.join([' '*8 + i for i in impl])
s += f'''__EXPORT\nvoid pkpy_vm_bind{name[3:]}(VM* vm, const char* mod, const char* name, {name} f) {{
PyVar obj = vm->new_module_if_not_existed(mod);
vm->bindFunc<{len(p_args)}>(obj, name, [f](VM* vm, const pkpy::Args& args) {{
{impl}
}});
}}''' + '\n'
data.append(s)
with open('src/_bindings.h', 'w') as f:
text = '\n'.join(data)
f.write('extern "C" {\n' + text + '}')

File diff suppressed because it is too large Load Diff

View File

@ -19,10 +19,10 @@ struct Timer{
int main(int argc, char** argv){ int main(int argc, char** argv){
VM* vm = pkpy_new_vm(true); VM* vm = pkpy_new_vm(true);
pkpy_vm_bind_str__(vm, "builtins", "input", [](){ vm->bindBuiltinFunc<0>("input", [](VM* vm, const pkpy::Args& args){
static std::string line; static std::string line;
std::getline(std::cin, line); std::getline(std::cin, line);
return line.c_str(); return vm->PyStr(line);
}); });
if(argc == 1){ if(argc == 1){
REPL* repl = pkpy_new_repl(vm); REPL* repl = pkpy_new_repl(vm);

View File

@ -792,6 +792,55 @@ extern "C" {
s_out->str(""); s_err->str(""); s_out->str(""); s_err->str("");
return strdup(ss.str().c_str()); return strdup(ss.str().c_str());
} }
typedef i64 (*f_int_t)(const char*);
typedef f64 (*f_float_t)(const char*);
typedef bool (*f_bool_t)(const char*);
typedef const char* (*f_str_t)(const char*);
typedef void (*f_None_t)(const char*);
static f_int_t f_int = nullptr;
static f_float_t f_float = nullptr;
static f_bool_t f_bool = nullptr;
static f_str_t f_str = nullptr;
static f_None_t f_None = nullptr;
__EXPORT
/// Setup the callback functions.
void pkpy_setup_callbacks(f_int_t f_int, f_float_t f_float, f_bool_t f_bool, f_str_t f_str, f_None_t f_None){
::f_int = f_int;
::f_float = f_float;
::f_bool = f_bool;
::f_str = f_str;
::f_None = f_None;
} }
#include "_bindings.h" __EXPORT
/// Bind a function to a virtual machine.
char* pkpy_vm_bind(VM* vm, const char* mod, const char* name, int ret_code){
if(!f_int || !f_float || !f_bool || !f_str || !f_None) return nullptr;
static int kGlobalBindId = 0;
for(int i=0; mod[i]; i++) if(mod[i] == ' ') return nullptr;
for(int i=0; name[i]; i++) if(name[i] == ' ') return nullptr;
std::string f_header = std::string(mod) + '.' + name + '#' + std::to_string(kGlobalBindId++);
PyVar obj = vm->new_module_if_not_existed(mod);
vm->bindFunc<-1>(obj, name, [ret_code, f_header](VM* vm, const pkpy::Args& args){
_StrStream ss;
ss << f_header << ' ';
for(int i=0; i<args.size(); i++){
PyVar x = vm->call(args[i], __json__);
ss << vm->PyStr_AS_C(x) << ' ';
}
switch(ret_code){
case 'i': return vm->PyInt(f_int(ss.str().c_str()));
case 'f': return vm->PyFloat(f_float(ss.str().c_str()));
case 'b': return vm->PyBool(f_bool(ss.str().c_str()));
case 's': return vm->PyStr(f_str(ss.str().c_str()));
case 'N': f_None(ss.str().c_str()); return vm->None;
}
UNREACHABLE();
return vm->None;
});
return strdup(f_header.c_str());
}
}