Fix vlang plugin to run scripts and add example ##script

This commit is contained in:
pancake 2019-10-01 01:43:13 +02:00
parent e0e8da58ef
commit a6ee807058
2 changed files with 71 additions and 51 deletions

11
doc/vlang-example.v Normal file
View File

@ -0,0 +1,11 @@
fn entry(core &R2) {
println('hello world')
println(core.cmd('pd 10'))
s := core.cmd('pxwj 64')
args := s.substr(1, s.len-1).split(',')
for i := 0 ; i < args.len; i++ {
n := args[i]
println(n)
}
println(args[0])
}

View File

@ -4,11 +4,69 @@
#include "r_core.h"
#include "r_lang.h"
static int lang_v_file(RLang *lang, const char *file) {
static int lang_v_file(RLang *lang, const char *file);
static const char *r2v = \
"module r2\n"
"\n"
"#flag `pkg-config --cflags --libs r_core`\n"
"\n"
"#include <r_core.h>\n"
"\n"
"struct R2 {}\n"
"\n"
"pub fn (core &R2)cmd(s string) string {\n"
" o := C.r_core_cmd_str (core, s.str)\n"
" strs := string(byteptr(o))\n"
" free(o)\n"
" return strs\n"
"}\n"
"\n"
"pub fn (core &R2)str() string {\n"
" return i64(core).str()\n"
"}\n"
"\n"
"pub fn (core &R2)free() {\n"
" C.r_core_free (core)\n"
"}\n"
"\n"
"pub fn new() &R2 {\n"
" return &R2(C.r_core_new ())\n"
"}\n";
static const char *vsk = \
"fn entry(core &R2) {\n";
static int __run(RLang *lang, const char *code, int len) {
FILE *fd = r_sandbox_fopen (".tmp.v", "w");
if (fd) {
fputs (r2v, fd);
if (len < 0) {
fputs (code, fd);
} else {
fputs (vsk, fd);
fputs (code, fd);
fputs ("}", fd);
}
fclose (fd);
lang_v_file (lang, ".tmp.v");
r_file_rm (".tmp.v");
return true;
}
eprintf ("Cannot open .tmp.v\n");
return false;
}
static int lang_v_file(RLang *lang, const char *file) {
if (!r_str_endswith (file, ".v")) {
return false;
}
if (strcmp (file, ".tmp.v")) {
char *code = r_file_slurp (file, NULL);
int r = __run (lang, code, -1);
free (code);
return r;
}
if (!r_file_exists (file)) {
eprintf ("file not found (%s)\n", file);
return false;
@ -53,56 +111,8 @@ static int lang_v_file(RLang *lang, const char *file) {
return 0;
}
static int lang_v_init(void *user) {
// TODO: check if "valac" is found in path
return true;
}
static const char *vsk = \
"module r2\n"
"\n"
"#flag -I /usr/local/include/libr\n"
"#flag -L /usr/local/lib -lr_core\n"
"// #flag -flto\n"
"\n"
"#include <r_core.h>\n"
"\n"
"struct R2 {}\n"
"\n"
"pub fn (core &R2)cmd(s string) string {\n"
" o := C.r_core_cmd_str (core, s.str)\n"
" strs := string(byteptr(o))\n"
" free(o)\n"
" return strs\n"
"}\n"
"\n"
"pub fn (core &R2)str() string {\n"
" return i64(core).str()\n"
"}\n"
"\n"
"pub fn (core &R2)free() {\n"
" C.r_core_free (core)\n"
"}\n"
"\n"
"pub fn new() &R2 {\n"
" return &R2(C.r_core_new ())\n"
"}\n"
"fn entry(core &R2) {\n";
static int lang_v_run(RLang *lang, const char *code, int len) {
FILE *fd = r_sandbox_fopen (".tmp.v", "w");
if (fd) {
fputs (vsk, fd);
fputs (code, fd);
fputs ("}", fd);
fclose (fd);
lang_v_file (lang, ".tmp.v");
r_file_rm (".tmp.v");
return true;
}
eprintf ("Cannot open .tmp.v\n");
return false;
return __run (lang, code, len);
}
static RLangPlugin r_lang_plugin_v = {
@ -111,6 +121,5 @@ static RLangPlugin r_lang_plugin_v = {
.desc = "V language extension",
.license = "LGPL",
.run = lang_v_run,
.init = (void*)lang_v_init,
.run_file = (void*)lang_v_file,
};