[gambit-list] Embedding a REPL in an executable

Christian Jaeger christian at pflanze.mine.nu
Sun Feb 10 07:38:28 EST 2008


Neil Baylis wrote:
> I'm building an executable that links with an object file that controls a
> machine via the USB bus.
>
> I have this working OK by linking everything into an executable. But that
> means I must recompile every time I make a change to my scheme code.
> Instead, I'd like to be able to load the scheme code at runtime. Originally
> I wanted to compile my c code into a library so that I could load it with
> gsi. However i was unable to get that to work.
>   

Well, you could (c-declare "#include ..") the C code into a yourfile.scm 
file and compile that file to a loadable object (using (compile-file 
"yourfile.scm") or gsc yourfile.scm) and then load it through (load 
"yourfile").

(The advantage would be that each time you alter your C code you could 
recompile and reload it into your running application, too)

See the attached example. Run it like:

in Emacs: C-u M-x run-scheme gsc
from the shell: just "gsc"

 > (begin (compile-file "reload-c-code") (load "reload-c-code"))
"/home/chris/schemedevelopment2/gambit/reload-c-code.o1"
 > (do-something "Hello")
We're doing something: Hello
0
 > (do-something "Hello")
We're doing something: Hello
1
 > (do-something "Hello")
We're doing something: Hello
2
 > (begin (compile-file "reload-c-code") (load "reload-c-code"))
"/home/chris/schemedevelopment2/gambit/reload-c-code.o2"
 > (do-something "Hello")
We're doing something: Hello
0
 > (do-something "Hello")
We're doing something: Hello
1

Now change the string given to fprintf in MyCcode.c and, from the still 
running gsc process:

 > (begin (compile-file "reload-c-code") (load "reload-c-code"))
"/home/chris/schemedevelopment2/gambit/reload-c-code.o3"
 > (do-something "Hello")
We're still doing something: Hello
0
 > (do-something "World")
We're still doing something: World
1


> Poking around in the examples, I found (##repl-debug-main), so I put that at
> the end of my c-api .scm file. This works well. When I run the executable, I
> get a repl, and can then load further scheme files as needed.
>
> Is this the recommended way to embed a Gambit repl in a c program? Where can
> I find this ##repl-debug-main documented?

Probably only in the sources, it's in lib/_repl.scm

> What's the significance of the  ##
> character? 

See http://dynamo.iro.umontreal.ca/~gambit/wiki/index.php/Namespaces

> Is there a way to pass startup arguments to this repl?
>   

As you can see from the sources, no.

There are also ##repl-debug and ##repl procedures being called by the 
former, check if they do what you want. (Read the sources as examples; 
as mentioned in the wiki page above, many or generally all of those 
procedures just segfault if you're feeding them data of the wrong type. 
You could try recompiling Gambit with (declare (safe)) in the places you 
want to play with, or maybe in header.scm.)

Christian.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: reload-c-code.scm
Type: text/x-scheme
Size: 217 bytes
Desc: not available
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20080210/73c116f3/attachment.bin>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: MyCcode.c
Type: text/x-csrc
Size: 149 bytes
Desc: not available
URL: <http://mailman.iro.umontreal.ca/pipermail/gambit-list/attachments/20080210/73c116f3/attachment.c>


More information about the Gambit-list mailing list