Thanks for the help. This was really useful.
I'm still getting errors on the python backend though, FWIW. It's not a huge deal, because I'm mostly targeting javascript, but I am a heavy python user, too, so it would be nice for future reference.
I may have botched something, and you'll see it immediately, but I'm not seeing it if I made a mistake. Here's what I did:
code.scm
;(declare
; (standard-bindings)
; (extended-bindings)
; (not safe)
; (fixnum)
; (block)
;)
(define (fib n)
(define (fib n)
(if (< n 2)
n
(+ (fib (- n 1))
(fib (- n 2)))))
(fib n))
;(println (eval `(* 1 1)))
(println (fib 5))
This is with that top declare block commented out:
$ python2 p.py
Traceback (most recent call last):
File "p.py", line 799, in <module>
NameError: global name 'g_cst0_code' is not defined
And if I have the top declare block on:
$ python2 p.py
Traceback (most recent call last):
File "p.py", line 26612, in <module>
NameError: global name 'g_cst1727___gambit' is not defined
These were the sequence of commands I used to build (after rebuilding the _gambit.py with make _gambit.py):
$ gsc -:=. -warnings -target python code.scm
$ gsc -:=. -warnings -target python -link -l ~/gambit/lib/_gambit.py code.py
$ cat code_.py code.py ~/gambit/lib/_gambit.py > p.py
$ python2 p.py
Thanks for your help.
-Jon
Yes that is mostly correct. However I would recommend the following commands:
for JavaScript:
$ cd lib
$ make _gambit.js
$ cd ..
$ gsc/gsc -:=. -warnings -target js code.scm
$ gsc/gsc -:=. -warnings -target js -link -l lib/_gambit.js code.js
$ cat code_.js code.js lib/_gambit.js > p.js
$ node p.js
for Python:
$ cd lib
$ make _gambit.py
$ cd ..
$ gsc/gsc -:=. -warnings -target python code.scm
$ gsc/gsc -:=. -warnings -target python -link -l lib/_gambit.py code.py
$ cat code_.py code.py lib/_gambit.py > p.py
$ python p.py
Note that there was a small ordering problem in the generated code (now fixed) that prevented the Python code to run properly.
The universal backend is a WIP but close to a “final product”. Remaining on the TODO:
1) changes to the Gambit library (lib/*.scm) and the runtime system to support more of the Gambit system features, in particular non-blocking I/O which is needed to support Gambit’s green threads
2) integration to the main installation process (to build and install _gambit.js, _gambit.py, etc in the Gambit installation directory)
3) documentation (!)
The universal backend is definitely usable without #1 (some users have built commercial products with it) as long as the application uses ##inline-host-* to interface with hand written code to do the user-interface or I/O.
Here is a simple example for using ##inline-host-* in JavaScript:
(declare (extended-bindings))
(##inline-host-declaration "
function rand(n) {
return Math.floor(n * Math.random());
}
")
(define (rand n)
(+ 1 (##inline-host-expression "g_host2scm(rand(g_scm2host(@1@)))" n)))
(println (rand 6))
(println (rand 6))
(println (rand 6))
(println (rand 6))
(##inline-host-statement "console.log(@1@);" 123)
(##inline-host-statement "console.log(@1@);" "hello")
(##inline-host-statement "console.log(@1@);" 'world)
(##inline-host-statement "console.log(@1@);" (cons 11 22))
(##inline-host-statement "console.log(@1@);" (vector 11 22 33))
(##inline-host-statement "console.log(@1@);" rand)
Hope that helps!
Marc
> On Aug 14, 2016, at 12:23 AM, Jon Woodring <woodring.jon@gmail.com> wrote:
>
> Is there any documentation on compiling to javascript, or is it mostly in the mailing list archives? After some scrounging, I was finally able to figure out how to compile and get a simple (println (fib 5)) working:
>
> $ cd gambit/lib
> $ gsc -:~~bin=../bin,~~lib=../lib,~~include=../include -f -target js -prelude "(##include \"header.scm\")" -o _gambit.js ../lib/_univlib.scm
>
> $ gsc -warnings -target js code.scm
> $ gsc -warnings -link -l _gambit.js code.js
> $ cat code_.js _gambit.js code.js > p.js
> $ node p.js
>
> 1) Is that mostly correct? I take it it is still a WIP because I couldn't find documentation.
>
> 2) What if I want to use the Python backend? I tried swapping in all of the targets with -target python but then get this error:
> $ python2 p.py
> Traceback (most recent call last):
> File "p.py", line 26310, in <module>
> g_bb1_serial_2d_number_2d__3e_object.name = g_cst1727___gambit
> NameError: global name 'g_cst1727___gambit' is not defined
>
> 3) Is there a concise example of #inline-host-expression (-statement & -declaration) to be able to have scheme call javascript and vice versa? Same with python? I'm not sure what is correct from my reading of the mailing archive.
>
> Thanks,
> -Jon
>
> _______________________________________________
> Gambit-list mailing list
> Gambit-list@iro.umontreal.ca
> https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list