项目作者: jdan

项目描述 :
a scheme->js compiler written in scheme
高级语言: Scheme
项目地址: git://github.com/jdan/scheme-to-js.git
创建时间: 2018-07-07T10:22:06Z
项目社区:https://github.com/jdan/scheme-to-js

开源协议:

下载


scheme-to-js

a scheme->js compiler written in scheme

Hi 👋

This is a Scheme compiler written in Scheme that produces JavaScript.

I’ve done this before (in JavaScript) - https://github.com/jdan/lispjs. You’ll find that writing the
compiler in Scheme itself means I can effectively skip parsing.

Running

I’ve written this repository with Chez Scheme in mind. It’s pretty
tiny and easy to install. I typically build it without X11.

Example

  1. $ cat example.scm
  2. (load "compiler.scm")
  3. (display
  4. (scheme->js
  5. (begin
  6. (define (string-join strs joiner)
  7. (define (helper strs acc)
  8. (if (null? strs)
  9. acc
  10. (helper (cdr strs)
  11. (+ acc
  12. (+ joiner (car strs))))))
  13. (if (null? strs)
  14. ""
  15. (helper (cdr strs) (car strs))))
  16. (println (string-join (Array "apples" "bananas" "cucumbers") ",")))))
  17. $ scheme --script example.scm | prettier --parser babylon
  18. (() => {
  19. function string$join(strs, joiner) {
  20. return (() => {
  21. function helper(strs, acc) {
  22. return 0 === strs.length
  23. ? acc
  24. : helper(strs.slice(1), acc + (joiner + strs[0]));
  25. }
  26. return 0 === strs.length ? "" : helper(strs.slice(1), strs[0]);
  27. })();
  28. }
  29. return console.log(string$join(Array("apples", "bananas", "cucumbers"), ","));
  30. })();
  31. $ scheme --script example.scm | prettier --parser babylon | node
  32. apples,bananas,cucumbers