0.0.0 • Published 3 years ago

gambit-scheme v0.0.0

Weekly downloads
48
License
Apache-2.0
Repository
github
Last release
3 years ago

Gambit Scheme

The Gambit programming system is a full implementation of the Scheme language which conforms to the R4RS, R5RS and IEEE Scheme standards.

The gambit-scheme UMD library allows many different uses of that runtime.

Installation

Easy! I choose yarn, but npm also works.

yarn add gambit-scheme

One probably wants to load .scm files. For that gambit-loader is one option.

yarn add gambit-loader --dev

Documentation

For details see the repo, man! https://github.com/drewc/gxjs

Usage

The package gambit-scheme contains a minimal runtime generated by the Gambit Scheme environment. It attempts to be small yet contain enough to develop and application with the option to add and load Gambit modules.

To load a .scm file, gambit-loader is the easy way. The options are the same as gxjs-loader.

Here is gambit-scheme-usage.scm.

(declare (extended-bindings))

(##inline-host-declaration "console.log('Started Gambit loaded file!')")


(define gambit-vector
  (##vector
   42 'this "is how we hake the moonshine"))


(define (this-is-gambit! #!optional (val 42))
  (let ((three (##inline-host-expression "{ answer: 42 };")))
    (##inline-host-statement "console.log('This is Gambit!', (@1@), (@2@), (@3@))"
                           val gambit-vector three)))

(##inline-host-statement "console.log('finished Gambit-loaded file');
 module.exports = RTS.scm2host(@1@);" this-is-gambit!)

Now for a JavaScript file, gambit-scheme-usage.js. Well make this a CommonJS module.

We do not actually need to require('gambit-scheme') here at all but the RTS is exported pretty much so we can see just that.

const RTS = require('gambit-scheme');

const thisIsGambit = require('gambit-loader!./gambit-scheme-usage.scm');

thisIsGambit(0.42);

module.exports = RTS;

Running that outputs this.

Started Gambit loaded file!
finished Gambit-loaded file
This is Gambit! { val: 0.42 } [
  42,
  { name: 'this', hash: 439079553, interned: true },
  {
    codes: [
      105, 115,  32, 104, 111, 119,  32,
      119, 101,  32, 104,  97, 107, 101,
       32, 116, 104, 101,  32, 109, 111,
      111, 110, 115, 104, 105, 110, 101
    ]
  }
] { answer: 42 }

By default gambit-loader automagically requires gambit-scheme by prepending const RTS = require('gambit-scheme'); to the top of the file.

(declare (extended-bindings))
(##inline-host-statement #<<EOF
console.log('Ok, auto RTS!', Object.keys(RTS.glo).length)

module.exports = RTS;

EOF
)

const RTS = require('./gambit-scheme-usage.js');
const sameRTS = require('gambit-loader!./gambit-scheme-auto.scm');

console.log('Same Runtime?', RTS === sameRTS);

That outputs this:

Started Gambit loaded file!
finished Gambit-loaded file
This is Gambit! { val: 0.42 } [
  42,
  { name: 'this', hash: 439079553, interned: true },
  {
    codes: [
      105, 115,  32, 104, 111, 119,  32,
      119, 101,  32, 104,  97, 107, 101,
       32, 116, 104, 101,  32, 109, 111,
      111, 110, 115, 104, 105, 110, 101
    ]
  }
] { answer: 42 }
Ok, auto RTS! 127
Same Runtime? true

Support

Go to https://github.com/drewc/gxjs/issues or contact the author.