Ok, here's my first attempt at using minipicolisp as a library. (Leaving aside the issue of get/put for now. Basically replacing main() with:
#if !LIBRARY /*** Main ***/ int main(int ac, char *av[]) { char *p; . . . #else // we're building as a library void miniPicoLisp_init( const char * home_dir ) { heapAlloc(); initSymbols(); if (home_dir) { int l = strlen(home_dir) + 1; Home = malloc( l ); memcpy(Home, home_dir, l ); Home[ l ] = '\0'; } Reloc = Nil; InFile = stdin, Env.get = getStdin; OutFile = stdout, Env.put = putStdout; // ?? ApplyArgs = cons(cons(consSym(Nil,0), Nil), Nil); ApplyBody = cons(Nil,Nil); } any miniPicoLisp_dostring( char *str ) { return load(NULL, 0, mkStr(str)); } #endif And then testing it with this: #include "pico.h" extern void miniPicoLisp_init( const char * home_dir ); extern any miniPicoLisp_dostring( char *str ); #define MAX_STR 1024 int main(int ac, char *av[]) { char buf[MAX_STR]; miniPicoLisp_init( NULL ); while (fgets(buf, MAX_STR, stdin) != NULL) { // read any v = miniPicoLisp_dostring( buf ); // eval doPrint( v ); // print } return 0; } Doug --- On Sat, 6/18/11, Alexander Burger <a...@software-lab.de> wrote: > From: Alexander Burger <a...@software-lab.de> > Subject: Re: embedding minipicolisp > To: picolisp@software-lab.de > Date: Saturday, June 18, 2011, 11:21 PM > Hi Doug, > > > I'd like to embed minipicolisp in C applications I'd > like to be able > > to create a minipicolisp environment, and then pass > strings to it > > (commands) for execution. It would also be great to be > able get back the > > results in a string. > > The mechanism in PicoLisp to execute a string is 'load'. If > an argument > to 'load' starts with a hyphen, the rest is interpreted as > an expression > (without the surrounding parentheses). This mechanism is > normally used > to run expressions from the command line. > > The lower level functions for that are 'str', both for > converting a > transient symbol to a list (without the surrounding > parentheses) and to > do the reverse, and EVAL() (which is actually not a > function but a > macro). > > Unfortunately, there is no way currently to convert a C > string directly > to an executable Lisp expression (without the intermediate > transient > symbol). > > > > In lua, you'd do something like, > > L = lua_open(); > > Here you'd need a function that initialized the PicoLisp > VM, as is done > by main(), i.e. calls heapAlloc(), initSymbol() etc. > > > > luaL_dostring(L, str_in ); > > str_out = l_results_to_string(L); > > You need a C function similar to 'loadAll()'. It would call > 'mkStr()' on > the C string to create the transient symbol, then load() to > run the > expression. > > Better would be to cook something more specialized, similar > to parse(), > to avoid the intermediate transient symbol step. > > > To pack the result into a C string, you need a mechanism > similar to > doStr(). > > You define a function which writes a single byte into a C > character > array, > > void putCharC(int c) { > *CBytePtr++ = c; > } > > and store a pointer to that function into 'Env.put': > > void (*putSave)(int); > > putSave = Env.put; > Env.put = putCharC; > print(result); > Env.put = putSave; > > > Hope this helps. > > Cheers, > - Alex > -- > UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe > -- UNSUBSCRIBE: mailto:picolisp@software-lab.de?subject=Unsubscribe