On 2013-09-24, at 10:03 AM, Alexander Shendi Alexander.Shendi@web.de wrote:
Dear List,
I am trying to use the Gambit-Scheme JS-backend. I am using the 4.7.0 release. I compiled the following program (obtained from this list) with: gsc -c -target js fib.scm ;; ===================== fib.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 (fib 30))
;; EOF
I tried to run it:
node fib.js
/home/alexshendi/sources/gambit/fib.js:42 print(obj); ^ ReferenceError: print is not defined at Gambit_println (/home/alexshendi/sources/gambit/fib.js:42:3) at Object.Gambit_bb1_println [as println] (/home/alexshendi/sources/gambit/fib.js:66:3) at Gambit_bb4__20_fib (/home/alexshendi/sources/gambit/fib.js:119:31) at Gambit_bb3__20_fib (/home/alexshendi/sources/gambit/fib.js:104:12) at Gambit_trampoline (/home/alexshendi/sources/gambit/fib.js:21:10) at Object.<anonymous> (/home/alexshendi/sources/gambit/fib.js:252:1) at Module._compile (module.js:456:26) at Object.Module._extensions..js (module.js:474:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:312:12)
Now I have the following questions:
- Apparently the generated code is for another JS implementation (Spidermonkey?)?
- How do I access e.g. console.log() from scheme?
- I tried to use the standard Scheme functions "display" and "newline" from fib.scm, but that didn't work. How do I use them?
- Where do I find more info on the JS backend?
Many thanks in advance for your help.
Best Regrads,
Alexander
I develop mostly with the shells d8 (the v8 shell) and js (the SpiderMonkey shell).
For node.js you could replace the call to "print" in the generated code by a call to "console.log" (either directly in the generated file, or modify the file gsc/_t-univ.scm (universal backend) where it calls print).
You could also write your code like this:
(declare (standard-bindings) (extended-bindings) (not safe) (fixnum) (block))
(define (console.log x) ;; Note: the parameter x will be in variable Gambit_r1 (##inline-host-code "console.log(Gambit_r1);\n") #f)
(define (exit) (##inline-host-code "process.exit();\n") #f)
(define (fib n) (define (fib n) (if (< n 2) n (+ (fib (- n 1)) (fib (- n 2))))) (fib n))
(console.log (fib 30))
(exit)
The ##inline-host-code can be used to insert arbitrary JS code in the generated code. You have to know that the global variables Gambit_r1, Gambit_r2, etc will contain the parameters (up to 3 parameters).
Currently there is substantial missing functionality in the "universal" backend (which generates JS, but also Ruby, Python, and PHP). Many Scheme predefined functions are not implemented, such as display and newline. The objective is to implement sufficient core functionality so that the Gambit runtime system (which defines the Scheme predefined functions and much more) can be compiled. In the long run, this will be less work and it will be more maintainable than writing each of these predefined functions directly in JS.
If you have specific needs just let me know and I can direct you on how to extend the universal backend.
Marc