[gambit-list] javascript transpiler

Marc Feeley feeley at iro.umontreal.ca
Fri Feb 11 09:00:08 EST 2022


Your version of Gambit is way too old.  You need the latest version (v4.9.4).  The documentation (https://gambitscheme.org/latest/manual/) gives the details on how to use the compiler.  For a simple use-case of the JavaScript backend you can do this (which executes the JavaScript code with nodejs):

$ cat hello.scm
(define msg "hello world!\n")
(display msg)
$ gsc -target js -exe -o hello.js hello.scm
$ node hello.js
hello world!

Here is an example which executes the JavaScript code in the browser:

$ cat hello-web.scm
(define msg "hello world!\n")
(display msg)
(define (alert str) (##inline-host-statement "alert(@scm2host@(@1@));" str))
(alert msg)
$ gsc -target js -exe -o hello-web.html hello-web.scm
$ open hello-web.html 

Note that by default `display` will send its output to the browser’s JavaScript console, i.e. the same place as the JavaScript console.log(…).  The last 2 lines of the program call the JavaScript `alert` function to also show the message in a dialog box.

Finally, `display` can be called with the port corresponding to the web REPL (which is implemented by the Gambit runtime library through a browser dialog box):

$ cat hello-web-repl.scm 
(define msg "hello world!\n")
(display msg (repl-output-port))
(##repl-debug) ;; start a REPL in a dialog box
$ gsc -target js -exe -o hello-web-repl.html hello-web-repl.scm
$ open hello-web-repl.html

The web REPL is a good way to do “live debugging” of the Scheme program.  It also allows tinkering with JavaScript.  For example, you could get the current date at the web REPL by entering:

(host-eval "new Date().toString()")

There’s also a JavaScript FFI based on infix syntax for an even more user-friendly way to interface to JavaScript.  Please visit try.gambitscheme.org for a demo (wait a few seconds for it to start automatically).  The demo shows off several features, such as the FFI, single-stepping, importing R7RS modules from the web, and the thread system.

Marc



> On Feb 11, 2022, at 5:37 AM, Jean-François Trevien <jef.trevien at gmail.com> wrote:
> 
> Hello,
> I tried to to use the transpiler:
>  gsc -target js hello.scm
> with hello.scm containing :
> (display "hello word")
> And i have the error message :
> *** ERROR IN "hello.js"@74.39 -- Datum expected
> 
> The version i use was install by chocolatey :
> gambit v4.6.6.20121126
> 
> what's wrong?
> _______________________________________________
> Gambit-list mailing list
> Gambit-list at iro.umontreal.ca
> https://mailman.iro.umontreal.ca/cgi-bin/mailman/listinfo/gambit-list




More information about the Gambit-list mailing list