udem-dlteam / ribbit

A portable, compact and extensible Scheme implementation that is fully R4RS compliant. This includes closures, I/O, tail calls, first-class continuations and a Read Eval Print Loop (REPL). The R4RS Scheme REPL fits inside 6.5Kb !
BSD 3-Clause "New" or "Revised" License
490 stars 43 forks source link

Interactive fiction on the r4rs/tc web repl? (cleaning the textarea and using read-line) #76

Open kakafarm opened 1 month ago

kakafarm commented 1 month ago

I am trying to write an interactive fiction game in Ribbit. Right now I am able to load arbitrary Scheme code as shown in the web repl examples, using this function after successfully fetching the source:

function feed_code(repl, text) {
    for (let i = 0; i < text.length; i++) {
        if (text.charCodeAt(i) == 10) {
            repl.dispatchEvent(
                new KeyboardEvent('keypress', {'keyCode': 13}),
            );
        } else {
            repl.value += text[i];
        };
    };
};

I am missing two things before I am able to use Ribbit for this game:

Clean the repl textarea after loading the source code:

When I repl.value = "", the repl stops working. No new user input makes the repl print a result.

Read the user input:

When I try to read user input using (read-line), the repl stops working and no new user input makes the repl print a result.

leo-ard commented 1 month ago

@kakafarm I just added some examples files to make your quest of making a text-driven game easier! (see #77 for details)

In general, I think you shouldn't try to "hook" into the r4rs repl with JavaScript code (like you are showing with the feed_code function). This can lead to read/write problems and is not very portable to other hosts. Instead, I suggest that you compile the game scheme file to a JavaScript program using ribbit and enable the "web" capabilities. To showcase what I mean, I added a text-game.scm program to the examples. You can compile it like this:

$ ./rsc.exe -t js -l r4rs/tc -f+ js/web -o game.js examples/text-game.scm

Now, game.js can be loaded in any html file and will create a text-area with the running scheme program. I added an example "console.html" under host/js/console.html with #77 as well. See the comments inside text-game.scm for information about how to run the scheme progam in web-javascript or python :)

I made these examples quickly, if you encounter problems, don't hesitate to report them

kakafarm commented 1 month ago

Thank you. Will post an update when there's something not very embarrassing, only a little.