Dear Mark,
Thanks for your pointers regarding scheme back-ends for JavaScript. I starting using Gambit but ran into a few problems as follows:
1. The -target compiler option is undocumented. I think Gambit is more capabilities than the public is aware of - i.e. JavaScript back-end.
2. When I compile anything into JS and run it in node I get:
pc = pc(); ^ TypeError: undefined is not a function
If I change the definition of gambit_trampoline to:
function gambit_trampoline(pc) { while (pc !== false && pc !== undefined) { pc = pc(); } }
it fixes the problem.
3. I'd like to print results to the console so I can see what is going on. I'd prefer to use (print) or (display) but I can't get (console.log) to work either.
I even tried:
(define (console.log x) ;; Note: the parameter x will be in variable Gambit_r1 (##inline-host-code "console.log(Gambit_r1);\n") #f)
but that didn't work either.
I checked, console.log seems to be a JS standard. Perhaps your compiler can convert print and display to console.log.
How can I display something?
Thanks.
Blake McBride
Afficher les réponses par date
On Jan 31, 2015, at 12:38 PM, Blake McBride blake@mcbride.name wrote:
Dear Mark,
Thanks for your pointers regarding scheme back-ends for JavaScript. I starting using Gambit but ran into a few problems as follows:
- The -target compiler option is undocumented. I think Gambit is more capabilities than the public is aware of - i.e. JavaScript back-end.
The JavaScript backend is not yet ready for general public usage. The JS code generator is close to complete, but the system lacks a full Scheme library. I am working on a full Scheme library (see https://github.com/feeley/univ-lib) and better debugging support, but it is not quite there yet. Some people are using the JS backend for commercial products but it requires some careful use of the compiler to avoid some of the current pitfalls.
When I compile anything into JS and run it in node I get:
pc = pc(); ^
TypeError: undefined is not a function
If I change the definition of gambit_trampoline to:
function gambit_trampoline(pc) { while (pc !== false && pc !== undefined) { pc = pc(); } }
it fixes the problem.
The problem is not in gambit_trampoline. You are probably calling a Scheme library function without knowing it, for example (+ 1 2) without having the proper declarations to ensure that the + will be inlined by the compiler (because currently a Scheme library is *not* linked with the app).
It would be good to see the code you are trying to compile.
- I'd like to print results to the console so I can see what is going on. I'd prefer to use (print) or (display) but I can't get (console.log) to work either.
I even tried:
(define (console.log x) ;; Note: the parameter x will be in variable Gambit_r1 (##inline-host-code "console.log(Gambit_r1);\n") #f)
but that didn't work either.
I checked, console.log seems to be a JS standard. Perhaps your compiler can convert print and display to console.log.
How can I display something?
The generated code assumes that the “print” JS function will print the argument on a line. This works with d8 and js, the V8 shell and SpiderMonkey shell. Using console.log will not work in those shells, but it will work in browser environments (in web pages). This is such a frequent gotcha that I just changed it so that a Scheme (println obj) will check at run time for the presence of console.log .
An interface to console.log can be written this way:
(define (console.log obj) (##inline-host-statement "console.log(gambit_scm2js(@1@));" obj))
In any case, here is a simple example and instructions on how to run the code in a JS shell and in the browser. If you want to use non-primitive predefined Scheme library functions (such as equal?, append, map, write, etc) you will need to look into univ-lib . The goal is to have all of this neatly packaged in gsc so that one invocation of gsc will do all that is necessary. One issue is linking… should the .js file contain all of the application code including Scheme libraries, or should it contain just the code for the compiled file (so that it can be dynamically loaded by the web page)? Both should be supported with an appropriate compile flag.
Marc
;; File: fib.scm ;; ;; compile with: gsc -c -target js fib.scm ;; ;; execute with: d8 fib.js ;; ;; or use it in an HTML page like this: ;; ;; <!DOCTYPE html> ;; <html> ;; <head> ;; <title>fib</title> ;; <script type="text/javascript"> ;; function print(x) { console.log(x); } ;; </script> ;; <script type="text/javascript" src="fib.js"></script> ;; </head> ;; <body></body> ;; </html>
(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))
(define start (real-time-milliseconds))
(println (fib 30))
(define end (real-time-milliseconds))
(println (- end start))
Here is the result of your example:
$ gsc -c -target js f1.scm $ node f1.js
/home/blake/gambit/f1.js:448 print(obj); ^ ReferenceError: print is not defined at gambit_println (/home/blake/gambit/f1.js:448:3) at gambit_bb1_println (/home/blake/gambit/f1.js:323:3) at gambit_trampoline (/home/blake/gambit/f1.js:348:10) at Object.<anonymous> (/home/blake/gambit/f1.js:688: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) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) $
In any case, here is a simple example and instructions on how to run the code in a JS shell and in the browser. If you want to use non-primitive predefined Scheme library functions (such as equal?, append, map, write, etc) you will need to look into univ-lib . The goal is to have all of this neatly packaged in gsc so that one invocation of gsc will do all that is necessary. One issue is linking… should the .js file contain all of the application code including Scheme libraries, or should it contain just the code for the compiled file (so that it can be dynamically loaded by the web page)? Both should be supported with an appropriate compile flag.
Marc
;; File: fib.scm ;; ;; compile with: gsc -c -target js fib.scm ;; ;; execute with: d8 fib.js ;; ;; or use it in an HTML page like this: ;; ;; <!DOCTYPE html> ;; <html> ;; <head> ;; <title>fib</title> ;; <script type="text/javascript"> ;; function print(x) { console.log(x); } ;; </script> ;; <script type="text/javascript" src="fib.js"></script> ;; </head> ;; <body></body> ;; </html>
(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))
(define start (real-time-milliseconds))
(println (fib 30))
(define end (real-time-milliseconds))
(println (- end start))
You have to pull the latest version from the github repo.
Marc
On Feb 1, 2015, at 11:35 PM, Blake McBride blake@mcbride.name wrote:
Here is the result of your example:
$ gsc -c -target js f1.scm $ node f1.js
/home/blake/gambit/f1.js:448 print(obj); ^ ReferenceError: print is not defined at gambit_println (/home/blake/gambit/f1.js:448:3) at gambit_bb1_println (/home/blake/gambit/f1.js:323:3) at gambit_trampoline (/home/blake/gambit/f1.js:348:10) at Object.<anonymous> (/home/blake/gambit/f1.js:688: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) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) $
In any case, here is a simple example and instructions on how to run the code in a JS shell and in the browser. If you want to use non-primitive predefined Scheme library functions (such as equal?, append, map, write, etc) you will need to look into univ-lib . The goal is to have all of this neatly packaged in gsc so that one invocation of gsc will do all that is necessary. One issue is linking… should the .js file contain all of the application code including Scheme libraries, or should it contain just the code for the compiled file (so that it can be dynamically loaded by the web page)? Both should be supported with an appropriate compile flag.
Marc
;; File: fib.scm ;; ;; compile with: gsc -c -target js fib.scm ;; ;; execute with: d8 fib.js ;; ;; or use it in an HTML page like this: ;; ;; <!DOCTYPE html> ;; <html> ;; <head> ;; <title>fib</title> ;; <script type="text/javascript"> ;; function print(x) { console.log(x); } ;; </script> ;; <script type="text/javascript" src="fib.js"></script> ;; </head> ;; <body></body> ;; </html>
(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))
(define start (real-time-milliseconds))
(println (fib 30))
(define end (real-time-milliseconds))
(println (- end start))
I did that two days ago and built from scratch. Are you talking about something after that?
Thanks.
On Mon, Feb 2, 2015 at 8:26 AM, Marc Feeley feeley@iro.umontreal.ca wrote:
You have to pull the latest version from the github repo.
Marc
On Feb 1, 2015, at 11:35 PM, Blake McBride blake@mcbride.name wrote:
Here is the result of your example:
$ gsc -c -target js f1.scm $ node f1.js
/home/blake/gambit/f1.js:448 print(obj); ^ ReferenceError: print is not defined at gambit_println (/home/blake/gambit/f1.js:448:3) at gambit_bb1_println (/home/blake/gambit/f1.js:323:3) at gambit_trampoline (/home/blake/gambit/f1.js:348:10) at Object.<anonymous> (/home/blake/gambit/f1.js:688: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) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) $
In any case, here is a simple example and instructions on how to run the
code in a JS shell and in the browser. If you want to use non-primitive predefined Scheme library functions (such as equal?, append, map, write, etc) you will need to look into univ-lib . The goal is to have all of this neatly packaged in gsc so that one invocation of gsc will do all that is necessary. One issue is linking… should the .js file contain all of the application code including Scheme libraries, or should it contain just the code for the compiled file (so that it can be dynamically loaded by the web page)? Both should be supported with an appropriate compile flag.
Marc
;; File: fib.scm ;; ;; compile with: gsc -c -target js fib.scm ;; ;; execute with: d8 fib.js ;; ;; or use it in an HTML page like this: ;; ;; <!DOCTYPE html> ;; <html> ;; <head> ;; <title>fib</title> ;; <script type="text/javascript"> ;; function print(x) { console.log(x); } ;; </script> ;; <script type="text/javascript" src="fib.js"></script> ;; </head> ;; <body></body> ;; </html>
(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))
(define start (real-time-milliseconds))
(println (fib 30))
(define end (real-time-milliseconds))
(println (- end start))
I did a git pull and rebuilt everything. Same error. Note that node doesn't have a "print" function.
Thanks.
Blake
On Mon, Feb 2, 2015 at 8:26 AM, Marc Feeley feeley@iro.umontreal.ca wrote:
You have to pull the latest version from the github repo.
Marc
On Feb 1, 2015, at 11:35 PM, Blake McBride blake@mcbride.name wrote:
Here is the result of your example:
$ gsc -c -target js f1.scm $ node f1.js
/home/blake/gambit/f1.js:448 print(obj); ^ ReferenceError: print is not defined at gambit_println (/home/blake/gambit/f1.js:448:3) at gambit_bb1_println (/home/blake/gambit/f1.js:323:3) at gambit_trampoline (/home/blake/gambit/f1.js:348:10) at Object.<anonymous> (/home/blake/gambit/f1.js:688: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) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) $
In any case, here is a simple example and instructions on how to run the
code in a JS shell and in the browser. If you want to use non-primitive predefined Scheme library functions (such as equal?, append, map, write, etc) you will need to look into univ-lib . The goal is to have all of this neatly packaged in gsc so that one invocation of gsc will do all that is necessary. One issue is linking… should the .js file contain all of the application code including Scheme libraries, or should it contain just the code for the compiled file (so that it can be dynamically loaded by the web page)? Both should be supported with an appropriate compile flag.
Marc
;; File: fib.scm ;; ;; compile with: gsc -c -target js fib.scm ;; ;; execute with: d8 fib.js ;; ;; or use it in an HTML page like this: ;; ;; <!DOCTYPE html> ;; <html> ;; <head> ;; <title>fib</title> ;; <script type="text/javascript"> ;; function print(x) { console.log(x); } ;; </script> ;; <script type="text/javascript" src="fib.js"></script> ;; </head> ;; <body></body> ;; </html>
(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))
(define start (real-time-milliseconds))
(println (fib 30))
(define end (real-time-milliseconds))
(println (- end start))
Hi.
Gambit build is a complex thing but it is managed wisely by the makefile if you use the right target. You cannot do just a “git pull; make”… After you pull the latest version do a :
make from-scratch
Then u will have a gambit built with the new feature Marc has just added for you.
Hope this helps. Francois
On Feb 3, 2015, at 10:13 AM, Blake McBride blake@mcbride.name wrote:
I did a git pull and rebuilt everything. Same error. Note that node doesn't have a "print" function.
Thanks.
Blake
On Mon, Feb 2, 2015 at 8:26 AM, Marc Feeley <feeley@iro.umontreal.ca mailto:feeley@iro.umontreal.ca> wrote: You have to pull the latest version from the github repo.
Marc
On Feb 1, 2015, at 11:35 PM, Blake McBride <blake@mcbride.name mailto:blake@mcbride.name> wrote:
Here is the result of your example:
$ gsc -c -target js f1.scm $ node f1.js
/home/blake/gambit/f1.js:448 print(obj); ^ ReferenceError: print is not defined at gambit_println (/home/blake/gambit/f1.js:448:3) at gambit_bb1_println (/home/blake/gambit/f1.js:323:3) at gambit_trampoline (/home/blake/gambit/f1.js:348:10) at Object.<anonymous> (/home/blake/gambit/f1.js:688: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) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) $
In any case, here is a simple example and instructions on how to run the code in a JS shell and in the browser. If you want to use non-primitive predefined Scheme library functions (such as equal?, append, map, write, etc) you will need to look into univ-lib . The goal is to have all of this neatly packaged in gsc so that one invocation of gsc will do all that is necessary. One issue is linking… should the .js file contain all of the application code including Scheme libraries, or should it contain just the code for the compiled file (so that it can be dynamically loaded by the web page)? Both should be supported with an appropriate compile flag.
Marc
;; File: fib.scm ;; ;; compile with: gsc -c -target js fib.scm ;; ;; execute with: d8 fib.js ;; ;; or use it in an HTML page like this: ;; ;; <!DOCTYPE html> ;; <html> ;; <head> ;; <title>fib</title> ;; <script type="text/javascript"> ;; function print(x) { console.log(x); } ;; </script> ;; <script type="text/javascript" src="fib.js"></script> ;; </head> ;; <body></body> ;; </html>
(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))
(define start (real-time-milliseconds))
(println (fib 30))
(define end (real-time-milliseconds))
(println (- end start))
Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
Actually, I did:
git pull rm -rf * git checkout --force ./configure --enable-single-host make sudo rm -rf /usr/local/Gambit-C sudo make install
Do I need more?
On Tue, Feb 3, 2015 at 9:26 AM, Francois Magnan < magnan@categoricaldesign.com> wrote:
Hi.
Gambit build is a complex thing but it is managed wisely by the makefile if you use the right target. You cannot do just a “git pull; make”… After you pull the latest version do a :
make from-scratch
Then u will have a gambit built with the new feature Marc has just added for you.
Hope this helps. Francois
On Feb 3, 2015, at 10:13 AM, Blake McBride blake@mcbride.name wrote:
I did a git pull and rebuilt everything. Same error. Note that node doesn't have a "print" function.
Thanks.
Blake
On Mon, Feb 2, 2015 at 8:26 AM, Marc Feeley feeley@iro.umontreal.ca wrote:
You have to pull the latest version from the github repo.
Marc
On Feb 1, 2015, at 11:35 PM, Blake McBride blake@mcbride.name wrote:
Here is the result of your example:
$ gsc -c -target js f1.scm $ node f1.js
/home/blake/gambit/f1.js:448 print(obj); ^ ReferenceError: print is not defined at gambit_println (/home/blake/gambit/f1.js:448:3) at gambit_bb1_println (/home/blake/gambit/f1.js:323:3) at gambit_trampoline (/home/blake/gambit/f1.js:348:10) at Object.<anonymous> (/home/blake/gambit/f1.js:688: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) at Function.Module.runMain (module.js:497:10) at startup (node.js:119:16) $
In any case, here is a simple example and instructions on how to run
the code in a JS shell and in the browser. If you want to use non-primitive predefined Scheme library functions (such as equal?, append, map, write, etc) you will need to look into univ-lib . The goal is to have all of this neatly packaged in gsc so that one invocation of gsc will do all that is necessary. One issue is linking… should the .js file contain all of the application code including Scheme libraries, or should it contain just the code for the compiled file (so that it can be dynamically loaded by the web page)? Both should be supported with an appropriate compile flag.
Marc
;; File: fib.scm ;; ;; compile with: gsc -c -target js fib.scm ;; ;; execute with: d8 fib.js ;; ;; or use it in an HTML page like this: ;; ;; <!DOCTYPE html> ;; <html> ;; <head> ;; <title>fib</title> ;; <script type="text/javascript"> ;; function print(x) { console.log(x); } ;; </script> ;; <script type="text/javascript" src="fib.js"></script> ;; </head> ;; <body></body> ;; </html>
(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))
(define start (real-time-milliseconds))
(println (fib 30))
(define end (real-time-milliseconds))
(println (- end start))
Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list