Skip to content

Commit

Permalink
readline: dynamically parse potion code from p2
Browse files Browse the repository at this point in the history
dlopen the potion syntax .so, parse and execute it by using p2_eval. (disabled)
export potion_find_file
  • Loading branch information
Reini Urban committed Sep 13, 2013
1 parent c3ecc87 commit d08be52
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
4 changes: 2 additions & 2 deletions core/load.c
Expand Up @@ -13,7 +13,7 @@
#include "internal.h"
#include "table.h"

PN potion_load_code(Potion *P, const char *filename) {
static PN potion_load_code(Potion *P, const char *filename) {
PN buf, code;
struct stat stats;
int fd = -1;
Expand Down Expand Up @@ -61,7 +61,7 @@ static char *potion_initializer_name(Potion *P, const char *filename, PN_SIZE le
return func_name;
}

PN potion_load_dylib(Potion *P, const char *filename) {
static PN potion_load_dylib(Potion *P, const char *filename) {
void *handle = dlopen(filename, RTLD_LAZY);
void (*func)(Potion *);
char *err, *init_func_name;
Expand Down
1 change: 1 addition & 0 deletions core/potion.h
Expand Up @@ -879,6 +879,7 @@ PN potion_run(Potion *, PN, int);
PN_F potion_jit_proto(Potion *, PN);

PN potion_load(Potion *, PN, PN, PN);
char *potion_find_file(char *str, PN_SIZE str_len);
PN potion_class_find(Potion *, PN);
PNType potion_class_type(Potion *, PN);

Expand Down
25 changes: 25 additions & 0 deletions front/p2.c
Expand Up @@ -368,6 +368,7 @@ int main(int argc, char *argv[]) {
potion_define_global(P, PN_STR("$0"), PN_STR("-i"));
potion_define_global(P, potion_str(P, "$P2::interactive"), PN_NUM(1));
// TODO: p5 not yet parsed
#if 0
p2_eval(P, potion_byte_str(P,
"load 'readline';\n" \
"while ($code = readline('>> ')) {\n" \
Expand All @@ -378,6 +379,30 @@ int main(int argc, char *argv[]) {
" say '=> ', $obj;\n" \
" }\n"
"}"));
#else
// dynamically parse potion code from p2
#include <dlfcn.h>
{
PN (*potion_parser)(Potion *, PN, char*);
void *handle = dlopen(potion_find_file("libsyntax",9), RTLD_LAZY);
potion_parser = (PN (*)(Potion *, PN, char*))dlsym(handle, "potion_parse");
PN code = potion_parser(P, potion_byte_str(P,
"load 'readline'\n" \
"loop:\n" \
" code = readline('>> ')\n" \
" if (not code): \"\\n\" print, break.\n" \
" if (code != ''):\n" \
" obj = code eval\n" \
" if (obj kind == Error):\n" \
" obj string print." \
" else: ('=> ', obj, \"\\n\") join print.\n" \
" .\n"
"."), "<eval>");
if (PN_TYPE(code) != PN_TSOURCE) return code;
code = potion_send(code, PN_compile, PN_NIL, PN_NIL);
return potion_run(P, code, P->flags & EXEC_JIT);
}
#endif
}
END:
#if !defined(POTION_JIT_TARGET) || defined(DEBUG)
Expand Down

0 comments on commit d08be52

Please sign in to comment.