psyntax notes - higepon/mosh GitHub Wiki

What is this?

As part of R7RS support? · Issue #49, we want to have a good understanding of how our current psyntax expander works.

The official document

The portable R6RS library and syntax-case system. To run an r6rs script on your system, you'll need the following files:

  • The script you want to run (say hello-world.ss).
  • The host.r6rs.ss file for your platform. => we don't have one. But implemented in the backend.
  • The pre-built/psyntax-host.pp file. => psyntax.scm

psyntax entry point in Mosh

In VM::activateR6RSMode, the VM loads compiled psyntax.scm. In pysntax/main.ss sexp read is executed as (compile-r6rs-top-level x*).

You can set isDebugExpand=true in ```activateR6RSMode(VM* vm, bool isDebugExpand) to see actual expanded results.

Example1

(import (rnrs))

(display "Hello, World")
=>
(begin (set! dummy (unspecified))
       (set! dummy (begin (display 'Hello, World) (void)))
       (set! mI1@mI0@dummy dummy) (void))

Example2

You can see pp is mapped to mJ198@mJ14@pp.

import (rnrs) (mosh pp))

(pp "Hello, World")
=>
(begin (set! dummy (unspecified))
       (set! dummy (begin (mJ198@mJ14@pp 'Hello, World) (void)))
       (set! mJ19d@mJ19c@dummy dummy) (void))

Example3

(import (rnrs) (srfi :176))

(display (version-alist))
=>
(begin (set! dummy (unspecified))
       (set! dummy (begin (display (mL12@mL11@version-alist)) (void))) (set! mL16@mL15@dummy dummy) (void))

You may wonder how (srfi :176) expanded. As you can see (mosh config) is also expanded there.

(begin
  (set! ob1@configurations (unspecified))
  (set! ob3@get-config (unspecified))
  (set! ob5@get-configs (unspecified))
  (set! ob1@configurations (cons (list 'library-path 'Show core library path. (standard-library-path)) 
                           (cons (list 'mosh-cache-dir 'Show auto-compilation-cache directory path (P6df@P5be@mosh-cache-dir))
                                      '((version Show version. 0.2.8-rc4) (prefix Show prefix. /usr/local))))) (set! obc@ob1@configurations ob1@configurations) (set! ob3@get-config (lambda (ob6@key) (let ((ob8@t (assoc ob6@key ob1@configurations))) (if ob8@t (caddr ob8@t) (error 'get-config 'unknown configuration parameter ob6@key))))) (set! obd@ob3@get-config ob3@get-config) (set! ob5@get-configs (lambda () (map (lambda (oba@x) (list (car oba@x) (cadr oba@x))) ob1@configurations))) (set! obe@ob5@get-configs ob5@get-configs) (void))(begin (set! ob11@version-alist (unspecified)) (set! ob11@version-alist (lambda () (list (list 'version (obd@ob3@get-config 'version)) (list 'command (mosh-executable-name)) '(scheme.id mosh) (list 'install-dir (obd@ob3@get-config 'prefix)) '(languages scheme r6rs) '(encodings utf-8) (list 'mosh.cache-dir (obd@ob3@get-config 'mosh-cache-dir)) '(website https://mosh.monaos.org/) (cons 'scheme.path (P998@P7d4@library-path))))) (set! ob12@ob11@version-alist ob11@version-alist) (void))

What psyntax is doing